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

Improve retry mechanism for transactions when WriteConflict error #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
wip: add context to Run(), remove 120 sec timeout which was moved to …
…juju/txn
nvinuesa committed Sep 4, 2023
commit c98f91874f360b7ce345b33460a456d3ec7b35b4
15 changes: 5 additions & 10 deletions sstxn/sstxn.go
Original file line number Diff line number Diff line change
@@ -24,9 +24,9 @@
package sstxn

import (
"context"
"errors"
"fmt"
"time"

"github.com/juju/mgo/v3"
"github.com/juju/mgo/v3/bson"
@@ -55,8 +55,6 @@ func (nilLogger) Criticalf(message string, args ...interface{}) {}

var _ Logger = nilLogger{}

const TRANSACTION_TIMEOUT = 120 * time.Second

var ErrTimeout = fmt.Errorf("transaction failed after retrying for 120 seconds")

// A Runner applies operations as part of a transaction onto any number
@@ -110,10 +108,7 @@ func NewRunner(db *mgo.Database, logger Logger) *Runner {
//
// Any number of transactions may be run concurrently, with one
// runner or many.
func (r *Runner) Run(ops []txn.Op, id bson.ObjectId, info interface{}) (err error) {
timeout := time.NewTimer(TRANSACTION_TIMEOUT)
defer timeout.Stop()

func (r *Runner) Run(ctx context.Context, ops []txn.Op, id bson.ObjectId, info interface{}) (err error) {
const efmt = "error in transaction op %d: %s"
for i := range ops {
op := &ops[i]
@@ -147,9 +142,9 @@ func (r *Runner) Run(ops []txn.Op, id bson.ObjectId, info interface{}) (err erro
return err
}
select {
case <-timeout.C:
r.logger.Debugf("transaction failed after retrying for 120 seconds, ops '%+v'", ops)
return ErrTimeout
case <-ctx.Done():
r.logger.Debugf("transaction cancelled by caller or timeout reached, ops '%+v'", ops)
return ctx.Err()
default:
}
r.logger.Tracef("retrying txn ops '%+v'", ops)
Loading