/
TransactionRunnerImpl.java
751 lines (695 loc) · 28.1 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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/*
* 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.api.core.ApiAsyncFunction;
import com.google.api.core.ApiFunction;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.core.SettableApiFuture;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.Options.QueryOption;
import com.google.cloud.spanner.Options.ReadOption;
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.common.util.concurrent.MoreExecutors;
import com.google.protobuf.ByteString;
import com.google.protobuf.Empty;
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.ExecuteBatchDmlResponse;
import com.google.spanner.v1.ExecuteSqlRequest;
import com.google.spanner.v1.ExecuteSqlRequest.QueryMode;
import com.google.spanner.v1.ResultSet;
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.ExecutionException;
import java.util.concurrent.Executor;
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 {
static class Builder extends AbstractReadContext.Builder<Builder, TransactionContextImpl> {
private ByteString transactionId;
private Builder() {}
Builder setTransactionId(ByteString transactionId) {
this.transactionId = transactionId;
return self();
}
@Override
TransactionContextImpl build() {
return new TransactionContextImpl(this);
}
}
static Builder newBuilder() {
return new Builder();
}
/**
* {@link AsyncResultSet} implementation that keeps track of the async operations that are still
* running for this {@link TransactionContext} and that should finish before the {@link
* TransactionContext} can commit and release its session back into the pool.
*/
private class TransactionContextAsyncResultSetImpl extends ForwardingAsyncResultSet
implements ListenableAsyncResultSet {
private TransactionContextAsyncResultSetImpl(ListenableAsyncResultSet delegate) {
super(delegate);
}
@Override
public ApiFuture<Void> setCallback(Executor exec, ReadyCallback cb) {
Runnable listener =
new Runnable() {
@Override
public void run() {
decreaseAsyncOperations();
}
};
try {
increaseAsynOperations();
addListener(listener);
return super.setCallback(exec, cb);
} catch (Throwable t) {
removeListener(listener);
decreaseAsyncOperations();
throw t;
}
}
@Override
public void addListener(Runnable listener) {
((ListenableAsyncResultSet) this.delegate).addListener(listener);
}
@Override
public void removeListener(Runnable listener) {
((ListenableAsyncResultSet) this.delegate).removeListener(listener);
}
}
@GuardedBy("lock")
private volatile boolean committing;
@GuardedBy("lock")
private volatile SettableApiFuture<Void> finishedAsyncOperations = SettableApiFuture.create();
@GuardedBy("lock")
private volatile int runningAsyncOperations;
@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 volatile ByteString transactionId;
private Timestamp commitTimestamp;
private TransactionContextImpl(Builder builder) {
super(builder);
this.transactionId = builder.transactionId;
this.finishedAsyncOperations.set(null);
}
private void increaseAsynOperations() {
synchronized (lock) {
if (runningAsyncOperations == 0) {
finishedAsyncOperations = SettableApiFuture.create();
}
runningAsyncOperations++;
}
}
private void decreaseAsyncOperations() {
synchronized (lock) {
runningAsyncOperations--;
if (runningAsyncOperations == 0) {
finishedAsyncOperations.set(null);
}
}
}
void ensureTxn() {
try {
ensureTxnAsync().get();
} catch (ExecutionException e) {
throw SpannerExceptionFactory.newSpannerException(e.getCause() == null ? e : e.getCause());
} catch (InterruptedException e) {
throw SpannerExceptionFactory.propagateInterrupt(e);
}
}
ApiFuture<Void> ensureTxnAsync() {
final SettableApiFuture<Void> res = SettableApiFuture.create();
if (transactionId == null || isAborted()) {
span.addAnnotation("Creating Transaction");
final ApiFuture<ByteString> fut = session.beginTransactionAsync();
fut.addListener(
new Runnable() {
@Override
public void run() {
try {
transactionId = fut.get();
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);
res.set(null);
} catch (ExecutionException e) {
span.addAnnotation(
"Transaction Creation Failed",
TraceUtil.getExceptionAnnotations(e.getCause() == null ? e : e.getCause()));
res.setException(e.getCause() == null ? e : e.getCause());
} catch (InterruptedException e) {
res.setException(SpannerExceptionFactory.propagateInterrupt(e));
}
}
},
MoreExecutors.directExecutor());
} 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);
res.set(null);
}
return res;
}
void commit() {
try {
commitTimestamp = commitAsync().get();
} catch (InterruptedException e) {
if (commitFuture != null) {
commitFuture.cancel(true);
}
throw SpannerExceptionFactory.propagateInterrupt(e);
} catch (ExecutionException e) {
throw SpannerExceptionFactory.newSpannerException(e.getCause() == null ? e : e.getCause());
}
}
volatile ApiFuture<CommitResponse> commitFuture;
ApiFuture<Timestamp> commitAsync() {
final SettableApiFuture<Timestamp> res = SettableApiFuture.create();
final SettableApiFuture<Void> latch;
synchronized (lock) {
latch = finishedAsyncOperations;
}
latch.addListener(
new Runnable() {
@Override
public void run() {
try {
latch.get();
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.addAnnotation("Starting Commit");
final Span opSpan =
tracer.spanBuilderWithExplicitParent(SpannerImpl.COMMIT, span).startSpan();
commitFuture = rpc.commitAsync(commitRequest, session.getOptions());
commitFuture.addListener(
tracer.withSpan(
opSpan,
new Runnable() {
@Override
public void run() {
try {
CommitResponse commitResponse = commitFuture.get();
if (!commitResponse.hasCommitTimestamp()) {
throw newSpannerException(
ErrorCode.INTERNAL,
"Missing commitTimestamp:\n" + session.getName());
}
Timestamp ts =
Timestamp.fromProto(commitResponse.getCommitTimestamp());
span.addAnnotation("Commit Done");
opSpan.end(TraceUtil.END_SPAN_OPTIONS);
res.set(ts);
} catch (Throwable e) {
if (e instanceof ExecutionException) {
e =
SpannerExceptionFactory.newSpannerException(
e.getCause() == null ? e : e.getCause());
} else if (e instanceof InterruptedException) {
e =
SpannerExceptionFactory.propagateInterrupt(
(InterruptedException) e);
} else {
e = SpannerExceptionFactory.newSpannerException(e);
}
span.addAnnotation(
"Commit Failed", TraceUtil.getExceptionAnnotations(e));
TraceUtil.endSpanWithFailure(opSpan, e);
onError((SpannerException) e);
res.setException(e);
}
}
}),
MoreExecutors.directExecutor());
} catch (InterruptedException e) {
res.setException(SpannerExceptionFactory.propagateInterrupt(e));
} catch (ExecutionException e) {
res.setException(
SpannerExceptionFactory.newSpannerException(
e.getCause() == null ? e : e.getCause()));
}
}
},
MoreExecutors.directExecutor());
return res;
}
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));
}
}
ApiFuture<Void> rollbackAsync() {
span.addAnnotation("Starting Rollback");
return ApiFutures.transformAsync(
rpc.rollbackAsync(
RollbackRequest.newBuilder()
.setSession(session.getName())
.setTransactionId(transactionId)
.build(),
session.getOptions()),
new ApiAsyncFunction<Empty, Void>() {
@Override
public ApiFuture<Void> apply(Empty input) throws Exception {
span.addAnnotation("Rollback Done");
return ApiFutures.immediateFuture(null);
}
},
MoreExecutors.directExecutor());
}
@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 ApiFuture<Long> executeUpdateAsync(Statement statement) {
beforeReadOrQuery();
final ExecuteSqlRequest.Builder builder =
getExecuteSqlRequestBuilder(statement, QueryMode.NORMAL);
ApiFuture<com.google.spanner.v1.ResultSet> resultSet;
try {
// Register the update as an async operation that must finish before the transaction may
// commit.
increaseAsynOperations();
resultSet = rpc.executeQueryAsync(builder.build(), session.getOptions());
} catch (Throwable t) {
decreaseAsyncOperations();
throw t;
}
ApiFuture<Long> updateCount =
ApiFutures.transform(
resultSet,
new ApiFunction<com.google.spanner.v1.ResultSet, Long>() {
@Override
public Long apply(ResultSet input) {
if (!input.hasStats()) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT,
"DML response missing stats possibly due to non-DML statement as input");
}
// For standard DML, using the exact row count.
return input.getStats().getRowCountExact();
}
},
MoreExecutors.directExecutor());
updateCount =
ApiFutures.catching(
updateCount,
Throwable.class,
new ApiFunction<Throwable, Long>() {
@Override
public Long apply(Throwable input) {
SpannerException e = SpannerExceptionFactory.newSpannerException(input);
onError(e);
throw e;
}
},
MoreExecutors.directExecutor());
updateCount.addListener(
new Runnable() {
@Override
public void run() {
decreaseAsyncOperations();
}
},
MoreExecutors.directExecutor());
return updateCount;
}
@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;
}
}
@Override
public ApiFuture<long[]> batchUpdateAsync(Iterable<Statement> statements) {
beforeReadOrQuery();
final ExecuteBatchDmlRequest.Builder builder = getExecuteBatchDmlRequestBuilder(statements);
ApiFuture<com.google.spanner.v1.ExecuteBatchDmlResponse> response;
try {
// Register the update as an async operation that must finish before the transaction may
// commit.
increaseAsynOperations();
response = rpc.executeBatchDmlAsync(builder.build(), session.getOptions());
} catch (Throwable t) {
decreaseAsyncOperations();
throw t;
}
final ApiFuture<long[]> updateCounts =
ApiFutures.transform(
response,
new ApiFunction<ExecuteBatchDmlResponse, long[]>() {
@Override
public long[] apply(ExecuteBatchDmlResponse input) {
long[] results = new long[input.getResultSetsCount()];
for (int i = 0; i < input.getResultSetsCount(); ++i) {
results[i] = input.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 (input.getStatus().getCode() == Code.ABORTED_VALUE) {
throw newSpannerException(
ErrorCode.fromRpcStatus(input.getStatus()), input.getStatus().getMessage());
} else if (input.getStatus().getCode() != 0) {
throw newSpannerBatchUpdateException(
ErrorCode.fromRpcStatus(input.getStatus()),
input.getStatus().getMessage(),
results);
}
return results;
}
},
MoreExecutors.directExecutor());
updateCounts.addListener(
new Runnable() {
@Override
public void run() {
try {
updateCounts.get();
} catch (ExecutionException e) {
onError(SpannerExceptionFactory.newSpannerException(e.getCause()));
} catch (InterruptedException e) {
onError(SpannerExceptionFactory.propagateInterrupt(e));
} finally {
decreaseAsyncOperations();
}
}
},
MoreExecutors.directExecutor());
return updateCounts;
}
private ListenableAsyncResultSet wrap(ListenableAsyncResultSet delegate) {
return new TransactionContextAsyncResultSetImpl(delegate);
}
@Override
public ListenableAsyncResultSet readAsync(
String table, KeySet keys, Iterable<String> columns, ReadOption... options) {
return wrap(super.readAsync(table, keys, columns, options));
}
@Override
public ListenableAsyncResultSet readUsingIndexAsync(
String table, String index, KeySet keys, Iterable<String> columns, ReadOption... options) {
return wrap(super.readUsingIndexAsync(table, index, keys, columns, options));
}
@Override
public ListenableAsyncResultSet executeQueryAsync(
final Statement statement, final QueryOption... options) {
return wrap(super.executeQueryAsync(statement, options));
}
}
private boolean blockNestedTxn = true;
private final SessionImpl session;
private 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.txn = session.newTransaction();
}
@Override
public void setSpan(Span span) {
this.span = span;
}
@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.setWithFailure(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 (attempt.get() > 0) {
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;
}
}