-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add sample for asymmetric autoscaling instances
- Loading branch information
Showing
3 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...c/main/java/com/example/spanner/CreateInstanceWithAsymmetricAutoscalingConfigExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
36 changes: 36 additions & 0 deletions
36
.../test/java/com/example/spanner/CreateInstanceWithAsymmetricAutoscalingConfigSampleIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |