Skip to content

Commit 547ca1b

Browse files
feat: Managed Autoscaler (#2015)
* feat: Managed Autoscaler * refactor: sample for managed autoscaler * refactor comments * refactor: integration tests * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * refactor: sample * refactor: comments * refactor: comments * refactor: comments --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 5bf9f30 commit 547ca1b

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
131131
| Deletes a user-managed instance configuration. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-config-delete.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/instance-config-delete.js,samples/README.md) |
132132
| Lists the instance configuration operations. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-config-get-operations.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/instance-config-get-operations.js,samples/README.md) |
133133
| Updates a user-managed instance configuration. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-config-update.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/instance-config-update.js,samples/README.md) |
134+
| Creates a instance with autoscaling config. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-with-autoscaling-config.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/instance-with-autoscaling-config.js,samples/README.md) |
134135
| Instance-with-processing-units | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-with-processing-units.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/instance-with-processing-units.js,samples/README.md) |
135136
| Instance | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/instance.js,samples/README.md) |
136137
| Json-add-column | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/json-add-column.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/json-add-column.js,samples/README.md) |

samples/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ and automatic, synchronous replication for high availability.
5656
* [Deletes a user-managed instance configuration.](#deletes-a-user-managed-instance-configuration.)
5757
* [Lists the instance configuration operations.](#lists-the-instance-configuration-operations.)
5858
* [Updates a user-managed instance configuration.](#updates-a-user-managed-instance-configuration.)
59+
* [Creates a instance with autoscaling config.](#creates-a-instance-with-autoscaling-config.)
5960
* [Instance-with-processing-units](#instance-with-processing-units)
6061
* [Instance](#instance)
6162
* [Json-add-column](#json-add-column)
@@ -841,6 +842,23 @@ __Usage:__
841842

842843

843844

845+
### Creates a instance with autoscaling config.
846+
847+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-with-autoscaling-config.js).
848+
849+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/instance-with-autoscaling-config.js,samples/README.md)
850+
851+
__Usage:__
852+
853+
854+
`node instance-with-autoscaling-config.js <INSTANCE_ID> <PROJECT_ID>`
855+
856+
857+
-----
858+
859+
860+
861+
844862
### Instance-with-processing-units
845863

846864
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-with-processing-units.js).
+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
// sample-metadata:
17+
// title: Creates a instance with autoscaling config.
18+
// usage: node instance-with-autoscaling-config.js <INSTANCE_ID> <PROJECT_ID>
19+
20+
'use strict';
21+
22+
function main(instanceId = 'my-instance', projectId = 'my-project-id') {
23+
async function createInstanceWithAutoscalingConfig() {
24+
// [START spanner_create_instance_with_autoscaling_config]
25+
// Imports the Google Cloud client library
26+
const {Spanner, protos} = require('@google-cloud/spanner');
27+
28+
/**
29+
* TODO(developer): Uncomment the following lines before running the sample.
30+
*/
31+
// const projectId = 'my-project-id';
32+
// const instanceId = 'my-instance';
33+
34+
// Creates a client
35+
const spanner = new Spanner({
36+
projectId: projectId,
37+
});
38+
39+
// Get the instance admin client
40+
const instanceAdminClient = spanner.getInstanceAdminClient();
41+
42+
const autoscalingConfig =
43+
protos.google.spanner.admin.instance.v1.AutoscalingConfig.create({
44+
// Only one of minNodes/maxNodes or minProcessingUnits/maxProcessingUnits can be set.
45+
autoscalingLimits:
46+
protos.google.spanner.admin.instance.v1.AutoscalingConfig.AutoscalingLimits.create(
47+
{
48+
minNodes: 1,
49+
maxNodes: 2,
50+
}
51+
),
52+
// highPriorityCpuUtilizationPercent and storageUtilizationPercent are both
53+
// percentages and must lie between 0 and 100.
54+
autoscalingTargets:
55+
protos.google.spanner.admin.instance.v1.AutoscalingConfig.AutoscalingTargets.create(
56+
{
57+
highPriorityCpuUtilizationPercent: 65,
58+
storageUtilizationPercent: 95,
59+
}
60+
),
61+
});
62+
63+
// Creates a new instance with autoscaling configuration
64+
// When autoscalingConfig is enabled, nodeCount and processingUnits fields
65+
// need not be specified.
66+
try {
67+
console.log(
68+
`Creating instance ${instanceAdminClient.instancePath(
69+
projectId,
70+
instanceId
71+
)}.`
72+
);
73+
const [operation] = await instanceAdminClient.createInstance({
74+
instanceId: instanceId,
75+
parent: instanceAdminClient.projectPath(projectId),
76+
instance: {
77+
config: instanceAdminClient.instanceConfigPath(
78+
projectId,
79+
'regional-us-central1'
80+
),
81+
displayName: 'Display name for the instance.',
82+
autoscalingConfig: autoscalingConfig,
83+
labels: {
84+
cloud_spanner_samples: 'true',
85+
created: Math.round(Date.now() / 1000).toString(), // current time
86+
},
87+
},
88+
});
89+
90+
console.log(`Waiting for operation on ${instanceId} to complete...`);
91+
await operation.promise();
92+
console.log(`Created instance ${instanceId}.`);
93+
94+
// get instance metadata
95+
const [metadata] = await instanceAdminClient.getInstance({
96+
name: instanceAdminClient.instancePath(projectId, instanceId),
97+
});
98+
console.log(
99+
`Autoscaling configurations of ${instanceId} are: ` +
100+
'\n' +
101+
`Min nodes: ${metadata.autoscalingConfig.autoscalingLimits.minNodes} ` +
102+
'nodes.' +
103+
'\n' +
104+
`Max nodes: ${metadata.autoscalingConfig.autoscalingLimits.maxNodes}` +
105+
' nodes.' +
106+
'\n' +
107+
`High priority cpu utilization percent: ${metadata.autoscalingConfig.autoscalingTargets.highPriorityCpuUtilizationPercent}.` +
108+
'\n' +
109+
`Storage utilization percent: ${metadata.autoscalingConfig.autoscalingTargets.storageUtilizationPercent}.`
110+
);
111+
} catch (err) {
112+
console.error('ERROR:', err);
113+
}
114+
// [END spanner_create_instance_with_autoscaling_config]
115+
}
116+
createInstanceWithAutoscalingConfig();
117+
}
118+
119+
process.on('unhandledRejection', err => {
120+
console.error(err.message);
121+
process.exitCode = 1;
122+
});
123+
main(...process.argv.slice(2));

samples/system-test/spanner.test.js

+33
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,39 @@ describe('Autogenerated Admin Clients', () => {
276276
new RegExp(`Instance ${SAMPLE_INSTANCE_ID} has 500 processing units.`)
277277
);
278278
});
279+
280+
// create_instance_with_autoscaling_config
281+
it('should create an example instance with autoscaling config', async () => {
282+
const output = execSync(
283+
`node instance-with-autoscaling-config.js "${SAMPLE_INSTANCE_ID}" ${PROJECT_ID}`
284+
);
285+
assert.match(
286+
output,
287+
new RegExp(
288+
`Waiting for operation on ${SAMPLE_INSTANCE_ID} to complete...`
289+
)
290+
);
291+
assert.match(
292+
output,
293+
new RegExp(`Created instance ${SAMPLE_INSTANCE_ID}.`)
294+
);
295+
assert.match(
296+
output,
297+
new RegExp(
298+
`Autoscaling configurations of ${SAMPLE_INSTANCE_ID} are: ` +
299+
'\n' +
300+
'Min nodes: 1 ' +
301+
'nodes.' +
302+
'\n' +
303+
'Max nodes: 2' +
304+
' nodes.' +
305+
'\n' +
306+
'High priority cpu utilization percent: 65.' +
307+
'\n' +
308+
'Storage utilization percent: 95.'
309+
)
310+
);
311+
});
279312
});
280313

281314
// check that base instance was created

0 commit comments

Comments
 (0)