|
| 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)); |
0 commit comments