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

kv: add a backoff to the retry loop in db.Txn #135173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion pkg/kv/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"math"
"time"

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/closedts"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/isolation"
Expand All @@ -23,6 +24,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/retry"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
Expand Down Expand Up @@ -1045,7 +1047,10 @@ func (e *AutoCommitError) Error() string {
func (txn *Txn) exec(ctx context.Context, fn func(context.Context, *Txn) error) (err error) {
// Run fn in a retry loop until we encounter a success or
// error condition this loop isn't capable of handling.
for attempt := 1; ; attempt++ {
retryOpts := base.DefaultRetryOptions()
retryOpts.InitialBackoff = 20 * time.Millisecond
retryOpts.MaxBackoff = 200 * time.Millisecond
for r := retry.Start(retryOpts); r.Next(); {
if err := ctx.Err(); err != nil {
return errors.Wrap(err, "txn exec")
}
Expand Down Expand Up @@ -1115,6 +1120,8 @@ func (txn *Txn) exec(ctx context.Context, fn func(context.Context, *Txn) error)
// txn.db.ctx.Settings == nil is only expected in tests.
maxRetries = int(MaxInternalTxnAutoRetries.Get(&txn.db.ctx.Settings.SV))
}
// Add 1 because r.CurrentAttempt() starts at 0.
attempt := r.CurrentAttempt() + 1
if attempt > maxRetries {
// If the retries limit has been exceeded, rollback and return an error.
rollbackErr := txn.Rollback(ctx)
Expand Down