31
31
import java .util .List ;
32
32
import java .util .Map ;
33
33
import java .util .concurrent .ScheduledExecutorService ;
34
+ import java .util .concurrent .atomic .AtomicInteger ;
34
35
import javax .annotation .concurrent .GuardedBy ;
35
36
36
37
/** Client for creating single sessions and batches of sessions. */
@@ -174,6 +175,12 @@ interface SessionConsumer {
174
175
private final DatabaseId db ;
175
176
private final Attributes commonAttributes ;
176
177
178
+ // SessionClient is created long before a DatabaseClientImpl is created,
179
+ // as batch sessions are firstly created then later attached to each Client.
180
+ private static AtomicInteger NTH_ID = new AtomicInteger (0 );
181
+ private final int nthId ;
182
+ private final AtomicInteger nthRequest ;
183
+
177
184
@ GuardedBy ("this" )
178
185
private volatile long sessionChannelCounter ;
179
186
@@ -186,6 +193,8 @@ interface SessionConsumer {
186
193
this .executorFactory = executorFactory ;
187
194
this .executor = executorFactory .get ();
188
195
this .commonAttributes = spanner .getTracer ().createCommonAttributes (db );
196
+ this .nthId = SessionClient .NTH_ID .incrementAndGet ();
197
+ this .nthRequest = new AtomicInteger (0 );
189
198
}
190
199
191
200
@ Override
@@ -206,19 +215,23 @@ SessionImpl createSession() {
206
215
// The sessionChannelCounter could overflow, but that will just flip it to Integer.MIN_VALUE,
207
216
// which is also a valid channel hint.
208
217
final Map <SpannerRpc .Option , ?> options ;
218
+ final long channelId ;
209
219
synchronized (this ) {
210
220
options = optionMap (SessionOption .channelHint (sessionChannelCounter ++));
221
+ channelId = sessionChannelCounter ;
211
222
}
212
223
ISpan span = spanner .getTracer ().spanBuilder (SpannerImpl .CREATE_SESSION , this .commonAttributes );
213
224
try (IScope s = spanner .getTracer ().withSpan (span )) {
225
+ XGoogSpannerRequestId reqId =
226
+ XGoogSpannerRequestId .of (this .nthId , this .nthRequest .incrementAndGet (), channelId , 1 );
214
227
com .google .spanner .v1 .Session session =
215
228
spanner
216
229
.getRpc ()
217
230
.createSession (
218
231
db .getName (),
219
232
spanner .getOptions ().getDatabaseRole (),
220
233
spanner .getOptions ().getSessionLabels (),
221
- options );
234
+ reqId . withOptions ( options ) );
222
235
SessionReference sessionReference =
223
236
new SessionReference (
224
237
session .getName (), session .getCreateTime (), session .getMultiplexed (), options );
@@ -387,6 +400,8 @@ private List<SessionImpl> internalBatchCreateSessions(
387
400
.spanBuilderWithExplicitParent (SpannerImpl .BATCH_CREATE_SESSIONS_REQUEST , parent );
388
401
span .addAnnotation (String .format ("Requesting %d sessions" , sessionCount ));
389
402
try (IScope s = spanner .getTracer ().withSpan (span )) {
403
+ XGoogSpannerRequestId reqId =
404
+ XGoogSpannerRequestId .of (this .nthId , this .nthRequest .incrementAndGet (), channelHint , 1 );
390
405
List <com .google .spanner .v1 .Session > sessions =
391
406
spanner
392
407
.getRpc ()
@@ -395,7 +410,7 @@ private List<SessionImpl> internalBatchCreateSessions(
395
410
sessionCount ,
396
411
spanner .getOptions ().getDatabaseRole (),
397
412
spanner .getOptions ().getSessionLabels (),
398
- options );
413
+ reqId . withOptions ( options ) );
399
414
span .addAnnotation (
400
415
String .format (
401
416
"Request for %d sessions returned %d sessions" , sessionCount , sessions .size ()));
0 commit comments