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
+ import java .util .Arrays ;
32
+ import java .util .concurrent .atomic .AtomicInteger ;
30
33
import javax .annotation .Nullable ;
31
34
32
35
class DatabaseClientImpl implements DatabaseClient {
@@ -40,6 +43,8 @@ class DatabaseClientImpl implements DatabaseClient {
40
43
@ VisibleForTesting final MultiplexedSessionDatabaseClient multiplexedSessionDatabaseClient ;
41
44
@ VisibleForTesting final boolean useMultiplexedSessionPartitionedOps ;
42
45
@ VisibleForTesting final boolean useMultiplexedSessionForRW ;
46
+ private final int dbId ;
47
+ private final AtomicInteger nthRequest ;
43
48
44
49
final boolean useMultiplexedSessionBlindWrite ;
45
50
@@ -86,6 +91,15 @@ class DatabaseClientImpl implements DatabaseClient {
86
91
this .tracer = tracer ;
87
92
this .useMultiplexedSessionForRW = useMultiplexedSessionForRW ;
88
93
this .commonAttributes = commonAttributes ;
94
+
95
+ this .dbId = this .dbIdFromClientId (this .clientId );
96
+ this .nthRequest = new AtomicInteger (0 );
97
+ }
98
+
99
+ private int dbIdFromClientId (String clientId ) {
100
+ int i = clientId .indexOf ("-" );
101
+ String strWithValue = clientId .substring (i + 1 );
102
+ return Integer .parseInt (strWithValue );
89
103
}
90
104
91
105
@ VisibleForTesting
@@ -179,8 +193,22 @@ public CommitResponse writeAtLeastOnceWithOptions(
179
193
return getMultiplexedSessionDatabaseClient ()
180
194
.writeAtLeastOnceWithOptions (mutations , options );
181
195
}
196
+
197
+ int nthRequest = this .nextNthRequest ();
198
+ 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
+
182
201
return runWithSessionRetry (
183
- session -> session .writeAtLeastOnceWithOptions (mutations , options ));
202
+ (session ) -> {
203
+ reqId .incrementAttempt ();
204
+ // 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
+ return session .writeAtLeastOnceWithOptions (
210
+ mutations , allOptions .toArray (new TransactionOption [0 ]));
211
+ });
184
212
} catch (RuntimeException e ) {
185
213
span .setStatus (e );
186
214
throw e ;
@@ -189,6 +217,10 @@ public CommitResponse writeAtLeastOnceWithOptions(
189
217
}
190
218
}
191
219
220
+ private int nextNthRequest () {
221
+ return this .nthRequest .addAndGet (1 );
222
+ }
223
+
192
224
@ Override
193
225
public ServerStream <BatchWriteResponse > batchWriteAtLeastOnce (
194
226
final Iterable <MutationGroup > mutationGroups , final TransactionOption ... options )
0 commit comments