/
txns.go
322 lines (287 loc) · 9.15 KB
/
txns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2012-2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"fmt"
"reflect"
jujutxn "github.com/juju/txn"
"github.com/juju/utils/set"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2/txn"
)
const (
txnAssertEnvIsAlive = true
txnAssertEnvIsNotAlive = false
)
// txnRunner returns a jujutxn.Runner instance.
//
// If st.transactionRunner is non-nil, then that will be
// returned and the session argument will be ignored. This
// is the case in tests only, when we want to test concurrent
// operations.
//
// If st.transactionRunner is nil, then we create a new
// transaction runner with the provided session and return
// that.
func (st *State) txnRunner(session *mgo.Session) jujutxn.Runner {
if st.transactionRunner != nil {
return st.transactionRunner
}
return newMultiEnvRunner(st.EnvironUUID(), st.db.With(session), txnAssertEnvIsAlive)
}
// txnRunnerNoEnvAliveAssert returns a jujutxn.Runner instance that does not
// add an assertion for a live environment to the transaction. It was
// introduced only to allow the initial environment to be created and should
// be used rarely.
func (st *State) txnRunnerNoEnvAliveAssert(session *mgo.Session) jujutxn.Runner {
if st.transactionRunner != nil {
return st.transactionRunner
}
return newMultiEnvRunner(st.EnvironUUID(), st.db.With(session), txnAssertEnvIsNotAlive)
}
// runTransactionNoEnvAliveAssert is a convenience method delegating to txnRunnerNoEnvAliveAssert.
func (st *State) runTransactionNoEnvAliveAssert(ops []txn.Op) error {
session := st.db.Session.Copy()
defer session.Close()
return st.txnRunnerNoEnvAliveAssert(session).RunTransaction(ops)
}
// runTransaction is a convenience method delegating to transactionRunner.
func (st *State) runTransaction(ops []txn.Op) error {
session := st.db.Session.Copy()
defer session.Close()
return st.txnRunner(session).RunTransaction(ops)
}
// run is a convenience method delegating to transactionRunner.
func (st *State) run(transactions jujutxn.TransactionSource) error {
session := st.db.Session.Copy()
defer session.Close()
return st.txnRunner(session).Run(transactions)
}
// ResumeTransactions resumes all pending transactions.
func (st *State) ResumeTransactions() error {
session := st.db.Session.Copy()
defer session.Close()
return st.txnRunner(session).ResumeTransactions()
}
// MaybePruneTransactions removes data for completed transactions.
func (st *State) MaybePruneTransactions() error {
session := st.db.Session.Copy()
defer session.Close()
// Prune txns only when txn count has doubled since last prune.
return st.txnRunner(session).MaybePruneTransactions(2.0)
}
func newMultiEnvRunner(envUUID string, db *mgo.Database, assertEnvAlive bool) jujutxn.Runner {
return &multiEnvRunner{
rawRunner: jujutxn.NewRunner(jujutxn.RunnerParams{Database: db}),
envUUID: envUUID,
assertEnvAlive: assertEnvAlive,
}
}
type multiEnvRunner struct {
rawRunner jujutxn.Runner
envUUID string
assertEnvAlive bool
}
// RunTransaction is part of the jujutxn.Runner interface. Operations
// that affect multi-environment collections will be modified in-place
// to ensure correct interaction with these collections.
func (r *multiEnvRunner) RunTransaction(ops []txn.Op) error {
ops = r.updateOps(ops)
return r.rawRunner.RunTransaction(ops)
}
// Run is part of the jujutxn.Runner interface. Operations returned by
// the given "transactions" function that affect multi-environment
// collections will be modified in-place to ensure correct interaction
// with these collections.
func (r *multiEnvRunner) Run(transactions jujutxn.TransactionSource) error {
return r.rawRunner.Run(func(attempt int) ([]txn.Op, error) {
ops, err := transactions(attempt)
if err != nil {
// Don't use Trace here as jujutxn doens't use juju/errors
// and won't deal correctly with some returned errors.
return nil, err
}
ops = r.updateOps(ops)
return ops, nil
})
}
// ResumeTransactions is part of the jujutxn.Runner interface.
func (r *multiEnvRunner) ResumeTransactions() error {
return r.rawRunner.ResumeTransactions()
}
// MaybePruneTransactions is part of the jujutxn.Runner interface.
func (r *multiEnvRunner) MaybePruneTransactions(pruneFactor float32) error {
return r.rawRunner.MaybePruneTransactions(pruneFactor)
}
func (r *multiEnvRunner) updateOps(ops []txn.Op) []txn.Op {
var opsNeedEnvAlive bool
for i, op := range ops {
if multiEnvCollections.Contains(op.C) {
var docID interface{}
if id, ok := op.Id.(string); ok {
docID = addEnvUUID(r.envUUID, id)
ops[i].Id = docID
} else {
docID = op.Id
}
if op.Insert != nil {
switch doc := op.Insert.(type) {
case bson.D:
ops[i].Insert = r.updateBsonD(doc, docID, op.C)
case bson.M:
r.updateBsonM(doc, docID, op.C)
case map[string]interface{}:
r.updateBsonM(bson.M(doc), docID, op.C)
default:
if !r.updateStruct(doc, docID, op.C) {
panic(fmt.Sprintf("unsupported document type for multi-environment collection "+
"(must be bson.D, bson.M or struct). Got %T for insert into %s.", doc, op.C))
}
}
if r.assertEnvAlive && !opsNeedEnvAlive && envAliveColls.Contains(op.C) {
opsNeedEnvAlive = true
}
}
}
}
if opsNeedEnvAlive {
ops = append(ops, assertEnvAliveOp(r.envUUID))
}
return ops
}
func assertEnvAliveOp(envUUID string) txn.Op {
return txn.Op{
C: environmentsC,
Id: envUUID,
Assert: isEnvAliveDoc,
}
}
var envAliveColls = newEnvAliveColls()
// newEnvAliveColls returns a copy of multiEnvCollections minus cleanupsC.
// This set is used to check if a txn needs to assert that there is a live
// environment be inserting docs.
func newEnvAliveColls() set.Strings {
e := set.NewStrings(multiEnvCollections.Values()...)
e.Remove(cleanupsC)
return e
}
func (r *multiEnvRunner) updateBsonD(doc bson.D, docID interface{}, collName string) bson.D {
idSeen := false
envUUIDSeen := false
for i, elem := range doc {
switch elem.Name {
case "_id":
idSeen = true
doc[i].Value = docID
case "env-uuid":
envUUIDSeen = true
if elem.Value != r.envUUID {
panic(fmt.Sprintf("environment UUID for document to insert into "+
"%s does not match state", collName))
}
}
}
if !idSeen {
doc = append(doc, bson.DocElem{"_id", docID})
}
if !envUUIDSeen {
doc = append(doc, bson.DocElem{"env-uuid", r.envUUID})
}
return doc
}
func (r *multiEnvRunner) updateBsonM(doc bson.M, docID interface{}, collName string) {
idSeen := false
envUUIDSeen := false
for key, value := range doc {
switch key {
case "_id":
idSeen = true
doc[key] = docID
case "env-uuid":
envUUIDSeen = true
if value != r.envUUID {
panic(fmt.Sprintf("environment UUID for document to insert into "+
"%s does not match state", collName))
}
}
}
if !idSeen {
doc["_id"] = docID
}
if !envUUIDSeen {
doc["env-uuid"] = r.envUUID
}
}
func (r *multiEnvRunner) updateStruct(doc, docID interface{}, collName string) bool {
v := reflect.ValueOf(doc)
t := v.Type()
if t.Kind() == reflect.Ptr {
v = v.Elem()
t = v.Type()
}
if t.Kind() != reflect.Struct {
return false
}
envUUIDSeen := false
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
switch f.Tag.Get("bson") {
case "_id":
r.updateStructField(v, f.Name, docID, collName, overrideField)
case "env-uuid":
r.updateStructField(v, f.Name, r.envUUID, collName, fieldMustMatch)
envUUIDSeen = true
}
}
if !envUUIDSeen {
panic(fmt.Sprintf("struct for insert into %s is missing an env-uuid field", collName))
}
return true
}
const overrideField = "override"
const fieldMustMatch = "mustmatch"
func (r *multiEnvRunner) updateStructField(v reflect.Value, name string, newValue interface{}, collName, updateType string) {
fv := v.FieldByName(name)
if fv.Interface() != newValue {
if updateType == fieldMustMatch && fv.String() != "" {
panic(fmt.Sprintf("%s for insert into %s does not match expected value",
name, collName))
}
if fv.CanSet() {
fv.Set(reflect.ValueOf(newValue))
} else {
panic(fmt.Sprintf("struct for insert into %s requires "+
"%s change but was passed by value", collName, name))
}
}
}
// rawTxnRunner returns a transaction runner that won't perform
// automatic addition of environment UUIDs into transaction
// operations, even for collections that contain documents for
// multiple environments. It should be used rarely.
func (st *State) rawTxnRunner(session *mgo.Session) jujutxn.Runner {
if st.transactionRunner != nil {
return getRawRunner(st.transactionRunner)
}
return jujutxn.NewRunner(jujutxn.RunnerParams{
Database: st.db.With(session),
})
}
// runRawTransaction is a convenience method that will run a single
// transaction using a "raw" transaction runner, as returned by
// rawTxnRunner.
func (st *State) runRawTransaction(ops []txn.Op) error {
session := st.db.Session.Copy()
defer session.Close()
runner := st.rawTxnRunner(session)
return runner.RunTransaction(ops)
}
// getRawRunner returns the underlying "raw" transaction runner from
// the passed transaction runner.
func getRawRunner(runner jujutxn.Runner) jujutxn.Runner {
if runner, ok := runner.(*multiEnvRunner); ok {
return runner.rawRunner
}
return runner
}