Skip to content

Commit 2fd0b4e

Browse files
committed
chore(x-goog-spanner-request-id): assert expectations in tests for retries + aborts
Updates #3537
1 parent 1dc5a3e commit 2fd0b4e

File tree

4 files changed

+70
-25
lines changed

4 files changed

+70
-25
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class DatabaseClientImpl implements DatabaseClient {
4848
@VisibleForTesting final MultiplexedSessionDatabaseClient multiplexedSessionDatabaseClient;
4949
@VisibleForTesting final boolean useMultiplexedSessionPartitionedOps;
5050
@VisibleForTesting final boolean useMultiplexedSessionForRW;
51-
private final int dbId;
51+
@VisibleForTesting final int dbId;
5252
private final AtomicInteger nthRequest;
5353
private final Map<String, Integer> clientIdToOrdinalMap;
5454

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public void run() {
148148
.addAnnotation(String.format("Creating %d sessions", sessionCount));
149149
while (remainingSessionsToCreate > 0) {
150150
try {
151+
System.out.println("\033[35mchannelHint: " + channelHint + "\033[00m");
151152
sessions = internalBatchCreateSessions(remainingSessionsToCreate, channelHint);
152153
} catch (Throwable t) {
153154
spanner.getTracer().getCurrentSpan().setStatus(t);

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2904,6 +2904,21 @@ public void testPartitionedDmlDoesNotTimeout() {
29042904
return null;
29052905
}));
29062906
assertEquals(ErrorCode.DEADLINE_EXCEEDED, e.getErrorCode());
2907+
2908+
DatabaseClientImpl dbImpl = ((DatabaseClientImpl) client);
2909+
int channelId = dbImpl.getSession().getChannel();
2910+
int dbId = dbImpl.dbId;
2911+
XGoogSpannerRequestIdTest.MethodAndRequestId[] wantUnaryValues = {
2912+
XGoogSpannerRequestIdTest.ofMethodAndRequestId(
2913+
"google.spanner.v1.Spanner/BatchCreateSessions",
2914+
new XGoogSpannerRequestId(1, dbId, channelId, 1)),
2915+
XGoogSpannerRequestIdTest.ofMethodAndRequestId(
2916+
"google.spanner.v1.Spanner/BatchCreateSessions",
2917+
new XGoogSpannerRequestId(1, dbId, channelId, 1)),
2918+
};
2919+
XGoogSpannerRequestIdTest.MethodAndRequestId[] wantStreamingValues = {};
2920+
xGoogReqIdInterceptor.checkExpectedUnaryXGoogRequestIds(wantUnaryValues);
2921+
xGoogReqIdInterceptor.checkExpectedStreamingXGoogRequestIds(wantStreamingValues);
29072922
}
29082923
}
29092924

@@ -5138,6 +5153,7 @@ public <ReqT, RespT> ApiCallContext configure(
51385153

51395154
@Test
51405155
public void testRetryOnResourceExhausted() {
5156+
// MARK: Retries here.
51415157
final RetrySettings retrySettings =
51425158
RetrySettings.newBuilder()
51435159
.setInitialRpcTimeoutDuration(Duration.ofSeconds(60L))
@@ -5172,9 +5188,9 @@ public void testRetryOnResourceExhausted() {
51725188
.setRetryableCodes(StatusCode.Code.UNAVAILABLE, StatusCode.Code.RESOURCE_EXHAUSTED)
51735189
.setRetrySettings(retrySettings);
51745190

5191+
DatabaseClient client;
51755192
try (Spanner spanner = builder.build().getService()) {
5176-
DatabaseClient client =
5177-
spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
5193+
client = spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
51785194
final int expectedRowCount = 5;
51795195
RandomResultSetGenerator generator = new RandomResultSetGenerator(expectedRowCount);
51805196
Statement statement = Statement.of("select * from random_table");
@@ -5219,6 +5235,8 @@ public void testRetryOnResourceExhausted() {
52195235
mockSpanner.clearRequests();
52205236
}
52215237
}
5238+
5239+
xGoogReqIdInterceptor.assertIntegrity();
52225240
}
52235241
}
52245242

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

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -157,40 +157,43 @@ private void assertMonotonicityOfIds(String prefix, List<XGoogSpannerRequestId>
157157
+ String.join("\n\t", violations.toArray(new String[0])));
158158
}
159159

160-
public static class methodAndRequestId {
161-
String method;
162-
String requestId;
163-
164-
public methodAndRequestId(String method, String requestId) {
165-
this.method = method;
166-
this.requestId = requestId;
167-
}
168-
169-
public String toString() {
170-
return "{" + this.method + ":" + this.requestId + "}";
171-
}
172-
}
173-
174-
public methodAndRequestId[] accumulatedUnaryValues() {
175-
List<methodAndRequestId> accumulated = new ArrayList();
160+
public MethodAndRequestId[] accumulatedUnaryValues() {
161+
List<MethodAndRequestId> accumulated = new ArrayList();
176162
this.unaryResults.forEach(
177163
(String method, CopyOnWriteArrayList<XGoogSpannerRequestId> values) -> {
178164
for (int i = 0; i < values.size(); i++) {
179-
accumulated.add(new methodAndRequestId(method, values.get(i).toString()));
165+
accumulated.add(new MethodAndRequestId(method, values.get(i).toString()));
180166
}
181167
});
182-
return accumulated.toArray(new methodAndRequestId[0]);
168+
return accumulated.toArray(new MethodAndRequestId[0]);
183169
}
184170

185-
public methodAndRequestId[] accumulatedStreamingValues() {
186-
List<methodAndRequestId> accumulated = new ArrayList();
171+
public MethodAndRequestId[] accumulatedStreamingValues() {
172+
List<MethodAndRequestId> accumulated = new ArrayList();
187173
this.streamingResults.forEach(
188174
(String method, CopyOnWriteArrayList<XGoogSpannerRequestId> values) -> {
189175
for (int i = 0; i < values.size(); i++) {
190-
accumulated.add(new methodAndRequestId(method, values.get(i).toString()));
176+
accumulated.add(new MethodAndRequestId(method, values.get(i).toString()));
191177
}
192178
});
193-
return accumulated.toArray(new methodAndRequestId[0]);
179+
return accumulated.toArray(new MethodAndRequestId[0]);
180+
}
181+
182+
public void checkExpectedUnaryXGoogRequestIds(MethodAndRequestId... wantUnaryValues) {
183+
MethodAndRequestId[] gotUnaryValues = this.accumulatedUnaryValues();
184+
System.out.println("\033[34mUnary: " + gotUnaryValues + "\033[00m");
185+
for (int i = 0; i < gotUnaryValues.length; i++) {
186+
System.out.println("ith: " + i + ":: " + gotUnaryValues[i]);
187+
}
188+
assertEquals(wantUnaryValues, gotUnaryValues);
189+
}
190+
191+
public void checkExpectedStreamingXGoogRequestIds(MethodAndRequestId... wantStreamingValues) {
192+
MethodAndRequestId[] gotStreamingValues = this.accumulatedStreamingValues();
193+
for (int i = 0; i < gotStreamingValues.length; i++) {
194+
System.out.println("ith: " + i + ":: " + gotStreamingValues[i]);
195+
}
196+
assertEquals(wantStreamingValues, gotStreamingValues);
194197
}
195198

196199
public void reset() {
@@ -199,4 +202,27 @@ public void reset() {
199202
this.streamingResults.clear();
200203
}
201204
}
205+
206+
public static class MethodAndRequestId {
207+
String method;
208+
String requestId;
209+
210+
public MethodAndRequestId(String method, String requestId) {
211+
this.method = method;
212+
this.requestId = requestId;
213+
}
214+
215+
public String toString() {
216+
return "{" + this.method + ":" + this.requestId + "}";
217+
}
218+
}
219+
220+
public static MethodAndRequestId ofMethodAndRequestId(String method, String reqId) {
221+
return new MethodAndRequestId(method, reqId);
222+
}
223+
224+
public static MethodAndRequestId ofMethodAndRequestId(
225+
String method, XGoogSpannerRequestId reqId) {
226+
return new MethodAndRequestId(method, reqId.toString());
227+
}
202228
}

0 commit comments

Comments
 (0)