|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * For instructions on how to run the full sample: |
| 20 | + * |
| 21 | + * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/spanner/README.md |
| 22 | + */ |
| 23 | + |
| 24 | +namespace Google\Cloud\Samples\Spanner; |
| 25 | + |
| 26 | +// [START spanner_create_instance_with_autoscaling_config] |
| 27 | +use Google\Cloud\Spanner\Admin\Instance\V1\AutoscalingConfig; |
| 28 | +use Google\Cloud\Spanner\Admin\Instance\V1\AutoscalingConfig\AutoscalingLimits; |
| 29 | +use Google\Cloud\Spanner\Admin\Instance\V1\AutoscalingConfig\AutoscalingTargets; |
| 30 | +use Google\Cloud\Spanner\Admin\Instance\V1\Client\InstanceAdminClient; |
| 31 | +use Google\Cloud\Spanner\Admin\Instance\V1\CreateInstanceRequest; |
| 32 | +use Google\Cloud\Spanner\Admin\Instance\V1\GetInstanceRequest; |
| 33 | +use Google\Cloud\Spanner\Admin\Instance\V1\Instance; |
| 34 | + |
| 35 | +/** |
| 36 | + * Creates an instance with autoscaling configuration. |
| 37 | + * Example: |
| 38 | + * ``` |
| 39 | + * create_instance_with_autoscaling_config($projectId, $instanceId, $databaseId); |
| 40 | + * ``` |
| 41 | + * |
| 42 | + * @param string $projectId The Google Cloud project ID. |
| 43 | + * @param string $instanceId The Spanner instance ID. |
| 44 | + */ |
| 45 | +function create_instance_with_autoscaling_config(string $projectId, string $instanceId): void |
| 46 | +{ |
| 47 | + $instanceAdminClient = new InstanceAdminClient(); |
| 48 | + |
| 49 | + $projectName = $instanceAdminClient->projectName($projectId); |
| 50 | + $instanceName = $instanceAdminClient->instanceName($projectId, $instanceId); |
| 51 | + $configName = $instanceAdminClient->instanceConfigName($projectId, 'regional-us-central1'); |
| 52 | + // Only one of minNodes/maxNodes or minProcessingUnits/maxProcessingUnits |
| 53 | + // can be set. Both min and max need to be set and |
| 54 | + // maxNodes/maxProcessingUnits can be at most 10X of |
| 55 | + // minNodes/minProcessingUnits. |
| 56 | + // highPriorityCpuUtilizationPercent and storageUtilizationPercent are both |
| 57 | + // percentages and must lie between 0 and 100. |
| 58 | + $autoScalingConfig = (new AutoscalingConfig()) |
| 59 | + ->setAutoscalingLimits((new AutoscalingLimits()) |
| 60 | + ->setMinNodes(1) |
| 61 | + ->setMaxNodes(2)) |
| 62 | + ->setAutoscalingTargets((new AutoscalingTargets()) |
| 63 | + ->setHighPriorityCpuUtilizationPercent(65) |
| 64 | + ->setStorageUtilizationPercent(95)); |
| 65 | + |
| 66 | + $instance = (new Instance()) |
| 67 | + ->setName($instanceName) |
| 68 | + ->setConfig($configName) |
| 69 | + ->setDisplayName('This is a display name.') |
| 70 | + ->setLabels(['cloud_spanner_samples' => true]) |
| 71 | + ->setAutoscalingConfig($autoScalingConfig); |
| 72 | + |
| 73 | + $operation = $instanceAdminClient->createInstance( |
| 74 | + (new CreateInstanceRequest()) |
| 75 | + ->setParent($projectName) |
| 76 | + ->setInstanceId($instanceId) |
| 77 | + ->setInstance($instance) |
| 78 | + ); |
| 79 | + |
| 80 | + print('Waiting for operation to complete...' . PHP_EOL); |
| 81 | + $operation->pollUntilComplete(); |
| 82 | + |
| 83 | + printf('Created instance %s' . PHP_EOL, $instanceId); |
| 84 | + |
| 85 | + $request = new GetInstanceRequest(['name' => $instanceName]); |
| 86 | + $instanceInfo = $instanceAdminClient->getInstance($request); |
| 87 | + printf( |
| 88 | + 'Instance %s has minNodes set to %d.' . PHP_EOL, |
| 89 | + $instanceId, |
| 90 | + $instanceInfo->getAutoscalingConfig()->getAutoscalingLimits()->getMinNodes() |
| 91 | + ); |
| 92 | +} |
| 93 | +// [END spanner_create_instance_with_autoscaling_config] |
| 94 | + |
| 95 | +// The following 2 lines are only needed to run the samples |
| 96 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 97 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments