Skip to content

Commit 077e118

Browse files
committed
add sample and integration tests for autoscaler
1 parent 80a8c90 commit 077e118

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
//[START spanner_create_instance_with_autoscaling]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.spanner.Instance;
23+
import com.google.cloud.spanner.InstanceAdminClient;
24+
import com.google.cloud.spanner.InstanceConfigId;
25+
import com.google.cloud.spanner.InstanceId;
26+
import com.google.cloud.spanner.InstanceInfo;
27+
import com.google.cloud.spanner.Spanner;
28+
import com.google.cloud.spanner.SpannerOptions;
29+
import com.google.spanner.admin.instance.v1.AutoscalingConfig;
30+
import com.google.spanner.admin.instance.v1.CreateInstanceMetadata;
31+
32+
class CreateInstanceWithAutoscalingExample {
33+
34+
static void createAutoscalingInstance() {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String projectId = "my-project";
37+
String instanceId = "my-instance";
38+
createAutoscalingInstance(projectId, instanceId);
39+
}
40+
41+
static void createAutoscalingInstance(String projectId, String instanceId) {
42+
Spanner spanner = SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
43+
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
44+
45+
// Set Instance configuration.
46+
String configId = "regional-us-central1";
47+
String displayName = "Descriptive name";
48+
// This will create an autoscaling config that can be passed to the InstanceInfo.
49+
AutoscalingConfig autoscaling_config = AutoscalingConfig.newBuilder()
50+
.setAutoscalingLimits(AutoscalingConfig.AutoscalingLimits.newBuilder()
51+
.setMinNodes(2)
52+
.setMaxNodes(10))
53+
.setAutoscalingTargets(AutoscalingConfig.AutoscalingTargets.newBuilder()
54+
.setHighPriorityCpuUtilizationPercent(65)
55+
.setStorageUtilizationPercent(95))
56+
.build();
57+
58+
try {
59+
// Creates a new instance
60+
System.out.printf("Creating autoscaling instance %s.%n", instanceId);
61+
OperationFuture<Instance, CreateInstanceMetadata> operation =
62+
instanceAdminClient.createInstance(InstanceInfo
63+
.newBuilder(InstanceId.of(projectId, instanceId))
64+
.setInstanceConfigId(InstanceConfigId.of(projectId, configId))
65+
.setDisplayName(displayName)
66+
.setAutoscalingConfig(autoscaling_config)
67+
.build());
68+
69+
// Wait for the createInstance operation to finish.
70+
System.out.printf("Waiting for operation on %s to complete...%n", instanceId);
71+
Instance createdInstance = operation.get();
72+
73+
System.out.printf("Created autoscaling instance %s.%n", createdInstance.getId().getInstance());
74+
75+
Instance instance = instanceAdminClient.getInstance(instanceId);
76+
System.out.printf("Instance %s autoscaling config: %s.%n", instance.getId().getInstance(),
77+
instance.getAutoscalingConfig());
78+
} catch (Exception e) {
79+
System.out.printf("Error: %s.%n", e.getMessage());
80+
}
81+
spanner.close();
82+
}
83+
}
84+
//[END spanner_create_instance_with_autoscaling]
85+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
import static org.junit.Assert.assertTrue;
20+
21+
import org.junit.Test;
22+
23+
public class CreateInstanceWithAutoscalingExampleIT extends SampleTestBase {
24+
25+
@Test
26+
public void testInstanceWithAutoscaling() throws Exception {
27+
// Runs sample
28+
final String out = SampleRunner.runSample(() ->
29+
CreateInstanceWithAutoscalingExample.createAutoscalingInstance(
30+
projectId,
31+
instanceId
32+
));
33+
34+
assertTrue(
35+
"Expected created instance with autoscaling",
36+
out.contains("Created autoscaling instance")
37+
);
38+
}
39+
}
40+

0 commit comments

Comments
 (0)