Skip to content

Commit

Permalink
add sample and integration tests for autoscaler
Browse files Browse the repository at this point in the history
  • Loading branch information
claire921 committed Oct 12, 2023
1 parent 80a8c90 commit 077e118
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2021 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_autoscaling]

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.spanner.Instance;
import com.google.cloud.spanner.InstanceAdminClient;
import com.google.cloud.spanner.InstanceConfigId;
import com.google.cloud.spanner.InstanceId;
import com.google.cloud.spanner.InstanceInfo;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.spanner.admin.instance.v1.AutoscalingConfig;
import com.google.spanner.admin.instance.v1.CreateInstanceMetadata;

class CreateInstanceWithAutoscalingExample {

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

static void createAutoscalingInstance(String projectId, String instanceId) {
Spanner spanner = SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();

// Set Instance configuration.
String configId = "regional-us-central1";
String displayName = "Descriptive name";
// This will create an autoscaling config that can be passed to the InstanceInfo.
AutoscalingConfig autoscaling_config = AutoscalingConfig.newBuilder()
.setAutoscalingLimits(AutoscalingConfig.AutoscalingLimits.newBuilder()
.setMinNodes(2)
.setMaxNodes(10))
.setAutoscalingTargets(AutoscalingConfig.AutoscalingTargets.newBuilder()
.setHighPriorityCpuUtilizationPercent(65)
.setStorageUtilizationPercent(95))
.build();

try {
// Creates a new instance
System.out.printf("Creating autoscaling instance %s.%n", instanceId);
OperationFuture<Instance, CreateInstanceMetadata> operation =
instanceAdminClient.createInstance(InstanceInfo
.newBuilder(InstanceId.of(projectId, instanceId))
.setInstanceConfigId(InstanceConfigId.of(projectId, configId))
.setDisplayName(displayName)
.setAutoscalingConfig(autoscaling_config)
.build());

// Wait for the createInstance operation to finish.
System.out.printf("Waiting for operation on %s to complete...%n", instanceId);
Instance createdInstance = operation.get();

System.out.printf("Created autoscaling instance %s.%n", createdInstance.getId().getInstance());

Instance instance = instanceAdminClient.getInstance(instanceId);
System.out.printf("Instance %s autoscaling config: %s.%n", instance.getId().getInstance(),
instance.getAutoscalingConfig());
} catch (Exception e) {
System.out.printf("Error: %s.%n", e.getMessage());
}
spanner.close();
}
}
//[END spanner_create_instance_with_autoscaling]

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2022 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 org.junit.Assert.assertTrue;

import org.junit.Test;

public class CreateInstanceWithAutoscalingExampleIT extends SampleTestBase {

@Test
public void testInstanceWithAutoscaling() throws Exception {
// Runs sample
final String out = SampleRunner.runSample(() ->
CreateInstanceWithAutoscalingExample.createAutoscalingInstance(
projectId,
instanceId
));

assertTrue(
"Expected created instance with autoscaling",
out.contains("Created autoscaling instance")
);
}
}

0 comments on commit 077e118

Please sign in to comment.