-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
operations.go
565 lines (540 loc) · 16.4 KB
/
operations.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package metamorphic
import (
"context"
"fmt"
"math"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/engine"
)
// opRun represents one operation run; an mvccOp reference as well as bound
// arguments.
type opRun struct {
op *mvccOp
args []operand
}
// An mvccOp instance represents one type of an operation. The run and
// dependantOps commands should be stateless, with all state stored in the
// passed-in test runner or its operand managers.
type mvccOp struct {
// Name of the operation. Used in file output and parsing.
name string
// Function to call to run this operation.
run func(ctx context.Context, m *metaTestRunner, args ...operand) string
// Returns a list of operation runs that must happen before this operation.
// Note that openers for non-existent operands are handled separately and
// don't need to be handled here.
dependentOps func(m *metaTestRunner, args ...operand) []opRun
// Operands this operation expects. Passed in the same order to run and
// dependentOps.
operands []operandType
// weight is used to denote frequency of this operation to the TPCC-style
// deck.
//
// Note that the generator tends to bias towards opener operations; since
// an opener can be generated outside of the deck shuffle, in resolveAndRunOp
// to create an instance of an operand that does not exist. To counter this
// bias, we try to keep the sum of opener operations to be less than half
// the sum of "closer" operations for an operand type, to prevent too many
// of that type of object from accumulating throughout the run.
weight int
}
// Helper function to generate iterator_close opRuns for all iterators on a
// passed-in Batch.
func closeItersOnBatch(m *metaTestRunner, reader engine.Reader) (results []opRun) {
// No need to close iters on non-batches (i.e. engines).
if batch, ok := reader.(engine.Batch); ok {
// Close all iterators for this batch first.
iterManager := m.managers[OPERAND_ITERATOR].(*iteratorManager)
for _, iter := range iterManager.readerToIter[batch] {
results = append(results, opRun{
op: m.nameToOp["iterator_close"],
args: []operand{iter},
})
}
}
return
}
// Returns true if the specified iterator is a batch iterator.
func isBatchIterator(m *metaTestRunner, iter engine.Iterator) bool {
found := false
iterManager := m.managers[OPERAND_ITERATOR].(*iteratorManager)
for _, iter2 := range iterManager.readerToIter[m.engine] {
if iter2 == iter {
found = true
break
}
}
return !found
}
// Helper function to run MVCCScan given a key range and a reader.
func runMvccScan(ctx context.Context, m *metaTestRunner, reverse bool, inconsistent bool, args []operand) string {
key := args[0].(engine.MVCCKey)
endKey := args[1].(engine.MVCCKey)
if endKey.Less(key) {
tmpKey := endKey
endKey = key
key = tmpKey
}
ts := key.Timestamp
var txn *roachpb.Transaction
if !inconsistent {
txn = args[2].(*roachpb.Transaction)
ts = txn.ReadTimestamp
}
// While MVCCScanning on a batch works in Pebble, it does not in rocksdb.
// This is due to batch iterators not supporting SeekForPrev. For now, use
// m.engine instead of a readWriterManager-generated engine.Reader, otherwise
// we will try MVCCScanning on batches and produce diffs between runs on
// different engines that don't point to an actual issue.
kvs, _, intent, err := engine.MVCCScan(ctx, m.engine, key.Key, endKey.Key, math.MaxInt64, ts, engine.MVCCScanOptions{
Inconsistent: inconsistent,
Tombstones: true,
Reverse: reverse,
Txn: txn,
})
if err != nil {
return fmt.Sprintf("error: %s", err)
}
return fmt.Sprintf("kvs = %v, intent = %v", kvs, intent)
}
// Prints the key where an iterator is positioned, or valid = false if invalid.
func printIterState(iter engine.Iterator) string {
if ok, err := iter.Valid(); !ok || err != nil {
if err != nil {
return fmt.Sprintf("valid = %v, err = %s", ok, err.Error())
} else {
return "valid = false"
}
}
return fmt.Sprintf("key = %s", iter.UnsafeKey().String())
}
// List of operations, where each operation is defined as one instance of mvccOp.
var operations = []mvccOp{
{
name: "mvcc_inconsistent_get",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
reader := args[0].(engine.Reader)
key := args[1].(engine.MVCCKey)
val, intent, err := engine.MVCCGet(ctx, reader, key.Key, key.Timestamp, engine.MVCCGetOptions{
Inconsistent: true,
Tombstones: true,
Txn: nil,
})
if err != nil {
return fmt.Sprintf("error: %s", err)
}
return fmt.Sprintf("val = %v, intent = %v", val, intent)
},
operands: []operandType{
OPERAND_READWRITER,
OPERAND_MVCC_KEY,
},
weight: 10,
},
{
name: "mvcc_get",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
reader := args[0].(engine.Reader)
key := args[1].(engine.MVCCKey)
txn := args[2].(*roachpb.Transaction)
val, intent, err := engine.MVCCGet(ctx, reader, key.Key, txn.ReadTimestamp, engine.MVCCGetOptions{
Inconsistent: false,
Tombstones: true,
Txn: txn,
})
if err != nil {
return fmt.Sprintf("error: %s", err)
}
return fmt.Sprintf("val = %v, intent = %v", val, intent)
},
operands: []operandType{
OPERAND_READWRITER,
OPERAND_MVCC_KEY,
OPERAND_TRANSACTION,
},
weight: 10,
},
{
name: "mvcc_put",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
writer := args[0].(engine.ReadWriter)
key := args[1].(engine.MVCCKey)
value := roachpb.MakeValueFromBytes(args[2].([]byte))
txn := args[3].(*roachpb.Transaction)
txn.Sequence++
err := engine.MVCCPut(ctx, writer, nil, key.Key, txn.WriteTimestamp, value, txn)
if err != nil {
return fmt.Sprintf("error: %s", err)
}
// Update the txn's intent spans to account for this intent being written.
txn.IntentSpans = append(txn.IntentSpans, roachpb.Span{
Key: key.Key,
})
// If this write happened on a batch, track that in the txn manager so
// that the batch is committed before the transaction is aborted or
// committed.
if batch, ok := writer.(engine.Batch); ok {
txnManager := m.managers[OPERAND_TRANSACTION].(*txnManager)
txnManager.inFlightBatches[txn] = append(txnManager.inFlightBatches[txn], batch)
}
return "ok"
},
operands: []operandType{
OPERAND_READWRITER,
OPERAND_MVCC_KEY,
OPERAND_VALUE,
OPERAND_TRANSACTION,
},
weight: 30,
},
{
name: "mvcc_delete",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
writer := args[0].(engine.ReadWriter)
key := args[1].(engine.MVCCKey)
txn := args[2].(*roachpb.Transaction)
txn.Sequence++
err := engine.MVCCDelete(ctx, writer, nil, key.Key, txn.WriteTimestamp, txn)
if err != nil {
return fmt.Sprintf("error: %s", err)
}
// Update the txn's intent spans to account for this intent being written.
txn.IntentSpans = append(txn.IntentSpans, roachpb.Span{
Key: key.Key,
})
// If this write happened on a batch, track that in the txn manager so
// that the batch is committed before the transaction is aborted or
// committed.
if batch, ok := writer.(engine.Batch); ok {
txnManager := m.managers[OPERAND_TRANSACTION].(*txnManager)
txnManager.inFlightBatches[txn] = append(txnManager.inFlightBatches[txn], batch)
}
return "ok"
},
operands: []operandType{
OPERAND_READWRITER,
OPERAND_MVCC_KEY,
OPERAND_TRANSACTION,
},
weight: 10,
},
{
name: "mvcc_scan",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
return runMvccScan(ctx, m, false, false, args)
},
operands: []operandType{
OPERAND_MVCC_KEY,
OPERAND_MVCC_KEY,
OPERAND_TRANSACTION,
},
weight: 10,
},
{
name: "mvcc_inconsistent_scan",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
return runMvccScan(ctx, m, false, true, args)
},
operands: []operandType{
OPERAND_MVCC_KEY,
OPERAND_MVCC_KEY,
},
weight: 10,
},
{
name: "mvcc_reverse_scan",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
return runMvccScan(ctx, m, true, false, args)
},
operands: []operandType{
OPERAND_MVCC_KEY,
OPERAND_MVCC_KEY,
OPERAND_TRANSACTION,
},
weight: 10,
},
{
name: "txn_open",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
txn := m.managers[OPERAND_TRANSACTION].(*txnManager).open()
return m.managers[OPERAND_TRANSACTION].toString(txn)
},
operands: []operandType{},
weight: 4,
},
{
name: "txn_commit",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
txnManager := m.managers[OPERAND_TRANSACTION].(*txnManager)
txn := args[0].(*roachpb.Transaction)
txn.Status = roachpb.COMMITTED
txnManager.close(txn)
return "ok"
},
dependentOps: func(m *metaTestRunner, args ...operand) (result []opRun) {
txnManager := m.managers[OPERAND_TRANSACTION].(*txnManager)
txn := args[0].(*roachpb.Transaction)
closedBatches := make(map[engine.Batch]struct{})
// A transaction could have in-flight writes in some batches. Get a list
// of all those batches, and dispatch batch_commit operations for them.
for _, batch := range txnManager.inFlightBatches[txn] {
if _, ok := closedBatches[batch]; ok {
continue
}
closedBatches[batch] = struct{}{}
result = append(result, opRun{
op: m.nameToOp["batch_commit"],
args: []operand{batch},
})
}
return
},
operands: []operandType{
OPERAND_TRANSACTION,
},
weight: 10,
},
{
name: "batch_open",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
batch := m.managers[OPERAND_READWRITER].(*readWriterManager).open()
return m.managers[OPERAND_READWRITER].toString(batch)
},
operands: []operandType{},
weight: 4,
},
{
name: "batch_commit",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
if batch, ok := args[0].(engine.Batch); ok {
err := batch.Commit(false)
m.managers[OPERAND_READWRITER].close(batch)
m.managers[OPERAND_TRANSACTION].(*txnManager).clearBatch(batch)
if err != nil {
return err.Error()
}
return "ok"
}
return "noop"
},
dependentOps: func(m *metaTestRunner, args ...operand) (results []opRun) {
return closeItersOnBatch(m, args[0].(engine.Reader))
},
operands: []operandType{
OPERAND_READWRITER,
},
weight: 10,
},
{
name: "iterator_open",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
iterManager := m.managers[OPERAND_ITERATOR].(*iteratorManager)
key := args[1].(engine.MVCCKey)
endKey := args[2].(engine.MVCCKey)
if endKey.Less(key) {
tmpKey := key
key = endKey
endKey = tmpKey
}
iter := iterManager.open(args[0].(engine.ReadWriter), engine.IterOptions{
Prefix: false,
LowerBound: key.Key,
UpperBound: endKey.Key.Next(),
})
if _, ok := args[0].(engine.Batch); ok {
// When Next()-ing on a newly initialized batch iter without a key,
// pebble's iterator stays invalid while RocksDB's finds the key after
// the first key. This is a known difference. For now seek the iterator
// to standardize behaviour for this test.
iter.SeekGE(key)
}
return iterManager.toString(iter)
},
dependentOps: func(m *metaTestRunner, args ...operand) (results []opRun) {
return closeItersOnBatch(m, args[0].(engine.Reader))
},
operands: []operandType{
OPERAND_READWRITER,
OPERAND_MVCC_KEY,
OPERAND_MVCC_KEY,
},
weight: 2,
},
{
name: "iterator_close",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
iterManager := m.managers[OPERAND_ITERATOR].(*iteratorManager)
iterManager.close(args[0].(engine.Iterator))
return "ok"
},
operands: []operandType{
OPERAND_ITERATOR,
},
weight: 5,
},
{
name: "iterator_seekge",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
iter := args[0].(engine.Iterator)
key := args[1].(engine.MVCCKey)
if isBatchIterator(m, iter) {
// RocksDB batch iterators do not account for lower bounds consistently:
// https://github.com/cockroachdb/cockroach/issues/44512
// In the meantime, ensure the SeekGE key >= lower bound.
iterManager := m.managers[OPERAND_ITERATOR].(*iteratorManager)
lowerBound := iterManager.iterToInfo[iter].lowerBound
if key.Key.Compare(lowerBound) < 0 {
key.Key = lowerBound
}
}
iter.SeekGE(key)
return printIterState(iter)
},
operands: []operandType{
OPERAND_ITERATOR,
OPERAND_MVCC_KEY,
},
weight: 5,
},
{
name: "iterator_seeklt",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
iter := args[0].(engine.Iterator)
key := args[1].(engine.MVCCKey)
// Check if this is an engine or batch iterator. If batch iterator, do
// nothing.
if isBatchIterator(m, iter) {
return "noop due to missing seekLT support in rocksdb batch iterators"
}
iter.SeekLT(key)
return printIterState(iter)
},
operands: []operandType{
OPERAND_ITERATOR,
OPERAND_MVCC_KEY,
},
weight: 5,
},
{
name: "iterator_next",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
iter := args[0].(engine.Iterator)
// The rocksdb iterator does not treat kindly to a Next() if it is already
// invalid. Don't run next if that is the case.
if ok, err := iter.Valid(); !ok || err != nil {
if err != nil {
return fmt.Sprintf("valid = %v, err = %s", ok, err.Error())
} else {
return "valid = false"
}
}
iter.Next()
return printIterState(iter)
},
operands: []operandType{
OPERAND_ITERATOR,
},
weight: 10,
},
{
name: "iterator_nextkey",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
iter := args[0].(engine.Iterator)
// The rocksdb iterator does not treat kindly to a NextKey() if it is
// already invalid. Don't run NextKey if that is the case.
if ok, err := iter.Valid(); !ok || err != nil {
if err != nil {
return fmt.Sprintf("valid = %v, err = %s", ok, err.Error())
} else {
return "valid = false"
}
}
iter.NextKey()
return printIterState(iter)
},
operands: []operandType{
OPERAND_ITERATOR,
},
weight: 10,
},
{
name: "iterator_prev",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
iter := args[0].(engine.Iterator)
if isBatchIterator(m, iter) {
return "noop due to missing Prev support in rocksdb batch iterators"
}
// The rocksdb iterator does not treat kindly to a Prev() if it is already
// invalid. Don't run prev if that is the case.
if ok, err := iter.Valid(); !ok || err != nil {
if err != nil {
return fmt.Sprintf("valid = %v, err = %s", ok, err.Error())
} else {
return "valid = false"
}
}
iter.Prev()
return printIterState(iter)
},
operands: []operandType{
OPERAND_ITERATOR,
},
weight: 10,
},
{
// Note that this is not an MVCC* operation; unlike MVCC{Put,Get,Scan}, etc,
// it does not respect transactions. This often yields interesting
// behaviour.
name: "delete_range",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
key := args[0].(engine.MVCCKey)
endKey := args[1].(engine.MVCCKey)
if endKey.Less(key) {
tmpKey := key
key = endKey
endKey = tmpKey
}
err := m.engine.ClearRange(key, endKey)
if err != nil {
return fmt.Sprintf("error: %s", err.Error())
}
return "ok"
},
operands: []operandType{
OPERAND_MVCC_KEY,
OPERAND_MVCC_KEY,
},
weight: 2,
},
{
name: "compact",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
key := args[0].(engine.MVCCKey).Key
endKey := args[1].(engine.MVCCKey).Key
if endKey.Compare(key) < 0 {
tmpKey := key
key = endKey
endKey = tmpKey
}
err := m.engine.CompactRange(key, endKey, false)
if err != nil {
return fmt.Sprintf("error: %s", err.Error())
}
return "ok"
},
operands: []operandType{
OPERAND_MVCC_KEY,
OPERAND_MVCC_KEY,
},
weight: 5,
},
}