-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add java sample for managed autoscaler #2709
Merged
Merged
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c36a99a
add the handwritten layer support for autoscaler
claire921 036601a
Address comments in the PR and add integration tests.
claire921 3930403
retrigger checks
claire921 bbfe85a
Address new comments
claire921 d9c1acc
fix the abstract method issue
claire921 e97f68c
Fix the instanceadminclientimpltest
claire921 40287ea
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 25fce0e
add autoscaling java sample
claire921 7e5d172
fix for the sample code
claire921 25f9549
resolve conflicts
claire921 79c1781
small fixes
claire921 3e344bb
fix formats
claire921 dab38f6
Merge branch 'googleapis:main' into autoscaler
claire921 7a51d2a
small modification to java sample
claire921 2fbeac4
revert the local change for sample base test
claire921 6076797
change year
claire921 4cac596
inline display name
claire921 4d884c9
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
...nippets/src/main/java/com/example/spanner/CreateInstanceWithAutoscalingConfigExample.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,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_config] | ||
|
||
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; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class CreateInstanceWithAutoscalingConfigExample { | ||
|
||
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) { | ||
Spanner spanner = SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient(); | ||
|
||
// Set Instance configuration. | ||
String configId = "regional-us-central1"; | ||
arpan14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Create an autoscaling config. | ||
AutoscalingConfig autoscalingConfig = | ||
AutoscalingConfig.newBuilder() | ||
.setAutoscalingLimits( | ||
AutoscalingConfig.AutoscalingLimits.newBuilder().setMinNodes(1).setMaxNodes(2)) | ||
.setAutoscalingTargets( | ||
AutoscalingConfig.AutoscalingTargets.newBuilder() | ||
.setHighPriorityCpuUtilizationPercent(65) | ||
.setStorageUtilizationPercent(95)) | ||
.build(); | ||
String displayName = "Descriptive name"; | ||
claire921 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Create an InstanceInfo object that will be used to create the instance. | ||
InstanceInfo instanceInfo = | ||
InstanceInfo.newBuilder(InstanceId.of(projectId, instanceId)) | ||
.setInstanceConfigId(InstanceConfigId.of(projectId, configId)) | ||
.setAutoscalingConfig(autoscalingConfig) | ||
.setDisplayName(displayName) | ||
.build(); | ||
OperationFuture<Instance, CreateInstanceMetadata> operation = | ||
instanceAdminClient.createInstance(instanceInfo); | ||
|
||
try { | ||
// Wait for the createInstance operation to finish. | ||
Instance instance = operation.get(); | ||
System.out.printf("Autoscaler instance %s was successfully created%n", instance.getId()); | ||
} catch (ExecutionException e) { | ||
System.out.printf( | ||
"Error: Creating instance %s failed with error message %s%n", | ||
instanceInfo.getId(), e.getMessage()); | ||
} catch (InterruptedException e) { | ||
System.out.println("Error: Waiting for createInstance operation to finish was interrupted"); | ||
} finally { | ||
spanner.close(); | ||
} | ||
} | ||
} | ||
// [END spanner_create_instance_with_autoscaling_config] |
41 changes: 41 additions & 0 deletions
41
...ippets/src/test/java/com/example/spanner/CreateInstanceWithAutoscalingConfigSampleIT.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,41 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
claire921 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* 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.cloud.spanner.InstanceId; | ||
import java.util.UUID; | ||
import org.junit.Test; | ||
|
||
public class CreateInstanceWithAutoscalingConfigSampleIT extends SampleTestBase { | ||
@Test | ||
claire921 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public void testCreateInstanceWithAutoscalingConfig() throws Exception { | ||
String instanceId = String.format("autoscaler-%s", UUID.randomUUID()); | ||
String out = | ||
SampleRunner.runSample( | ||
() -> { | ||
try { | ||
CreateInstanceWithAutoscalingConfigExample.createInstance(projectId, instanceId); | ||
} finally { | ||
spanner.getInstanceAdminClient().deleteInstance(instanceId); | ||
} | ||
}); | ||
assertThat(out) | ||
.contains(String.format("Autoscaler instance %s", InstanceId.of(projectId, instanceId))); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.