-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathoperations.go
785 lines (745 loc) · 23 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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
// 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"
"os"
"path/filepath"
"sort"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
)
// 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[operandIterator].(*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[operandIterator].(*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) {
key, endKey = endKey, key
}
var ts hlc.Timestamp
var txn *roachpb.Transaction
if inconsistent {
ts = args[2].(hlc.Timestamp)
} else {
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.
result, 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, intents = %v", result.KVs, result.Intents)
}
// 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())
}
return "valid = false"
}
return fmt.Sprintf("key = %s", iter.UnsafeKey().String())
}
// List of operations, where each operation is defined as one instance of mvccOp.
//
// TODO(itsbilal): Add more missing MVCC operations, such as:
// - MVCCBlindPut
// - MVCCMerge
// - MVCCIncrement
// - MVCCResolveWriteIntent in the aborted case
// - and any others that would be important to test.
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)
ts := args[2].(hlc.Timestamp)
// TODO: Specify these bools as operands instead of having a separate
// operation for inconsistent cases. This increases visibility for anyone
// reading the output file.
val, intent, err := engine.MVCCGet(ctx, reader, key.Key, ts, 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{
operandReadWriter,
operandMVCCKey,
operandPastTS,
},
weight: 100,
},
{
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{
operandReadWriter,
operandMVCCKey,
operandTransaction,
},
weight: 100,
},
{
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,
})
// Track this write in the txn manager. This ensures the batch will be
// committed before the transaction is committed
m.managers[operandTransaction].(*txnManager).trackWriteOnBatch(writer, txn)
return "ok"
},
operands: []operandType{
operandReadWriter,
operandMVCCKey,
operandValue,
operandTransaction,
},
weight: 500,
},
{
name: "mvcc_conditional_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))
expVal := roachpb.MakeValueFromBytes(args[3].([]byte))
txn := args[4].(*roachpb.Transaction)
txn.Sequence++
err := engine.MVCCConditionalPut(ctx, writer, nil, key.Key, txn.WriteTimestamp, value, &expVal, true, 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,
})
// Track this write in the txn manager. This ensures the batch will be
// committed before the transaction is committed
m.managers[operandTransaction].(*txnManager).trackWriteOnBatch(writer, txn)
return "ok"
},
operands: []operandType{
operandReadWriter,
operandMVCCKey,
operandValue,
operandValue,
operandTransaction,
},
weight: 50,
},
{
name: "mvcc_init_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.MVCCInitPut(ctx, writer, nil, key.Key, txn.WriteTimestamp, value, false, 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,
})
// Track this write in the txn manager. This ensures the batch will be
// committed before the transaction is committed
m.managers[operandTransaction].(*txnManager).trackWriteOnBatch(writer, txn)
return "ok"
},
operands: []operandType{
operandReadWriter,
operandMVCCKey,
operandValue,
operandTransaction,
},
weight: 50,
},
{
name: "mvcc_delete_range",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
writer := args[0].(engine.ReadWriter)
key := args[1].(engine.MVCCKey).Key
endKey := args[2].(engine.MVCCKey).Key
txn := args[3].(*roachpb.Transaction)
txn.Sequence++
if endKey.Compare(key) < 0 {
key, endKey = endKey, key
}
keys, _, _, err := engine.MVCCDeleteRange(ctx, writer, nil, key, endKey, 0, txn.WriteTimestamp, txn, true)
if err != nil {
return fmt.Sprintf("error: %s", err)
}
// Update the txn's intent spans to account for this intent being written.
for _, key := range keys {
txn.IntentSpans = append(txn.IntentSpans, roachpb.Span{
Key: key,
})
}
// Track this write in the txn manager. This ensures the batch will be
// committed before the transaction is committed
m.managers[operandTransaction].(*txnManager).trackWriteOnBatch(writer, txn)
return fmt.Sprintf("keys = %v", keys)
},
dependentOps: func(m *metaTestRunner, args ...operand) (results []opRun) {
return closeItersOnBatch(m, args[0].(engine.Reader))
},
operands: []operandType{
operandReadWriter,
operandMVCCKey,
operandMVCCKey,
operandTransaction,
},
weight: 20,
},
{
name: "mvcc_clear_time_range",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
writer := args[0].(engine.ReadWriter)
key := args[1].(engine.MVCCKey).Key
endKey := args[2].(engine.MVCCKey).Key
startTime := args[3].(hlc.Timestamp)
endTime := args[3].(hlc.Timestamp)
if endKey.Compare(key) < 0 {
key, endKey = endKey, key
}
if endTime.Less(startTime) {
startTime, endTime = endTime, startTime
}
span, err := engine.MVCCClearTimeRange(ctx, writer, nil, key, endKey, startTime, endTime, math.MaxInt64)
if err != nil {
return fmt.Sprintf("error: %s", err)
}
return fmt.Sprintf("ok, span = %v", span)
},
dependentOps: func(m *metaTestRunner, args ...operand) (results []opRun) {
return closeItersOnBatch(m, args[0].(engine.Reader))
},
operands: []operandType{
operandReadWriter,
operandMVCCKey,
operandMVCCKey,
operandPastTS,
operandPastTS,
},
weight: 20,
},
{
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,
})
// Track this write in the txn manager. This ensures the batch will be
// committed before the transaction is committed
m.managers[operandTransaction].(*txnManager).trackWriteOnBatch(writer, txn)
return "ok"
},
operands: []operandType{
operandReadWriter,
operandMVCCKey,
operandTransaction,
},
weight: 100,
},
{
name: "mvcc_find_split_key",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
key, _ := keys.Addr(args[0].(engine.MVCCKey).Key)
endKey, _ := keys.Addr(args[1].(engine.MVCCKey).Key)
splitSize := int64(1024)
splitKey, err := engine.MVCCFindSplitKey(ctx, m.engine, key, endKey, splitSize)
if err != nil {
return fmt.Sprintf("error: %s", err)
}
return fmt.Sprintf("ok, splitSize = %d, splitKey = %v", splitSize, splitKey)
},
operands: []operandType{
operandMVCCKey,
operandMVCCKey,
},
weight: 20,
},
{
name: "mvcc_scan",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
return runMvccScan(ctx, m, false, false, args)
},
operands: []operandType{
operandMVCCKey,
operandMVCCKey,
operandTransaction,
},
weight: 100,
},
{
name: "mvcc_inconsistent_scan",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
return runMvccScan(ctx, m, false, true, args)
},
operands: []operandType{
operandMVCCKey,
operandMVCCKey,
operandPastTS,
},
weight: 100,
},
{
name: "mvcc_reverse_scan",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
return runMvccScan(ctx, m, true, false, args)
},
operands: []operandType{
operandMVCCKey,
operandMVCCKey,
operandTransaction,
},
weight: 100,
},
{
name: "txn_open",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
txn := m.managers[operandTransaction].(*txnManager).open()
return m.managers[operandTransaction].toString(txn)
},
operands: []operandType{},
weight: 40,
},
{
name: "txn_commit",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
txnManager := m.managers[operandTransaction].(*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[operandTransaction].(*txnManager)
txn := args[0].(*roachpb.Transaction)
// 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.openBatches[txn] {
result = append(result, opRun{
op: m.nameToOp["batch_commit"],
args: []operand{batch},
})
}
return
},
operands: []operandType{
operandTransaction,
},
weight: 100,
},
{
name: "batch_open",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
batch := m.managers[operandReadWriter].(*readWriterManager).open()
return m.managers[operandReadWriter].toString(batch)
},
operands: []operandType{},
weight: 40,
},
{
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[operandReadWriter].close(batch)
m.managers[operandTransaction].(*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{
operandReadWriter,
},
weight: 100,
},
{
name: "iterator_open",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
iterManager := m.managers[operandIterator].(*iteratorManager)
key := args[1].(engine.MVCCKey)
endKey := args[2].(engine.MVCCKey)
if endKey.Less(key) {
key, endKey = endKey, key
}
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 behavior 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{
operandReadWriter,
operandMVCCKey,
operandMVCCKey,
},
weight: 20,
},
{
name: "iterator_close",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
iterManager := m.managers[operandIterator].(*iteratorManager)
iterManager.close(args[0].(engine.Iterator))
return "ok"
},
operands: []operandType{
operandIterator,
},
weight: 50,
},
{
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[operandIterator].(*iteratorManager)
lowerBound := iterManager.iterToInfo[iter].lowerBound
if key.Key.Compare(lowerBound) < 0 {
key.Key = lowerBound
}
}
iter.SeekGE(key)
return printIterState(iter)
},
operands: []operandType{
operandIterator,
operandMVCCKey,
},
weight: 50,
},
{
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{
operandIterator,
operandMVCCKey,
},
weight: 50,
},
{
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())
}
return "valid = false"
}
iter.Next()
return printIterState(iter)
},
operands: []operandType{
operandIterator,
},
weight: 100,
},
{
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())
}
return "valid = false"
}
iter.NextKey()
return printIterState(iter)
},
operands: []operandType{
operandIterator,
},
weight: 100,
},
{
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())
}
return "valid = false"
}
iter.Prev()
return printIterState(iter)
},
operands: []operandType{
operandIterator,
},
weight: 100,
},
{
// Note that this is not an MVCC* operation; unlike MVCC{Put,Get,Scan}, etc,
// it does not respect transactions. This often yields interesting
// behavior.
name: "delete_range",
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 {
key, endKey = endKey, key
} else if endKey.Equal(key) {
// Range tombstones where start = end can exhibit different behavior on
// different engines; rocks treats it as a point delete, while pebble
// treats it as a nonexistent tombstone. For the purposes of this test,
// standardize behavior.
endKey = endKey.Next()
}
// All ClearRange calls in Cockroach usually happen with metadata keys, so
// mimic the same behavior here.
err := m.engine.ClearRange(engine.MakeMVCCMetadataKey(key), engine.MakeMVCCMetadataKey(endKey))
if err != nil {
return fmt.Sprintf("error: %s", err.Error())
}
return "ok"
},
operands: []operandType{
operandMVCCKey,
operandMVCCKey,
},
weight: 20,
},
{
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 {
key, endKey = endKey, key
}
err := m.engine.CompactRange(key, endKey, false)
if err != nil {
return fmt.Sprintf("error: %s", err.Error())
}
return "ok"
},
operands: []operandType{
operandMVCCKey,
operandMVCCKey,
},
weight: 10,
},
{
name: "ingest",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
sstPath := filepath.Join(m.path, "ingest.sst")
f, err := os.Create(sstPath)
if err != nil {
return fmt.Sprintf("error = %s", err.Error())
}
defer f.Close()
var keys []engine.MVCCKey
for _, arg := range args {
keys = append(keys, arg.(engine.MVCCKey))
}
// SST Writer expects keys in sorted order, so sort them first.
sort.Slice(keys, func(i, j int) bool {
return keys[i].Less(keys[j])
})
sstWriter := engine.MakeIngestionSSTWriter(f)
for _, key := range keys {
_ = sstWriter.Put(key, []byte("ingested"))
}
if err := sstWriter.Finish(); err != nil {
return fmt.Sprintf("error = %s", err.Error())
}
sstWriter.Close()
if err := m.engine.IngestExternalFiles(ctx, []string{sstPath}); err != nil {
return fmt.Sprintf("error = %s", err.Error())
}
return "ok"
},
operands: []operandType{
operandMVCCKey,
operandMVCCKey,
operandMVCCKey,
operandMVCCKey,
operandMVCCKey,
},
weight: 10,
},
{
name: "restart",
run: func(ctx context.Context, m *metaTestRunner, args ...operand) string {
if !m.restarts {
m.printComment("no-op due to restarts being disabled")
return "ok"
}
oldEngineName, newEngineName := m.restart()
m.printComment(fmt.Sprintf("restarting: %s -> %s", oldEngineName, newEngineName))
return "ok"
},
operands: nil,
weight: 4,
},
}