~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/github.com/juju/txn/txn.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 
15
15
import (
16
16
        stderrors "errors"
 
17
        "strings"
17
18
 
18
19
        "github.com/juju/loggo"
19
20
        "gopkg.in/mgo.v2"
 
21
        "gopkg.in/mgo.v2/bson"
20
22
        "gopkg.in/mgo.v2/txn"
21
23
)
22
24
 
76
78
        MaybePruneTransactions(pruneFactor float32) error
77
79
}
78
80
 
 
81
type txnRunner interface {
 
82
        Run([]txn.Op, bson.ObjectId, interface{}) error
 
83
        ResumeAll() error
 
84
}
 
85
 
79
86
type transactionRunner struct {
80
87
        db                        *mgo.Database
81
88
        transactionCollectionName string
82
89
        changeLogName             string
83
90
        testHooks                 chan ([]TestHook)
 
91
 
 
92
        newRunner func() txnRunner
84
93
}
85
94
 
86
95
var _ Runner = (*transactionRunner)(nil)
119
128
        }
120
129
        txnRunner.testHooks = make(chan ([]TestHook), 1)
121
130
        txnRunner.testHooks <- nil
 
131
        txnRunner.newRunner = txnRunner.newRunnerImpl
122
132
        return txnRunner
123
133
}
124
134
 
125
 
func (tr *transactionRunner) newRunner() *txn.Runner {
 
135
func (tr *transactionRunner) newRunnerImpl() txnRunner {
126
136
        db := tr.db
127
137
        runner := txn.NewRunner(db.C(tr.transactionCollectionName))
128
138
        runner.ChangeLog(db.C(tr.changeLogName))
145
155
                if err := tr.RunTransaction(ops); err == nil {
146
156
                        return nil
147
157
                } else if err != txn.ErrAborted {
148
 
                        return err
 
158
                        // Mongo very occasionally returns an intermittent
 
159
                        // "unexpected message" error. Retry those.
 
160
                        // However if this is the last time, return that error
 
161
                        // rather than the excessive contention error.
 
162
                        if !strings.HasSuffix(err.Error(), "unexpected message") || i == (nrRetries-1) {
 
163
                                return err
 
164
                        }
149
165
                }
150
166
        }
151
167
        return ErrExcessiveContention