-
Notifications
You must be signed in to change notification settings - Fork 21
/
incrementalprune_test.go
430 lines (407 loc) · 12.8 KB
/
incrementalprune_test.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Copyright 2018 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package txn
import (
"fmt"
"time"
"github.com/juju/errors"
"github.com/juju/mgo/v3"
"github.com/juju/mgo/v3/bson"
mgotesting "github.com/juju/mgo/v3/testing"
"github.com/juju/mgo/v3/txn"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
)
var _ = gc.Suite(&IncrementalPruneSuite{})
type IncrementalPruneSuite struct {
TxnSuite
}
func (s *IncrementalPruneSuite) TestPruneAlsoCleans(c *gc.C) {
s.runTxn(c, txn.Op{
C: "docs",
Id: "1",
Insert: bson.M{"key": "value"},
})
var doc docWithQueue
c.Assert(s.db.C("docs").FindId("1").One(&doc), jc.ErrorIsNil)
c.Check(doc.Queue, gc.HasLen, 1)
pruner := NewIncrementalPruner(IncrementalPruneArgs{})
stats, err := pruner.Prune(s.txns)
c.Assert(err, jc.ErrorIsNil)
c.Check(stats.TxnsRemoved, gc.Equals, int64(1))
c.Check(stats.DocReads, gc.Equals, int64(1))
c.Check(stats.DocQueuesCleaned, gc.Equals, int64(1))
c.Check(stats.DocTokensCleaned, gc.Equals, int64(1))
// We should have cleaned the document, as well as deleting the transaction
c.Assert(s.db.C("docs").FindId("1").One(&doc), jc.ErrorIsNil)
c.Check(doc.Queue, gc.DeepEquals, []string{})
count, err := s.txns.Count()
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 0)
}
func (s *IncrementalPruneSuite) TestPruneHandlesMissingDocs(c *gc.C) {
s.runTxn(c, txn.Op{
C: "docs",
Id: "1",
Insert: bson.M{"key": "value"},
})
// Now that we have the doc, we forcefully delete it
res, err := s.db.C("docs").RemoveAll(nil)
c.Assert(err, jc.ErrorIsNil)
c.Check(res.Removed, gc.Equals, 1)
pruner := NewIncrementalPruner(IncrementalPruneArgs{})
stats, err := pruner.Prune(s.txns)
c.Assert(err, jc.ErrorIsNil)
c.Check(stats.TxnsRemoved, gc.Equals, int64(1))
c.Check(stats.DocReads, gc.Equals, int64(0))
c.Check(stats.DocQueuesCleaned, gc.Equals, int64(0))
c.Check(stats.DocTokensCleaned, gc.Equals, int64(0))
c.Check(stats.DocsMissing, gc.Equals, int64(1))
// The txn gets cleaned up since the docs are missing, thus nothing refers to it.
count, err := s.txns.Count()
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 0)
}
func (s *IncrementalPruneSuite) TestPruneIgnoresPendingTransactions(c *gc.C) {
s.runTxn(c, txn.Op{
C: "docs",
Id: "1",
Insert: bson.M{"key": "value"},
})
s.runInterruptedTxn(c, "set-applying", txn.Op{
C: "docs",
Id: "1",
Update: bson.M{"$set": bson.M{"key": "newvalue"}},
})
count, err := s.txns.Count()
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 2)
var doc docWithQueue
c.Assert(s.db.C("docs").FindId("1").One(&doc), jc.ErrorIsNil)
c.Check(doc.Queue, gc.HasLen, 2)
pruner := NewIncrementalPruner(IncrementalPruneArgs{})
stats, err := pruner.Prune(s.txns)
c.Assert(err, jc.ErrorIsNil)
c.Check(stats.TxnsRemoved, gc.Equals, int64(1))
c.Check(stats.DocReads, gc.Equals, int64(1))
c.Check(stats.DocQueuesCleaned, gc.Equals, int64(1))
c.Check(stats.DocTokensCleaned, gc.Equals, int64(1))
c.Check(stats.DocsMissing, gc.Equals, int64(0))
count, err = s.txns.Count()
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 1)
c.Assert(s.db.C("docs").FindId("1").One(&doc), jc.ErrorIsNil)
c.Check(doc.Queue, gc.HasLen, 1)
}
func (s *IncrementalPruneSuite) TestPruneIgnoresRecentTxns(c *gc.C) {
s.runTxn(c, txn.Op{
C: "docs",
Id: "1",
Insert: bson.M{"key": "value"},
})
count, err := s.txns.Count()
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 1)
var doc docWithQueue
c.Assert(s.db.C("docs").FindId("1").One(&doc), jc.ErrorIsNil)
c.Check(doc.Queue, gc.HasLen, 1)
pruner := NewIncrementalPruner(IncrementalPruneArgs{
MaxTime: time.Now().Add(-time.Hour),
})
// Nothing is touched because the txn is newer than 1 hour old
stats, err := pruner.Prune(s.txns)
c.Assert(err, jc.ErrorIsNil)
c.Check(stats.TxnsRemoved, gc.Equals, int64(0))
c.Check(stats.DocReads, gc.Equals, int64(0))
c.Check(stats.DocQueuesCleaned, gc.Equals, int64(0))
c.Check(stats.DocTokensCleaned, gc.Equals, int64(0))
c.Check(stats.DocsMissing, gc.Equals, int64(0))
count, err = s.txns.Count()
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 1)
c.Assert(s.db.C("docs").FindId("1").One(&doc), jc.ErrorIsNil)
c.Check(doc.Queue, gc.HasLen, 1)
}
func (s *IncrementalPruneSuite) TestPruneCleansUpStash(c *gc.C) {
s.runTxn(c, txn.Op{
C: "docs",
Id: "1",
Insert: bson.M{"key": "value"},
})
s.runTxn(c, txn.Op{
C: "docs",
Id: "1",
Remove: true,
})
count, err := s.txns.Count()
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 2)
var doc docWithQueue
// The doc should be in the stash
c.Assert(errors.Cause(s.db.C("docs").FindId("1").One(&doc)), gc.Equals, mgo.ErrNotFound)
txnsStash := s.db.C("txns.stash")
stashId := bson.D{{"c", "docs"}, {"id", "1"}}
c.Assert(txnsStash.FindId(stashId).One(&doc), jc.ErrorIsNil)
// Old versions of github.com/juju/mgo/v2 did not clean up the txn-queue during remove
// handle both versions here
extraTokens := 0
if len(doc.Queue) == 2 {
extraTokens = 1
c.Check(doc.Queue, gc.HasLen, 2)
} else {
c.Check(doc.Queue, gc.HasLen, 1)
}
pruner := NewIncrementalPruner(IncrementalPruneArgs{})
stats, err := pruner.Prune(s.txns)
c.Assert(err, jc.ErrorIsNil)
c.Check(stats.TxnsRemoved, gc.Equals, int64(2))
c.Check(stats.DocReads, gc.Equals, int64(0))
c.Check(stats.DocQueuesCleaned, gc.Equals, int64(1))
c.Check(stats.DocTokensCleaned, gc.Equals, int64(1+extraTokens))
c.Check(stats.DocsMissing, gc.Equals, int64(0))
c.Check(stats.StashDocReads, gc.Equals, int64(1))
c.Check(stats.StashDocsRemoved, gc.Equals, int64(1))
err = txnsStash.FindId(stashId).One(&doc)
c.Assert(errors.Cause(err), gc.Equals, mgo.ErrNotFound)
count, err = s.txns.Count()
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 0)
count, err = txnsStash.Count()
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 0)
}
func (s *IncrementalPruneSuite) TestPruneDoesntRereadCachedDocs(c *gc.C) {
// We create a lot of trnansactions updating the same doc
s.runTxn(c, txn.Op{
C: "docs",
Id: "1",
Insert: bson.M{"key": "value"},
})
for i := 0; i < 20; i++ {
s.runTxn(c, txn.Op{
C: "docs",
Id: "1",
Update: bson.M{"$set": bson.M{"key": fmt.Sprint(i)}},
})
}
count, err := s.txns.Count()
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 21)
var doc docWithQueue
// The doc should be in the stash
c.Assert(errors.Cause(s.db.C("docs").FindId("1").One(&doc)), jc.ErrorIsNil)
c.Check(doc.Queue, gc.HasLen, 1)
pruner := NewIncrementalPruner(IncrementalPruneArgs{})
stats, err := pruner.Prune(s.txns)
c.Assert(err, jc.ErrorIsNil)
c.Check(stats.TxnsRemoved, gc.Equals, int64(21))
c.Check(stats.DocReads, gc.Equals, int64(1))
c.Check(stats.DocQueuesCleaned, gc.Equals, int64(1))
c.Check(stats.DocTokensCleaned, gc.Equals, int64(1))
c.Check(stats.DocsMissing, gc.Equals, int64(0))
c.Check(stats.StashDocReads, gc.Equals, int64(0))
c.Check(stats.StashDocsRemoved, gc.Equals, int64(0))
c.Assert(errors.Cause(s.db.C("docs").FindId("1").One(&doc)), jc.ErrorIsNil)
c.Check(doc.Queue, gc.HasLen, 0)
count, err = s.txns.Count()
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 0)
}
func (s *IncrementalPruneSuite) TestPruneLeavesIncompleteStashAlone(c *gc.C) {
s.runTxn(c, txn.Op{
C: "docs",
Id: "1",
Insert: bson.M{"key": "value"},
})
s.runTxn(c, txn.Op{
C: "docs",
Id: "1",
Remove: true,
})
s.runInterruptedTxn(c, "set-applying", txn.Op{
C: "docs",
Id: "1",
Insert: bson.M{"key": "new-value"},
})
// Inserting the document again will try to restore it from the stash, but
// leave a pending txn
count, err := s.txns.Count()
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 3)
var doc docWithQueue
// The doc should be in the stash
c.Assert(errors.Cause(s.db.C("docs").FindId("1").One(&doc)), gc.Equals, mgo.ErrNotFound)
txnsStash := s.db.C("txns.stash")
stashId := bson.D{{"c", "docs"}, {"id", "1"}}
c.Assert(txnsStash.FindId(stashId).One(&doc), jc.ErrorIsNil)
// Old versions of github.com/juju/mgo/v2 did not clean up the txn-queue during remove
// handle both versions here
extraTokens := 0
if len(doc.Queue) == 3 {
extraTokens = 1
c.Check(doc.Queue, gc.HasLen, 3)
} else {
c.Check(doc.Queue, gc.HasLen, 2)
}
pruner := NewIncrementalPruner(IncrementalPruneArgs{})
stats, err := pruner.Prune(s.txns)
c.Assert(err, jc.ErrorIsNil)
// We can remove the finished txns, but not the incomplete one, and we must
// leave the doc in txns.stash
c.Check(stats.TxnsRemoved, gc.Equals, int64(2))
c.Check(stats.DocReads, gc.Equals, int64(0))
c.Check(stats.DocQueuesCleaned, gc.Equals, int64(1))
c.Check(stats.DocTokensCleaned, gc.Equals, int64(1+extraTokens))
c.Check(stats.DocsMissing, gc.Equals, int64(0))
c.Check(stats.StashDocReads, gc.Equals, int64(1))
c.Check(stats.StashDocsRemoved, gc.Equals, int64(0))
c.Assert(txnsStash.FindId(stashId).One(&doc), jc.ErrorIsNil)
c.Check(doc.Queue, gc.HasLen, 1)
count, err = s.txns.Count()
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 1)
count, err = txnsStash.Count()
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 1)
}
type TxnSuite struct {
mgotesting.IsolatedMgoSuite
db *mgo.Database
txns *mgo.Collection
runner *txn.Runner
}
func (s *TxnSuite) SetUpTest(c *gc.C) {
s.IsolatedMgoSuite.SetUpTest(c)
txn.SetChaos(txn.Chaos{})
s.db = s.Session.DB("mgo-test")
s.txns = s.db.C("txns")
s.runner = txn.NewRunner(s.txns)
}
func (s *TxnSuite) TearDownTest(c *gc.C) {
// Make sure we've removed any Chaos
txn.SetChaos(txn.Chaos{})
s.IsolatedMgoSuite.TearDownTest(c)
}
func (s *TxnSuite) runTxn(c *gc.C, ops ...txn.Op) bson.ObjectId {
txnId := bson.NewObjectId()
err := s.runner.Run(ops, txnId, nil)
c.Assert(err, jc.ErrorIsNil)
return txnId
}
// runInterruptedTxn starts a txn but uses Chaos to force that txn to not complete
func (s *TxnSuite) runInterruptedTxn(c *gc.C, breakpoint string, ops ...txn.Op) bson.ObjectId {
txnId := bson.NewObjectId()
txn.SetChaos(txn.Chaos{
KillChance: 1,
Breakpoint: breakpoint,
})
err := s.runner.Run(ops, txnId, nil)
c.Assert(err, gc.Equals, txn.ErrChaos)
txn.SetChaos(txn.Chaos{})
return txnId
}
type PrunerStatsSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&PrunerStatsSuite{})
func (*PrunerStatsSuite) TestBaseString(c *gc.C) {
v1 := PrunerStats{}
c.Check(v1.String(), gc.Equals, `
PrunerStats(
CacheLookupTime: 0.000
DocReadTime: 0.000
DocLookupTime: 0.000
DocCleanupTime: 0.000
StashLookupTime: 0.000
StashRemoveTime: 0.000
TxnReadTime: 0.000
TxnRemoveTime: 0.000
DocCacheHits: 0
DocCacheMisses: 0
DocMissingCacheHit: 0
DocsMissing: 0
CollectionQueries: 0
DocReads: 0
DocStillMissing: 0
StashQueries: 0
StashDocReads: 0
StashDocsRemoved: 0
DocQueuesCleaned: 0
DocTokensCleaned: 0
DocsAlreadyClean: 0
TxnsRemoved: 0
TxnsNotRemoved: 0
StrCacheHits: 0
StrCacheMisses: 0
)`[1:])
}
func (*PrunerStatsSuite) TestAlignedTimes(c *gc.C) {
v1 := PrunerStats{
CacheLookupTime: time.Duration(12345 * time.Millisecond),
DocReadTime: time.Duration(23456789 * time.Microsecond),
StashLookupTime: time.Duration(200 * time.Millisecond),
}
c.Check(v1.String(), gc.Equals, `
PrunerStats(
CacheLookupTime: 12.345
DocReadTime: 23.457
DocLookupTime: 0.000
DocCleanupTime: 0.000
StashLookupTime: 0.200
StashRemoveTime: 0.000
TxnReadTime: 0.000
TxnRemoveTime: 0.000
DocCacheHits: 0
DocCacheMisses: 0
DocMissingCacheHit: 0
DocsMissing: 0
CollectionQueries: 0
DocReads: 0
DocStillMissing: 0
StashQueries: 0
StashDocReads: 0
StashDocsRemoved: 0
DocQueuesCleaned: 0
DocTokensCleaned: 0
DocsAlreadyClean: 0
TxnsRemoved: 0
TxnsNotRemoved: 0
StrCacheHits: 0
StrCacheMisses: 0
)`[1:])
}
func (*PrunerStatsSuite) TestAlignedValues(c *gc.C) {
v1 := PrunerStats{
StashDocsRemoved: 1000,
StashDocReads: 12345,
}
c.Check(v1.String(), gc.Equals, `
PrunerStats(
CacheLookupTime: 0.000
DocReadTime: 0.000
DocLookupTime: 0.000
DocCleanupTime: 0.000
StashLookupTime: 0.000
StashRemoveTime: 0.000
TxnReadTime: 0.000
TxnRemoveTime: 0.000
DocCacheHits: 0
DocCacheMisses: 0
DocMissingCacheHit: 0
DocsMissing: 0
CollectionQueries: 0
DocReads: 0
DocStillMissing: 0
StashQueries: 0
StashDocReads: 12345
StashDocsRemoved: 1000
DocQueuesCleaned: 0
DocTokensCleaned: 0
DocsAlreadyClean: 0
TxnsRemoved: 0
TxnsNotRemoved: 0
StrCacheHits: 0
StrCacheMisses: 0
)`[1:])
}