Skip to content

Commit d408129

Browse files
Matt Whelansravotto
authored andcommitted
Avoid SAVEPOINT with backoff
1 parent 0a27be5 commit d408129

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

crdb/common.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,29 @@ func ExecuteInTx(ctx context.Context, tx Tx, fn func() error) (err error) {
8787
return err
8888
}
8989

90-
if rollbackErr := tx.Exec(ctx, "ROLLBACK TO SAVEPOINT cockroach_restart"); rollbackErr != nil {
91-
return newTxnRestartError(rollbackErr, err)
90+
// We have a retryable error. Check the retry policy.
91+
delay, retryErr := retryFunc(err)
92+
if delay > 0 && retryErr == nil {
93+
// We don't want to hold locks while waiting for a backoff, so restart the entire transaction
94+
if restartErr := tx.Exec(ctx, "ROLLBACK"); restartErr != nil {
95+
return newTxnRestartError(restartErr, err)
96+
}
97+
if restartErr := tx.Exec(ctx, "BEGIN"); restartErr != nil {
98+
return newTxnRestartError(restartErr, err)
99+
}
100+
if restartErr := tx.Exec(ctx, "SAVEPOINT cockroach_restart"); restartErr != nil {
101+
return newTxnRestartError(restartErr, err)
102+
}
103+
} else {
104+
if rollbackErr := tx.Exec(ctx, "ROLLBACK TO SAVEPOINT cockroach_restart"); rollbackErr != nil {
105+
return newTxnRestartError(rollbackErr, err)
106+
}
92107
}
93108

94-
delay, retryErr := retryFunc(err)
95109
if retryErr != nil {
96110
return retryErr
97111
}
112+
98113
if delay > 0 {
99114
select {
100115
case <-time.After(delay):

crdb/tx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (tx stdlibTxnAdapter) Commit(context.Context) error {
278278
return tx.tx.Commit()
279279
}
280280

281-
// Commit is part of the tx interface.
281+
// Rollback is part of the tx interface.
282282
func (tx stdlibTxnAdapter) Rollback(context.Context) error {
283283
return tx.tx.Rollback()
284284
}

0 commit comments

Comments
 (0)