Skip to content

Commit 127ea9a

Browse files
committed
More code review updates
More updates DatabaseClientImpl.runWithSessionRetry update on ID Move reqId attempt increase out of runWithRetries Apply allOptions copy everywhere Complete testrunWithSessionRetry_withRequestId
1 parent 8c4f245 commit 127ea9a

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseClientImpl.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import com.google.common.util.concurrent.ListenableFuture;
2828
import com.google.spanner.v1.BatchWriteResponse;
2929
import io.opentelemetry.api.common.Attributes;
30-
import java.util.ArrayList;
31-
import java.util.Arrays;
3230
import java.util.HashMap;
3331
import java.util.Map;
3432
import java.util.concurrent.ExecutionException;

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionClient.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ Object value() {
117117
Option.REQUEST_ID, requestId);
118118
}
119119

120-
static Map<SpannerRpc.Option, ?> createRequestOptions(
121-
XGoogSpannerRequestId requestId) {
120+
static Map<SpannerRpc.Option, ?> createRequestOptions(XGoogSpannerRequestId requestId) {
122121
return ImmutableMap.of(Option.REQUEST_ID, requestId);
123122
}
124123

@@ -206,8 +205,6 @@ interface SessionConsumer {
206205
this.executorFactory = executorFactory;
207206
this.executor = executorFactory.get();
208207
this.commonAttributes = spanner.getTracer().createCommonAttributes(db);
209-
this.nthId = SessionClient.NTH_ID.incrementAndGet();
210-
this.nthRequest = new AtomicInteger(0);
211208
}
212209

213210
@Override

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionImpl.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,6 @@ public CommitResponse writeAtLeastOnceWithOptions(
305305
try (IScope s = tracer.withSpan(span)) {
306306
return SpannerRetryHelper.runTxWithRetriesOnAborted(
307307
() -> {
308-
// TODO(@odeke-em): Only increment on UNAVAILABLE and INCREMENT,
309-
// instead increment the nthRequest on ABORTED and others.
310-
reqId.incrementAttempt();
311308
return new CommitResponse(
312309
spanner.getRpc().commit(request, reqId.withOptions(getOptions())));
313310
});

google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static org.junit.Assert.assertTrue;
3535
import static org.junit.Assume.assumeFalse;
3636
import static org.junit.Assume.assumeTrue;
37+
import static org.mockito.ArgumentMatchers.any;
3738
import static org.mockito.Mockito.mock;
3839
import static org.mockito.Mockito.verify;
3940
import static org.mockito.Mockito.when;
@@ -5653,25 +5654,33 @@ public void testdbIdFromClientId() {
56535654
@Test
56545655
public void testrunWithSessionRetry_withRequestId() {
56555656
// Tests that DatabaseClientImpl.runWithSessionRetry correctly returns a XGoogSpannerRequestId
5656-
// and correctly increases its nthRequest ordinal number and that attempts stay at 1.
5657+
// and correctly increases its nthRequest ordinal number and that attempts stay at 1, given
5658+
// a fresh session returned on SessionNotFoundException.
56575659
SessionPool pool = mock(SessionPool.class);
56585660
PooledSessionFuture sessionFut = mock(PooledSessionFuture.class);
56595661
when(pool.getSession()).thenReturn(sessionFut);
5660-
// TODO:(@olavloite) to kindly help with resolving this mocking that's failing.
5661-
// when(pool.getPooledSessionReplacementHandler()).thenReturn(pool.new
5662-
// PooledSessionReplacementHandler());
5663-
TransactionOption option = mock(TransactionOption.class);
5662+
SessionPool.PooledSession pooledSession = mock(SessionPool.PooledSession.class);
5663+
when(sessionFut.get()).thenReturn(pooledSession);
5664+
SessionPool.PooledSessionReplacementHandler sessionReplacementHandler =
5665+
mock(SessionPool.PooledSessionReplacementHandler.class);
5666+
when(pool.getPooledSessionReplacementHandler()).thenReturn(sessionReplacementHandler);
5667+
when(sessionReplacementHandler.replaceSession(any(), any())).thenReturn(sessionFut);
56645668
DatabaseClientImpl client = new DatabaseClientImpl(pool, mock(TraceWrapper.class));
56655669

5666-
// 1. Run with no fail has a single attempt.
5670+
// 1. Run with no fail runs a single attempt.
5671+
final AtomicInteger nCalls = new AtomicInteger(0);
56675672
client.runWithSessionRetry(
56685673
(session, reqId) -> {
56695674
assertEquals(reqId.getAttempt(), 1);
5675+
nCalls.incrementAndGet();
56705676
return 1;
56715677
});
5678+
assertEquals(nCalls.get(), 1);
5679+
5680+
// Reset the call counter.
5681+
nCalls.set(0);
56725682

5673-
// 2. Run with SessionNotFoundException.
5674-
final AtomicInteger i = new AtomicInteger(0);
5683+
// 2. Run with SessionNotFoundException and ensure that a fresh requestId is returned each time.
56755684
SessionNotFoundException excSessionNotFound =
56765685
SpannerExceptionFactoryTest.newSessionNotFoundException(
56775686
"projects/p/instances/i/databases/d/sessions/s");
@@ -5687,11 +5696,13 @@ public void testrunWithSessionRetry_withRequestId() {
56875696
// a fresh requestId is generated.
56885697
assertEquals(reqId.getAttempt(), 1);
56895698

5690-
if (i.addAndGet(1) < 4) {
5699+
if (nCalls.addAndGet(1) < 4) {
56915700
throw excSessionNotFound;
56925701
}
56935702

56945703
return 1;
56955704
});
5705+
5706+
assertEquals(nCalls.get(), 4);
56965707
}
56975708
}

google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionClientTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertNull;
2221
import static org.junit.Assert.assertNotNull;
2322
import static org.junit.Assert.assertTrue;
2423
import static org.mockito.ArgumentMatchers.any;

0 commit comments

Comments
 (0)