Skip to content
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

fix: retry aborted errors for writeAtLeastOnce #2627

Merged
merged 4 commits into from
Sep 25, 2023
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ If you are using Maven without the BOM, add this to your dependencies:
If you are using Gradle 5.x or later, add this to your dependencies:

```Groovy
implementation platform('com.google.cloud:libraries-bom:26.22.0')
implementation platform('com.google.cloud:libraries-bom:26.23.0')

implementation 'com.google.cloud:google-cloud-spanner'
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ public CommitResponse writeAtLeastOnceWithOptions(
}
requestBuilder.setRequestOptions(requestOptionsBuilder.build());
}
CommitRequest request = requestBuilder.build();
Span span = tracer.spanBuilder(SpannerImpl.COMMIT).startSpan();
try (Scope s = tracer.withSpan(span)) {
com.google.spanner.v1.CommitResponse response =
spanner.getRpc().commit(requestBuilder.build(), this.options);
return new CommitResponse(response);
return SpannerRetryHelper.runTxWithRetriesOnAborted(
() -> new CommitResponse(spanner.getRpc().commit(request, this.options)));
} catch (RuntimeException e) {
TraceUtil.setWithFailure(span, e);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,44 @@ public void testWrite() {
assertEquals(Priority.PRIORITY_UNSPECIFIED, commit.getRequestOptions().getPriority());
}

@Test
public void testWriteAborted() {
DatabaseClient client =
spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
// Force the Commit RPC to return Aborted the first time it is called. The exception is cleared
// after the first call, so the retry should succeed.
mockSpanner.setCommitExecutionTime(
SimulatedExecutionTime.ofException(
mockSpanner.createAbortedException(ByteString.copyFromUtf8("test"))));
Timestamp timestamp =
client.write(
Collections.singletonList(
Mutation.newInsertBuilder("FOO").set("ID").to(1L).set("NAME").to("Bar").build()));
assertNotNull(timestamp);

List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
assertEquals(2, commitRequests.size());
}

@Test
public void testWriteAtLeastOnceAborted() {
DatabaseClient client =
spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
// Force the Commit RPC to return Aborted the first time it is called. The exception is cleared
// after the first call, so the retry should succeed.
mockSpanner.setCommitExecutionTime(
SimulatedExecutionTime.ofException(
mockSpanner.createAbortedException(ByteString.copyFromUtf8("test"))));
Timestamp timestamp =
client.writeAtLeastOnce(
Collections.singletonList(
Mutation.newInsertBuilder("FOO").set("ID").to(1L).set("NAME").to("Bar").build()));
assertNotNull(timestamp);

List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
assertEquals(2, commitRequests.size());
}

@Test
public void testWriteWithOptions() {
DatabaseClient client =
Expand Down