Skip to content

Commit 660a57d

Browse files
feat(Spanner): Add read write retry sample (GoogleCloudPlatform#1694)
Add Spanner read write retry sample
1 parent 3168ea5 commit 660a57d

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

spanner/src/read_write_retry.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
/**
3+
* Copyright 2022 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/master/spanner/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Spanner;
25+
26+
// [START spanner_read_write_retry]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
use Google\Cloud\Spanner\Transaction;
29+
30+
/**
31+
* Performs a read-write transaction to update two sample records in the
32+
* database. if the transaction fails, it will retry for exactly $maxRetries times.
33+
*
34+
* Before running this sample, you will need to run the `update_data` sample
35+
* to populate the fields.
36+
* Example:
37+
* ```
38+
* read_write_retry($instanceId, $databaseId);
39+
* ```
40+
*
41+
* @param string $instanceId The Spanner instance ID.
42+
* @param string $databaseId The Spanner database ID.
43+
*/
44+
function read_write_retry(string $instanceId, string $databaseId): void
45+
{
46+
$spanner = new SpannerClient();
47+
$instance = $spanner->instance($instanceId);
48+
$database = $instance->database($databaseId);
49+
$maxRetries = 2;
50+
51+
$database->runTransaction(function (Transaction $t) use ($spanner) {
52+
// Read the second album's budget.
53+
$secondAlbumKey = [2, 2];
54+
$secondAlbumKeySet = $spanner->keySet(['keys' => [$secondAlbumKey]]);
55+
$secondAlbumResult = $t->read(
56+
'Albums',
57+
$secondAlbumKeySet,
58+
['MarketingBudget'],
59+
['limit' => 1]
60+
);
61+
$firstRow = $secondAlbumResult->rows()->current();
62+
$secondAlbumBudget = $firstRow['MarketingBudget'];
63+
64+
printf('Setting second album\'s budget as the first album\'s budget.' . PHP_EOL);
65+
66+
// Update the row.
67+
$t->updateBatch('Albums', [
68+
['SingerId' => 1, 'AlbumId' => 1, 'MarketingBudget' => $secondAlbumBudget],
69+
]);
70+
71+
// Commit the transaction!
72+
$t->commit();
73+
74+
print('Transaction complete.' . PHP_EOL);
75+
}, ['maxRetries' => $maxRetries]);
76+
}
77+
// [END spanner_read_write_retry]
78+
79+
// The following 2 lines are only needed to run the samples
80+
require_once __DIR__ . '/../../testing/sample_helpers.php';
81+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/test/spannerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,16 @@ public function testAddDropDatabaseRole()
938938
$this->assertStringContainsString('Revoked privileges and dropped roles new_child and new_parent ' . PHP_EOL, $output);
939939
}
940940

941+
/**
942+
* @depends testUpdateData
943+
*/
944+
public function testReadWriteRetry()
945+
{
946+
$output = $this->runFunctionSnippet('read_write_retry');
947+
$this->assertStringContainsString('Setting second album\'s budget as the first album\'s budget.', $output);
948+
$this->assertStringContainsString('Transaction complete.', $output);
949+
}
950+
941951
private function testGetInstanceConfig()
942952
{
943953
$output = $this->runFunctionSnippet('get_instance_config', [

0 commit comments

Comments
 (0)