-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathSpannerImpl.java
437 lines (391 loc) · 15.5 KB
/
SpannerImpl.java
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* 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 com.google.api.gax.core.ExecutorProvider;
import com.google.api.gax.core.GaxProperties;
import com.google.api.gax.paging.Page;
import com.google.cloud.BaseService;
import com.google.cloud.PageImpl;
import com.google.cloud.PageImpl.NextPageFetcher;
import com.google.cloud.grpc.GrpcTransportOptions;
import com.google.cloud.spanner.SessionClient.SessionId;
import com.google.cloud.spanner.SpannerOptions.CloseableExecutorProvider;
import com.google.cloud.spanner.admin.database.v1.stub.DatabaseAdminStubSettings;
import com.google.cloud.spanner.admin.instance.v1.stub.InstanceAdminStubSettings;
import com.google.cloud.spanner.spi.v1.GapicSpannerRpc;
import com.google.cloud.spanner.spi.v1.SpannerRpc;
import com.google.cloud.spanner.spi.v1.SpannerRpc.Paginated;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions;
import io.opencensus.metrics.LabelValue;
import io.opencensus.trace.Tracing;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
/** Default implementation of the Cloud Spanner interface. */
class SpannerImpl extends BaseService<SpannerOptions> implements Spanner {
private static final Logger logger = Logger.getLogger(SpannerImpl.class.getName());
final TraceWrapper tracer =
new TraceWrapper(
Tracing.getTracer(),
getOptions()
.getOpenTelemetry()
.getTracer(
MetricRegistryConstants.INSTRUMENTATION_SCOPE,
GaxProperties.getLibraryVersion(this.getOptions().getClass())),
getOptions().isEnableExtendedTracing());
static final String CREATE_MULTIPLEXED_SESSION = "CloudSpannerOperation.CreateMultiplexedSession";
static final String CREATE_SESSION = "CloudSpannerOperation.CreateSession";
static final String BATCH_CREATE_SESSIONS = "CloudSpannerOperation.BatchCreateSessions";
static final String BATCH_CREATE_SESSIONS_REQUEST =
"CloudSpannerOperation.BatchCreateSessionsRequest";
static final String DELETE_SESSION = "CloudSpannerOperation.DeleteSession";
static final String BEGIN_TRANSACTION = "CloudSpannerOperation.BeginTransaction";
static final String COMMIT = "CloudSpannerOperation.Commit";
static final String QUERY = "CloudSpannerOperation.ExecuteStreamingQuery";
static final String READ = "CloudSpannerOperation.ExecuteStreamingRead";
static final String BATCH_WRITE = "CloudSpannerOperation.BatchWrite";
static final String UPDATE = "CloudSpannerOperation.ExecuteUpdate";
static final String BATCH_UPDATE = "CloudSpannerOperation.BatchUpdate";
private static final Object CLIENT_ID_LOCK = new Object();
@GuardedBy("CLIENT_ID_LOCK")
private static final Map<DatabaseId, Long> CLIENT_IDS = new HashMap<>();
private static String nextDatabaseClientId(DatabaseId databaseId) {
synchronized (CLIENT_ID_LOCK) {
Long id = CLIENT_IDS.get(databaseId);
if (id == null) {
id = 1L;
} else {
id++;
}
CLIENT_IDS.put(databaseId, id);
return String.format("client-%d", id);
}
}
private final SpannerRpc gapicRpc;
@GuardedBy("this")
private final Map<DatabaseId, DatabaseClientImpl> dbClients = new HashMap<>();
@GuardedBy("dbBatchClientLock")
private final Map<DatabaseId, BatchClientImpl> dbBatchClients = new HashMap<>();
private final ReentrantLock dbBatchClientLock = new ReentrantLock();
private final CloseableExecutorProvider asyncExecutorProvider;
@GuardedBy("this")
private final Map<DatabaseId, SessionClient> sessionClients = new HashMap<>();
private final DatabaseAdminClient dbAdminClient;
private final InstanceAdminClient instanceClient;
/**
* Exception class used to track the stack trace at the point when a Spanner instance is closed.
* This exception will be thrown if a user tries to use any resources that were returned by this
* Spanner instance after the instance has been closed. This makes it easier to track down the
* code that (accidentally) closed the Spanner instance.
*/
static final class ClosedException extends RuntimeException {
private static final long serialVersionUID = 1451131180314064914L;
ClosedException() {
super("Spanner client was closed at " + Instant.now());
}
}
@GuardedBy("this")
private ClosedException closedException;
@VisibleForTesting
SpannerImpl(SpannerRpc gapicRpc, SpannerOptions options) {
super(options);
this.gapicRpc = gapicRpc;
this.asyncExecutorProvider =
MoreObjects.firstNonNull(
options.getAsyncExecutorProvider(),
SpannerOptions.createDefaultAsyncExecutorProvider());
this.dbAdminClient = new DatabaseAdminClientImpl(options.getProjectId(), gapicRpc);
this.instanceClient =
new InstanceAdminClientImpl(options.getProjectId(), gapicRpc, dbAdminClient);
}
SpannerImpl(SpannerOptions options) {
this(options.getSpannerRpcV1(), options);
}
/** Returns the {@link SpannerRpc} of this {@link SpannerImpl} instance. */
SpannerRpc getRpc() {
return gapicRpc;
}
/** Returns the default setting for prefetchChunks of this {@link SpannerImpl} instance. */
int getDefaultPrefetchChunks() {
return getOptions().getPrefetchChunks();
}
DecodeMode getDefaultDecodeMode() {
return getOptions().getDecodeMode();
}
/** Returns the default query options that should be used for the specified database. */
QueryOptions getDefaultQueryOptions(DatabaseId databaseId) {
return getOptions().getDefaultQueryOptions(databaseId);
}
TraceWrapper getTracer() {
return this.tracer;
}
/**
* Returns the {@link ExecutorProvider} to use for async methods that need a background executor.
*/
public ExecutorProvider getAsyncExecutorProvider() {
return asyncExecutorProvider;
}
SessionImpl sessionWithId(String name) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "name is null or empty");
SessionId id = SessionId.of(name);
return getSessionClient(id.getDatabaseId()).sessionWithId(name);
}
void checkClosed() {
synchronized (this) {
if (closedException != null) {
throw new IllegalStateException("Cloud Spanner client has been closed", closedException);
}
}
}
SessionClient getSessionClient(DatabaseId db) {
synchronized (this) {
checkClosed();
if (sessionClients.containsKey(db)) {
return sessionClients.get(db);
} else {
SessionClient client =
new SessionClient(
this,
db,
((GrpcTransportOptions) getOptions().getTransportOptions()).getExecutorFactory());
sessionClients.put(db, client);
return client;
}
}
}
@Override
public DatabaseAdminClient getDatabaseAdminClient() {
return dbAdminClient;
}
@Override
public com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient
createDatabaseAdminClient() {
try {
final DatabaseAdminStubSettings settings =
Preconditions.checkNotNull(gapicRpc.getDatabaseAdminStubSettings());
return com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient.create(
settings.createStub());
} catch (IOException ex) {
throw SpannerExceptionFactory.newSpannerException(ex);
}
}
@Override
public InstanceAdminClient getInstanceAdminClient() {
return instanceClient;
}
@Override
public com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient
createInstanceAdminClient() {
try {
final InstanceAdminStubSettings settings =
Preconditions.checkNotNull(gapicRpc.getInstanceAdminStubSettings());
return com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient.create(
settings.createStub());
} catch (IOException ex) {
throw SpannerExceptionFactory.newSpannerException(ex);
}
}
@Override
public DatabaseClient getDatabaseClient(DatabaseId db) {
synchronized (this) {
checkClosed();
String clientId = null;
if (dbClients.containsKey(db) && !dbClients.get(db).isValid()) {
// Close the invalidated client and remove it.
dbClients.get(db).closeAsync(new ClosedException());
clientId = dbClients.get(db).clientId;
dbClients.remove(db);
}
if (dbClients.containsKey(db)) {
return dbClients.get(db);
} else {
if (clientId == null) {
clientId = nextDatabaseClientId(db);
}
List<LabelValue> labelValues =
ImmutableList.of(
LabelValue.create(clientId),
LabelValue.create(db.getDatabase()),
LabelValue.create(db.getInstanceId().getName()),
LabelValue.create(GaxProperties.getLibraryVersion(getOptions().getClass())));
AttributesBuilder attributesBuilder = Attributes.builder();
attributesBuilder.put("client_id", clientId);
attributesBuilder.put("database", db.getDatabase());
attributesBuilder.put("instance_id", db.getInstanceId().getName());
boolean useMultiplexedSession =
getOptions().getSessionPoolOptions().getUseMultiplexedSession();
boolean useMultiplexedSessionForRW =
getOptions().getSessionPoolOptions().getUseMultiplexedSessionForRW();
MultiplexedSessionDatabaseClient multiplexedSessionDatabaseClient =
useMultiplexedSession
? new MultiplexedSessionDatabaseClient(SpannerImpl.this.getSessionClient(db))
: null;
AtomicLong numMultiplexedSessionsAcquired =
useMultiplexedSession
? multiplexedSessionDatabaseClient.getNumSessionsAcquired()
: new AtomicLong();
AtomicLong numMultiplexedSessionsReleased =
useMultiplexedSession
? multiplexedSessionDatabaseClient.getNumSessionsReleased()
: new AtomicLong();
SessionPool pool =
SessionPool.createPool(
getOptions(),
SpannerImpl.this.getSessionClient(db),
this.tracer,
labelValues,
attributesBuilder.build(),
numMultiplexedSessionsAcquired,
numMultiplexedSessionsReleased);
pool.maybeWaitOnMinSessions();
DatabaseClientImpl dbClient =
createDatabaseClient(
clientId,
pool,
getOptions().getSessionPoolOptions().getUseMultiplexedSessionBlindWrite(),
multiplexedSessionDatabaseClient,
getOptions().getSessionPoolOptions().getUseMultiplexedSessionPartitionedOps(),
useMultiplexedSessionForRW,
this.tracer.createCommonAttributes(db));
dbClients.put(db, dbClient);
return dbClient;
}
}
}
@VisibleForTesting
DatabaseClientImpl createDatabaseClient(
String clientId,
SessionPool pool,
boolean useMultiplexedSessionBlindWrite,
@Nullable MultiplexedSessionDatabaseClient multiplexedSessionClient,
boolean useMultiplexedSessionPartitionedOps,
boolean useMultiplexedSessionForRW,
Attributes commonAttributes) {
return new DatabaseClientImpl(
clientId,
pool,
useMultiplexedSessionBlindWrite,
multiplexedSessionClient,
useMultiplexedSessionPartitionedOps,
tracer,
useMultiplexedSessionForRW,
commonAttributes);
}
@Override
public BatchClient getBatchClient(DatabaseId db) {
if (getOptions().getSessionPoolOptions().getUseMultiplexedSessionPartitionedOps()) {
this.dbBatchClientLock.lock();
try {
if (this.dbBatchClients.containsKey(db)) {
return this.dbBatchClients.get(db);
}
BatchClientImpl batchClient =
new BatchClientImpl(
getSessionClient(db), /*useMultiplexedSessionPartitionedOps=*/ true);
this.dbBatchClients.put(db, batchClient);
return batchClient;
} finally {
this.dbBatchClientLock.unlock();
}
}
return new BatchClientImpl(
getSessionClient(db), /*useMultiplexedSessionPartitionedOps=*/ false);
}
@Override
public void close() {
close(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
}
// Suppressed for initial Error Prone rollout.
@SuppressWarnings("GuardedBy")
void close(long timeout, TimeUnit unit) {
List<ListenableFuture<Void>> closureFutures;
synchronized (this) {
checkClosed();
closedException = new ClosedException();
}
try {
closureFutures = new ArrayList<>();
for (DatabaseClientImpl dbClient : dbClients.values()) {
closureFutures.add(dbClient.closeAsync(closedException));
}
dbClients.clear();
Futures.successfulAsList(closureFutures).get(timeout, unit);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
throw SpannerExceptionFactory.newSpannerException(e);
} finally {
for (SessionClient sessionClient : sessionClients.values()) {
sessionClient.close();
}
sessionClients.clear();
asyncExecutorProvider.close();
try {
if (timeout == Long.MAX_VALUE || !(gapicRpc instanceof GapicSpannerRpc)) {
gapicRpc.shutdown();
} else {
((GapicSpannerRpc) gapicRpc).shutdownNow();
}
} catch (RuntimeException e) {
logger.log(Level.WARNING, "Failed to close channels", e);
}
}
}
@Override
public boolean isClosed() {
synchronized (this) {
return closedException != null;
}
}
/** Helper class for gRPC calls that can return paginated results. */
abstract static class PageFetcher<S, T> implements NextPageFetcher<S> {
private String nextPageToken;
@Override
public Page<S> getNextPage() {
Paginated<T> nextPage = getNextPage(nextPageToken);
this.nextPageToken = nextPage.getNextPageToken();
List<S> results = new ArrayList<>();
for (T proto : nextPage.getResults()) {
results.add(fromProto(proto));
}
return new PageImpl<>(this, nextPageToken, results);
}
void setNextPageToken(String nextPageToken) {
this.nextPageToken = nextPageToken;
}
abstract Paginated<T> getNextPage(@Nullable String nextPageToken);
abstract S fromProto(T proto);
}
}