Skip to content

Commit

Permalink
feat: Add sample for asymmetric autoscaling instances
Browse files Browse the repository at this point in the history
  • Loading branch information
hibiken committed Dec 16, 2024
1 parent 430143c commit 103382e
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-spanner/tree/
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.spanner;

// [START spanner_create_instance_with_asymmetric_autoscaling_config]

import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient;
import com.google.spanner.admin.instance.v1.AutoscalingConfig;
import com.google.spanner.admin.instance.v1.CreateInstanceRequest;
import com.google.spanner.admin.instance.v1.Instance;
import com.google.spanner.admin.instance.v1.InstanceConfigName;
import com.google.spanner.admin.instance.v1.ProjectName;
import com.google.spanner.admin.instance.v1.ReplicaSelection;
import java.util.concurrent.ExecutionException;

class CreateInstanceWithAsymmetricAutoscalingConfigExample {

static void createInstance() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project";
String instanceId = "my-instance";
createInstance(projectId, instanceId);
}

static void createInstance(String projectId, String instanceId) {
try (Spanner spanner =
SpannerOptions.newBuilder()
.setProjectId(projectId)
.build()
.getService();
InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) {
// Set Instance configuration.
String configId = "nam-eur-asia3";
String displayName = "Descriptive name";

// Create an autoscaling config.
// When autoscaling_config is enabled, node_count and processing_units fields
// need not be specified.
// The read-only replicas listed in the asymmetric autoscaling options scale independently
// from other replicas.
AutoscalingConfig autoscalingConfig =
AutoscalingConfig.newBuilder()
.setAutoscalingLimits(
AutoscalingConfig.AutoscalingLimits.newBuilder().setMinNodes(1).setMaxNodes(2))
.setAutoscalingTargets(
AutoscalingConfig.AutoscalingTargets.newBuilder()
.setHighPriorityCpuUtilizationPercent(65)
.setStorageUtilizationPercent(95))
.addAsymmetricAutoscalingOption(
AutoscalingConfig.AsymmetricAutoscalingOption.newBuilder()
.setReplicaSelection(ReplicaSelection.newBuilder().setLocation("europe-west1")))
.addAsymmetricAutoscalingOption(
AutoscalingConfig.AsymmetricAutoscalingOption.newBuilder()
.setReplicaSelection(ReplicaSelection.newBuilder().setLocation("europe-west4")))
.addAsymmetricAutoscalingOption(
AutoscalingConfig.AsymmetricAutoscalingOption.newBuilder()
.setReplicaSelection(ReplicaSelection.newBuilder().setLocation("asia-east1")))
.build();
Instance instance =
Instance.newBuilder()
.setAutoscalingConfig(autoscalingConfig)
.setDisplayName(displayName)
.setConfig(
InstanceConfigName.of(projectId, configId).toString())
.build();

// Creates a new instance
System.out.printf("Creating instance %s.%n", instanceId);
try {
// Wait for the createInstance operation to finish.
Instance instanceResult = instanceAdminClient.createInstanceAsync(
CreateInstanceRequest.newBuilder()
.setParent(ProjectName.of(projectId).toString())
.setInstanceId(instanceId)
.setInstance(instance)
.build()).get();
System.out.printf("Autoscaler instance %s was successfully created%n",
instanceResult.getName());
} catch (ExecutionException e) {
System.out.printf(
"Error: Creating instance %s failed with error message %s%n",
instance.getName(), e.getMessage());
} catch (InterruptedException e) {
System.out.println("Error: Waiting for createInstance operation to finish was interrupted");
}
}
}
}
// [END spanner_create_instance_with_asymmetric_autoscaling_config]
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.spanner;

import static com.google.common.truth.Truth.assertThat;

import com.google.spanner.admin.database.v1.InstanceName;
import org.junit.Test;

public class CreateInstanceWithAsymmetricAutoscalingConfigSampleIT extends SampleTestBaseV2 {

@Test
public void testCreateInstanceWithAsymmetricAutoscalingConfig() throws Exception {
String instanceId = idGenerator.generateInstanceId();
String out =
SampleRunner.runSample(
() -> CreateInstanceWithAsymmetricAutoscalingConfigExample.createInstance(projectId, instanceId));
assertThat(out)
.contains(String.format("Asymmetric Autoscaling instance %s",
InstanceName.of(projectId, instanceId).toString()));
}
}

0 comments on commit 103382e

Please sign in to comment.