Skip to content

Commit

Permalink
feat: Managed Autoscaler (#2015)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
alkatrivedi and gcf-owl-bot[bot] committed Mar 6, 2024
1 parent 5bf9f30 commit 547ca1b
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
Expand Down
18 changes: 18 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ and automatic, synchronous replication for high availability.
* [Deletes a user-managed instance configuration.](#deletes-a-user-managed-instance-configuration.)
* [Lists the instance configuration operations.](#lists-the-instance-configuration-operations.)
* [Updates a user-managed instance configuration.](#updates-a-user-managed-instance-configuration.)
* [Creates a instance with autoscaling config.](#creates-a-instance-with-autoscaling-config.)
* [Instance-with-processing-units](#instance-with-processing-units)
* [Instance](#instance)
* [Json-add-column](#json-add-column)
Expand Down Expand Up @@ -841,6 +842,23 @@ __Usage:__



### Creates a instance with autoscaling config.

View the [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)

__Usage:__


`node instance-with-autoscaling-config.js <INSTANCE_ID> <PROJECT_ID>`


-----




### Instance-with-processing-units

View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/instance-with-processing-units.js).
Expand Down
123 changes: 123 additions & 0 deletions samples/instance-with-autoscaling-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* 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.
*/

// sample-metadata:
// title: Creates a instance with autoscaling config.
// usage: node instance-with-autoscaling-config.js <INSTANCE_ID> <PROJECT_ID>

'use strict';

function main(instanceId = 'my-instance', projectId = 'my-project-id') {
async function createInstanceWithAutoscalingConfig() {
// [START spanner_create_instance_with_autoscaling_config]
// Imports the Google Cloud client library
const {Spanner, protos} = require('@google-cloud/spanner');

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';

// Creates a client
const spanner = new Spanner({
projectId: projectId,
});

// Get the instance admin client
const instanceAdminClient = spanner.getInstanceAdminClient();

const autoscalingConfig =
protos.google.spanner.admin.instance.v1.AutoscalingConfig.create({
// Only one of minNodes/maxNodes or minProcessingUnits/maxProcessingUnits can be set.
autoscalingLimits:
protos.google.spanner.admin.instance.v1.AutoscalingConfig.AutoscalingLimits.create(
{
minNodes: 1,
maxNodes: 2,
}
),
// highPriorityCpuUtilizationPercent and storageUtilizationPercent are both
// percentages and must lie between 0 and 100.
autoscalingTargets:
protos.google.spanner.admin.instance.v1.AutoscalingConfig.AutoscalingTargets.create(
{
highPriorityCpuUtilizationPercent: 65,
storageUtilizationPercent: 95,
}
),
});

// Creates a new instance with autoscaling configuration
// When autoscalingConfig is enabled, nodeCount and processingUnits fields
// need not be specified.
try {
console.log(
`Creating instance ${instanceAdminClient.instancePath(
projectId,
instanceId
)}.`
);
const [operation] = await instanceAdminClient.createInstance({
instanceId: instanceId,
parent: instanceAdminClient.projectPath(projectId),
instance: {
config: instanceAdminClient.instanceConfigPath(
projectId,
'regional-us-central1'
),
displayName: 'Display name for the instance.',
autoscalingConfig: autoscalingConfig,
labels: {
cloud_spanner_samples: 'true',
created: Math.round(Date.now() / 1000).toString(), // current time
},
},
});

console.log(`Waiting for operation on ${instanceId} to complete...`);
await operation.promise();
console.log(`Created instance ${instanceId}.`);

// get instance metadata
const [metadata] = await instanceAdminClient.getInstance({
name: instanceAdminClient.instancePath(projectId, instanceId),
});
console.log(
`Autoscaling configurations of ${instanceId} are: ` +
'\n' +
`Min nodes: ${metadata.autoscalingConfig.autoscalingLimits.minNodes} ` +
'nodes.' +
'\n' +
`Max nodes: ${metadata.autoscalingConfig.autoscalingLimits.maxNodes}` +
' nodes.' +
'\n' +
`High priority cpu utilization percent: ${metadata.autoscalingConfig.autoscalingTargets.highPriorityCpuUtilizationPercent}.` +
'\n' +
`Storage utilization percent: ${metadata.autoscalingConfig.autoscalingTargets.storageUtilizationPercent}.`
);
} catch (err) {
console.error('ERROR:', err);
}
// [END spanner_create_instance_with_autoscaling_config]
}
createInstanceWithAutoscalingConfig();
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
33 changes: 33 additions & 0 deletions samples/system-test/spanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,39 @@ describe('Autogenerated Admin Clients', () => {
new RegExp(`Instance ${SAMPLE_INSTANCE_ID} has 500 processing units.`)
);
});

// create_instance_with_autoscaling_config
it('should create an example instance with autoscaling config', async () => {
const output = execSync(
`node instance-with-autoscaling-config.js "${SAMPLE_INSTANCE_ID}" ${PROJECT_ID}`
);
assert.match(
output,
new RegExp(
`Waiting for operation on ${SAMPLE_INSTANCE_ID} to complete...`
)
);
assert.match(
output,
new RegExp(`Created instance ${SAMPLE_INSTANCE_ID}.`)
);
assert.match(
output,
new RegExp(
`Autoscaling configurations of ${SAMPLE_INSTANCE_ID} are: ` +
'\n' +
'Min nodes: 1 ' +
'nodes.' +
'\n' +
'Max nodes: 2' +
' nodes.' +
'\n' +
'High priority cpu utilization percent: 65.' +
'\n' +
'Storage utilization percent: 95.'
)
);
});
});

// check that base instance was created
Expand Down

0 comments on commit 547ca1b

Please sign in to comment.