Skip to content

sort requestids before checking monotonicity #4017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,10 @@ XGoogSpannerRequestId withNthClientId(long replacementClientId) {
return XGoogSpannerRequestId.of(
replacementClientId, this.nthChannelId, this.nthRequest, this.attempt);
}

@VisibleForTesting
XGoogSpannerRequestId withAttempt(long replacementAttempt) {
return XGoogSpannerRequestId.of(
this.nthClientId, this.nthChannelId, this.nthRequest, replacementAttempt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1417,8 +1417,7 @@ public void testWriteAtLeastOnceAborted() {

List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
assertEquals(2, commitRequests.size());
// TODO(@odeke-em): Enable in later PR.
// xGoogReqIdInterceptor.assertIntegrity();
xGoogReqIdInterceptor.assertIntegrity();
}

@Test
Expand Down Expand Up @@ -2910,34 +2909,34 @@ public void testPartitionedDmlDoesNotTimeout() {

DatabaseClientImpl dbImpl = ((DatabaseClientImpl) client);
int channelId = 0;
try (Session session = dbImpl.getSession()) {
channelId = ((PooledSessionFuture) session).getChannel();
try (PooledSessionFuture session = dbImpl.getSession()) {
channelId = session.getChannel();
}
int dbId = dbImpl.dbId;
long NON_DETERMINISTIC = XGoogSpannerRequestIdTest.NON_DETERMINISTIC;
XGoogSpannerRequestIdTest.MethodAndRequestId[] wantStreamingValues = {
XGoogSpannerRequestIdTest.ofMethodAndRequestId(
"google.spanner.v1.Spanner/ExecuteStreamingSql",
new XGoogSpannerRequestId(NON_DETERMINISTIC, channelId, 6, 1)),
new XGoogSpannerRequestId(
NON_DETERMINISTIC, NON_DETERMINISTIC, NON_DETERMINISTIC, NON_DETERMINISTIC)),
};
if (false) { // TODO(@odeke-em): enable in next PRs.
xGoogReqIdInterceptor.checkExpectedStreamingXGoogRequestIds(wantStreamingValues);
}
xGoogReqIdInterceptor.checkExpectedStreamingXGoogRequestIds(wantStreamingValues);

XGoogSpannerRequestIdTest.MethodAndRequestId[] wantUnaryValues = {
XGoogSpannerRequestIdTest.ofMethodAndRequestId(
"google.spanner.v1.Spanner/BeginTransaction",
new XGoogSpannerRequestId(NON_DETERMINISTIC, channelId, 7, 1)),
new XGoogSpannerRequestId(
NON_DETERMINISTIC, NON_DETERMINISTIC, NON_DETERMINISTIC, NON_DETERMINISTIC)),
XGoogSpannerRequestIdTest.ofMethodAndRequestId(
"google.spanner.v1.Spanner/CreateSession",
new XGoogSpannerRequestId(NON_DETERMINISTIC, 0, 1, 1)),
new XGoogSpannerRequestId(
NON_DETERMINISTIC, NON_DETERMINISTIC, NON_DETERMINISTIC, NON_DETERMINISTIC)),
XGoogSpannerRequestIdTest.ofMethodAndRequestId(
"google.spanner.v1.Spanner/ExecuteSql",
new XGoogSpannerRequestId(NON_DETERMINISTIC, channelId, 8, 1)),
new XGoogSpannerRequestId(
NON_DETERMINISTIC, NON_DETERMINISTIC, NON_DETERMINISTIC, NON_DETERMINISTIC)),
};
if (false) { // TODO(@odeke-em): enable in next PRs.
xGoogReqIdInterceptor.checkExpectedUnaryXGoogRequestIdsAsSuffixes(wantUnaryValues);
}
xGoogReqIdInterceptor.checkExpectedUnaryXGoogRequestIdsAsSuffixes(wantUnaryValues);
}
}

Expand Down Expand Up @@ -3023,35 +3022,35 @@ public void testPartitionedDmlWithHigherTimeout() {

DatabaseClientImpl dbImpl = ((DatabaseClientImpl) client);
int channelId = 0;
try (Session session = dbImpl.getSession()) {
channelId = ((PooledSessionFuture) session).getChannel();
try (PooledSessionFuture session = dbImpl.getSession()) {
channelId = session.getChannel();
}
int dbId = dbImpl.dbId;
long NON_DETERMINISTIC = XGoogSpannerRequestIdTest.NON_DETERMINISTIC;
XGoogSpannerRequestIdTest.MethodAndRequestId[] wantStreamingValues = {
XGoogSpannerRequestIdTest.ofMethodAndRequestId(
"google.spanner.v1.Spanner/ExecuteStreamingSql",
new XGoogSpannerRequestId(NON_DETERMINISTIC, channelId, 6, 1)),
new XGoogSpannerRequestId(
NON_DETERMINISTIC, NON_DETERMINISTIC, NON_DETERMINISTIC, NON_DETERMINISTIC)),
};

if (false) { // TODO(@odeke-em): enable in next PRs.
xGoogReqIdInterceptor.checkExpectedStreamingXGoogRequestIds(wantStreamingValues);
}
xGoogReqIdInterceptor.checkExpectedStreamingXGoogRequestIds(wantStreamingValues);

XGoogSpannerRequestIdTest.MethodAndRequestId[] wantUnaryValues = {
XGoogSpannerRequestIdTest.ofMethodAndRequestId(
"google.spanner.v1.Spanner/BeginTransaction",
new XGoogSpannerRequestId(NON_DETERMINISTIC, channelId, 7, 1)),
new XGoogSpannerRequestId(
NON_DETERMINISTIC, NON_DETERMINISTIC, NON_DETERMINISTIC, NON_DETERMINISTIC)),
XGoogSpannerRequestIdTest.ofMethodAndRequestId(
"google.spanner.v1.Spanner/CreateSession",
new XGoogSpannerRequestId(NON_DETERMINISTIC, 0, 1, 1)),
new XGoogSpannerRequestId(
NON_DETERMINISTIC, NON_DETERMINISTIC, NON_DETERMINISTIC, NON_DETERMINISTIC)),
XGoogSpannerRequestIdTest.ofMethodAndRequestId(
"google.spanner.v1.Spanner/ExecuteSql",
new XGoogSpannerRequestId(NON_DETERMINISTIC, channelId, 8, 1)),
new XGoogSpannerRequestId(
NON_DETERMINISTIC, NON_DETERMINISTIC, NON_DETERMINISTIC, NON_DETERMINISTIC)),
};
if (false) { // TODO(@odeke-em): enable in next PRs.
xGoogReqIdInterceptor.checkExpectedUnaryXGoogRequestIdsAsSuffixes(wantUnaryValues);
}
xGoogReqIdInterceptor.checkExpectedUnaryXGoogRequestIdsAsSuffixes(wantUnaryValues);
}
}

Expand Down Expand Up @@ -5304,8 +5303,7 @@ public void testSelectHasXGoogRequestIdHeader() {
assertEquals(1L, resultSet.getLong(0));
assertFalse(resultSet.next());
} finally {
// TODO(@odeke-em): Enable in later PR.
// xGoogReqIdInterceptor.assertIntegrity();
xGoogReqIdInterceptor.assertIntegrity();
}
}

Expand Down Expand Up @@ -5394,9 +5392,7 @@ public void testSessionPoolExhaustedError_containsStackTraces() {
"google.spanner.v1.Spanner/CreateSession",
new XGoogSpannerRequestId(NON_DETERMINISTIC, 0, 1, 1)),
};
if (false) { // TODO(@odeke-em): enable in next PRs.
xGoogReqIdInterceptor.checkExpectedUnaryXGoogRequestIdsAsSuffixes(wantUnaryValues);
}
xGoogReqIdInterceptor.checkExpectedUnaryXGoogRequestIdsAsSuffixes(wantUnaryValues);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,9 @@ public static class ServerHeaderEnforcer implements ServerInterceptor {
private Set<String> checkMethods;

ServerHeaderEnforcer(Set<String> checkMethods) {
this.gotValues = new CopyOnWriteArrayList<String>();
this.unaryResults =
new ConcurrentHashMap<String, CopyOnWriteArrayList<XGoogSpannerRequestId>>();
this.streamingResults =
new ConcurrentHashMap<String, CopyOnWriteArrayList<XGoogSpannerRequestId>>();
this.gotValues = new CopyOnWriteArrayList<>();
this.unaryResults = new ConcurrentHashMap<>();
this.streamingResults = new ConcurrentHashMap<>();
this.checkMethods = checkMethods;
}

Expand Down Expand Up @@ -139,6 +137,11 @@ public void assertIntegrity() {
}

private void assertMonotonicityOfIds(String prefix, List<XGoogSpannerRequestId> reqIds) {
reqIds.sort(
(id1, id2) -> {
if (id1.equals(id2)) return 0;
return id1.isGreaterThan(id2) ? 1 : -1;
});
int size = reqIds.size();

List<String> violations = new ArrayList<>();
Expand All @@ -161,7 +164,7 @@ private void assertMonotonicityOfIds(String prefix, List<XGoogSpannerRequestId>
}

public MethodAndRequestId[] accumulatedUnaryValues() {
List<MethodAndRequestId> accumulated = new ArrayList();
List<MethodAndRequestId> accumulated = new ArrayList<>();
this.unaryResults.forEach(
(String method, CopyOnWriteArrayList<XGoogSpannerRequestId> values) -> {
for (int i = 0; i < values.size(); i++) {
Expand All @@ -172,7 +175,7 @@ public MethodAndRequestId[] accumulatedUnaryValues() {
}

public MethodAndRequestId[] accumulatedStreamingValues() {
List<MethodAndRequestId> accumulated = new ArrayList();
List<MethodAndRequestId> accumulated = new ArrayList<>();
this.streamingResults.forEach(
(String method, CopyOnWriteArrayList<XGoogSpannerRequestId> values) -> {
for (int i = 0; i < values.size(); i++) {
Expand Down Expand Up @@ -308,7 +311,13 @@ static void massageValues(MethodAndRequestId[] mreqs) {
|| mreq.method.compareTo("google.spanner.v1.Spanner/CreateSession") == 0
|| mreq.method.compareTo("google.spanner.v1.Spanner/Commit") == 0) {
mreqs[i] =
new MethodAndRequestId(mreq.method, mreq.requestId.withNthClientId(NON_DETERMINISTIC));
new MethodAndRequestId(
mreq.method,
mreq.requestId
.withNthClientId(NON_DETERMINISTIC)
.withChannelId(NON_DETERMINISTIC)
.withNthRequest(NON_DETERMINISTIC)
.withAttempt(NON_DETERMINISTIC));
}
}
}
Expand Down
Loading