/
TransactionRunnerImpl.java
403 lines (372 loc) · 15 KB
/
TransactionRunnerImpl.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
/*
* Copyright 2019 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.cloud.spanner.SpannerExceptionFactory.newSpannerBatchUpdateException;
import static com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.SessionImpl.SessionTransaction;
import com.google.cloud.spanner.spi.v1.SpannerRpc;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.ByteString;
import com.google.rpc.Code;
import com.google.spanner.v1.CommitRequest;
import com.google.spanner.v1.CommitResponse;
import com.google.spanner.v1.ExecuteBatchDmlRequest;
import com.google.spanner.v1.ExecuteSqlRequest;
import com.google.spanner.v1.ExecuteSqlRequest.QueryMode;
import com.google.spanner.v1.RollbackRequest;
import com.google.spanner.v1.TransactionSelector;
import io.opencensus.common.Scope;
import io.opencensus.trace.AttributeValue;
import io.opencensus.trace.Span;
import io.opencensus.trace.Tracer;
import io.opencensus.trace.Tracing;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
/** Default implementation of {@link TransactionRunner}. */
class TransactionRunnerImpl implements SessionTransaction, TransactionRunner {
private static final Tracer tracer = Tracing.getTracer();
private static final Logger txnLogger = Logger.getLogger(TransactionRunner.class.getName());
@VisibleForTesting
static class TransactionContextImpl extends AbstractReadContext implements TransactionContext {
@GuardedBy("lock")
private List<Mutation> mutations = new ArrayList<>();
@GuardedBy("lock")
private boolean aborted;
/** Default to -1 to indicate not available. */
@GuardedBy("lock")
private long retryDelayInMillis = -1L;
private ByteString transactionId;
private Timestamp commitTimestamp;
TransactionContextImpl(
SessionImpl session,
@Nullable ByteString transactionId,
SpannerRpc rpc,
int defaultPrefetchChunks) {
super(session, rpc, defaultPrefetchChunks);
this.transactionId = transactionId;
}
void ensureTxn() {
if (transactionId == null || isAborted()) {
span.addAnnotation("Creating Transaction");
try {
transactionId = session.beginTransaction();
span.addAnnotation(
"Transaction Creation Done",
ImmutableMap.of(
"Id", AttributeValue.stringAttributeValue(transactionId.toStringUtf8())));
txnLogger.log(
Level.FINER,
"Started transaction {0}",
txnLogger.isLoggable(Level.FINER) ? transactionId.asReadOnlyByteBuffer() : null);
} catch (SpannerException e) {
span.addAnnotation("Transaction Creation Failed", TraceUtil.getExceptionAnnotations(e));
throw e;
}
} else {
span.addAnnotation(
"Transaction Initialized",
ImmutableMap.of(
"Id", AttributeValue.stringAttributeValue(transactionId.toStringUtf8())));
txnLogger.log(
Level.FINER,
"Using prepared transaction {0}",
txnLogger.isLoggable(Level.FINER) ? transactionId.asReadOnlyByteBuffer() : null);
}
}
void commit() {
span.addAnnotation("Starting Commit");
CommitRequest.Builder builder =
CommitRequest.newBuilder().setSession(session.getName()).setTransactionId(transactionId);
synchronized (lock) {
if (!mutations.isEmpty()) {
List<com.google.spanner.v1.Mutation> mutationsProto = new ArrayList<>();
Mutation.toProto(mutations, mutationsProto);
builder.addAllMutations(mutationsProto);
}
// Ensure that no call to buffer mutations that would be lost can succeed.
mutations = null;
}
final CommitRequest commitRequest = builder.build();
Span opSpan = tracer.spanBuilderWithExplicitParent(SpannerImpl.COMMIT, span).startSpan();
try (Scope s = tracer.withSpan(opSpan)) {
CommitResponse commitResponse = rpc.commit(commitRequest, session.getOptions());
if (!commitResponse.hasCommitTimestamp()) {
throw newSpannerException(
ErrorCode.INTERNAL, "Missing commitTimestamp:\n" + session.getName());
}
commitTimestamp = Timestamp.fromProto(commitResponse.getCommitTimestamp());
opSpan.end(TraceUtil.END_SPAN_OPTIONS);
} catch (RuntimeException e) {
span.addAnnotation("Commit Failed", TraceUtil.getExceptionAnnotations(e));
TraceUtil.endSpanWithFailure(opSpan, e);
if (e instanceof SpannerException) {
onError((SpannerException) e);
}
throw e;
}
span.addAnnotation("Commit Done");
}
Timestamp commitTimestamp() {
checkState(commitTimestamp != null, "run() has not yet returned normally");
return commitTimestamp;
}
boolean isAborted() {
synchronized (lock) {
return aborted;
}
}
void rollback() {
// We're exiting early due to a user exception, but the transaction is still active.
// Send a rollback for the transaction to release any locks held.
// TODO(user): Make this an async fire-and-forget request.
try {
// Note that we're not retrying this request since we don't particularly care about the
// response. Normally, the next thing that will happen is that we will make a fresh
// transaction attempt, which should implicitly abort this one.
span.addAnnotation("Starting Rollback");
rpc.rollback(
RollbackRequest.newBuilder()
.setSession(session.getName())
.setTransactionId(transactionId)
.build(),
session.getOptions());
span.addAnnotation("Rollback Done");
} catch (SpannerException e) {
txnLogger.log(Level.FINE, "Exception during rollback", e);
span.addAnnotation("Rollback Failed", TraceUtil.getExceptionAnnotations(e));
}
}
@Nullable
@Override
TransactionSelector getTransactionSelector() {
return TransactionSelector.newBuilder().setId(transactionId).build();
}
@Override
public void onError(SpannerException e) {
if (e.getErrorCode() == ErrorCode.ABORTED) {
long delay = -1L;
if (e instanceof AbortedException) {
delay = ((AbortedException) e).getRetryDelayInMillis();
}
if (delay == -1L) {
txnLogger.log(Level.FINE, "Retry duration is missing from the exception.", e);
}
synchronized (lock) {
retryDelayInMillis = delay;
aborted = true;
}
}
}
@Override
public void buffer(Mutation mutation) {
synchronized (lock) {
checkNotNull(mutations, "Context is closed");
mutations.add(checkNotNull(mutation));
}
}
@Override
public void buffer(Iterable<Mutation> mutations) {
synchronized (lock) {
checkNotNull(this.mutations, "Context is closed");
for (Mutation mutation : mutations) {
this.mutations.add(checkNotNull(mutation));
}
}
}
@Override
public long executeUpdate(Statement statement) {
beforeReadOrQuery();
final ExecuteSqlRequest.Builder builder =
getExecuteSqlRequestBuilder(statement, QueryMode.NORMAL);
try {
com.google.spanner.v1.ResultSet resultSet =
rpc.executeQuery(builder.build(), session.getOptions());
if (!resultSet.hasStats()) {
throw new IllegalArgumentException(
"DML response missing stats possibly due to non-DML statement as input");
}
// For standard DML, using the exact row count.
return resultSet.getStats().getRowCountExact();
} catch (SpannerException e) {
onError(e);
throw e;
}
}
@Override
public long[] batchUpdate(Iterable<Statement> statements) {
beforeReadOrQuery();
final ExecuteBatchDmlRequest.Builder builder = getExecuteBatchDmlRequestBuilder(statements);
try {
com.google.spanner.v1.ExecuteBatchDmlResponse response =
rpc.executeBatchDml(builder.build(), session.getOptions());
long[] results = new long[response.getResultSetsCount()];
for (int i = 0; i < response.getResultSetsCount(); ++i) {
results[i] = response.getResultSets(i).getStats().getRowCountExact();
}
// If one of the DML statements was aborted, we should throw an aborted exception.
// In all other cases, we should throw a BatchUpdateException.
if (response.getStatus().getCode() == Code.ABORTED_VALUE) {
throw newSpannerException(
ErrorCode.fromRpcStatus(response.getStatus()), response.getStatus().getMessage());
} else if (response.getStatus().getCode() != 0) {
throw newSpannerBatchUpdateException(
ErrorCode.fromRpcStatus(response.getStatus()),
response.getStatus().getMessage(),
results);
}
return results;
} catch (SpannerException e) {
onError(e);
throw e;
}
}
}
private boolean blockNestedTxn = true;
private final SessionImpl session;
private final Span span;
private TransactionContextImpl txn;
private volatile boolean isValid = true;
@Override
public TransactionRunner allowNestedTransaction() {
blockNestedTxn = false;
return this;
}
TransactionRunnerImpl(SessionImpl session, SpannerRpc rpc, int defaultPrefetchChunks) {
this.session = session;
this.span = Tracing.getTracer().getCurrentSpan();
this.txn = session.newTransaction();
}
@Nullable
@Override
public <T> T run(TransactionCallable<T> callable) {
try (Scope s = tracer.withSpan(span)) {
if (blockNestedTxn) {
SessionImpl.hasPendingTransaction.set(Boolean.TRUE);
}
return runInternal(callable);
} catch (RuntimeException e) {
TraceUtil.endSpanWithFailure(span, e);
throw e;
} finally {
// Remove threadLocal rather than set to FALSE to avoid a possible memory leak.
// We also do this unconditionally in case a user has modified the flag when the transaction
// was running.
SessionImpl.hasPendingTransaction.remove();
}
}
private <T> T runInternal(final TransactionCallable<T> txCallable) {
final AtomicInteger attempt = new AtomicInteger();
Callable<T> retryCallable =
new Callable<T>() {
@Override
public T call() {
if (txn.isAborted()) {
txn = session.newTransaction();
}
checkState(
isValid,
"TransactionRunner has been invalidated by a new operation on the session");
attempt.incrementAndGet();
// TODO(user): When using streaming reads, consider using the first read to begin
// the txn.
span.addAnnotation(
"Starting Transaction Attempt",
ImmutableMap.of("Attempt", AttributeValue.longAttributeValue(attempt.longValue())));
txn.ensureTxn();
T result;
boolean shouldRollback = true;
try {
result = txCallable.run(txn);
shouldRollback = false;
} catch (Exception e) {
txnLogger.log(Level.FINE, "User-provided TransactionCallable raised exception", e);
if (txn.isAborted() || (e instanceof AbortedException)) {
span.addAnnotation(
"Transaction Attempt Aborted in user operation. Retrying",
ImmutableMap.of(
"Attempt", AttributeValue.longAttributeValue(attempt.longValue())));
shouldRollback = false;
if (e instanceof AbortedException) {
throw (AbortedException) e;
}
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.ABORTED, e.getMessage(), e);
}
SpannerException toThrow;
if (e instanceof SpannerException) {
toThrow = (SpannerException) e;
} else {
toThrow = newSpannerException(ErrorCode.UNKNOWN, e.getMessage(), e);
}
span.addAnnotation(
"Transaction Attempt Failed in user operation",
ImmutableMap.<String, AttributeValue>builder()
.putAll(TraceUtil.getExceptionAnnotations(toThrow))
.put("Attempt", AttributeValue.longAttributeValue(attempt.longValue()))
.build());
throw toThrow;
} finally {
if (shouldRollback) {
txn.rollback();
}
}
try {
txn.commit();
span.addAnnotation(
"Transaction Attempt Succeeded",
ImmutableMap.of(
"Attempt", AttributeValue.longAttributeValue(attempt.longValue())));
return result;
} catch (AbortedException e) {
txnLogger.log(Level.FINE, "Commit aborted", e);
span.addAnnotation(
"Transaction Attempt Aborted in Commit. Retrying",
ImmutableMap.of(
"Attempt", AttributeValue.longAttributeValue(attempt.longValue())));
throw e;
} catch (SpannerException e) {
span.addAnnotation(
"Transaction Attempt Failed in Commit",
ImmutableMap.<String, AttributeValue>builder()
.putAll(TraceUtil.getExceptionAnnotations(e))
.put("Attempt", AttributeValue.longAttributeValue(attempt.longValue()))
.build());
throw e;
}
}
};
return SpannerRetryHelper.runTxWithRetriesOnAborted(retryCallable);
}
@Override
public Timestamp getCommitTimestamp() {
checkState(txn != null, "run() has not yet returned normally");
return txn.commitTimestamp();
}
@Override
public void invalidate() {
isValid = false;
}
}