Repository navigation
chore(spanner): restore active transaction state before retrying aborted commit - #15075
sakthivelmanii wants to merge 2 commits into
Conversation
…ted commit ### Summary Restores active read/write transaction state when `Connection.runTransaction(...)` retries after `commit()` encounters an `AbortedException`, ensuring retry attempts execute atomically within a single read/write transaction rather than falling back to autocommit mode. ### What Changed & Why - **Preserve transactional atomicity across commit-abort retries**: Previously, an `AbortedException` during `commit()` cleared the connection's active transaction markers before entering the retry loop. When `autocommit` was enabled, statements in the next retry attempt executed as individual autocommit operations instead of a single atomic read/write transaction, and the retry commit failed with `FAILED_PRECONDITION`. Retrying now restores active transaction state, rolls back uncommitted transaction-local connection properties, clears buffered mutations and savepoints from the aborted attempt, and reuses the underlying transaction session and tracing span across retries. - **Clean failure and cancellation teardown**: Non-abort commit errors, swallowed statement aborts, and thread interruptions during commit or retry backoff now clean up connection state, active batches, session resources, and OpenTelemetry spans without masking the original error with a secondary `FAILED_PRECONDITION`. - **Behavior change for aborts without server retry metadata**: If a transaction callable throws a synthetic `AbortedException` (or an abort arrives without `RetryInfo` metadata), `Connection.runTransaction(...)` now retries using jittered exponential backoff (capped at 32 seconds) instead of failing with an invalid sleep delay or null transaction state.
There was a problem hiding this comment.
Code Review
This pull request enhances the transaction retry and abort handling mechanism in the Spanner connection API. It introduces a configuration to keep aborted transactions open so that they can be cleanly reset or rolled back by the transaction runner, and implements a jittered exponential backoff for synthetic aborts that lack a retry delay. The review feedback highlights two important improvements: first, a potential IllegalStateException when resetting the transaction manager during synthetic aborts because resetForRetry() requires the manager to be in the ABORTED state; second, the need to populate this.abortedException in handleAborted when keeping aborted transactions open to prevent null pointer or state issues downstream.
| if (!keepAbortedTransactionOpen && txManager != null) { | ||
| try { | ||
| txManager.close(); | ||
| } catch (Throwable t) { | ||
| // ignore | ||
| } | ||
| txManager = null; | ||
| } | ||
| // Internal retry is not enabled. | ||
| this.state = UnitOfWorkState.ABORTED; |
There was a problem hiding this comment.
In handleAborted(AbortedException aborted), when keepAbortedTransactionOpen is true, the transaction state transitions to ABORTED but this.abortedException is not populated. This could lead to abortedException being null when TransactionRunnerImpl or other components expect it to be populated. We should set this.abortedException = aborted here as well for robustness.
if (!keepAbortedTransactionOpen && txManager != null) {
try {
txManager.close();
} catch (Throwable t) {
// ignore
}
txManager = null;
}
// Internal retry is not enabled.
this.state = UnitOfWorkState.ABORTED;
if (keepAbortedTransactionOpen) {
this.abortedException = aborted;
}
Summary
Restores active read/write transaction state when
Connection.runTransaction(...)retries aftercommit()encounters anAbortedException, ensuring retry attempts execute atomically within a single read/write transaction rather than falling back to autocommit mode.What Changed & Why
AbortedExceptionduringcommit()cleared the connection's active transaction markers before entering the retry loop. Whenautocommitwas enabled, statements in the next retry attempt executed as individual autocommit operations instead of a single atomic read/write transaction, and the retry commit failed withFAILED_PRECONDITION. Retrying now restores active transaction state, rolls back uncommitted transaction-local connection properties, clears buffered mutations and savepoints from the aborted attempt, and reuses the underlying transaction session and tracing span across retries.FAILED_PRECONDITION.AbortedException(or an abort arrives withoutRetryInfometadata),Connection.runTransaction(...)now retries using jittered exponential backoff (capped at 32 seconds) instead of failing with an invalid sleep delay or null transaction state.