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