forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
400 lines (343 loc) · 13.2 KB
/
database.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright 2012-2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"runtime/debug"
"strings"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/loggo"
jujutxn "github.com/juju/txn"
"github.com/juju/utils/featureflag"
"github.com/kr/pretty"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/txn"
"github.com/juju/juju/controller"
"github.com/juju/juju/feature"
"github.com/juju/juju/mongo"
)
var txnLogger = loggo.GetLogger("juju.state.txn")
type SessionCloser func()
func dontCloseAnything() {}
// Database exposes the mongodb capabilities that most of state should see.
type Database interface {
// Copy returns a matching Database with its own session, and a
// func that must be called when the Database is no longer needed.
//
// GetCollection and TransactionRunner results from the resulting Database
// will all share a session; this does not absolve you of responsibility
// for calling those collections' closers.
Copy() (Database, SessionCloser)
// CopyForModel returns a matching Database with its own session and
// its own modelUUID and a func that must be called when the Database is no
// longer needed.
//
// Same warnings apply for CopyForModel than for Copy.
CopyForModel(modelUUID string) (Database, SessionCloser)
// GetCollection returns the named Collection, and a func that must be
// called when the Collection is no longer needed. The returned Collection
// might or might not have its own session, depending on the Database; the
// closer must always be called regardless.
//
// If the schema specifies model-filtering for the named collection,
// the returned collection will automatically filter queries; for details,
// see modelStateCollection.
GetCollection(name string) (mongo.Collection, SessionCloser)
// GetCollecitonFor returns the named Collection, scoped for the
// model specified. As for GetCollection, a closer is also returned.
GetCollectionFor(modelUUID, name string) (mongo.Collection, SessionCloser)
// GetRawCollection returns the named mgo Collection. As no
// automatic model filtering is performed by the returned
// collection it should be rarely used. GetCollection() should be
// used in almost all cases.
GetRawCollection(name string) (*mgo.Collection, SessionCloser)
// TransactionRunner() returns a runner responsible for making changes to
// the database, and a func that must be called when the runner is no longer
// needed. The returned Runner might or might not have its own session,
// depending on the Database; the closer must always be called regardless.
//
// It will reject transactions that reference raw-access (or unknown)
// collections; it will automatically rewrite operations that reference
// non-global collections; and it will ensure that non-global documents can
// only be inserted while the corresponding model is still Alive.
TransactionRunner() (jujutxn.Runner, SessionCloser)
// RunTransaction is a convenience method for running a single
// transaction.
RunTransaction(ops []txn.Op) error
// RunTransaction is a convenience method for running a single
// transaction for the model specified.
RunTransactionFor(modelUUID string, ops []txn.Op) error
// RunRawTransaction is a convenience method that will run a
// single transaction using a "raw" transaction runner that won't
// perform model filtering.
RunRawTransaction(ops []txn.Op) error
// Run is a convenience method running a transaction using a
// transaction building function.
Run(transactions jujutxn.TransactionSource) error
// Schema returns the schema used to load the database. The returned schema
// is not a copy and must not be modified.
Schema() collectionSchema
}
// Change represents any mgo/txn-representable change to a Database.
type Change interface {
// Prepare ensures that db is in a valid base state for applying
// the change, and returns mgo/txn operations that will fail any
// enclosing transaction if the state has materially changed; or
// returns an error.
Prepare(db Database) ([]txn.Op, error)
}
// ErrChangeComplete can be returned from Prepare to finish an Apply
// attempt and report success without taking any further action.
var ErrChangeComplete = errors.New("change complete")
// Apply runs the supplied Change against the supplied Database. If it
// returns no error, the change succeeded.
func Apply(db Database, change Change) error {
db, closer := db.Copy()
defer closer()
buildTxn := func(int) ([]txn.Op, error) {
ops, err := change.Prepare(db)
if errors.Cause(err) == ErrChangeComplete {
return nil, jujutxn.ErrNoOperations
}
if err != nil {
return nil, errors.Trace(err)
}
return ops, nil
}
runner, closer := db.TransactionRunner()
defer closer()
if err := runner.Run(buildTxn); err != nil {
return errors.Trace(err)
}
return nil
}
// collectionInfo describes important features of a collection.
type collectionInfo struct {
// explicitCreate, if non-nil, will cause the collection to be explicitly
// Create~d (with the given value) before ensuring indexes.
explicitCreate *mgo.CollectionInfo
// indexes listed here will be EnsureIndex~ed before state is opened.
indexes []mgo.Index
// global collections will not have model filtering applied. Non-
// global collections will have both transactions and reads filtered by
// relevant model uuid.
global bool
// rawAccess collections can be safely accessed as a mongo.WriteCollection.
// Direct database access to txn-aware collections is strongly discouraged:
// merely writing directly to a field makes it impossible to use that field
// with mgo/txn; in the worst case, document deletion can destroy critical
// parts of the state distributed by mgo/txn, causing different runners to
// choose different global transaction orderings; this can in turn cause
// operations to be skipped.
//
// Short explanation follows: two different runners pick different -- but
// overlapping -- "next" transactions; they each pick the same txn-id; the
// first runner writes to an overlapping document and records the txn-id;
// and then the second runner inspects that document, sees that the chosen
// txn-id has already been applied, and <splat> skips that operation.
//
// Goodbye consistency. So please don't mix txn and non-txn writes without
// very careful analysis; and then, please, just don't do it anyway. If you
// need raw mgo, use a rawAccess collection.
rawAccess bool
}
// collectionSchema defines the set of collections used in juju.
type collectionSchema map[string]collectionInfo
// Create causes all recorded collections to be created and indexed as specified
func (schema collectionSchema) Create(
db *mgo.Database,
settings *controller.Config,
) error {
for name, info := range schema {
rawCollection := db.C(name)
if spec := info.explicitCreate; spec != nil {
// We allow the max txn log collection size to be overridden by the user.
if name == txnLogC && settings != nil {
maxSize := settings.MaxTxnLogSizeMB()
if maxSize > 0 {
logger.Infof("overriding max txn log collection size: %dM", maxSize)
spec.MaxBytes = maxSize * 1024 * 1024
}
}
if err := createCollection(rawCollection, spec); err != nil {
return mongo.MaybeUnauthorizedf(err, "cannot create collection %q", name)
}
}
for _, index := range info.indexes {
if err := rawCollection.EnsureIndex(index); err != nil {
return mongo.MaybeUnauthorizedf(err, "cannot create index")
}
}
}
return nil
}
// createCollection swallows collection-already-exists errors.
func createCollection(raw *mgo.Collection, spec *mgo.CollectionInfo) error {
err := raw.Create(spec)
// The lack of error code for this error was reported upstream:
// https://jira.mongodb.org/browse/SERVER-6992
if err == nil || strings.HasSuffix(err.Error(), "already exists") {
return nil
}
return err
}
// database implements Database.
type database struct {
// raw is the underlying mgo Database.
raw *mgo.Database
// schema specifies how the various collections must be handled.
schema collectionSchema
// modelUUID is used to automatically filter queries and operations on
// certain collections (as defined in .schema).
modelUUID string
// runner exists for testing purposes; if non-nil, the result of
// TransactionRunner will always ultimately use this value to run
// all transactions. Setting it renders the database goroutine-unsafe.
runner jujutxn.Runner
// ownSession is used to avoid copying additional sessions in a database
// resulting from Copy.
ownSession bool
// runTransactionObserver is passed on to txn.TransactionRunner, to be
// invoked after calls to Run and RunTransaction.
runTransactionObserver RunTransactionObserverFunc
// clock is used to time how long transactions take to run
clock clock.Clock
}
// RunTransactionObserverFunc is the type of a function to be called
// after an mgo/txn transaction is run.
type RunTransactionObserverFunc func(dbName, modelUUID string, ops []txn.Op, err error)
func (db *database) copySession(modelUUID string) (*database, SessionCloser) {
session := db.raw.Session.Copy()
return &database{
raw: db.raw.With(session),
schema: db.schema,
modelUUID: modelUUID,
runner: db.runner,
ownSession: true,
clock: db.clock,
}, session.Close
}
// Copy is part of the Database interface.
func (db *database) Copy() (Database, SessionCloser) {
return db.copySession(db.modelUUID)
}
// CopyForModel is part of the Database interface.
func (db *database) CopyForModel(modelUUID string) (Database, SessionCloser) {
return db.copySession(modelUUID)
}
// GetCollection is part of the Database interface.
func (db *database) GetCollection(name string) (collection mongo.Collection, closer SessionCloser) {
info, found := db.schema[name]
if !found {
logger.Errorf("using unknown collection %q", name)
if featureflag.Enabled(feature.DeveloperMode) {
logger.Errorf("from %s", string(debug.Stack()))
}
}
// Copy session if necessary.
if db.ownSession {
collection = mongo.WrapCollection(db.raw.C(name))
closer = dontCloseAnything
} else {
collection, closer = mongo.CollectionFromName(db.raw, name)
}
// Apply model filtering.
if !info.global {
collection = &modelStateCollection{
WriteCollection: collection.Writeable(),
modelUUID: db.modelUUID,
}
}
// Prevent layer-breaking.
if !info.rawAccess {
// TODO(fwereade): it would be nice to tweak the mongo.Collection
// interface a bit to drop Writeable in this situation, but it's
// not convenient yet.
}
return collection, closer
}
// GetCollectionFor is part of the Database interface.
func (db *database) GetCollectionFor(modelUUID, name string) (mongo.Collection, SessionCloser) {
newDb, dbcloser := db.CopyForModel(modelUUID)
collection, closer := newDb.GetCollection(name)
return collection, func() {
closer()
dbcloser()
}
}
// GetRawCollection is part of the Database interface.
func (db *database) GetRawCollection(name string) (*mgo.Collection, SessionCloser) {
collection, closer := db.GetCollection(name)
return collection.Writeable().Underlying(), closer
}
// TransactionRunner is part of the Database interface.
func (db *database) TransactionRunner() (runner jujutxn.Runner, closer SessionCloser) {
runner = db.runner
closer = dontCloseAnything
if runner == nil {
raw := db.raw
if !db.ownSession {
session := raw.Session.Copy()
raw = raw.With(session)
closer = session.Close
}
observer := func(t jujutxn.ObservedTransaction) {
txnLogger.Tracef("ran transaction in %.3fs %# v\nerr: %v",
t.Duration.Seconds(), pretty.Formatter(t.Ops), t.Error)
}
if db.runTransactionObserver != nil {
observer = func(t jujutxn.ObservedTransaction) {
txnLogger.Tracef("ran transaction in %.3fs %# v\nerr: %v",
t.Duration.Seconds(), pretty.Formatter(t.Ops), t.Error)
db.runTransactionObserver(
db.raw.Name, db.modelUUID,
t.Ops, t.Error,
)
}
}
params := jujutxn.RunnerParams{
Database: raw,
RunTransactionObserver: observer,
Clock: db.clock,
}
runner = jujutxn.NewRunner(params)
}
return &multiModelRunner{
rawRunner: runner,
modelUUID: db.modelUUID,
schema: db.schema,
}, closer
}
// RunTransaction is part of the Database interface.
func (db *database) RunTransaction(ops []txn.Op) error {
runner, closer := db.TransactionRunner()
defer closer()
return runner.RunTransaction(ops)
}
// RunTransactionFor is part of the Database interface.
func (db *database) RunTransactionFor(modelUUID string, ops []txn.Op) error {
newDB, dbcloser := db.CopyForModel(modelUUID)
defer dbcloser()
runner, closer := newDB.TransactionRunner()
defer closer()
return runner.RunTransaction(ops)
}
// RunRawTransaction is part of the Database interface.
func (db *database) RunRawTransaction(ops []txn.Op) error {
runner, closer := db.TransactionRunner()
defer closer()
if multiRunner, ok := runner.(*multiModelRunner); ok {
runner = multiRunner.rawRunner
}
return runner.RunTransaction(ops)
}
// Run is part of the Database interface.
func (db *database) Run(transactions jujutxn.TransactionSource) error {
runner, closer := db.TransactionRunner()
defer closer()
return runner.Run(transactions)
}
// Schema is part of the Database interface.
func (db *database) Schema() collectionSchema {
return db.schema
}