-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathmvcc_incremental_iterator_test.go
837 lines (766 loc) · 27.1 KB
/
mvcc_incremental_iterator_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
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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
// Copyright 2019 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 engine
import (
"bytes"
"context"
"fmt"
"math"
"path/filepath"
"testing"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/engine/enginepb"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)
func iterateExpectErr(
e Engine,
iterFn func(*MVCCIncrementalIterator),
startKey, endKey roachpb.Key,
startTime, endTime hlc.Timestamp,
errString string,
) func(*testing.T) {
return func(t *testing.T) {
t.Helper()
iter := NewMVCCIncrementalIterator(e, MVCCIncrementalIterOptions{
IterOptions: IterOptions{
UpperBound: endKey,
},
StartTime: startTime,
EndTime: endTime,
})
defer iter.Close()
for iter.SeekGE(MakeMVCCMetadataKey(startKey)); ; iterFn(iter) {
if ok, _ := iter.Valid(); !ok || iter.UnsafeKey().Key.Compare(endKey) >= 0 {
break
}
// pass
}
if _, err := iter.Valid(); !testutils.IsError(err, errString) {
t.Fatalf("expected error %q but got %v", errString, err)
}
}
}
func assertEqualKVs(
e Engine,
iterFn func(*MVCCIncrementalIterator),
startKey, endKey roachpb.Key,
startTime, endTime hlc.Timestamp,
expected []MVCCKeyValue,
) func(*testing.T) {
return func(t *testing.T) {
t.Helper()
iter := NewMVCCIncrementalIterator(e, MVCCIncrementalIterOptions{
IterOptions: IterOptions{
UpperBound: endKey,
},
StartTime: startTime,
EndTime: endTime,
})
defer iter.Close()
var kvs []MVCCKeyValue
for iter.SeekGE(MakeMVCCMetadataKey(startKey)); ; iterFn(iter) {
if ok, err := iter.Valid(); err != nil {
t.Fatalf("unexpected error: %+v", err)
} else if !ok || iter.UnsafeKey().Key.Compare(endKey) >= 0 {
break
}
kvs = append(kvs, MVCCKeyValue{Key: iter.Key(), Value: iter.Value()})
}
if len(kvs) != len(expected) {
t.Fatalf("got %d kvs but expected %d: %v", len(kvs), len(expected), kvs)
}
for i := range kvs {
if !kvs[i].Key.Equal(expected[i].Key) {
t.Fatalf("%d key: got %v but expected %v", i, kvs[i].Key, expected[i].Key)
}
if !bytes.Equal(kvs[i].Value, expected[i].Value) {
t.Fatalf("%d value: got %x but expected %x", i, kvs[i].Value, expected[i].Value)
}
}
}
}
func TestMVCCIncrementalIterator(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx := context.Background()
var (
keyMin = roachpb.KeyMin
keyMax = roachpb.KeyMax
testKey1 = roachpb.Key("/db1")
testKey2 = roachpb.Key("/db2")
testValue1 = []byte("val1")
testValue2 = []byte("val2")
testValue3 = []byte("val3")
testValue4 = []byte("val4")
tsMin = hlc.Timestamp{WallTime: 0, Logical: 0}
ts1 = hlc.Timestamp{WallTime: 1, Logical: 0}
ts2 = hlc.Timestamp{WallTime: 2, Logical: 0}
ts3 = hlc.Timestamp{WallTime: 3, Logical: 0}
ts4 = hlc.Timestamp{WallTime: 4, Logical: 0}
tsMax = hlc.Timestamp{WallTime: math.MaxInt64, Logical: 0}
)
makeKVT := func(key roachpb.Key, value []byte, ts hlc.Timestamp) MVCCKeyValue {
return MVCCKeyValue{Key: MVCCKey{Key: key, Timestamp: ts}, Value: value}
}
kv1_1_1 := makeKVT(testKey1, testValue1, ts1)
kv1_4_4 := makeKVT(testKey1, testValue4, ts4)
kv1_2_2 := makeKVT(testKey1, testValue2, ts2)
kv2_2_2 := makeKVT(testKey2, testValue3, ts2)
kv1_3Deleted := makeKVT(testKey1, nil, ts3)
kvs := func(kvs ...MVCCKeyValue) []MVCCKeyValue { return kvs }
for _, engineImpl := range mvccEngineImpls {
t.Run(engineImpl.name, func(t *testing.T) {
e := engineImpl.create()
defer e.Close()
fn := (*MVCCIncrementalIterator).NextKey
t.Run("empty", assertEqualKVs(e, fn, keyMin, keyMax, tsMin, ts3, nil))
for _, kv := range kvs(kv1_1_1, kv1_2_2, kv2_2_2) {
v := roachpb.Value{RawBytes: kv.Value}
if err := MVCCPut(ctx, e, nil, kv.Key.Key, kv.Key.Timestamp, v, nil); err != nil {
t.Fatal(err)
}
}
// Exercise time ranges.
t.Run("ts (0-0]", assertEqualKVs(e, fn, keyMin, keyMax, tsMin, tsMin, nil))
t.Run("ts (0-1]", assertEqualKVs(e, fn, keyMin, keyMax, tsMin, ts1, kvs(kv1_1_1)))
t.Run("ts (0-∞]", assertEqualKVs(e, fn, keyMin, keyMax, tsMin, tsMax, kvs(kv1_2_2, kv2_2_2)))
t.Run("ts (1-1]", assertEqualKVs(e, fn, keyMin, keyMax, ts1, ts1, nil))
t.Run("ts (1-2]", assertEqualKVs(e, fn, keyMin, keyMax, ts1, ts2, kvs(kv1_2_2, kv2_2_2)))
t.Run("ts (2-2]", assertEqualKVs(e, fn, keyMin, keyMax, ts2, ts2, nil))
// Exercise key ranges.
t.Run("kv [1-1)", assertEqualKVs(e, fn, testKey1, testKey1, tsMin, tsMax, nil))
t.Run("kv [1-2)", assertEqualKVs(e, fn, testKey1, testKey2, tsMin, tsMax, kvs(kv1_2_2)))
// Exercise deletion.
if err := MVCCDelete(ctx, e, nil, testKey1, ts3, nil); err != nil {
t.Fatal(err)
}
t.Run("del", assertEqualKVs(e, fn, keyMin, keyMax, ts1, tsMax, kvs(kv1_3Deleted, kv2_2_2)))
// Exercise intent handling.
txn1ID := uuid.MakeV4()
txn1 := roachpb.Transaction{
TxnMeta: enginepb.TxnMeta{
Key: testKey1,
ID: txn1ID,
Epoch: 1,
WriteTimestamp: ts4,
},
ReadTimestamp: ts4,
}
txn1Val := roachpb.Value{RawBytes: testValue4}
if err := MVCCPut(ctx, e, nil, txn1.TxnMeta.Key, txn1.ReadTimestamp, txn1Val, &txn1); err != nil {
t.Fatal(err)
}
txn2ID := uuid.MakeV4()
txn2 := roachpb.Transaction{
TxnMeta: enginepb.TxnMeta{
Key: testKey2,
ID: txn2ID,
Epoch: 1,
WriteTimestamp: ts4,
},
ReadTimestamp: ts4,
}
txn2Val := roachpb.Value{RawBytes: testValue4}
if err := MVCCPut(ctx, e, nil, txn2.TxnMeta.Key, txn2.ReadTimestamp, txn2Val, &txn2); err != nil {
t.Fatal(err)
}
t.Run("intents",
iterateExpectErr(e, fn, testKey1, testKey1.PrefixEnd(), tsMin, tsMax, "conflicting intents"))
t.Run("intents",
iterateExpectErr(e, fn, testKey2, testKey2.PrefixEnd(), tsMin, tsMax, "conflicting intents"))
t.Run("intents",
iterateExpectErr(e, fn, keyMin, keyMax, tsMin, ts4, "conflicting intents"))
// Intents above the upper time bound or beneath the lower time bound must
// be ignored (#28358). Note that the lower time bound is exclusive while
// the upper time bound is inclusive.
t.Run("intents", assertEqualKVs(e, fn, keyMin, keyMax, tsMin, ts3, kvs(kv1_3Deleted, kv2_2_2)))
t.Run("intents", assertEqualKVs(e, fn, keyMin, keyMax, ts4, tsMax, kvs()))
t.Run("intents", assertEqualKVs(e, fn, keyMin, keyMax, ts4.Next(), tsMax, kvs()))
intent1 := roachpb.MakeLockUpdate(&txn1, roachpb.Span{Key: testKey1})
intent1.Status = roachpb.COMMITTED
if _, err := MVCCResolveWriteIntent(ctx, e, nil, intent1); err != nil {
t.Fatal(err)
}
intent2 := roachpb.MakeLockUpdate(&txn2, roachpb.Span{Key: testKey2})
intent2.Status = roachpb.ABORTED
if _, err := MVCCResolveWriteIntent(ctx, e, nil, intent2); err != nil {
t.Fatal(err)
}
t.Run("intents", assertEqualKVs(e, fn, keyMin, keyMax, tsMin, tsMax, kvs(kv1_4_4, kv2_2_2)))
})
}
for _, engineImpl := range mvccEngineImpls {
t.Run(engineImpl.name, func(t *testing.T) {
e := engineImpl.create()
defer e.Close()
fn := (*MVCCIncrementalIterator).Next
t.Run("empty", assertEqualKVs(e, fn, keyMin, keyMax, tsMin, ts3, nil))
for _, kv := range kvs(kv1_1_1, kv1_2_2, kv2_2_2) {
v := roachpb.Value{RawBytes: kv.Value}
if err := MVCCPut(ctx, e, nil, kv.Key.Key, kv.Key.Timestamp, v, nil); err != nil {
t.Fatal(err)
}
}
// Exercise time ranges.
t.Run("ts (0-0]", assertEqualKVs(e, fn, keyMin, keyMax, tsMin, tsMin, nil))
t.Run("ts (0-1]", assertEqualKVs(e, fn, keyMin, keyMax, tsMin, ts1, kvs(kv1_1_1)))
t.Run("ts (0-∞]", assertEqualKVs(e, fn, keyMin, keyMax, tsMin, tsMax, kvs(kv1_2_2, kv1_1_1, kv2_2_2)))
t.Run("ts (1-1]", assertEqualKVs(e, fn, keyMin, keyMax, ts1, ts1, nil))
t.Run("ts (1-2]", assertEqualKVs(e, fn, keyMin, keyMax, ts1, ts2, kvs(kv1_2_2, kv2_2_2)))
t.Run("ts (2-2]", assertEqualKVs(e, fn, keyMin, keyMax, ts2, ts2, nil))
// Exercise key ranges.
t.Run("kv [1-1)", assertEqualKVs(e, fn, testKey1, testKey1, tsMin, tsMax, nil))
t.Run("kv [1-2)", assertEqualKVs(e, fn, testKey1, testKey2, tsMin, tsMax, kvs(kv1_2_2, kv1_1_1)))
// Exercise deletion.
if err := MVCCDelete(ctx, e, nil, testKey1, ts3, nil); err != nil {
t.Fatal(err)
}
t.Run("del", assertEqualKVs(e, fn, keyMin, keyMax, ts1, tsMax, kvs(kv1_3Deleted, kv1_2_2, kv2_2_2)))
// Exercise intent handling.
txn1ID := uuid.MakeV4()
txn1 := roachpb.Transaction{
TxnMeta: enginepb.TxnMeta{
Key: testKey1,
ID: txn1ID,
Epoch: 1,
WriteTimestamp: ts4,
},
ReadTimestamp: ts4,
}
txn1Val := roachpb.Value{RawBytes: testValue4}
if err := MVCCPut(ctx, e, nil, txn1.TxnMeta.Key, txn1.ReadTimestamp, txn1Val, &txn1); err != nil {
t.Fatal(err)
}
txn2ID := uuid.MakeV4()
txn2 := roachpb.Transaction{
TxnMeta: enginepb.TxnMeta{
Key: testKey2,
ID: txn2ID,
Epoch: 1,
WriteTimestamp: ts4,
},
ReadTimestamp: ts4,
}
txn2Val := roachpb.Value{RawBytes: testValue4}
if err := MVCCPut(ctx, e, nil, txn2.TxnMeta.Key, txn2.ReadTimestamp, txn2Val, &txn2); err != nil {
t.Fatal(err)
}
t.Run("intents",
iterateExpectErr(e, fn, testKey1, testKey1.PrefixEnd(), tsMin, tsMax, "conflicting intents"))
t.Run("intents",
iterateExpectErr(e, fn, testKey2, testKey2.PrefixEnd(), tsMin, tsMax, "conflicting intents"))
t.Run("intents",
iterateExpectErr(e, fn, keyMin, keyMax, tsMin, ts4, "conflicting intents"))
// Intents above the upper time bound or beneath the lower time bound must
// be ignored (#28358). Note that the lower time bound is exclusive while
// the upper time bound is inclusive.
t.Run("intents", assertEqualKVs(e, fn, keyMin, keyMax, tsMin, ts3, kvs(kv1_3Deleted, kv1_2_2, kv1_1_1, kv2_2_2)))
t.Run("intents", assertEqualKVs(e, fn, keyMin, keyMax, ts4, tsMax, kvs()))
t.Run("intents", assertEqualKVs(e, fn, keyMin, keyMax, ts4.Next(), tsMax, kvs()))
intent1 := roachpb.MakeLockUpdate(&txn1, roachpb.Span{Key: testKey1})
intent1.Status = roachpb.COMMITTED
if _, err := MVCCResolveWriteIntent(ctx, e, nil, intent1); err != nil {
t.Fatal(err)
}
intent2 := roachpb.MakeLockUpdate(&txn2, roachpb.Span{Key: testKey2})
intent2.Status = roachpb.ABORTED
if _, err := MVCCResolveWriteIntent(ctx, e, nil, intent2); err != nil {
t.Fatal(err)
}
t.Run("intents", assertEqualKVs(e, fn, keyMin, keyMax, tsMin, tsMax, kvs(kv1_4_4, kv1_3Deleted, kv1_2_2, kv1_1_1, kv2_2_2)))
})
}
}
func slurpKVsInTimeRange(
reader Reader, prefix roachpb.Key, startTime, endTime hlc.Timestamp,
) ([]MVCCKeyValue, error) {
endKey := prefix.PrefixEnd()
iter := NewMVCCIncrementalIterator(reader, MVCCIncrementalIterOptions{
IterOptions: IterOptions{
UpperBound: endKey,
},
StartTime: startTime,
EndTime: endTime,
})
defer iter.Close()
var kvs []MVCCKeyValue
for iter.SeekGE(MakeMVCCMetadataKey(prefix)); ; iter.Next() {
if ok, err := iter.Valid(); err != nil {
return nil, err
} else if !ok || iter.UnsafeKey().Key.Compare(endKey) >= 0 {
break
}
kvs = append(kvs, MVCCKeyValue{Key: iter.Key(), Value: iter.Value()})
}
return kvs, nil
}
// TestMVCCIncrementalIteratorIntentRewrittenConcurrently verifies that the
// workaround in MVCCIncrementalIterator to double-check for deleted intents
// properly handles cases where an intent originally in a time-bound iterator's
// time range is rewritten at a timestamp outside of its time range.
func TestMVCCIncrementalIteratorIntentRewrittenConcurrently(t *testing.T) {
defer leaktest.AfterTest(t)()
for _, engineImpl := range mvccEngineImpls {
t.Run(engineImpl.name, func(t *testing.T) {
e := engineImpl.create()
defer e.Close()
// Create a DB containing a single intent.
ctx := context.Background()
kA := roachpb.Key("kA")
vA1 := roachpb.MakeValueFromString("vA1")
vA2 := roachpb.MakeValueFromString("vA2")
ts0 := hlc.Timestamp{WallTime: 0}
ts1 := hlc.Timestamp{WallTime: 1}
ts2 := hlc.Timestamp{WallTime: 2}
ts3 := hlc.Timestamp{WallTime: 3}
txn := &roachpb.Transaction{
TxnMeta: enginepb.TxnMeta{
Key: roachpb.Key("b"),
ID: uuid.MakeV4(),
Epoch: 1,
WriteTimestamp: ts1,
Sequence: 1,
},
ReadTimestamp: ts1,
}
if err := MVCCPut(ctx, e, nil, kA, ts1, vA1, txn); err != nil {
t.Fatal(err)
}
// Concurrently iterate over the intent using a time-bound iterator and move
// the intent out of the time-bound iterator's time range by writing to it
// again at a higher timestamp.
g, _ := errgroup.WithContext(ctx)
g.Go(func() error {
// Re-write the intent with a higher timestamp.
txn.WriteTimestamp = ts3
txn.Sequence = 2
return MVCCPut(ctx, e, nil, kA, ts1, vA2, txn)
})
g.Go(func() error {
// Iterate with a time range that includes the initial intent but does
// not include the new intent.
kvs, err := slurpKVsInTimeRange(e, kA, ts0, ts2)
// There are two permissible outcomes from the scan. If the iteration
// wins the race with the put that moves the intent then it should
// observe the intent and return a write intent error. If the iteration
// loses the race with the put that moves the intent then it should
// observe and return nothing because there will be no committed or
// provisional keys in its time range.
if err != nil {
if !testutils.IsError(err, `conflicting intents on "kA"`) {
return err
}
} else {
if len(kvs) != 0 {
return errors.Errorf(`unexpected kvs: %v`, kvs)
}
}
return nil
})
if err := g.Wait(); err != nil {
t.Fatal(err)
}
})
}
}
// TestMVCCIncrementalIteratorIntentDeletion checks a workaround in
// MVCCIncrementalIterator for a bug in time-bound iterators, where an intent
// has been deleted, but the time-bound iterator doesn't see the deletion.
func TestMVCCIncrementalIteratorIntentDeletion(t *testing.T) {
defer leaktest.AfterTest(t)()
txn := func(key roachpb.Key, ts hlc.Timestamp) *roachpb.Transaction {
return &roachpb.Transaction{
TxnMeta: enginepb.TxnMeta{
Key: key,
ID: uuid.MakeV4(),
Epoch: 1,
WriteTimestamp: ts,
},
ReadTimestamp: ts,
}
}
intent := func(txn *roachpb.Transaction) roachpb.LockUpdate {
intent := roachpb.MakeLockUpdate(txn, roachpb.Span{Key: txn.Key})
intent.Status = roachpb.COMMITTED
return intent
}
ctx := context.Background()
kA := roachpb.Key("kA")
vA1 := roachpb.MakeValueFromString("vA1")
vA2 := roachpb.MakeValueFromString("vA2")
vA3 := roachpb.MakeValueFromString("vA3")
kB := roachpb.Key("kB")
vB1 := roachpb.MakeValueFromString("vB1")
kC := roachpb.Key("kC")
vC1 := roachpb.MakeValueFromString("vC1")
ts0 := hlc.Timestamp{WallTime: 0}
ts1 := hlc.Timestamp{WallTime: 1}
ts2 := hlc.Timestamp{WallTime: 2}
ts3 := hlc.Timestamp{WallTime: 3}
txnA1 := txn(kA, ts1)
txnA3 := txn(kA, ts3)
txnB1 := txn(kB, ts1)
txnC1 := txn(kC, ts1)
db := NewInMem(ctx, DefaultStorageEngine, roachpb.Attributes{}, 10<<20)
defer db.Close()
// Set up two sstables very specifically:
//
// sst1 (time-bound metadata ts1->ts1)
// kA -> (intent)
// kA:1 -> vA1
// kB -> (intent)
// kB:1 -> vB1
// kC -> (intent)
// kC:1 -> vC1
//
// sst2 (time-bound metadata ts2->ts3) the intent deletions are for the
// intents at ts1, but there's no way know that when constructing the
// metadata (hence the time-bound iterator bug)
// kA -> (intent) [NB this overwrites the intent deletion]
// kA:3 -> vA3
// kA:2 -> vA2
// kB -> (intent deletion)
require.NoError(t, MVCCPut(ctx, db, nil, kA, txnA1.ReadTimestamp, vA1, txnA1))
require.NoError(t, MVCCPut(ctx, db, nil, kB, txnB1.ReadTimestamp, vB1, txnB1))
require.NoError(t, MVCCPut(ctx, db, nil, kC, txnC1.ReadTimestamp, vC1, txnC1))
require.NoError(t, db.Flush())
require.NoError(t, db.Compact())
_, err := MVCCResolveWriteIntent(ctx, db, nil, intent(txnA1))
require.NoError(t, err)
_, err = MVCCResolveWriteIntent(ctx, db, nil, intent(txnB1))
require.NoError(t, err)
require.NoError(t, MVCCPut(ctx, db, nil, kA, ts2, vA2, nil))
require.NoError(t, MVCCPut(ctx, db, nil, kA, txnA3.WriteTimestamp, vA3, txnA3))
require.NoError(t, db.Flush())
if rocks, ok := db.(*RocksDB); ok {
// Double-check that we've created the SSTs we intended to.
userProps, err := rocks.GetUserProperties()
require.NoError(t, err)
require.Len(t, userProps.Sst, 2)
require.Equal(t, userProps.Sst[0].TsMin, &ts1)
require.Equal(t, userProps.Sst[0].TsMax, &ts1)
require.Equal(t, userProps.Sst[1].TsMin, &ts2)
require.Equal(t, userProps.Sst[1].TsMax, &ts3)
}
// The kA ts1 intent has been resolved. There's now a new intent on kA, but
// the timestamp (ts3) is too new so it should be ignored.
kvs, err := slurpKVsInTimeRange(db, kA, ts0, ts1)
require.NoError(t, err)
require.Equal(t, []MVCCKeyValue{
{Key: MVCCKey{Key: kA, Timestamp: ts1}, Value: vA1.RawBytes},
}, kvs)
// kA has a value at ts2. Again the intent is too new (ts3), so ignore.
kvs, err = slurpKVsInTimeRange(db, kA, ts0, ts2)
require.NoError(t, err)
require.Equal(t, []MVCCKeyValue{
{Key: MVCCKey{Key: kA, Timestamp: ts2}, Value: vA2.RawBytes},
{Key: MVCCKey{Key: kA, Timestamp: ts1}, Value: vA1.RawBytes},
}, kvs)
// At ts3, we should see the new intent
_, err = slurpKVsInTimeRange(db, kA, ts0, ts3)
require.EqualError(t, err, `conflicting intents on "kA"`)
// Similar to the kA ts1 check, but there is no newer intent. We expect to
// pick up the intent deletion and it should cancel out the intent, leaving
// only the value at ts1.
kvs, err = slurpKVsInTimeRange(db, kB, ts0, ts1)
require.NoError(t, err)
require.Equal(t, []MVCCKeyValue{
{Key: MVCCKey{Key: kB, Timestamp: ts1}, Value: vB1.RawBytes},
}, kvs)
// Sanity check that we see the still unresolved intent for kC ts1.
_, err = slurpKVsInTimeRange(db, kC, ts0, ts1)
require.EqualError(t, err, `conflicting intents on "kC"`)
}
func TestMVCCIncrementalIteratorIntentStraddlesSStables(t *testing.T) {
defer leaktest.AfterTest(t)()
// Create a DB containing 2 keys, a and b, where b has an intent. We use the
// regular MVCCPut operation to generate these keys, which we'll later be
// copying into manually created sstables.
ctx := context.Background()
db1 := NewInMem(ctx, DefaultStorageEngine, roachpb.Attributes{}, 10<<20 /* 10 MB */)
defer db1.Close()
put := func(key, value string, ts int64, txn *roachpb.Transaction) {
v := roachpb.MakeValueFromString(value)
if err := MVCCPut(
ctx, db1, nil, roachpb.Key(key), hlc.Timestamp{WallTime: ts}, v, txn,
); err != nil {
t.Fatal(err)
}
}
put("a", "a value", 1, nil)
put("b", "b value", 2, &roachpb.Transaction{
TxnMeta: enginepb.TxnMeta{
Key: roachpb.Key("b"),
ID: uuid.MakeV4(),
Epoch: 1,
WriteTimestamp: hlc.Timestamp{WallTime: 2},
},
ReadTimestamp: hlc.Timestamp{WallTime: 2},
})
// Create a second DB in which we'll create a specific SSTable structure: the
// first SSTable contains 2 KVs where the first is a regular versioned key
// and the second is the MVCC metadata entry (i.e. an intent). The next
// SSTable contains the provisional value for the intent. The effect is that
// the metadata entry is separated from the entry it is metadata for.
//
// SSTable 1:
// a@1
// b@<meta>
//
// SSTable 2:
// b@2
db2 := NewInMem(ctx, DefaultStorageEngine, roachpb.Attributes{}, 10<<20 /* 10 MB */)
defer db2.Close()
ingest := func(it Iterator, count int) {
sst, err := MakeRocksDBSstFileWriter()
if err != nil {
t.Fatal(err)
}
defer sst.Close()
for i := 0; i < count; i++ {
ok, err := it.Valid()
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("expected key")
}
if err := sst.Put(it.Key(), it.Value()); err != nil {
t.Fatal(err)
}
it.Next()
}
sstContents, err := sst.Finish()
if err != nil {
t.Fatal(err)
}
if err := db2.WriteFile(`ingest`, sstContents); err != nil {
t.Fatal(err)
}
if err := db2.IngestExternalFiles(ctx, []string{`ingest`}); err != nil {
t.Fatal(err)
}
}
{
// Iterate over the entries in the first DB, ingesting them into SSTables
// in the second DB.
it := db1.NewIterator(IterOptions{
UpperBound: keys.MaxKey,
})
defer it.Close()
it.SeekGE(MVCCKey{Key: keys.MinKey})
ingest(it, 2)
ingest(it, 1)
}
{
// Use an incremental iterator to simulate an incremental backup from (1,
// 2]. Note that incremental iterators are exclusive on the start time and
// inclusive on the end time. The expectation is that we'll see a write
// intent error.
it := NewMVCCIncrementalIterator(db2, MVCCIncrementalIterOptions{
IterOptions: IterOptions{UpperBound: keys.MaxKey},
StartTime: hlc.Timestamp{WallTime: 1},
EndTime: hlc.Timestamp{WallTime: 2},
})
defer it.Close()
for it.SeekGE(MVCCKey{Key: keys.MinKey}); ; it.Next() {
ok, err := it.Valid()
if err != nil {
if _, ok = err.(*roachpb.WriteIntentError); ok {
// This is the write intent error we were expecting.
return
}
t.Fatalf("%T: %s", err, err)
}
if !ok {
break
}
}
t.Fatalf("expected write intent error, but found success")
}
}
func TestMVCCIterateTimeBound(t *testing.T) {
defer leaktest.AfterTest(t)()
dir, cleanupFn := testutils.TempDir(t)
defer cleanupFn()
const numKeys = 1000
const numBatches = 10
const batchTimeSpan = 10
const valueSize = 8
eng, err := loadTestData(filepath.Join(dir, "mvcc_data"),
numKeys, numBatches, batchTimeSpan, valueSize)
if err != nil {
t.Fatal(err)
}
defer eng.Close()
for _, testCase := range []struct {
start hlc.Timestamp
end hlc.Timestamp
}{
// entire time range
{hlc.Timestamp{WallTime: 0, Logical: 0}, hlc.Timestamp{WallTime: 110, Logical: 0}},
// one SST
{hlc.Timestamp{WallTime: 10, Logical: 0}, hlc.Timestamp{WallTime: 19, Logical: 0}},
// one SST, plus the min of the following SST
{hlc.Timestamp{WallTime: 10, Logical: 0}, hlc.Timestamp{WallTime: 20, Logical: 0}},
// one SST, plus the max of the preceding SST
{hlc.Timestamp{WallTime: 9, Logical: 0}, hlc.Timestamp{WallTime: 19, Logical: 0}},
// one SST, plus the min of the following and the max of the preceding SST
{hlc.Timestamp{WallTime: 9, Logical: 0}, hlc.Timestamp{WallTime: 21, Logical: 0}},
// one SST, not min or max
{hlc.Timestamp{WallTime: 17, Logical: 0}, hlc.Timestamp{WallTime: 18, Logical: 0}},
// one SST's max
{hlc.Timestamp{WallTime: 18, Logical: 0}, hlc.Timestamp{WallTime: 19, Logical: 0}},
// one SST's min
{hlc.Timestamp{WallTime: 19, Logical: 0}, hlc.Timestamp{WallTime: 20, Logical: 0}},
// random endpoints
{hlc.Timestamp{WallTime: 32, Logical: 0}, hlc.Timestamp{WallTime: 78, Logical: 0}},
} {
t.Run(fmt.Sprintf("%s-%s", testCase.start, testCase.end), func(t *testing.T) {
defer leaktest.AfterTest(t)()
var expectedKVs []MVCCKeyValue
iter := eng.NewIterator(IterOptions{UpperBound: roachpb.KeyMax})
defer iter.Close()
iter.SeekGE(MVCCKey{})
for {
ok, err := iter.Valid()
if err != nil {
t.Fatal(err)
} else if !ok {
break
}
ts := iter.Key().Timestamp
if (ts.Less(testCase.end) || testCase.end == ts) && testCase.start.Less(ts) {
expectedKVs = append(expectedKVs, MVCCKeyValue{Key: iter.Key(), Value: iter.Value()})
}
iter.Next()
}
if len(expectedKVs) < 1 {
t.Fatalf("source of truth had no expected KVs; likely a bug in the test itself")
}
fn := (*MVCCIncrementalIterator).NextKey
assertEqualKVs(eng, fn, keys.MinKey, keys.MaxKey, testCase.start, testCase.end, expectedKVs)(t)
})
}
}
func TestMVCCIncrementalIteratorPhantomKeys(t *testing.T) {
defer leaktest.AfterTest(t)()
// Create a DB containing 2 keys, a and b, where b has an intent. We use the
// regular MVCCPut operation to generate these keys, which we'll later be
// copying into manually created sstables.
ctx := context.Background()
db1 := NewInMem(ctx, DefaultStorageEngine, roachpb.Attributes{}, 10<<20 /* 10 MB */)
defer db1.Close()
put := func(key, value string, ts int64, txn *roachpb.Transaction) {
v := roachpb.MakeValueFromString(value)
if err := MVCCPut(
ctx, db1, nil, roachpb.Key(key), hlc.Timestamp{WallTime: ts}, v, txn,
); err != nil {
t.Fatal(err)
}
}
put("a", "a value", 1, nil)
put("b", "b value", 2, &roachpb.Transaction{
TxnMeta: enginepb.TxnMeta{
Key: roachpb.Key("b"),
ID: uuid.MakeV4(),
Epoch: 1,
WriteTimestamp: hlc.Timestamp{WallTime: 2},
},
ReadTimestamp: hlc.Timestamp{WallTime: 2},
})
// Create a second DB in which we'll create a specific SSTable structure: the
// first SSTable contains 2 KVs where the first is a regular versioned key
// and the second is the MVCC metadata entry (i.e. an intent). The next
// SSTable contains the provisional value for the intent. The effect is that
// the metadata entry is separated from the entry it is metadata for.
//
// SSTable 1:
// a@1
// b@<meta>
//
// SSTable 2:
// b@2
db2 := NewInMem(ctx, DefaultStorageEngine, roachpb.Attributes{}, 10<<20 /* 10 MB */)
defer db2.Close()
ingest := func(it Iterator, count int) {
sst, err := MakeRocksDBSstFileWriter()
if err != nil {
t.Fatal(err)
}
defer sst.Close()
for i := 0; i < count; i++ {
ok, err := it.Valid()
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("expected key")
}
if err := sst.Put(it.Key(), it.Value()); err != nil {
t.Fatal(err)
}
it.Next()
}
sstContents, err := sst.Finish()
if err != nil {
t.Fatal(err)
}
if err := db2.WriteFile(`ingest`, sstContents); err != nil {
t.Fatal(err)
}
if err := db2.IngestExternalFiles(ctx, []string{`ingest`}); err != nil {
t.Fatal(err)
}
}
{
// Iterate over the entries in the first DB, ingesting them into SSTables
// in the second DB.
it := db1.NewIterator(IterOptions{
UpperBound: keys.MaxKey,
})
defer it.Close()
it.SeekGE(MVCCKey{Key: keys.MinKey})
ingest(it, 2)
ingest(it, 1)
}
{
// Use an incremental iterator to simulate an incremental backup from (1,
// 2]. Note that incremental iterators are exclusive on the start time and
// inclusive on the end time. The expectation is that we'll see a write
// intent error.
it := NewMVCCIncrementalIterator(db2, MVCCIncrementalIterOptions{
IterOptions: IterOptions{UpperBound: keys.MaxKey},
StartTime: hlc.Timestamp{WallTime: 1},
EndTime: hlc.Timestamp{WallTime: 2},
})
defer it.Close()
for it.SeekGE(MVCCKey{Key: keys.MinKey}); ; it.Next() {
ok, err := it.Valid()
if err != nil {
if _, ok = err.(*roachpb.WriteIntentError); ok {
// This is the write intent error we were expecting.
return
}
t.Fatalf("%T: %s", err, err)
}
if !ok {
break
}
}
t.Fatalf("expected write intent error, but found success")
}
}