|
| 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] |
0 commit comments