Skip to content

Commit 8f3e7ba

Browse files
committed
Use session.getChannel() and assert for results
1 parent b84aae2 commit 8f3e7ba

File tree

5 files changed

+62
-19
lines changed

5 files changed

+62
-19
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ private List<Partition> partitionReadUsingIndex(
251251
builder.setPartitionOptions(pbuilder.build());
252252

253253
XGoogSpannerRequestId reqId =
254-
session.getRequestIdCreator().nextRequestId(1 /*TODO: retrieve channelId*/, 1);
254+
session.getRequestIdCreator().nextRequestId(session.getChannel(), 1);
255255
final PartitionReadRequest request = builder.build();
256256
try {
257257
PartitionResponse response = rpc.partitionRead(request, reqId.withOptions(options));
@@ -317,7 +317,7 @@ private List<Partition> partitionQuery(
317317
builder.setPartitionOptions(pbuilder.build());
318318

319319
XGoogSpannerRequestId reqId =
320-
session.getRequestIdCreator().nextRequestId(1 /*TODO: retrieve channelId*/, 1);
320+
session.getRequestIdCreator().nextRequestId(session.getChannel(), 1);
321321
final PartitionQueryRequest request = builder.build();
322322
try {
323323
PartitionResponse response = rpc.partitionQuery(request, reqId.withOptions(options));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ long executeStreamingPartitionedUpdate(
8282
Options options = Options.fromUpdateOptions(updateOptions);
8383
XGoogSpannerRequestId reqId = options.reqId();
8484
if (reqId == null) {
85-
reqId = session.getRequestIdCreator().nextRequestId(1 /*TODO: infer channelId*/, 0);
85+
reqId = session.getRequestIdCreator().nextRequestId(session.getChannel(), 0);
8686
}
8787

8888
try {
@@ -223,7 +223,7 @@ private ByteString initTransaction(final Options options) {
223223
.build();
224224
XGoogSpannerRequestId reqId = options.reqId();
225225
if (reqId == null) {
226-
reqId = session.getRequestIdCreator().nextRequestId(1 /*TODO: infer channelId*/, 1);
226+
reqId = session.getRequestIdCreator().nextRequestId(session.getChannel(), 1);
227227
}
228228
Transaction tx = rpc.beginTransaction(request, reqId.withOptions(session.getOptions()), true);
229229
if (tx.getId().isEmpty()) {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,15 +465,17 @@ public AsyncTransactionManagerImpl transactionManagerAsync(TransactionOption...
465465

466466
@Override
467467
public ApiFuture<Empty> asyncClose() {
468-
XGoogSpannerRequestId reqId = this.getRequestIdCreator().nextRequestId(this.getChannel(), 0);
468+
XGoogSpannerRequestId reqId =
469+
this.getRequestIdCreator().nextRequestId(this.getChannel(), 1);
469470
return spanner.getRpc().asyncDeleteSession(getName(), reqId.withOptions(getOptions()));
470471
}
471472

472473
@Override
473474
public void close() {
474475
ISpan span = tracer.spanBuilder(SpannerImpl.DELETE_SESSION);
475476
try (IScope s = tracer.withSpan(span)) {
476-
XGoogSpannerRequestId reqId = this.getRequestIdCreator().nextRequestId(this.getChannel(), 0);
477+
XGoogSpannerRequestId reqId =
478+
this.getRequestIdCreator().nextRequestId(this.getChannel(), 1);
477479
spanner.getRpc().deleteSession(getName(), reqId.withOptions(getOptions()));
478480
} catch (RuntimeException e) {
479481
span.setStatus(e);
@@ -504,7 +506,8 @@ ApiFuture<Transaction> beginTransactionAsync(
504506
}
505507
final BeginTransactionRequest request = requestBuilder.build();
506508
final ApiFuture<Transaction> requestFuture;
507-
XGoogSpannerRequestId reqId = this.getRequestIdCreator().nextRequestId(this.getChannel(), 1);
509+
XGoogSpannerRequestId reqId =
510+
this.getRequestIdCreator().nextRequestId(this.getChannel(), 1);
508511
try (IScope ignore = tracer.withSpan(span)) {
509512
requestFuture =
510513
spanner

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,9 +2918,13 @@ public void testPartitionedDmlDoesNotTimeout() {
29182918
"google.spanner.v1.Spanner/BatchCreateSessions",
29192919
new XGoogSpannerRequestId(1, dbId, channelId, 1)),
29202920
};
2921-
XGoogSpannerRequestIdTest.MethodAndRequestId[] wantStreamingValues = {};
2922-
xGoogReqIdInterceptor.checkExpectedUnaryXGoogRequestIds(wantUnaryValues);
2921+
XGoogSpannerRequestIdTest.MethodAndRequestId[] wantStreamingValues = {
2922+
XGoogSpannerRequestIdTest.ofMethodAndRequestId(
2923+
"google.spanner.v1.Spanner/ExecuteStreamingSql",
2924+
new XGoogSpannerRequestId(1, channelId, dbId, 2)),
2925+
};
29232926
xGoogReqIdInterceptor.checkExpectedStreamingXGoogRequestIds(wantStreamingValues);
2927+
xGoogReqIdInterceptor.checkExpectedUnaryXGoogRequestIds(wantUnaryValues);
29242928
}
29252929
}
29262930

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

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import io.grpc.ServerInterceptor;
2929
import io.grpc.Status;
3030
import java.util.ArrayList;
31+
import java.util.Arrays;
32+
import java.util.Comparator;
3133
import java.util.List;
3234
import java.util.Map;
3335
import java.util.Objects;
@@ -162,7 +164,7 @@ public MethodAndRequestId[] accumulatedUnaryValues() {
162164
this.unaryResults.forEach(
163165
(String method, CopyOnWriteArrayList<XGoogSpannerRequestId> values) -> {
164166
for (int i = 0; i < values.size(); i++) {
165-
accumulated.add(new MethodAndRequestId(method, values.get(i).toString()));
167+
accumulated.add(new MethodAndRequestId(method, values.get(i)));
166168
}
167169
});
168170
return accumulated.toArray(new MethodAndRequestId[0]);
@@ -173,25 +175,33 @@ public MethodAndRequestId[] accumulatedStreamingValues() {
173175
this.streamingResults.forEach(
174176
(String method, CopyOnWriteArrayList<XGoogSpannerRequestId> values) -> {
175177
for (int i = 0; i < values.size(); i++) {
176-
accumulated.add(new MethodAndRequestId(method, values.get(i).toString()));
178+
accumulated.add(new MethodAndRequestId(method, values.get(i)));
177179
}
178180
});
179181
return accumulated.toArray(new MethodAndRequestId[0]);
180182
}
181183

182184
public void checkExpectedUnaryXGoogRequestIds(MethodAndRequestId... wantUnaryValues) {
183185
MethodAndRequestId[] gotUnaryValues = this.accumulatedUnaryValues();
184-
System.out.println("\033[34mUnary: " + gotUnaryValues + "\033[00m");
185186
for (int i = 0; i < gotUnaryValues.length; i++) {
186-
System.out.println("ith: " + i + ":: " + gotUnaryValues[i]);
187+
System.out.println("\033[34misUnary: #" + i + ":: " + gotUnaryValues[i] + "\033[00m");
188+
}
189+
sortValues(gotUnaryValues);
190+
for (int i = 0; i < gotUnaryValues.length; i++) {
191+
System.out.println("\033[33misUnary: #" + i + ":: " + gotUnaryValues[i] + "\033[00m");
187192
}
188193
assertEquals(wantUnaryValues, gotUnaryValues);
189194
}
190195

196+
private void sortValues(MethodAndRequestId[] values) {
197+
Arrays.sort(values, new MethodAndRequestIdComparator());
198+
}
199+
191200
public void checkExpectedStreamingXGoogRequestIds(MethodAndRequestId... wantStreamingValues) {
192201
MethodAndRequestId[] gotStreamingValues = this.accumulatedStreamingValues();
202+
sortValues(gotStreamingValues);
193203
for (int i = 0; i < gotStreamingValues.length; i++) {
194-
System.out.println("ith: " + i + ":: " + gotStreamingValues[i]);
204+
System.out.println("\033[32misStreaming: #" + i + ":: " + gotStreamingValues[i] + "\033[00m");
195205
}
196206
assertEquals(wantStreamingValues, gotStreamingValues);
197207
}
@@ -205,24 +215,50 @@ public void reset() {
205215

206216
public static class MethodAndRequestId {
207217
String method;
208-
String requestId;
218+
XGoogSpannerRequestId requestId;
209219

210-
public MethodAndRequestId(String method, String requestId) {
220+
public MethodAndRequestId(String method, XGoogSpannerRequestId requestId) {
211221
this.method = method;
212222
this.requestId = requestId;
213223
}
214224

215225
public String toString() {
216-
return "{" + this.method + ":" + this.requestId + "}";
226+
return "{" + this.method + ":" + this.requestId.toString() + "}";
217227
}
228+
229+
@Override
230+
public boolean equals(Object o) {
231+
if (!(o instanceof MethodAndRequestId)) {
232+
return false;
233+
}
234+
MethodAndRequestId other = (MethodAndRequestId) o;
235+
return Objects.equals(this.method, other.method) && Objects.equals(this.requestId, other.requestId);
236+
}
237+
}
238+
239+
static class MethodAndRequestIdComparator implements Comparator<MethodAndRequestId> {
240+
@Override
241+
public int compare(MethodAndRequestId mr1, MethodAndRequestId mr2) {
242+
int cmpMethod = mr1.method.compareTo(mr2.method);
243+
if (cmpMethod != 0) {
244+
return cmpMethod;
245+
}
246+
if (Objects.equals(mr1.requestId, mr2.requestId)) {
247+
return 0;
248+
}
249+
if (mr1.requestId.isGreaterThan(mr2.requestId)) {
250+
return +1;
251+
}
252+
return -1;
253+
}
218254
}
219255

220256
public static MethodAndRequestId ofMethodAndRequestId(String method, String reqId) {
221-
return new MethodAndRequestId(method, reqId);
257+
return new MethodAndRequestId(method, XGoogSpannerRequestId.of(reqId));
222258
}
223259

224260
public static MethodAndRequestId ofMethodAndRequestId(
225261
String method, XGoogSpannerRequestId reqId) {
226-
return new MethodAndRequestId(method, reqId.toString());
262+
return new MethodAndRequestId(method, reqId);
227263
}
228264
}

0 commit comments

Comments
 (0)