Skip to content

Commit e5e6923

Browse files
authored
feat: Add support for Managed Autoscaler (#2624)
- Add the ability to create / update an instance with autoscaling config - Note that now user can only specify one of the node_count, processing_units or autoscaling_config as the compute capacity for an instance. - Add the ability to see autoscaling config with an instance
1 parent 712c65f commit e5e6923

File tree

7 files changed

+472
-34
lines changed

7 files changed

+472
-34
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Instance.java

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.cloud.spanner.Options.ListOption;
2424
import com.google.longrunning.Operation;
2525
import com.google.spanner.admin.database.v1.CreateDatabaseMetadata;
26+
import com.google.spanner.admin.instance.v1.AutoscalingConfig;
2627
import com.google.spanner.admin.instance.v1.UpdateInstanceMetadata;
2728
import java.util.Map;
2829

@@ -86,6 +87,12 @@ public Builder setProcessingUnits(int processingUnits) {
8687
return this;
8788
}
8889

90+
@Override
91+
public Builder setAutoscalingConfig(AutoscalingConfig autoscalingConfig) {
92+
infoBuilder.setAutoscalingConfig(autoscalingConfig);
93+
return this;
94+
}
95+
8996
@Override
9097
public Builder setState(State state) {
9198
infoBuilder.setState(state);
@@ -220,6 +227,7 @@ static Instance fromProto(
220227
.setNodeCount(proto.getNodeCount())
221228
.setCreateTime(Timestamp.fromProto(proto.getCreateTime()))
222229
.setUpdateTime(Timestamp.fromProto(proto.getUpdateTime()))
230+
.setAutoscalingConfig(proto.getAutoscalingConfig())
223231
.setProcessingUnits(proto.getProcessingUnits());
224232
State state;
225233
switch (proto.getState()) {

google-cloud-spanner/src/main/java/com/google/cloud/spanner/InstanceAdminClientImpl.java

-3
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,6 @@ public Operation fromProto(Operation proto) {
193193
@Override
194194
public OperationFuture<Instance, CreateInstanceMetadata> createInstance(InstanceInfo instance)
195195
throws SpannerException {
196-
Preconditions.checkArgument(
197-
instance.getNodeCount() == 0 || instance.getProcessingUnits() == 0,
198-
"Only one of nodeCount and processingUnits can be set when creating a new instance");
199196
String projectName = PROJECT_NAME_TEMPLATE.instantiate("project", projectId);
200197
OperationFuture<com.google.spanner.admin.instance.v1.Instance, CreateInstanceMetadata>
201198
rawOperationFuture =

google-cloud-spanner/src/main/java/com/google/cloud/spanner/InstanceInfo.java

+44-9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.common.base.MoreObjects;
2424
import com.google.common.collect.ImmutableMap;
2525
import com.google.protobuf.FieldMask;
26+
import com.google.spanner.admin.instance.v1.AutoscalingConfig;
2627
import java.util.HashMap;
2728
import java.util.Map;
2829
import java.util.Objects;
@@ -35,13 +36,16 @@ public enum InstanceField implements FieldSelector {
3536
DISPLAY_NAME("display_name"),
3637
NODE_COUNT("node_count"),
3738
PROCESSING_UNITS("processing_units"),
39+
AUTOSCALING_CONFIG("autoscaling_config"),
3840
LABELS("labels");
3941

4042
static InstanceField[] defaultFieldsToUpdate(InstanceInfo info) {
41-
if (info.getNodeCount() > 0) {
42-
return new InstanceField[] {DISPLAY_NAME, NODE_COUNT, LABELS};
43+
if (info.getAutoscalingConfig() != null) {
44+
return new InstanceField[] {DISPLAY_NAME, AUTOSCALING_CONFIG, LABELS};
45+
} else if (info.getNodeCount() > 0) {
46+
return new InstanceField[] {DISPLAY_NAME, AUTOSCALING_CONFIG, NODE_COUNT, LABELS};
4347
} else {
44-
return new InstanceField[] {DISPLAY_NAME, PROCESSING_UNITS, LABELS};
48+
return new InstanceField[] {DISPLAY_NAME, AUTOSCALING_CONFIG, PROCESSING_UNITS, LABELS};
4549
}
4650
}
4751

@@ -87,21 +91,31 @@ Builder setCreateTime(Timestamp createTime) {
8791
}
8892

8993
/**
90-
* Sets the number of nodes for the instance. Exactly one of processing units or node count must
91-
* be set when creating a new instance.
94+
* Sets the number of nodes for the instance. Exactly one of processing units, node count or
95+
* autoscaling config must be set when creating a new instance.
9296
*/
9397
public abstract Builder setNodeCount(int nodeCount);
9498

9599
/**
96-
* Sets the number of processing units for the instance. Exactly one of processing units or node
97-
* count must be set when creating a new instance. Processing units must be between 1 and 999
98-
* (inclusive) when creating a new instance with node count = 0. Processing units from 1000 and
99-
* up must always be a multiple of 1000 (that is equal to an integer number of nodes).
100+
* Sets the number of processing units for the instance. Exactly one of processing units, node
101+
* count, or autoscaling config must be set when creating a new instance. Processing units must
102+
* be between 1 and 999 (inclusive) when creating a new instance with node count = 0. Processing
103+
* units from 1000 and up must always be a multiple of 1000 (that is equal to an integer number
104+
* of nodes).
100105
*/
101106
public Builder setProcessingUnits(int processingUnits) {
102107
throw new UnsupportedOperationException("Unimplemented");
103108
}
104109

110+
/**
111+
* Sets the autoscaling config for the instance, which will enable the autoscaling for this
112+
* instance. Exactly one of processing units, node count, or autoscaling config must be set when
113+
* creating a new instance.
114+
*/
115+
public Builder setAutoscalingConfig(AutoscalingConfig autoscalingConfig) {
116+
throw new UnsupportedOperationException("Unimplemented");
117+
}
118+
105119
public abstract Builder setState(State state);
106120

107121
public abstract Builder addLabel(String key, String value);
@@ -117,6 +131,7 @@ static class BuilderImpl extends Builder {
117131
private String displayName;
118132
private int nodeCount;
119133
private int processingUnits;
134+
private AutoscalingConfig autoscalingConfig;
120135
private State state;
121136
private Map<String, String> labels;
122137
private Timestamp updateTime;
@@ -133,6 +148,7 @@ static class BuilderImpl extends Builder {
133148
this.displayName = instance.displayName;
134149
this.nodeCount = instance.nodeCount;
135150
this.processingUnits = instance.processingUnits;
151+
this.autoscalingConfig = instance.autoscalingConfig;
136152
this.state = instance.state;
137153
this.labels = new HashMap<>(instance.labels);
138154
this.updateTime = instance.updateTime;
@@ -175,6 +191,12 @@ public BuilderImpl setProcessingUnits(int processingUnits) {
175191
return this;
176192
}
177193

194+
@Override
195+
public BuilderImpl setAutoscalingConfig(AutoscalingConfig autoscalingConfig) {
196+
this.autoscalingConfig = autoscalingConfig;
197+
return this;
198+
}
199+
178200
@Override
179201
public BuilderImpl setState(State state) {
180202
this.state = state;
@@ -204,6 +226,7 @@ public InstanceInfo build() {
204226
private final String displayName;
205227
private final int nodeCount;
206228
private final int processingUnits;
229+
private final AutoscalingConfig autoscalingConfig;
207230
private final State state;
208231
private final ImmutableMap<String, String> labels;
209232
private final Timestamp updateTime;
@@ -215,6 +238,7 @@ public InstanceInfo build() {
215238
this.displayName = builder.displayName;
216239
this.nodeCount = builder.nodeCount;
217240
this.processingUnits = builder.processingUnits;
241+
this.autoscalingConfig = builder.autoscalingConfig;
218242
this.state = builder.state;
219243
this.labels = ImmutableMap.copyOf(builder.labels);
220244
this.updateTime = builder.updateTime;
@@ -254,6 +278,11 @@ public int getProcessingUnits() {
254278
return processingUnits;
255279
}
256280

281+
/** Returns the autoscaling config of the instance. */
282+
public AutoscalingConfig getAutoscalingConfig() {
283+
return autoscalingConfig;
284+
}
285+
257286
/** Returns the current state of the instance. */
258287
public State getState() {
259288
return state;
@@ -276,6 +305,7 @@ public String toString() {
276305
.add("displayName", displayName)
277306
.add("nodeCount", nodeCount)
278307
.add("processingUnits", processingUnits)
308+
.add("autoscaling_config", autoscalingConfig)
279309
.add("state", state)
280310
.add("labels", labels)
281311
.add("createTime", createTime)
@@ -297,6 +327,7 @@ public boolean equals(Object o) {
297327
&& Objects.equals(displayName, that.displayName)
298328
&& nodeCount == that.nodeCount
299329
&& processingUnits == that.processingUnits
330+
&& Objects.equals(autoscalingConfig, that.autoscalingConfig)
300331
&& state == that.state
301332
&& Objects.equals(labels, that.labels)
302333
&& Objects.equals(updateTime, that.updateTime)
@@ -311,6 +342,7 @@ public int hashCode() {
311342
displayName,
312343
nodeCount,
313344
processingUnits,
345+
autoscalingConfig,
314346
state,
315347
labels,
316348
updateTime,
@@ -330,6 +362,9 @@ com.google.spanner.admin.instance.v1.Instance toProto() {
330362
if (getInstanceConfigId() != null) {
331363
builder.setConfig(getInstanceConfigId().getName());
332364
}
365+
if (getAutoscalingConfig() != null) {
366+
builder.setAutoscalingConfig(getAutoscalingConfig());
367+
}
333368
return builder.build();
334369
}
335370

0 commit comments

Comments
 (0)