Skip to content

Commit e73c671

Browse files
docs: add sample for managed autoscaler (#1111)
* docs: add sample for managed autoscaler * incorporate suggestions --------- Co-authored-by: Sri Harsha CH <[email protected]>
1 parent ccdd592 commit e73c671

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

samples/samples/snippets.py

+62-1
Original file line numberDiff line numberDiff line change
@@ -3043,8 +3043,8 @@ def directed_read_options(
30433043
def set_custom_timeout_and_retry(instance_id, database_id):
30443044
"""Executes a snapshot read with custom timeout and retry."""
30453045
# [START spanner_set_custom_timeout_and_retry]
3046-
from google.api_core import retry
30473046
from google.api_core import exceptions as core_exceptions
3047+
from google.api_core import retry
30483048

30493049
# instance_id = "your-spanner-instance"
30503050
# database_id = "your-spanner-db-id"
@@ -3085,6 +3085,65 @@ def set_custom_timeout_and_retry(instance_id, database_id):
30853085
# [END spanner_set_custom_timeout_and_retry]
30863086

30873087

3088+
# [START spanner_create_instance_with_autoscaling_config]
3089+
def create_instance_with_autoscaling_config(instance_id):
3090+
"""Creates a Cloud Spanner instance with an autoscaling configuration."""
3091+
from google.cloud.spanner_admin_instance_v1.types import \
3092+
spanner_instance_admin
3093+
3094+
spanner_client = spanner.Client()
3095+
3096+
config_name = "{}/instanceConfigs/regional-us-central1".format(
3097+
spanner_client.project_name
3098+
)
3099+
3100+
autoscaling_config = spanner_instance_admin.AutoscalingConfig(
3101+
# Only one of minNodes/maxNodes or minProcessingUnits/maxProcessingUnits can be set.
3102+
autoscaling_limits=spanner_instance_admin.AutoscalingConfig.AutoscalingLimits(
3103+
min_nodes=1,
3104+
max_nodes=2,
3105+
),
3106+
# highPriorityCpuUtilizationPercent and storageUtilizationPercent are both
3107+
# percentages and must lie between 0 and 100.
3108+
autoscaling_targets=spanner_instance_admin.AutoscalingConfig.AutoscalingTargets(
3109+
high_priority_cpu_utilization_percent=65,
3110+
storage_utilization_percent=95,
3111+
),
3112+
)
3113+
3114+
# Creates a new instance with autoscaling configuration
3115+
# When autoscalingConfig is enabled, nodeCount and processingUnits fields
3116+
# need not be specified.
3117+
request = spanner_instance_admin.CreateInstanceRequest(
3118+
parent=spanner_client.project_name,
3119+
instance_id=instance_id,
3120+
instance=spanner_instance_admin.Instance(
3121+
config=config_name,
3122+
display_name="This is a display name.",
3123+
autoscaling_config=autoscaling_config,
3124+
labels={
3125+
"cloud_spanner_samples": "true",
3126+
"sample_name": "snippets-create_instance_with_autoscaling_config",
3127+
"created": str(int(time.time())),
3128+
},
3129+
),
3130+
)
3131+
3132+
operation = spanner_client.instance_admin_api.create_instance(request=request)
3133+
3134+
print("Waiting for operation to complete...")
3135+
instance = operation.result(OPERATION_TIMEOUT_SECONDS)
3136+
3137+
print(
3138+
"Created instance {} with {} autoscaling config".format(
3139+
instance_id, instance.autoscaling_config
3140+
)
3141+
)
3142+
3143+
3144+
# [END spanner_create_instance_with_autoscaling_config]
3145+
3146+
30883147
if __name__ == "__main__": # noqa: C901
30893148
parser = argparse.ArgumentParser(
30903149
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
@@ -3366,3 +3425,5 @@ def set_custom_timeout_and_retry(instance_id, database_id):
33663425
directed_read_options(args.instance_id, args.database_id)
33673426
elif args.command == "set_custom_timeout_and_retry":
33683427
set_custom_timeout_and_retry(args.instance_id, args.database_id)
3428+
elif args.command == "create_instance_with_autoscaling_config":
3429+
create_instance_with_autoscaling_config(args.instance_id)

samples/samples/snippets_test.py

+12
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,18 @@ def test_create_instance_with_processing_units(capsys, lci_instance_id):
154154
retry_429(instance.delete)()
155155

156156

157+
def test_create_instance_with_autoscaling_config(capsys, lci_instance_id):
158+
retry_429(snippets.create_instance_with_autoscaling_config)(
159+
lci_instance_id,
160+
)
161+
out, _ = capsys.readouterr()
162+
assert lci_instance_id in out
163+
assert "autoscaling config" in out
164+
spanner_client = spanner.Client()
165+
instance = spanner_client.instance(lci_instance_id)
166+
retry_429(instance.delete)()
167+
168+
157169
def test_update_database(capsys, instance_id, sample_database):
158170
snippets.update_database(instance_id, sample_database.database_id)
159171
out, _ = capsys.readouterr()

0 commit comments

Comments
 (0)