-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathBatchClient.php
More file actions
273 lines (248 loc) · 8.35 KB
/
BatchClient.php
File metadata and controls
273 lines (248 loc) · 8.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Spanner\Batch;
use Google\Cloud\Core\TimeTrait;
use Google\Cloud\Spanner\Duration;
use Google\Cloud\Spanner\Operation;
use Google\Cloud\Spanner\Timestamp;
use Google\Cloud\Spanner\TransactionConfigurationTrait;
/**
* Provides Batch APIs used to read data from a Cloud Spanner database.
*
* Batch Clients are useful when one wants to read or query a large amount of
* data from Cloud Spanner across multiple processes, even across multiple
* machines. It allows creation of partitions of a Cloud Spanner database in
* order to facilitate reading or querying of each partition independently at
* the same snapshot.
*
* Example:
* ```
* use Google\Cloud\Spanner\SpannerClient;
*
* $spanner = new SpannerClient();
* $batch = $spanner->batch('instance-id', 'database-id');
* ```
*
* ```
* // Using Cloud Pub/Sub to share partitions with workers
* use Google\Cloud\PubSub\PubSubClient;
*
* $pubsub = new PubSubClient();
* $topic = $pubsub->topic('partition-queries');
*
* $snapshot = $batch->snapshot();
*
* // Executing a partition query will return a list of Partitions.
* $partitions = $snapshot->partitionQuery('SELECT * FROM Users WHERE firstName = %s AND location = %s', [
* 'parameters' => [
* 'firstName' => 'John',
* 'location' => 'USA'
* ]
* ]);
*
* // Each partition is published to Cloud Pub/Sub, where it can be executed by
* // a worker.
* foreach ($partitions as $partition) {
* $topic->publish([
* 'attributes' => [
* 'snapshot' => $snapshot->serialize(),
* 'partition' => $partition->serialize()
* ]
* ]);
* }
*
* // Once all workers have finished, we will close the snapshot.
* // The logic to determine whether the snapshot is no longer needed will vary
* // and is not implemented here.
* do {
* $finished = areWorkersDone();
*
* if ($finished) {
* $snapshot->close();
* }
* } while(!$finished);
* ```
*
* ```
* // Using Cloud Pub/Sub to consume a partition and return a result.
* use Google\Cloud\PubSub\PubSubClient;
*
* $pubsub = new PubSubClient();
* $subscription = $pubsub->subscription('partition-query-consumer');
*
* $messages = $subscription->pull([
* 'returnImmediately' => true,
* 'maxMessages' => 1
* ]);
*
* if ($messages) {
* $message = $messages[0];
* $snapshot = $batch->snapshotFromString($message->attribute('snapshot'));
* $partition = $batch->partitionFromString($message->attribute('partition'));
*
* // Do something with the query result.
* processResult($snapshot->executePartition($partition));
* }
* ```
*/
class BatchClient
{
use TimeTrait;
use TransactionConfigurationTrait;
const PARTITION_TYPE_KEY = '__partitionTypeName';
/**
* @var Operation
*/
private $operation;
/**
* @var string
*/
private $databaseName;
/**
* @var string|null
*/
private $databaseRole;
/**
* @var array
*/
private $allowedPartitionTypes = [
QueryPartition::class,
ReadPartition::class
];
/**
* @param Operation $operation A Cloud Spanner Operations wrapper.
* @param string $databaseName The database name to which the batch client
* instance is scoped.
* @param array $options [optional] {
* Configuration options.
*
* @type string $databaseRole The user created database role which creates the session.
* }
*/
public function __construct(Operation $operation, $databaseName, array $options = [])
{
$this->operation = $operation;
$this->databaseName = $databaseName;
$this->databaseRole = $options['databaseRole'] ?? '';
}
/**
* Create a batch snapshot.
*
* Example:
* ```
* $snapshot = $batch->snapshot();
* ```
*
* @param array $options [optional] {
* Configuration Options
*
* See [ReadOnly](https://cloud.google.com/spanner/reference/rpc/google.spanner.v1#google.spanner.v1.TransactionOptions.ReadOnly)
* for detailed description of available options.
*
* @type bool $transactionOptions.strong Read at a timestamp where all previously committed
* transactions are visible.
* @type Timestamp $transactionOptions.readTimestamp Executes all reads at the given
* timestamp.
* @type Duration $transactionOptions.exactStaleness Represents a number of seconds. Executes
* all reads at a timestamp that is $exactStaleness old.
* @type array $sessionOptions Configuration options for session creation.
* }
* @return BatchSnapshot
*/
public function snapshot(array $options = [])
{
$options += [
'transactionOptions' => [],
];
$sessionOptions = $this->pluck('sessionOptions', $options, false) ?: [];
// Single Use transactions are not supported in batch mode.
$options['transactionOptions']['singleUse'] = false;
$transactionOptions = $this->pluck('transactionOptions', $options);
$transactionOptions['returnReadTimestamp'] = true;
$transactionOptions = $this->configureReadOnlyTransactionOptions($transactionOptions);
if ($this->databaseRole !== null) {
$sessionOptions['creator_role'] = $this->databaseRole;
}
$session = $this->operation->createSession(
$this->databaseName,
$sessionOptions
);
return $this->operation->snapshot($session, [
'className' => BatchSnapshot::class,
'transactionOptions' => $transactionOptions
] + $options);
}
/**
* Create a {@see \Google\Cloud\Spanner\Batch\BatchSnapshot} from a snapshot
* identifier.
*
* This method can be used to deserialize a snapshot which is
* shared across multiple servers or processes.
*
* Example:
* ```
* $snapshot = $batch->snapshotFromString($snapshotString);
* ```
*
* @param string $identifier A stringified representation of {@see \Google\Cloud\Spanner\Batch\BatchSnapshot}.
* @return BatchSnapshot
*/
public function snapshotFromString($identifier)
{
$data = json_decode(base64_decode($identifier), true);
$missing = array_diff(
['sessionName', 'transactionId', 'readTimestamp'],
array_keys($data)
);
if ($missing) {
throw new \InvalidArgumentException('Invalid identifier.');
}
$session = $this->operation->session($data['sessionName']);
$readTime = $this->parseTimeString($data['readTimestamp']);
return $this->operation->createSnapshot($session, [
'id' => $data['transactionId'],
'readTimestamp' => $data['readTimestamp']
], BatchSnapshot::class);
}
/**
* Create a {@see \Google\Cloud\Spanner\Batch\PartitionInterface} instance.
*
* This method can be used to deserialize a partition which is
* shared across multiple servers or processes.
*
* Example:
* ```
* $partition = $batch->partitionFromString($partitionString);
* ```
*
* @param string $partition Partition data
* @return PartitionInterface
*/
public function partitionFromString($partition)
{
$data = json_decode(base64_decode($partition), true);
if (!isset($data[self::PARTITION_TYPE_KEY])) {
throw new \InvalidArgumentException('Invalid partition data.');
}
$class = $data[self::PARTITION_TYPE_KEY];
if (!in_array($class, $this->allowedPartitionTypes)) {
throw new \InvalidArgumentException('Invalid partition type.');
}
unset($data[self::PARTITION_TYPE_KEY]);
return $class::hydrate($data);
}
}