forked from googleapis/java-spanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatchClientImpl.java
More file actions
388 lines (358 loc) · 15.4 KB
/
BatchClientImpl.java
File metadata and controls
388 lines (358 loc) · 15.4 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* Copyright 2017 Google LLC
*
* 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.
*/
package com.google.cloud.spanner;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.cloud.spanner.AbstractReadContext.MultiUseReadOnlyTransaction;
import com.google.cloud.spanner.Options.QueryOption;
import com.google.cloud.spanner.Options.ReadOption;
import com.google.cloud.spanner.spi.v1.SpannerRpc;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.protobuf.Struct;
import com.google.spanner.v1.ExecuteSqlRequest.QueryMode;
import com.google.spanner.v1.PartitionQueryRequest;
import com.google.spanner.v1.PartitionReadRequest;
import com.google.spanner.v1.PartitionResponse;
import com.google.spanner.v1.TransactionSelector;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
/** Default implementation for Batch Client interface. */
public class BatchClientImpl implements BatchClient {
private final SessionClient sessionClient;
private final boolean isMultiplexedSessionEnabled;
/** Lock to protect the multiplexed session. */
private final ReentrantLock multiplexedSessionLock = new ReentrantLock();
/** The duration before we try to replace the multiplexed session. The default is 7 days. */
private final Duration sessionExpirationDuration;
/** The expiration date/time of the current multiplexed session. */
@GuardedBy("multiplexedSessionLock")
private final AtomicReference<Instant> expirationDate;
@GuardedBy("multiplexedSessionLock")
private final AtomicReference<SessionImpl> multiplexedSessionReference;
/**
* This flag is set to true if the server return UNIMPLEMENTED when partitioned transaction is
* executed on a multiplexed session. TODO: Remove once this is guaranteed to be available.
*/
@VisibleForTesting
static final AtomicBoolean unimplementedForPartitionedOps = new AtomicBoolean(false);
BatchClientImpl(SessionClient sessionClient, boolean isMultiplexedSessionEnabled) {
this.sessionClient = checkNotNull(sessionClient);
this.isMultiplexedSessionEnabled = isMultiplexedSessionEnabled;
this.sessionExpirationDuration =
Duration.ofMillis(
sessionClient
.getSpanner()
.getOptions()
.getSessionPoolOptions()
.getMultiplexedSessionMaintenanceDuration()
.toMillis());
// Initialize the expiration date to the start of time to avoid unnecessary null checks.
// This also ensured that a new session is created on first request.
this.expirationDate = new AtomicReference<>(Instant.MIN);
this.multiplexedSessionReference = new AtomicReference<>();
}
@Override
@Nullable
public String getDatabaseRole() {
return this.sessionClient.getSpanner().getOptions().getDatabaseRole();
}
@Override
public BatchReadOnlyTransaction batchReadOnlyTransaction(TimestampBound bound) {
SessionImpl session;
if (canUseMultiplexedSession()) {
session = getMultiplexedSession();
} else {
session = sessionClient.createSession();
}
return new BatchReadOnlyTransactionImpl(
MultiUseReadOnlyTransaction.newBuilder()
.setSession(session)
.setCancelQueryWhenClientIsClosed(true)
.setRpc(sessionClient.getSpanner().getRpc())
.setTimestampBound(bound)
.setDefaultQueryOptions(
sessionClient.getSpanner().getDefaultQueryOptions(sessionClient.getDatabaseId()))
.setExecutorProvider(sessionClient.getSpanner().getAsyncExecutorProvider())
.setDefaultPrefetchChunks(sessionClient.getSpanner().getDefaultPrefetchChunks())
.setDefaultDecodeMode(sessionClient.getSpanner().getDefaultDecodeMode())
.setDefaultDirectedReadOptions(
sessionClient.getSpanner().getOptions().getDirectedReadOptions())
.setSpan(sessionClient.getSpanner().getTracer().getCurrentSpan())
.setTracer(sessionClient.getSpanner().getTracer()),
checkNotNull(bound),
sessionClient);
}
@Override
public BatchReadOnlyTransaction batchReadOnlyTransaction(BatchTransactionId batchTransactionId) {
SessionImpl session =
sessionClient.sessionWithId(checkNotNull(batchTransactionId).getSessionId());
return new BatchReadOnlyTransactionImpl(
MultiUseReadOnlyTransaction.newBuilder()
.setSession(session)
.setCancelQueryWhenClientIsClosed(true)
.setRpc(sessionClient.getSpanner().getRpc())
.setTransactionId(batchTransactionId.getTransactionId())
.setTimestamp(batchTransactionId.getTimestamp())
.setDefaultQueryOptions(
sessionClient.getSpanner().getDefaultQueryOptions(sessionClient.getDatabaseId()))
.setExecutorProvider(sessionClient.getSpanner().getAsyncExecutorProvider())
.setDefaultPrefetchChunks(sessionClient.getSpanner().getDefaultPrefetchChunks())
.setDefaultDecodeMode(sessionClient.getSpanner().getDefaultDecodeMode())
.setDefaultDirectedReadOptions(
sessionClient.getSpanner().getOptions().getDirectedReadOptions())
.setSpan(sessionClient.getSpanner().getTracer().getCurrentSpan())
.setTracer(sessionClient.getSpanner().getTracer()),
batchTransactionId,
sessionClient);
}
private boolean canUseMultiplexedSession() {
return isMultiplexedSessionEnabled && !unimplementedForPartitionedOps.get();
}
private SessionImpl getMultiplexedSession() {
this.multiplexedSessionLock.lock();
try {
if (Clock.systemUTC().instant().isAfter(this.expirationDate.get())
|| this.multiplexedSessionReference.get() == null) {
this.multiplexedSessionReference.set(this.sessionClient.createMultiplexedSession());
this.expirationDate.set(Clock.systemUTC().instant().plus(this.sessionExpirationDuration));
}
return this.multiplexedSessionReference.get();
} finally {
this.multiplexedSessionLock.unlock();
}
}
private static class BatchReadOnlyTransactionImpl extends MultiUseReadOnlyTransaction
implements BatchReadOnlyTransaction {
private String sessionName;
private final Map<SpannerRpc.Option, ?> options;
private final SessionClient sessionClient;
private final AtomicBoolean fallbackInitiated = new AtomicBoolean(false);
BatchReadOnlyTransactionImpl(
MultiUseReadOnlyTransaction.Builder builder,
TimestampBound bound,
SessionClient sessionClient) {
super(builder.setTimestampBound(bound));
this.sessionClient = sessionClient;
this.sessionName = session.getName();
this.options = session.getOptions();
initTransaction();
}
BatchReadOnlyTransactionImpl(
MultiUseReadOnlyTransaction.Builder builder,
BatchTransactionId batchTransactionId,
SessionClient sessionClient) {
super(builder.setTransactionId(batchTransactionId.getTransactionId()));
this.sessionClient = sessionClient;
this.sessionName = session.getName();
this.options = session.getOptions();
}
@Override
public BatchTransactionId getBatchTransactionId() {
return new BatchTransactionId(sessionName, getTransactionId(), getReadTimestamp());
}
@Override
public List<Partition> partitionRead(
PartitionOptions partitionOptions,
String table,
KeySet keys,
Iterable<String> columns,
ReadOption... options)
throws SpannerException {
return partitionReadUsingIndex(
partitionOptions, table, null /*index*/, keys, columns, options);
}
@Override
public List<Partition> partitionReadUsingIndex(
PartitionOptions partitionOptions,
String table,
String index,
KeySet keys,
Iterable<String> columns,
ReadOption... option)
throws SpannerException {
return partitionReadUsingIndex(partitionOptions, table, index, keys, columns, false, option);
}
private List<Partition> partitionReadUsingIndex(
PartitionOptions partitionOptions,
String table,
String index,
KeySet keys,
Iterable<String> columns,
boolean isFallback,
ReadOption... option)
throws SpannerException {
Options readOptions = Options.fromReadOptions(option);
Preconditions.checkArgument(
!readOptions.hasLimit(),
"Limit option not supported by partitionRead|partitionReadUsingIndex");
final PartitionReadRequest.Builder builder =
PartitionReadRequest.newBuilder()
.setSession(sessionName)
.setTable(checkNotNull(table))
.addAllColumns(columns);
keys.appendToProto(builder.getKeySetBuilder());
if (index != null) {
builder.setIndex(index);
}
TransactionSelector selector = getTransactionSelector();
if (selector != null) {
builder.setTransaction(selector);
}
com.google.spanner.v1.PartitionOptions.Builder pbuilder =
com.google.spanner.v1.PartitionOptions.newBuilder();
if (partitionOptions != null) {
partitionOptions.appendToProto(pbuilder);
}
builder.setPartitionOptions(pbuilder.build());
XGoogSpannerRequestId reqId =
session.getRequestIdCreator().nextRequestId(session.getChannel(), 1);
final PartitionReadRequest request = builder.build();
try {
PartitionResponse response = rpc.partitionRead(request, reqId.withOptions(options));
ImmutableList.Builder<Partition> partitions = ImmutableList.builder();
for (com.google.spanner.v1.Partition p : response.getPartitionsList()) {
Partition partition =
Partition.createReadPartition(
p.getPartitionToken(),
partitionOptions,
table,
index,
keys,
columns,
readOptions);
partitions.add(partition);
}
return partitions.build();
} catch (SpannerException e) {
if (!isFallback && maybeMarkUnimplementedForPartitionedOps(e)) {
return partitionReadUsingIndex(
partitionOptions, table, index, keys, columns, true, option);
}
e.setRequestId(reqId);
throw e;
}
}
@Override
public List<Partition> partitionQuery(
PartitionOptions partitionOptions, Statement statement, QueryOption... option)
throws SpannerException {
return partitionQuery(partitionOptions, statement, false, option);
}
private List<Partition> partitionQuery(
PartitionOptions partitionOptions,
Statement statement,
boolean isFallback,
QueryOption... option)
throws SpannerException {
Options queryOptions = Options.fromQueryOptions(option);
final PartitionQueryRequest.Builder builder =
PartitionQueryRequest.newBuilder().setSession(sessionName).setSql(statement.getSql());
Map<String, Value> stmtParameters = statement.getParameters();
if (!stmtParameters.isEmpty()) {
Struct.Builder paramsBuilder = builder.getParamsBuilder();
for (Map.Entry<String, Value> param : stmtParameters.entrySet()) {
paramsBuilder.putFields(param.getKey(), Value.toProto(param.getValue()));
if (param.getValue() != null && param.getValue().getType() != null) {
builder.putParamTypes(param.getKey(), param.getValue().getType().toProto());
}
}
}
TransactionSelector selector = getTransactionSelector();
if (selector != null) {
builder.setTransaction(selector);
}
com.google.spanner.v1.PartitionOptions.Builder pbuilder =
com.google.spanner.v1.PartitionOptions.newBuilder();
if (partitionOptions != null) {
partitionOptions.appendToProto(pbuilder);
}
builder.setPartitionOptions(pbuilder.build());
XGoogSpannerRequestId reqId =
session.getRequestIdCreator().nextRequestId(session.getChannel(), 1);
final PartitionQueryRequest request = builder.build();
try {
PartitionResponse response = rpc.partitionQuery(request, reqId.withOptions(options));
ImmutableList.Builder<Partition> partitions = ImmutableList.builder();
for (com.google.spanner.v1.Partition p : response.getPartitionsList()) {
Partition partition =
Partition.createQueryPartition(
p.getPartitionToken(), partitionOptions, statement, queryOptions);
partitions.add(partition);
}
return partitions.build();
} catch (SpannerException e) {
if (!isFallback && maybeMarkUnimplementedForPartitionedOps(e)) {
return partitionQuery(partitionOptions, statement, true, option);
}
e.setRequestId(reqId);
throw e;
}
}
boolean maybeMarkUnimplementedForPartitionedOps(SpannerException spannerException) {
if (MultiplexedSessionDatabaseClient.verifyErrorMessage(
spannerException, "Partitioned operations are not supported with multiplexed sessions")) {
synchronized (fallbackInitiated) {
if (!fallbackInitiated.get()) {
session.setFallbackSessionReference(
sessionClient.createSession().getSessionReference());
sessionName = session.getName();
initFallbackTransaction();
unimplementedForPartitionedOps.set(true);
fallbackInitiated.set(true);
}
return true;
}
}
return false;
}
@Override
public ResultSet execute(Partition partition) throws SpannerException {
if (partition.getStatement() != null) {
return executeQueryInternalWithOptions(
partition.getStatement(),
QueryMode.NORMAL,
partition.getQueryOptions(),
partition.getPartitionToken());
}
return readInternalWithOptions(
partition.getTable(),
partition.getIndex(),
partition.getKeys(),
partition.getColumns(),
partition.getReadOptions(),
partition.getPartitionToken());
}
/**
* Closes the session as part of the cleanup. It is the responsibility of the caller to make a
* call to this method once the transaction completes execution across all the channels (which
* is understandably hard to identify). It is okay if the caller does not call the method
* because the backend will anyways clean up the unused session.
*/
@Override
public void cleanup() {
session.close();
}
}
}