Skip to content

Commit 4fdc797

Browse files
feat(spanner): Add autoscaling config sample (#1984)
1 parent 3c9ca01 commit 4fdc797

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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);

spanner/test/spannerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class spannerTest extends TestCase
4141

4242
use RetryTrait, EventuallyConsistentTestTrait;
4343

44+
/** @var string autoscalingInstanceId */
45+
protected static $autoscalingInstanceId;
46+
4447
/** @var string instanceId */
4548
protected static $instanceId;
4649

@@ -117,6 +120,7 @@ public static function setUpBeforeClass(): void
117120
'projectId' => self::$projectId,
118121
]);
119122

123+
self::$autoscalingInstanceId = 'test-' . time() . rand();
120124
self::$instanceId = 'test-' . time() . rand();
121125
self::$lowCostInstanceId = 'test-' . time() . rand();
122126
self::$databaseId = 'test-' . time() . rand();
@@ -168,6 +172,17 @@ public function testCreateInstanceConfig()
168172
$this->assertStringContainsString(sprintf('Created instance configuration %s', self::$customInstanceConfigId), $output);
169173
}
170174

175+
public function testCreateInstanceWithAutoscalingConfig()
176+
{
177+
$output = $this->runAdminFunctionSnippet('create_instance_with_autoscaling_config', [
178+
'project_id' => self::$projectId,
179+
'instance_id' => self::$autoscalingInstanceId
180+
]);
181+
$this->assertStringContainsString('Waiting for operation to complete...', $output);
182+
$this->assertStringContainsString('Created instance test-', $output);
183+
$this->assertStringContainsString('minNodes set to 1', $output);
184+
}
185+
171186
/**
172187
* @depends testCreateInstanceConfig
173188
*/

0 commit comments

Comments
 (0)