Skip to content

Commit 9ea4f4f

Browse files
authored
feat: Add java sample for managed autoscaler (#2709)
Add java sample for managed autoscaler. See the support for autoscaler here: https://togithub.com/claire921/java-spanner/pull/1/commits/e5e6923a351670ab237c411bb4a549533dac1b6b.
1 parent 4b17331 commit 9ea4f4f

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-spanner/tree/
278278
| Create Database With Version Retention Period Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CreateDatabaseWithVersionRetentionPeriodSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CreateDatabaseWithVersionRetentionPeriodSample.java) |
279279
| Create Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CreateInstanceConfigSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CreateInstanceConfigSample.java) |
280280
| Create Instance Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CreateInstanceExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CreateInstanceExample.java) |
281+
| Create Instance With Autoscaling Config Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CreateInstanceWithAutoscalingConfigExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CreateInstanceWithAutoscalingConfigExample.java) |
281282
| Create Instance With Processing Units Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CreateInstanceWithProcessingUnitsExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CreateInstanceWithProcessingUnitsExample.java) |
282283
| Create Sequence Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CreateSequenceSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CreateSequenceSample.java) |
283284
| Create Table With Foreign Key Delete Cascade Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CreateTableWithForeignKeyDeleteCascadeSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CreateTableWithForeignKeyDeleteCascadeSample.java) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2023 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_config]
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+
import java.util.concurrent.ExecutionException;
32+
33+
class CreateInstanceWithAutoscalingConfigExample {
34+
35+
static void createInstance() {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String projectId = "my-project";
38+
String instanceId = "my-instance";
39+
createInstance(projectId, instanceId);
40+
}
41+
42+
static void createInstance(String projectId, String instanceId) {
43+
Spanner spanner = SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
44+
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
45+
46+
// Set Instance configuration.
47+
String configId = "regional-us-central1";
48+
// Create an autoscaling config.
49+
AutoscalingConfig autoscalingConfig =
50+
AutoscalingConfig.newBuilder()
51+
.setAutoscalingLimits(
52+
AutoscalingConfig.AutoscalingLimits.newBuilder().setMinNodes(1).setMaxNodes(2))
53+
.setAutoscalingTargets(
54+
AutoscalingConfig.AutoscalingTargets.newBuilder()
55+
.setHighPriorityCpuUtilizationPercent(65)
56+
.setStorageUtilizationPercent(95))
57+
.build();
58+
59+
// Create an InstanceInfo object that will be used to create the instance.
60+
InstanceInfo instanceInfo =
61+
InstanceInfo.newBuilder(InstanceId.of(projectId, instanceId))
62+
.setInstanceConfigId(InstanceConfigId.of(projectId, configId))
63+
.setAutoscalingConfig(autoscalingConfig)
64+
.setDisplayName("Descriptive name")
65+
.build();
66+
OperationFuture<Instance, CreateInstanceMetadata> operation =
67+
instanceAdminClient.createInstance(instanceInfo);
68+
69+
try {
70+
// Wait for the createInstance operation to finish.
71+
Instance instance = operation.get();
72+
System.out.printf("Autoscaler instance %s was successfully created%n", instance.getId());
73+
} catch (ExecutionException e) {
74+
System.out.printf(
75+
"Error: Creating instance %s failed with error message %s%n",
76+
instanceInfo.getId(), e.getMessage());
77+
} catch (InterruptedException e) {
78+
System.out.println("Error: Waiting for createInstance operation to finish was interrupted");
79+
} finally {
80+
spanner.close();
81+
}
82+
}
83+
}
84+
// [END spanner_create_instance_with_autoscaling_config]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2023 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 com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.spanner.InstanceId;
22+
import java.util.UUID;
23+
import org.junit.Test;
24+
25+
public class CreateInstanceWithAutoscalingConfigSampleIT extends SampleTestBase {
26+
27+
@Test
28+
public void testCreateInstanceWithAutoscalingConfig() throws Exception {
29+
String instanceId = String.format("autoscaler-%s", UUID.randomUUID());
30+
String out =
31+
SampleRunner.runSample(
32+
() -> {
33+
try {
34+
CreateInstanceWithAutoscalingConfigExample.createInstance(projectId, instanceId);
35+
} finally {
36+
spanner.getInstanceAdminClient().deleteInstance(instanceId);
37+
}
38+
});
39+
assertThat(out)
40+
.contains(String.format("Autoscaler instance %s", InstanceId.of(projectId, instanceId)));
41+
}
42+
}

0 commit comments

Comments
 (0)