Skip to content

Commit c21aeb1

Browse files
committed
Address review feedback
1 parent fe05ea5 commit c21aeb1

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
import io.opentelemetry.api.common.Attributes;
3030
import java.util.ArrayList;
3131
import java.util.Arrays;
32-
import java.util.Objects;
32+
import java.util.HashMap;
33+
import java.util.Map;
3334
import java.util.concurrent.ExecutionException;
3435
import java.util.concurrent.Future;
3536
import java.util.concurrent.TimeUnit;
@@ -51,6 +52,7 @@ class DatabaseClientImpl implements DatabaseClient {
5152
@VisibleForTesting final boolean useMultiplexedSessionForRW;
5253
private final int dbId;
5354
private final AtomicInteger nthRequest;
55+
private final Map<String, Integer> clientIdToOrdinalMap;
5456

5557
final boolean useMultiplexedSessionBlindWrite;
5658

@@ -98,18 +100,19 @@ class DatabaseClientImpl implements DatabaseClient {
98100
this.useMultiplexedSessionForRW = useMultiplexedSessionForRW;
99101
this.commonAttributes = commonAttributes;
100102

103+
this.clientIdToOrdinalMap = new HashMap<String, Integer>();
101104
this.dbId = this.dbIdFromClientId(this.clientId);
102105
this.nthRequest = new AtomicInteger(0);
103106
}
104107

105108
@VisibleForTesting
106-
int dbIdFromClientId(String clientId) {
107-
int i = clientId.indexOf("-");
108-
String strWithValue = clientId.substring(i + 1);
109-
if (Objects.equals(strWithValue, "")) {
110-
strWithValue = "0";
109+
synchronized int dbIdFromClientId(String clientId) {
110+
Integer id = this.clientIdToOrdinalMap.get(clientId);
111+
if (id == null) {
112+
id = this.clientIdToOrdinalMap.size() + 1;
113+
this.clientIdToOrdinalMap.put(clientId, id);
111114
}
112-
return Integer.parseInt(strWithValue);
115+
return id;
113116
}
114117

115118
@VisibleForTesting
@@ -423,9 +426,13 @@ private UpdateOption[] withReqId(
423426
if (reqId == null) {
424427
return options;
425428
}
426-
ArrayList<UpdateOption> allOptions = new ArrayList(Arrays.asList(options));
427-
allOptions.add(new Options.RequestIdOption(reqId));
428-
return allOptions.toArray(new UpdateOption[0]);
429+
if (options == null || options.length == 0) {
430+
return new UpdateOption[] {new Options.RequestIdOption(reqId)};
431+
}
432+
UpdateOption[] allOptions = new UpdateOption[options.length + 1];
433+
System.arraycopy(options, 0, allOptions, 0, options.length);
434+
allOptions[options.length] = new Options.RequestIdOption(reqId);
435+
return allOptions;
429436
}
430437

431438
private TransactionOption[] withReqId(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,8 @@ public CommitResponse writeAtLeastOnceWithOptions(
305305
try (IScope s = tracer.withSpan(span)) {
306306
return SpannerRetryHelper.runTxWithRetriesOnAborted(
307307
() -> {
308-
// TODO: Detect an abort and then refresh the reqId.
308+
// TODO(@odeke-em): Only increment on UNAVAILABLE and INCREMENT,
309+
// instead increment the nthRequest on ABORTED and others.
309310
reqId.incrementAttempt();
310311
return new CommitResponse(
311312
spanner.getRpc().commit(request, reqId.withOptions(getOptions())));

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5643,10 +5643,11 @@ public void testdbIdFromClientId() {
56435643
TransactionOption option = mock(TransactionOption.class);
56445644
DatabaseClientImpl client = new DatabaseClientImpl(pool, mock(TraceWrapper.class));
56455645

5646-
assertEquals(client.dbIdFromClientId(""), 0);
5647-
assertEquals(client.dbIdFromClientId("client-10"), 10);
5648-
assertEquals(client.dbIdFromClientId("client--10"), -10);
5649-
assertThrows(NumberFormatException.class, () -> client.dbIdFromClientId("client10"));
5646+
for (int i = 0; i < 10; i++) {
5647+
String dbId = String.format("%d", i);
5648+
int id = client.dbIdFromClientId(dbId);
5649+
assertEquals(id, i + 2); // There was already 1 dbId after new DatabaseClientImpl.
5650+
}
56505651
}
56515652

56525653
@Test

0 commit comments

Comments
 (0)