Skip to content

Improve transaction failure handling #1598

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

Merged
merged 1 commit into from
Jan 8, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,10 @@ public CompletionStage<Void> terminateAsync() {

@Override
public <T> T execute(Function<Throwable, T> causeOfTerminationConsumer) {
return executeWithLock(lock, () -> causeOfTerminationConsumer.apply(causeOfTermination));
return executeWithLock(lock, () -> {
var throwable = causeOfTermination == null ? null : failedTxException(causeOfTermination);
return causeOfTerminationConsumer.apply(throwable);
});
}

private void ensureCanRunQueries() {
Expand All @@ -347,15 +350,7 @@ private void ensureCanRunQueries() {
if (causeOfTermination instanceof TransactionTerminatedException transactionTerminatedException) {
throw transactionTerminatedException;
} else {
var message =
"Cannot run more queries in this transaction, it has either experienced an fatal error or was explicitly terminated";
throw new TransactionTerminatedException(
GqlStatusError.UNKNOWN.getStatus(),
GqlStatusError.UNKNOWN.getStatusDescription(message),
"N/A",
message,
GqlStatusError.DIAGNOSTIC_RECORD,
causeOfTermination);
throw failedTxException(causeOfTermination);
}
} else if (commitFuture != null) {
var message = "Cannot run more queries in this transaction, it is being committed";
Expand Down Expand Up @@ -578,6 +573,18 @@ private CompletionStage<Void> closeAsync(boolean commit, boolean completeWithNul
return stage;
}

private static TransactionTerminatedException failedTxException(Throwable cause) {
var message =
"Cannot run more queries in this transaction, it has either experienced a fatal error or was explicitly terminated";
return new TransactionTerminatedException(
GqlStatusError.UNKNOWN.getStatus(),
GqlStatusError.UNKNOWN.getStatusDescription(message),
"N/A",
message,
GqlStatusError.DIAGNOSTIC_RECORD,
cause);
}

private static class BeginResponseHandler implements DriverResponseHandler {
final CompletableFuture<UnmanagedTransaction> summaryFuture = new CompletableFuture<>();
private final ApiTelemetryWork apiTelemetryWork;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ void shouldPreventPullAfterTransactionTermination(boolean iterate) {
result.list();
}
});
assertEquals(terminationException, exception);
assertEquals(terminationException, exception.getCause());
}
tx.close();
}
Expand All @@ -441,8 +441,8 @@ void shouldPreventDiscardAfterTransactionTermination() {

// Then
for (var result : List.of(result0, result1)) {
var exception = assertThrows(ClientException.class, result::consume);
assertEquals(terminationException, exception);
var exception = assertThrows(TransactionTerminatedException.class, result::consume);
assertEquals(terminationException, exception.getCause());
}
tx.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void shouldPreventPullAfterTransactionTermination() {
for (var result : List.of(result0, result1)) {
var exception = assertThrows(
ClientException.class, () -> Flux.from(result.records()).blockFirst());
assertEquals(terminationException, exception);
assertEquals(terminationException, exception.getCause());
}
Mono.fromDirect(tx.close()).block();
}
Expand Down Expand Up @@ -92,7 +92,7 @@ void shouldPreventDiscardAfterTransactionTermination() {
for (var result : List.of(result0, result1)) {
var exception = assertThrows(ClientException.class, () -> Mono.fromDirect(result.consume())
.block());
assertEquals(terminationException, exception);
assertEquals(terminationException, exception.getCause());
}
Mono.fromDirect(tx.close()).block();
}
Expand Down