Skip to content

Commit 4f13b14

Browse files
committed
chore(x-goog-spanner-request-id): add SessionImpl.getChannel() and Options.RequestIdOption.(equals, hashCode)
Implements channelId retrieval that'll then be used to plumb into x-goog-spanner-request-id's channel. Also implements Options.RequestIdOption.(equals, hashCode) Fixes #3899 Implement Options.RequestIdOption.(equals, hashCode)
1 parent 2b0f2ff commit 4f13b14

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,12 +1088,20 @@ void appendToOptions(Options options) {
10881088

10891089
@Override
10901090
public int hashCode() {
1091-
return RequestIdOption.class.hashCode();
1091+
return this.reqId.hashCode();
10921092
}
10931093

10941094
@Override
10951095
public boolean equals(Object o) {
1096-
return o instanceof RequestIdOption;
1096+
// instanceof for a null object returns false.
1097+
if (!(o instanceof RequestIdOption)) {
1098+
return false;
1099+
}
1100+
RequestIdOption other = (RequestIdOption) o;
1101+
if (this.reqId == null || other.reqId == null) {
1102+
return this.reqId == null && other.reqId == null;
1103+
}
1104+
return this.reqId.equals(other.reqId);
10971105
}
10981106
}
10991107
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ public CommitResponse writeAtLeastOnceWithOptions(
319319
private XGoogSpannerRequestId reqIdOrFresh(Options options) {
320320
XGoogSpannerRequestId reqId = options.reqId();
321321
if (reqId == null) {
322-
reqId = this.getRequestIdCreator().nextRequestId(1 /* TODO: channelId */, 1);
322+
reqId = this.getRequestIdCreator().nextRequestId(this.getChannel(), 1);
323323
}
324324
return reqId;
325325
}
@@ -464,17 +464,15 @@ public AsyncTransactionManagerImpl transactionManagerAsync(TransactionOption...
464464

465465
@Override
466466
public ApiFuture<Empty> asyncClose() {
467-
XGoogSpannerRequestId reqId =
468-
this.getRequestIdCreator().nextRequestId(1 /* TODO: channelId */, 0);
467+
XGoogSpannerRequestId reqId = this.getRequestIdCreator().nextRequestId(this.getChannel(), 0);
469468
return spanner.getRpc().asyncDeleteSession(getName(), reqId.withOptions(getOptions()));
470469
}
471470

472471
@Override
473472
public void close() {
474473
ISpan span = tracer.spanBuilder(SpannerImpl.DELETE_SESSION);
475474
try (IScope s = tracer.withSpan(span)) {
476-
XGoogSpannerRequestId reqId =
477-
this.getRequestIdCreator().nextRequestId(1 /* TODO: channelId */, 0);
475+
XGoogSpannerRequestId reqId = this.getRequestIdCreator().nextRequestId(this.getChannel(), 0);
478476
spanner.getRpc().deleteSession(getName(), reqId.withOptions(getOptions()));
479477
} catch (RuntimeException e) {
480478
span.setStatus(e);
@@ -505,8 +503,7 @@ ApiFuture<Transaction> beginTransactionAsync(
505503
}
506504
final BeginTransactionRequest request = requestBuilder.build();
507505
final ApiFuture<Transaction> requestFuture;
508-
XGoogSpannerRequestId reqId =
509-
this.getRequestIdCreator().nextRequestId(1 /* TODO: channelId */, 1);
506+
XGoogSpannerRequestId reqId = this.getRequestIdCreator().nextRequestId(this.getChannel(), 1);
510507
try (IScope ignore = tracer.withSpan(span)) {
511508
requestFuture =
512509
spanner
@@ -597,4 +594,9 @@ public void setRequestIdCreator(XGoogSpannerRequestId.RequestIdCreator creator)
597594
public XGoogSpannerRequestId.RequestIdCreator getRequestIdCreator() {
598595
return this.requestIdCreator;
599596
}
597+
598+
int getChannel() {
599+
Long channelHint = (Long) this.getOptions().get(SpannerRpc.Option.CHANNEL_HINT);
600+
return (int) (channelHint % this.spanner.getOptions().getNumChannels());
601+
}
600602
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,22 @@ public void testRequestId() {
900900
assertThat(option3.toString()).doesNotContain("requestId");
901901
}
902902

903+
@Test
904+
public void testRequestIdOptionEqualsAndHashCode() {
905+
XGoogSpannerRequestId reqId1 = XGoogSpannerRequestId.of(1, 2, 3, 4);
906+
XGoogSpannerRequestId reqId2 = XGoogSpannerRequestId.of(2, 3, 4, 5);
907+
Options.RequestIdOption opt1 = Options.requestId(reqId1);
908+
Options.RequestIdOption opt1Prime = Options.requestId(reqId1);
909+
Options.RequestIdOption opt2 = Options.requestId(reqId2);
910+
911+
assertTrue(opt1.equals(opt1));
912+
assertTrue(opt1.equals(opt1Prime));
913+
assertEquals(opt1.hashCode(), opt1Prime.hashCode());
914+
assertFalse(opt1.equals(opt2));
915+
assertNotEquals(opt1, opt2);
916+
assertNotEquals(opt1.hashCode(), opt2.hashCode());
917+
}
918+
903919
@Test
904920
public void testOptions_WithMultipleDifferentRequestIds() {
905921
XGoogSpannerRequestId reqId1 = XGoogSpannerRequestId.of(1, 1, 1, 1);

0 commit comments

Comments
 (0)