Skip to content
This repository was archived by the owner on Apr 7, 2026. It is now read-only.

Commit 96bb8f4

Browse files
committed
chore(x-goog-spanner-request-id): plumb for BatchCreateSessions
This change plumbs x-goog-spanner-request-id into BatchCreateSessions and asserts that the header is present for that method. Updates #3537
1 parent 7c7218f commit 96bb8f4

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ interface SessionConsumer {
206206
this.executorFactory = executorFactory;
207207
this.executor = executorFactory.get();
208208
this.commonAttributes = spanner.getTracer().createCommonAttributes(db);
209+
this.nthId = SessionClient.NTH_ID.incrementAndGet();
210+
this.nthRequest = new AtomicInteger(0);
209211
}
210212

211213
@Override
@@ -239,6 +241,7 @@ SessionImpl createSession() {
239241
XGoogSpannerRequestId reqId = nextRequestId(channelId, 1);
240242
ISpan span = spanner.getTracer().spanBuilder(SpannerImpl.CREATE_SESSION, this.commonAttributes);
241243
try (IScope s = spanner.getTracer().withSpan(span)) {
244+
XGoogSpannerRequestId reqId = this.nextRequestId(channelId, 1);
242245
com.google.spanner.v1.Session session =
243246
spanner
244247
.getRpc()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +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.
309+
reqId.incrementAttempt();
308310
return new CommitResponse(
309311
spanner.getRpc().commit(request, reqId.withOptions(getOptions())));
310312
});

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,26 @@ public XGoogSpannerRequestId nextRequestId(long channelId, int attempt) {
159159
return XGoogSpannerRequestId.of(1, 1, 1, 0);
160160
}
161161
}
162+
163+
public static void assertMonotonicityOfIds(String prefix, List<XGoogSpannerRequestId> reqIds) {
164+
int size = reqIds.size();
165+
166+
List<String> violations = new ArrayList<>();
167+
for (int i = 1; i < size; i++) {
168+
XGoogSpannerRequestId prev = reqIds.get(i - 1);
169+
XGoogSpannerRequestId curr = reqIds.get(i);
170+
if (prev.isGreaterThan(curr)) {
171+
violations.add(String.format("#%d(%s) > #%d(%s)", i - 1, prev, i, curr));
172+
}
173+
}
174+
175+
if (violations.size() == 0) {
176+
return;
177+
}
178+
179+
throw new IllegalStateException(
180+
prefix
181+
+ " monotonicity violation:"
182+
+ String.join("\n\t", violations.toArray(new String[0])));
183+
}
162184
}

0 commit comments

Comments
 (0)