27
27
import com .google .common .util .concurrent .ListenableFuture ;
28
28
import com .google .spanner .v1 .BatchWriteResponse ;
29
29
import io .opentelemetry .api .common .Attributes ;
30
- import java .util .ArrayList ;
31
30
import java .util .Arrays ;
31
+ import java .util .List ;
32
32
import java .util .concurrent .atomic .AtomicInteger ;
33
33
import javax .annotation .Nullable ;
34
34
@@ -169,7 +169,15 @@ public CommitResponse writeWithOptions(
169
169
if (canUseMultiplexedSessionsForRW () && getMultiplexedSessionDatabaseClient () != null ) {
170
170
return getMultiplexedSessionDatabaseClient ().writeWithOptions (mutations , options );
171
171
}
172
- return runWithSessionRetry (session -> session .writeWithOptions (mutations , options ));
172
+ int channelId = 1 ; /* TODO: infer the channelId from the gRPC channel of the session */
173
+ XGoogSpannerRequestId reqId =
174
+ XGoogSpannerRequestId .of (this .dbId , channelId , this .nextNthRequest (), 0 );
175
+ return runWithSessionRetry (
176
+ session -> {
177
+ reqId .incrementAttempt ();
178
+ // TODO: Update the channelId depending on the session that is inferred.
179
+ return session .writeWithOptions (mutations , appendReqIdToOptions (reqId , options ));
180
+ });
173
181
} catch (RuntimeException e ) {
174
182
span .setStatus (e );
175
183
throw e ;
@@ -194,20 +202,15 @@ public CommitResponse writeAtLeastOnceWithOptions(
194
202
.writeAtLeastOnceWithOptions (mutations , options );
195
203
}
196
204
197
- int nthRequest = this .nextNthRequest ();
198
205
int channelId = 1 ; /* TODO: infer the channelId from the gRPC channel of the session */
199
- XGoogSpannerRequestId reqId = XGoogSpannerRequestId . of ( this . dbId , channelId , nthRequest , 0 );
200
-
206
+ XGoogSpannerRequestId reqId =
207
+ XGoogSpannerRequestId . of ( this . dbId , channelId , this . nextNthRequest (), 0 );
201
208
return runWithSessionRetry (
202
209
(session ) -> {
203
210
reqId .incrementAttempt ();
204
211
// TODO: Update the channelId depending on the session that is inferred.
205
- ArrayList <TransactionOption > allOptions = new ArrayList <>();
206
- allOptions .add (new Options .RequestIdOption (reqId ));
207
- allOptions .addAll (Arrays .asList (options ));
208
-
209
212
return session .writeAtLeastOnceWithOptions (
210
- mutations , allOptions . toArray ( new TransactionOption [ 0 ] ));
213
+ mutations , appendReqIdToOptions ( reqId , options ));
211
214
});
212
215
} catch (RuntimeException e ) {
213
216
span .setStatus (e );
@@ -217,8 +220,22 @@ public CommitResponse writeAtLeastOnceWithOptions(
217
220
}
218
221
}
219
222
223
+ private TransactionOption [] appendReqIdToOptions (
224
+ XGoogSpannerRequestId reqId , TransactionOption ... options ) {
225
+ List <TransactionOption > allOptions = Arrays .asList (options );
226
+ allOptions .add (new Options .RequestIdOption (reqId ));
227
+ return allOptions .toArray (new TransactionOption [0 ]);
228
+ }
229
+
230
+ private UpdateOption [] appendReqIdToOptions (
231
+ XGoogSpannerRequestId reqId , UpdateOption ... options ) {
232
+ List <UpdateOption > allOptions = Arrays .asList (options );
233
+ allOptions .add (new Options .RequestIdOption (reqId ));
234
+ return allOptions .toArray (new UpdateOption [0 ]);
235
+ }
236
+
220
237
private int nextNthRequest () {
221
- return this .nthRequest .addAndGet ( 1 );
238
+ return this .nthRequest .incrementAndGet ( );
222
239
}
223
240
224
241
@ Override
@@ -230,7 +247,17 @@ public ServerStream<BatchWriteResponse> batchWriteAtLeastOnce(
230
247
if (canUseMultiplexedSessionsForRW () && getMultiplexedSessionDatabaseClient () != null ) {
231
248
return getMultiplexedSessionDatabaseClient ().batchWriteAtLeastOnce (mutationGroups , options );
232
249
}
233
- return runWithSessionRetry (session -> session .batchWriteAtLeastOnce (mutationGroups , options ));
250
+
251
+ int channelId = 1 ; /* TODO: infer the channelId from the gRPC channel of the session */
252
+ XGoogSpannerRequestId reqId =
253
+ XGoogSpannerRequestId .of (this .dbId , channelId , this .nextNthRequest (), 0 );
254
+
255
+ return runWithSessionRetry (
256
+ (session ) -> {
257
+ reqId .incrementAttempt ();
258
+ return session .batchWriteAtLeastOnce (
259
+ mutationGroups , appendReqIdToOptions (reqId , options ));
260
+ });
234
261
} catch (RuntimeException e ) {
235
262
span .setStatus (e );
236
263
throw e ;
@@ -378,7 +405,14 @@ private long executePartitionedUpdateWithPooledSession(
378
405
final Statement stmt , final UpdateOption ... options ) {
379
406
ISpan span = tracer .spanBuilder (PARTITION_DML_TRANSACTION , commonAttributes );
380
407
try (IScope s = tracer .withSpan (span )) {
381
- return runWithSessionRetry (session -> session .executePartitionedUpdate (stmt , options ));
408
+ int channelId = 1 ; /* TODO: infer the channelId from the gRPC channel of the session */
409
+ XGoogSpannerRequestId reqId =
410
+ XGoogSpannerRequestId .of (this .dbId , channelId , this .nextNthRequest (), 0 );
411
+ return runWithSessionRetry (
412
+ session -> {
413
+ reqId .incrementAttempt ();
414
+ return session .executePartitionedUpdate (stmt , appendReqIdToOptions (reqId , options ));
415
+ });
382
416
} catch (RuntimeException e ) {
383
417
span .setStatus (e );
384
418
span .end ();
0 commit comments