Skip to content

Commit 3584b81

Browse files
authored
feat: Add sample for asymmetric autoscaling instances (#3562)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/java-spanner/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) If you write sample code, please follow the [samples format]( https://togithub.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md).
1 parent a2aac5e commit 3584b81

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-spanner/tree/
598598
| Create Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/archived/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/admin/archived/CreateInstanceConfigSample.java) |
599599
| Create Instance Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/archived/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/admin/archived/CreateInstanceExample.java) |
600600
| Create Instance With Autoscaling Config Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/archived/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/admin/archived/CreateInstanceWithAutoscalingConfigExample.java) |
601+
| Create Instance With Asymmetric Autoscaling Config Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/archived/CreateInstanceWithAsymmetricAutoscalingConfigExample.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/admin/archived/CreateInstanceWithAsymmetricAutoscalingConfigExample.java) |
601602
| Create Instance With Processing Units Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/archived/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/admin/archived/CreateInstanceWithProcessingUnitsExample.java) |
602603
| Create Sequence Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/archived/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/admin/archived/CreateSequenceSample.java) |
603604
| 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/admin/archived/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/admin/archived/CreateTableWithForeignKeyDeleteCascadeSample.java) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2024 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_asymmetric_autoscaling_config]
20+
21+
import com.google.cloud.spanner.Spanner;
22+
import com.google.cloud.spanner.SpannerOptions;
23+
import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient;
24+
import com.google.spanner.admin.instance.v1.AutoscalingConfig;
25+
import com.google.spanner.admin.instance.v1.CreateInstanceRequest;
26+
import com.google.spanner.admin.instance.v1.Instance;
27+
import com.google.spanner.admin.instance.v1.InstanceConfigName;
28+
import com.google.spanner.admin.instance.v1.ProjectName;
29+
import com.google.spanner.admin.instance.v1.ReplicaSelection;
30+
import java.util.concurrent.ExecutionException;
31+
32+
class CreateInstanceWithAsymmetricAutoscalingConfigExample {
33+
34+
static void createInstance() {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String projectId = "my-project";
37+
String instanceId = "my-instance";
38+
createInstance(projectId, instanceId);
39+
}
40+
41+
static void createInstance(String projectId, String instanceId) {
42+
try (Spanner spanner =
43+
SpannerOptions.newBuilder()
44+
.setProjectId(projectId)
45+
.build()
46+
.getService();
47+
InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) {
48+
// Set Instance configuration.
49+
String configId = "nam-eur-asia3";
50+
String displayName = "Descriptive name";
51+
52+
// Create an autoscaling config.
53+
// When autoscaling_config is enabled, node_count and processing_units fields
54+
// need not be specified.
55+
// The read-only replicas listed in the asymmetric autoscaling options scale independently
56+
// from other replicas.
57+
AutoscalingConfig autoscalingConfig =
58+
AutoscalingConfig.newBuilder()
59+
.setAutoscalingLimits(
60+
AutoscalingConfig.AutoscalingLimits.newBuilder().setMinNodes(1).setMaxNodes(2))
61+
.setAutoscalingTargets(
62+
AutoscalingConfig.AutoscalingTargets.newBuilder()
63+
.setHighPriorityCpuUtilizationPercent(65)
64+
.setStorageUtilizationPercent(95))
65+
.addAsymmetricAutoscalingOptions(
66+
AutoscalingConfig.AsymmetricAutoscalingOption.newBuilder()
67+
.setReplicaSelection(ReplicaSelection.newBuilder().setLocation("europe-west1")))
68+
.addAsymmetricAutoscalingOptions(
69+
AutoscalingConfig.AsymmetricAutoscalingOption.newBuilder()
70+
.setReplicaSelection(ReplicaSelection.newBuilder().setLocation("europe-west4")))
71+
.addAsymmetricAutoscalingOptions(
72+
AutoscalingConfig.AsymmetricAutoscalingOption.newBuilder()
73+
.setReplicaSelection(ReplicaSelection.newBuilder().setLocation("asia-east1")))
74+
.build();
75+
Instance instance =
76+
Instance.newBuilder()
77+
.setAutoscalingConfig(autoscalingConfig)
78+
.setDisplayName(displayName)
79+
.setConfig(
80+
InstanceConfigName.of(projectId, configId).toString())
81+
.build();
82+
83+
// Creates a new instance
84+
System.out.printf("Creating instance %s.%n", instanceId);
85+
try {
86+
// Wait for the createInstance operation to finish.
87+
Instance instanceResult = instanceAdminClient.createInstanceAsync(
88+
CreateInstanceRequest.newBuilder()
89+
.setParent(ProjectName.of(projectId).toString())
90+
.setInstanceId(instanceId)
91+
.setInstance(instance)
92+
.build()).get();
93+
System.out.printf("Autoscaler instance %s was successfully created%n",
94+
instanceResult.getName());
95+
} catch (ExecutionException e) {
96+
System.out.printf(
97+
"Error: Creating instance %s failed with error message %s%n",
98+
instance.getName(), e.getMessage());
99+
} catch (InterruptedException e) {
100+
System.out.println("Error: Waiting for createInstance operation to finish was interrupted");
101+
}
102+
}
103+
}
104+
}
105+
// [END spanner_create_instance_with_asymmetric_autoscaling_config]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2024 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.spanner.admin.database.v1.InstanceName;
22+
import org.junit.Test;
23+
24+
public class CreateInstanceWithAsymmetricAutoscalingConfigSampleIT extends SampleTestBaseV2 {
25+
26+
@Test
27+
public void testCreateInstanceWithAsymmetricAutoscalingConfig() throws Exception {
28+
String instanceId = idGenerator.generateInstanceId();
29+
String out =
30+
SampleRunner.runSample(
31+
() -> CreateInstanceWithAsymmetricAutoscalingConfigExample
32+
.createInstance(projectId, instanceId));
33+
assertThat(out)
34+
.contains(String.format("Asymmetric Autoscaling instance %s",
35+
InstanceName.of(projectId, instanceId).toString()));
36+
}
37+
}

0 commit comments

Comments
 (0)