Skip to content

Commit 53295d7

Browse files
Autoscaling cluster config bigtable GoogleCloudPlatform#1620
Adding samples (and tests) for autoscaling Bigtable clusters: create_cluster_autoscale_config, update_cluster_autoscale_config and disable_cluster_autoscale_config
1 parent ea68c93 commit 53295d7

File tree

4 files changed

+353
-6
lines changed

4 files changed

+353
-6
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 2022 Google LLC.
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/master/bigtable/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Bigtable;
25+
26+
// [START bigtable_api_cluster_create_autoscaling]
27+
use Google\Cloud\Bigtable\Admin\V2\AutoscalingLimits;
28+
use Google\Cloud\Bigtable\Admin\V2\AutoscalingTargets;
29+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
30+
use Google\Cloud\Bigtable\Admin\V2\Cluster;
31+
use Google\Cloud\Bigtable\Admin\V2\Cluster\ClusterAutoscalingConfig;
32+
use Google\Cloud\Bigtable\Admin\V2\Cluster\ClusterConfig;
33+
use Google\Cloud\Bigtable\Admin\V2\StorageType;
34+
35+
/**
36+
* Creates a new autoscaling cluster in an existing Bigtable instance.
37+
*
38+
* @param string $projectId The Google Cloud project ID
39+
* @param string $instanceId The ID of the Bigtable instance
40+
* @param string $clusterId The ID of the cluster to be created
41+
* @param string $locationId The Bigtable region ID where you want your cluster to reside
42+
*/
43+
function create_cluster_autoscale_config(
44+
string $projectId,
45+
string $instanceId,
46+
string $clusterId,
47+
string $locationId = 'us-east1-b'
48+
): void {
49+
$instanceAdminClient = new BigtableInstanceAdminClient();
50+
$autoscalingLimits = new AutoscalingLimits([
51+
'min_serve_nodes' => 2,
52+
'max_serve_nodes' => 5,
53+
]);
54+
$autoscalingTargets = new AutoscalingTargets([
55+
'cpu_utilization_percent' => 10,
56+
]);
57+
$clusterAutoscaleConfig = new ClusterAutoscalingConfig([
58+
'autoscaling_limits' => $autoscalingLimits,
59+
'autoscaling_targets' => $autoscalingTargets,
60+
]);
61+
62+
$clusterConfig = new ClusterConfig([
63+
'cluster_autoscaling_config' => $clusterAutoscaleConfig,
64+
]);
65+
66+
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
67+
printf('Adding Cluster to Instance %s' . PHP_EOL, $instanceId);
68+
$cluster = new Cluster();
69+
70+
// if both serve nodes and autoscaling are set
71+
// the server will silently ignore the serve nodes
72+
// and use auto scaling functionality
73+
// $cluster->setServeNodes($newNumNodes);
74+
$cluster->setDefaultStorageType(StorageType::SSD);
75+
$cluster->setLocation(
76+
$instanceAdminClient->locationName(
77+
$projectId,
78+
$locationId
79+
)
80+
);
81+
$cluster->setClusterConfig($clusterConfig);
82+
$operationResponse = $instanceAdminClient->createCluster($instanceName, $clusterId, $cluster);
83+
84+
$operationResponse->pollUntilComplete();
85+
if ($operationResponse->operationSucceeded()) {
86+
$result = $operationResponse->getResult();
87+
printf('Cluster created: %s' . PHP_EOL, $clusterId);
88+
} else {
89+
$error = $operationResponse->getError();
90+
printf('Cluster not created: %s' . PHP_EOL, $error->getMessage());
91+
}
92+
}
93+
// [END bigtable_api_cluster_create_autoscaling]
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);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google LLC.
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/master/bigtable/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Bigtable;
25+
26+
// [START bigtable_api_cluster_disable_autoscaling]
27+
use Google\ApiCore\ApiException;
28+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
29+
use Google\Cloud\Bigtable\Admin\V2\Cluster\ClusterConfig;
30+
31+
use Google\Protobuf\FieldMask;
32+
33+
/**
34+
* Disables autoscaling config in an existing Bigtable cluster.
35+
*
36+
* @param string $projectId The Google Cloud project ID
37+
* @param string $instanceId The ID of the Bigtable instance
38+
* @param string $clusterId The ID of the cluster to be updated
39+
* @param int $newNumNodes The fixed number of serve nodes the cluster should have
40+
*/
41+
function disable_cluster_autoscale_config(
42+
string $projectId,
43+
string $instanceId,
44+
string $clusterId,
45+
int $newNumNodes = 1
46+
): void {
47+
$instanceAdminClient = new BigtableInstanceAdminClient();
48+
$clusterName = $instanceAdminClient->clusterName($projectId, $instanceId, $clusterId);
49+
$cluster = $instanceAdminClient->getCluster($clusterName);
50+
51+
// static serve node is required to disable auto scale config
52+
$cluster->setServeNodes($newNumNodes);
53+
// clearing the autoscale config
54+
55+
$cluster->setClusterConfig(new ClusterConfig());
56+
57+
$updateMask = new FieldMask([
58+
'paths' => ['serve_nodes', 'cluster_config'],
59+
]);
60+
61+
try {
62+
$operationResponse = $instanceAdminClient->partialUpdateCluster($cluster, $updateMask);
63+
$operationResponse->pollUntilComplete();
64+
if ($operationResponse->operationSucceeded()) {
65+
$updatedCluster = $operationResponse->getResult();
66+
printf('Cluster updated with the new num of nodes: %s.' . PHP_EOL, $updatedCluster->getServeNodes());
67+
} else {
68+
$error = $operationResponse->getError();
69+
printf('Cluster %s failed to update: %s.' . PHP_EOL, $clusterId, $error->getMessage());
70+
}
71+
} catch (ApiException $e) {
72+
if ($e->getStatus() === 'NOT_FOUND') {
73+
printf('Cluster %s does not exist.' . PHP_EOL, $clusterId);
74+
return;
75+
}
76+
throw $e;
77+
}
78+
}
79+
// [END bigtable_api_cluster_disable_autoscaling]
80+
81+
// The following 2 lines are only needed to run the samples
82+
require_once __DIR__ . '/../../testing/sample_helpers.php';
83+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google LLC.
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/master/bigtable/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Bigtable;
25+
26+
// [START bigtable_api_cluster_update_autoscaling]
27+
use Google\ApiCore\ApiException;
28+
use Google\Cloud\Bigtable\Admin\V2\AutoscalingLimits;
29+
use Google\Cloud\Bigtable\Admin\V2\AutoscalingTargets;
30+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
31+
use Google\Cloud\Bigtable\Admin\V2\Cluster\ClusterAutoscalingConfig;
32+
use Google\Cloud\Bigtable\Admin\V2\Cluster\ClusterConfig;
33+
use Google\Protobuf\FieldMask;
34+
35+
/**
36+
* Updates autoscale configurations for an existing Bigtable cluster.
37+
*
38+
* @param string $projectId The Google Cloud project ID
39+
* @param string $instanceId The ID of the Bigtable instance
40+
* @param string $clusterId The ID of the cluster to be updated
41+
*/
42+
function update_cluster_autoscale_config(
43+
string $projectId,
44+
string $instanceId,
45+
string $clusterId
46+
): void {
47+
$instanceAdminClient = new BigtableInstanceAdminClient();
48+
$clusterName = $instanceAdminClient->clusterName($projectId, $instanceId, $clusterId);
49+
$cluster = $instanceAdminClient->getCluster($clusterName);
50+
51+
$autoscalingLimits = new AutoscalingLimits([
52+
'min_serve_nodes' => 2,
53+
'max_serve_nodes' => 5,
54+
]);
55+
$autoscalingTargets = new AutoscalingTargets([
56+
'cpu_utilization_percent' => 20,
57+
]);
58+
$clusterAutoscaleConfig = new ClusterAutoscalingConfig([
59+
'autoscaling_limits' => $autoscalingLimits,
60+
'autoscaling_targets' => $autoscalingTargets,
61+
]);
62+
$clusterConfig = new ClusterConfig([
63+
'cluster_autoscaling_config' => $clusterAutoscaleConfig,
64+
]);
65+
66+
$cluster->setClusterConfig($clusterConfig);
67+
68+
$updateMask = new FieldMask([
69+
'paths' => [
70+
// if both serve nodes and autoscaling configs are set
71+
// the server will silently ignore the `serve_nodes` agument
72+
// 'serve_nodes',
73+
'cluster_config'
74+
],
75+
]);
76+
77+
try {
78+
$operationResponse = $instanceAdminClient->partialUpdateCluster($cluster, $updateMask);
79+
80+
$operationResponse->pollUntilComplete();
81+
if ($operationResponse->operationSucceeded()) {
82+
$updatedCluster = $operationResponse->getResult();
83+
printf('Cluster %s updated with autoscale config.' . PHP_EOL, $clusterId);
84+
} else {
85+
$error = $operationResponse->getError();
86+
printf('Cluster %s failed to update: %s.' . PHP_EOL, $clusterId, $error->getMessage());
87+
}
88+
} catch (ApiException $e) {
89+
if ($e->getStatus() === 'NOT_FOUND') {
90+
printf('Cluster %s does not exist.' . PHP_EOL, $clusterId);
91+
return;
92+
}
93+
throw $e;
94+
}
95+
}
96+
// [END bigtable_api_cluster_update_autoscaling]
97+
98+
// The following 2 lines are only needed to run the samples
99+
require_once __DIR__ . '/../../testing/sample_helpers.php';
100+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/test/bigtableTest.php

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ final class BigtableTest extends TestCase
1010
{
1111
use BigtableTestTrait;
1212

13-
const INSTANCE_ID_PREFIX = 'php-instance-';
14-
const CLUSTER_ID_PREFIX = 'php-cluster-';
15-
const TABLE_ID_PREFIX = 'php-table-';
16-
const APP_PROFILE_ID_PREFIX = 'php-app-profile-';
17-
const SERVICE_ACCOUNT_ID_PREFIX = 'php-sa-'; // Shortened due to length constraint b/w 6 and 30.
13+
public const CLUSTER_ID_PREFIX = 'php-cluster-';
14+
public const INSTANCE_ID_PREFIX = 'php-instance-';
15+
public const TABLE_ID_PREFIX = 'php-table-';
16+
public const APP_PROFILE_ID_PREFIX = 'php-app-profile-';
17+
public const SERVICE_ACCOUNT_ID_PREFIX = 'php-sa-'; // Shortened due to length constraint b/w 6 and 30.
1818

19+
private static $autoscalingClusterId;
1920
private static $clusterId;
2021
private static $appProfileId;
2122
private static $serviceAccountId;
@@ -34,8 +35,9 @@ public function setUp(): void
3435

3536
public function testCreateProductionInstance()
3637
{
37-
self::$instanceId = uniqid(self::INSTANCE_ID_PREFIX);
38+
self::$autoscalingClusterId = uniqid(self::CLUSTER_ID_PREFIX);
3839
self::$clusterId = uniqid(self::CLUSTER_ID_PREFIX);
40+
self::$instanceId = uniqid(self::INSTANCE_ID_PREFIX);
3941
self::$appProfileId = uniqid(self::APP_PROFILE_ID_PREFIX);
4042

4143
$content = self::runFunctionSnippet('create_production_instance', [
@@ -233,6 +235,71 @@ public function testCreateAndDeleteCluster()
233235
}
234236
}
235237

238+
/**
239+
* @depends testCreateProductionInstance
240+
*/
241+
public function testCreateClusterWithAutoscaling()
242+
{
243+
$content = self::runFunctionSnippet('create_cluster_autoscale_config', [
244+
self::$projectId,
245+
self::$instanceId,
246+
self::$autoscalingClusterId,
247+
'us-east1-c'
248+
]);
249+
250+
// get the cluster name created with above id
251+
$clusterName = self::$instanceAdminClient->clusterName(
252+
self::$projectId,
253+
self::$instanceId,
254+
self::$autoscalingClusterId,
255+
);
256+
257+
$this->checkCluster($clusterName);
258+
$this->assertStringContainsString(sprintf(
259+
'Cluster created: %s',
260+
self::$autoscalingClusterId,
261+
), $content);
262+
}
263+
264+
/**
265+
* @depends testCreateClusterWithAutoscaling
266+
*/
267+
public function testUpdateClusterWithAutoscaling()
268+
{
269+
// Update autoscale config in cluster
270+
$content = self::runFunctionSnippet('update_cluster_autoscale_config', [
271+
self::$projectId,
272+
self::$instanceId,
273+
self::$autoscalingClusterId,
274+
]);
275+
276+
$this->assertStringContainsString(sprintf(
277+
'Cluster %s updated with autoscale config.',
278+
self::$autoscalingClusterId,
279+
), $content);
280+
}
281+
282+
/**
283+
* @depends testCreateClusterWithAutoscaling
284+
*/
285+
public function testDisableAutoscalingInCluster()
286+
{
287+
$numNodes = 2;
288+
289+
// Disable autoscale config in cluster
290+
$content = self::runFunctionSnippet('disable_cluster_autoscale_config', [
291+
self::$projectId,
292+
self::$instanceId,
293+
self::$autoscalingClusterId,
294+
$numNodes
295+
]);
296+
297+
$this->assertStringContainsString(sprintf(
298+
'Cluster updated with the new num of nodes: %s.',
299+
$numNodes,
300+
), $content);
301+
}
302+
236303
public function testCreateDevInstance()
237304
{
238305
$instanceId = uniqid(self::INSTANCE_ID_PREFIX);

0 commit comments

Comments
 (0)