-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathtee.go
1332 lines (1170 loc) · 38.3 KB
/
tee.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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 storage
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/storage/fs"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
)
// TeeEngine sends all write operations to both the underlying
// engine instances, and for all read operations, it verifies that
// any keys and values returned match. Implements the Engine interface.
//
// This engine is only meant to be used in testing. No performance or
// stability guarantees are made about this engine in production.
type TeeEngine struct {
ctx context.Context
eng1 Engine
eng2 Engine
}
var _ Engine = &TeeEngine{}
// NewTee creates a new instance of the TeeEngine engine. Conventionally,
// the first engine is the engine that's more likely to be correct, and the
// second engine is the one that's being tested and compared with the first.
// All error messages have results from the first engine ordered before those
// from the second. For example, in:
//
// "one of engine iters invalid but not both: false != true"
//
// The iterator from engine 1 is returning false while the iterator from
// engine2 is returning true.
func NewTee(ctx context.Context, eng1 Engine, eng2 Engine) *TeeEngine {
return &TeeEngine{
ctx: ctx,
eng1: eng1,
eng2: eng2,
}
}
func fatalOnErrorMismatch(ctx context.Context, err error, err2 error) error {
if err != nil && err2 != nil {
return err
} else if err != nil || err2 != nil {
log.Fatalf(ctx, "error mismatch between engines: %v != %v", err, err2)
}
return nil
}
// Close implements the Engine interface.
func (t *TeeEngine) Close() {
t.eng1.Close()
t.eng2.Close()
}
// Closed implements the Engine interface.
func (t *TeeEngine) Closed() bool {
eng1Closed := t.eng1.Closed()
eng2Closed := t.eng2.Closed()
if eng1Closed && eng2Closed {
return true
} else if eng1Closed || eng2Closed {
log.Fatalf(t.ctx, "only one of engines closed: %v != %v", eng1Closed, eng2Closed)
}
return false
}
// ExportToSst implements the Engine interface.
func (t *TeeEngine) ExportToSst(
startKey, endKey roachpb.Key,
startTS, endTS hlc.Timestamp,
exportAllRevisions bool,
targetSize, maxSize uint64,
io IterOptions,
) ([]byte, roachpb.BulkOpSummary, roachpb.Key, error) {
eng1Sst, bulkOpSummary, resume1, err := t.eng1.ExportToSst(startKey, endKey, startTS, endTS, exportAllRevisions, targetSize, maxSize, io)
rocksSst, _, resume2, err2 := t.eng2.ExportToSst(startKey, endKey, startTS, endTS, exportAllRevisions, targetSize, maxSize, io)
if err = fatalOnErrorMismatch(t.ctx, err, err2); err != nil {
return nil, bulkOpSummary, nil, err
}
if !bytes.Equal(eng1Sst, rocksSst) {
log.Fatalf(t.ctx, "mismatching SSTs returned by engines: %v != %v", eng1Sst, rocksSst)
}
if !resume1.Equal(resume2) {
log.Fatalf(t.ctx, "mismatching resume key returned by engines: %v != %v", resume1, resume2)
}
return eng1Sst, bulkOpSummary, resume1, err
}
// Get implements the Engine interface.
func (t *TeeEngine) Get(key MVCCKey) ([]byte, error) {
value, err := t.eng1.Get(key)
value2, err2 := t.eng2.Get(key)
if err = fatalOnErrorMismatch(t.ctx, err, err2); err != nil {
return nil, err
}
if !bytes.Equal(value, value2) {
log.Fatalf(t.ctx, "values mismatch between engines: %v != %v", value, value2)
}
return value, nil
}
// GetProto implements the Engine interface.
func (t *TeeEngine) GetProto(
key MVCCKey, msg protoutil.Message,
) (ok bool, keyBytes, valBytes int64, err error) {
if len(key.Key) == 0 {
return false, 0, 0, emptyKeyError()
}
// Piggyback on the value / error checks in Get.
val, err := t.Get(key)
if err != nil || val == nil {
return false, 0, 0, err
}
if msg != nil {
err = protoutil.Unmarshal(val, msg)
}
keyBytes = int64(key.Len())
valBytes = int64(len(val))
return true, keyBytes, valBytes, err
}
// Iterate implements the Engine interface.
func (t *TeeEngine) Iterate(
start, end roachpb.Key, f func(MVCCKeyValue) (stop bool, err error),
) error {
return iterateOnReader(t, start, end, f)
}
// NewIterator implements the Engine interface.
func (t *TeeEngine) NewIterator(opts IterOptions) Iterator {
return &TeeEngineIter{
ctx: t.ctx,
iter1: t.eng1.NewIterator(opts),
iter2: t.eng2.NewIterator(opts),
}
}
// ApplyBatchRepr implements the Engine interface.
func (t *TeeEngine) ApplyBatchRepr(repr []byte, sync bool) error {
err := t.eng1.ApplyBatchRepr(repr, sync)
err2 := t.eng2.ApplyBatchRepr(repr, sync)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// Clear implements the Engine interface.
func (t *TeeEngine) Clear(key MVCCKey) error {
err := t.eng1.Clear(key)
err2 := t.eng2.Clear(key)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// SingleClear implements the Engine interface.
func (t *TeeEngine) SingleClear(key MVCCKey) error {
err := t.eng1.SingleClear(key)
err2 := t.eng2.SingleClear(key)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// ClearRange implements the Engine interface.
func (t *TeeEngine) ClearRange(start, end MVCCKey) error {
err := t.eng1.ClearRange(start, end)
err2 := t.eng2.ClearRange(start, end)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// ClearIterRange implements the Engine interface.
func (t *TeeEngine) ClearIterRange(iter Iterator, start, end roachpb.Key) error {
err := t.eng1.ClearIterRange(iter.(*TeeEngineIter).iter1, start, end)
err2 := t.eng2.ClearIterRange(iter.(*TeeEngineIter).iter2, start, end)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// Merge implements the Engine interface.
func (t *TeeEngine) Merge(key MVCCKey, value []byte) error {
err := t.eng1.Merge(key, value)
err2 := t.eng2.Merge(key, value)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// Put implements the Engine interface.
func (t *TeeEngine) Put(key MVCCKey, value []byte) error {
err := t.eng1.Put(key, value)
err2 := t.eng2.Put(key, value)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// LogData implements the Engine interface.
func (t *TeeEngine) LogData(data []byte) error {
err := t.eng1.LogData(data)
err2 := t.eng2.LogData(data)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// LogLogicalOp implements the Engine interface.
func (t *TeeEngine) LogLogicalOp(op MVCCLogicalOpType, details MVCCLogicalOpDetails) {
t.eng1.LogLogicalOp(op, details)
t.eng2.LogLogicalOp(op, details)
}
// Attrs implements the Engine interface.
func (t *TeeEngine) Attrs() roachpb.Attributes {
return t.eng1.Attrs()
}
// Capacity implements the Engine interface.
func (t *TeeEngine) Capacity() (roachpb.StoreCapacity, error) {
return t.eng1.Capacity()
}
// Compact implements the Engine interface.
func (t *TeeEngine) Compact() error {
err := t.eng1.Compact()
err2 := t.eng2.Compact()
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// Flush implements the Engine interface.
func (t *TeeEngine) Flush() error {
err := t.eng1.Flush()
err2 := t.eng2.Flush()
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// GetSSTables implements the Engine interface.
func (t *TeeEngine) GetSSTables() SSTableInfos {
return t.eng1.GetSSTables()
}
// GetCompactionStats implements the Engine interface.
func (t *TeeEngine) GetCompactionStats() string {
return t.eng1.GetCompactionStats()
}
// GetStats implements the Engine interface.
func (t *TeeEngine) GetStats() (*Stats, error) {
return t.eng1.GetStats()
}
// GetEncryptionRegistries implements the Engine interface.
func (t *TeeEngine) GetEncryptionRegistries() (*EncryptionRegistries, error) {
return t.eng1.GetEncryptionRegistries()
}
// GetEnvStats implements the Engine interface.
func (t *TeeEngine) GetEnvStats() (*EnvStats, error) {
return t.eng2.GetEnvStats()
}
// GetAuxiliaryDir implements the Engine interface.
func (t *TeeEngine) GetAuxiliaryDir() string {
// Treat the eng1 path as the main aux dir, so that checkpoints are made in
// subdirectories within it.
return t.eng1.GetAuxiliaryDir()
}
// NewBatch implements the Engine interface.
func (t *TeeEngine) NewBatch() Batch {
batch1 := t.eng1.NewBatch()
batch2 := t.eng2.NewBatch()
return &TeeEngineBatch{
ctx: t.ctx,
batch1: batch1,
batch2: batch2,
}
}
// NewReadOnly implements the Engine interface.
func (t TeeEngine) NewReadOnly() ReadWriter {
reader1 := t.eng1.NewReadOnly()
reader2 := t.eng2.NewReadOnly()
return &TeeEngineReadWriter{TeeEngineReader{
ctx: t.ctx,
reader1: reader1,
reader2: reader2,
}}
}
// NewWriteOnlyBatch implements the Engine interface.
func (t *TeeEngine) NewWriteOnlyBatch() Batch {
batch1 := t.eng1.NewWriteOnlyBatch()
batch2 := t.eng2.NewWriteOnlyBatch()
return &TeeEngineBatch{
ctx: t.ctx,
batch1: batch1,
batch2: batch2,
}
}
// NewSnapshot implements the Engine interface.
func (t *TeeEngine) NewSnapshot() Reader {
snap1 := t.eng1.NewSnapshot()
snap2 := t.eng2.NewSnapshot()
return &TeeEngineReader{
ctx: t.ctx,
reader1: snap1,
reader2: snap2,
}
}
// Type implements the Engine interface.
func (t *TeeEngine) Type() enginepb.EngineType {
return enginepb.EngineTypeTeePebbleRocksDB
}
// Helper to remap a path in the first engine's aux dir, into the same path in
// the second engine's aux dir. Returns ok = true only if the provided path
// is in the first engine's aux dir.
func (t *TeeEngine) remapPath(path1 string) (path2 string, ok bool) {
auxDir1 := t.eng1.GetAuxiliaryDir()
if !strings.HasPrefix(path1, auxDir1) {
// This dir isn't in the first engine's aux dir.
return path1, false
}
ok = true
path2 = filepath.Join(t.eng2.GetAuxiliaryDir(), strings.TrimPrefix(path1, auxDir1))
return
}
// IngestExternalFiles implements the Engine interface.
func (t *TeeEngine) IngestExternalFiles(ctx context.Context, paths []string) error {
var err, err2 error
// The paths should be in eng1's aux directory. Map them to eng2's aux
// directory.
paths2 := make([]string, len(paths))
for i, path := range paths {
var ok bool
paths2[i], ok = t.remapPath(path)
if !ok {
paths2[i] = filepath.Join(t.eng2.GetAuxiliaryDir(), "temp-ingest", filepath.Base(path))
data, err := t.eng1.ReadFile(path)
if err != nil {
return err
}
f, err := t.eng2.Create(paths2[i])
if err != nil {
return err
}
_, _ = f.Write(data)
_ = f.Sync()
_ = f.Close()
}
}
err = t.eng1.IngestExternalFiles(ctx, paths)
err2 = t.eng2.IngestExternalFiles(ctx, paths2)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// PreIngestDelay implements the Engine interface.
func (t *TeeEngine) PreIngestDelay(ctx context.Context) {
t.eng1.PreIngestDelay(ctx)
}
// ApproximateDiskBytes implements the Engine interface.
func (t *TeeEngine) ApproximateDiskBytes(from, to roachpb.Key) (uint64, error) {
bytes, err := t.eng1.ApproximateDiskBytes(from, to)
bytes2, err2 := t.eng2.ApproximateDiskBytes(from, to)
if err = fatalOnErrorMismatch(t.ctx, err, err2); err != nil {
return 0, err
}
return bytes + bytes2, nil
}
// CompactRange implements the Engine interface.
func (t *TeeEngine) CompactRange(start, end roachpb.Key, forceBottommost bool) error {
err := t.eng1.CompactRange(start, end, forceBottommost)
err2 := t.eng2.CompactRange(start, end, forceBottommost)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// InMem implements the Engine interface.
func (t *TeeEngine) InMem() bool {
return t.eng1.InMem()
}
// Create implements the FS interface.
func (t *TeeEngine) Create(filename string) (fs.File, error) {
_ = os.MkdirAll(filepath.Dir(filename), 0755)
file1, err := t.eng1.Create(filename)
filename2, ok := t.remapPath(filename)
if !ok {
return file1, err
}
_ = os.MkdirAll(filepath.Dir(filename2), 0755)
file2, err2 := t.eng2.Create(filename2)
if err = fatalOnErrorMismatch(t.ctx, err, err2); err != nil {
return nil, err
}
return &TeeEngineFile{
ctx: t.ctx,
file1: file1,
file2: file2,
}, nil
}
// CreateWithSync implements the FS interface.
func (t *TeeEngine) CreateWithSync(filename string, bytesPerSync int) (fs.File, error) {
_ = os.MkdirAll(filepath.Dir(filename), 0755)
file1, err := t.eng1.CreateWithSync(filename, bytesPerSync)
filename2, ok := t.remapPath(filename)
if !ok {
return file1, err
}
_ = os.MkdirAll(filepath.Dir(filename2), 0755)
file2, err2 := t.eng2.CreateWithSync(filename2, bytesPerSync)
if err = fatalOnErrorMismatch(t.ctx, err, err2); err != nil {
return nil, err
}
return &TeeEngineFile{
ctx: t.ctx,
file1: file1,
file2: file2,
}, nil
}
// Open implements the FS interface.
func (t *TeeEngine) Open(filename string) (fs.File, error) {
file1, err := t.eng1.Open(filename)
filename2, ok := t.remapPath(filename)
if !ok {
return file1, err
}
file2, err2 := t.eng2.Open(filename2)
if err = fatalOnErrorMismatch(t.ctx, err, err2); err != nil {
return nil, err
}
return &TeeEngineFile{
ctx: t.ctx,
file1: file1,
file2: file2,
}, nil
}
// OpenDir implements the FS interface.
func (t *TeeEngine) OpenDir(name string) (fs.File, error) {
file1, err := t.eng1.OpenDir(name)
name2, ok := t.remapPath(name)
if !ok {
return file1, err
}
file2, err2 := t.eng2.OpenDir(name2)
if err = fatalOnErrorMismatch(t.ctx, err, err2); err != nil {
return nil, err
}
return &TeeEngineFile{
ctx: t.ctx,
file1: file1,
file2: file2,
}, nil
}
// ReadFile implements the Engine interface.
func (t *TeeEngine) ReadFile(filename string) ([]byte, error) {
return t.eng1.ReadFile(filename)
}
// WriteFile implements the Engine interface.
func (t *TeeEngine) WriteFile(filename string, data []byte) error {
err := t.eng1.WriteFile(filename, data)
filename2, ok := t.remapPath(filename)
if !ok {
return err
}
_ = os.MkdirAll(filepath.Dir(filename2), 0755)
err2 := t.eng2.WriteFile(filename2, data)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// Remove implements the FS interface.
func (t *TeeEngine) Remove(filename string) error {
err := t.eng1.Remove(filename)
filename2, ok := t.remapPath(filename)
if !ok {
return err
}
err2 := t.eng2.Remove(filename2)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// RemoveAll implements the Engine interface.
func (t *TeeEngine) RemoveAll(dir string) error {
err := t.eng1.RemoveAll(dir)
dir2, ok := t.remapPath(dir)
if !ok {
return err
}
err2 := t.eng2.RemoveAll(dir2)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// Link implements the FS interface.
func (t *TeeEngine) Link(oldname, newname string) error {
err := t.eng1.Link(oldname, newname)
oldname2, ok := t.remapPath(oldname)
if !ok {
return err
}
newname2, _ := t.remapPath(newname)
err2 := t.eng2.Link(oldname2, newname2)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// Rename implements the FS interface.
func (t *TeeEngine) Rename(oldname, newname string) error {
err := t.eng1.Rename(oldname, newname)
oldname2, ok := t.remapPath(oldname)
if !ok {
return err
}
newname2, _ := t.remapPath(newname)
err2 := t.eng2.Rename(oldname2, newname2)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// MkdirAll implements the FS interface.
func (t *TeeEngine) MkdirAll(name string) error {
err := t.eng1.MkdirAll(name)
name2, ok := t.remapPath(name)
if !ok {
return err
}
err2 := t.eng2.MkdirAll(name2)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// RemoveDir implements the FS interface.
func (t *TeeEngine) RemoveDir(name string) error {
err := t.eng1.RemoveDir(name)
name2, ok := t.remapPath(name)
if !ok {
return err
}
err2 := t.eng2.RemoveDir(name2)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// List implements the FS interface.
func (t *TeeEngine) List(name string) ([]string, error) {
list1, err := t.eng1.List(name)
name2, ok := t.remapPath(name)
if !ok {
return list1, err
}
_, err2 := t.eng2.List(name2)
if err = fatalOnErrorMismatch(t.ctx, err, err2); err != nil {
return nil, err
}
// TODO(sbhola): compare the slices.
return list1, nil
}
// Stat implements the FS interface.
func (t *TeeEngine) Stat(name string) (os.FileInfo, error) {
info1, err := t.eng1.Stat(name)
name2, ok := t.remapPath(name)
if !ok {
return info1, err
}
_, err2 := t.eng2.Stat(name2)
if err = fatalOnErrorMismatch(t.ctx, err, err2); err != nil {
return nil, err
}
// TODO(jackson): compare the FileInfos?
return info1, err
}
// CreateCheckpoint implements the Engine interface.
func (t *TeeEngine) CreateCheckpoint(dir string) error {
path1 := filepath.Join(dir, "eng1")
path2 := filepath.Join(dir, "eng2")
err := t.eng1.CreateCheckpoint(path1)
err2 := t.eng2.CreateCheckpoint(path2)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
func (t TeeEngine) String() string {
return t.eng1.(fmt.Stringer).String()
}
// TeeEngineFile is a File that forwards to both underlying eng1
// and eng2 files.
type TeeEngineFile struct {
ctx context.Context
file1 fs.File
file2 fs.File
}
var _ fs.File = &TeeEngineFile{}
// Close implements the File interface.
func (t *TeeEngineFile) Close() error {
err := t.file1.Close()
err2 := t.file2.Close()
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// Sync implements the File interface.
func (t *TeeEngineFile) Sync() error {
err := t.file1.Sync()
err2 := t.file2.Sync()
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// Write implements the File interface.
func (t *TeeEngineFile) Write(p []byte) (int, error) {
n, err := t.file1.Write(p)
n2, err2 := t.file2.Write(p)
if err = fatalOnErrorMismatch(t.ctx, err, err2); err != nil {
return 0, err
}
if n != n2 {
log.Fatalf(t.ctx, "mismatching number of bytes written by engines: %d != %d", n, n2)
}
return n, nil
}
// Read implements the File interface.
func (t *TeeEngineFile) Read(p []byte) (n int, err error) {
p2 := make([]byte, len(p))
n, err = t.file1.Read(p)
n2, err2 := t.file2.Read(p2)
if err = fatalOnErrorMismatch(t.ctx, err, err2); err != nil {
return 0, err
}
if n != n2 {
log.Fatalf(t.ctx, "mismatching number of bytes read by engines: %d != %d", n, n2)
}
if !bytes.Equal(p[:n], p2[:n]) {
log.Fatalf(t.ctx, "different bytes read by engines: %s != %s", hex.EncodeToString(p[:n]), hex.EncodeToString(p2[:n]))
}
return n, nil
}
// ReadAt implements the File interface.
func (t *TeeEngineFile) ReadAt(p []byte, off int64) (n int, err error) {
p2 := make([]byte, len(p))
n, err = t.file1.ReadAt(p, off)
n2, err2 := t.file2.ReadAt(p2, off)
if err = fatalOnErrorMismatch(t.ctx, err, err2); err != nil {
return 0, err
}
if n != n2 {
log.Fatalf(t.ctx, "mismatching number of bytes read by engines: %d != %d", n, n2)
}
if !bytes.Equal(p[:n], p2[:n]) {
log.Fatalf(t.ctx, "different bytes read by engines: %s != %s", hex.EncodeToString(p[:n]), hex.EncodeToString(p2[:n]))
}
return n, nil
}
// TeeEngineReadWriter implements a generic ReadWriter. Used for
// implementing ReadOnly.
type TeeEngineReadWriter struct {
TeeEngineReader
}
var _ ReadWriter = &TeeEngineReadWriter{}
// ApplyBatchRepr implements the ReadWriter interface.
func (t *TeeEngineReadWriter) ApplyBatchRepr(repr []byte, sync bool) error {
err := t.reader1.(ReadWriter).ApplyBatchRepr(repr, sync)
err2 := t.reader2.(ReadWriter).ApplyBatchRepr(repr, sync)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// Clear implements the ReadWriter interface.
func (t *TeeEngineReadWriter) Clear(key MVCCKey) error {
err := t.reader1.(ReadWriter).Clear(key)
err2 := t.reader2.(ReadWriter).Clear(key)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// SingleClear implements the ReadWriter interface.
func (t *TeeEngineReadWriter) SingleClear(key MVCCKey) error {
err := t.reader1.(ReadWriter).SingleClear(key)
err2 := t.reader2.(ReadWriter).SingleClear(key)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// ClearRange implements the ReadWriter interface.
func (t *TeeEngineReadWriter) ClearRange(start, end MVCCKey) error {
err := t.reader1.(ReadWriter).ClearRange(start, end)
err2 := t.reader2.(ReadWriter).ClearRange(start, end)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// ClearIterRange implements the ReadWriter interface.
func (t *TeeEngineReadWriter) ClearIterRange(iter Iterator, start, end roachpb.Key) error {
err := t.reader1.(ReadWriter).ClearIterRange(iter.(*TeeEngineIter).iter1, start, end)
err2 := t.reader2.(ReadWriter).ClearIterRange(iter.(*TeeEngineIter).iter2, start, end)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// Merge implements the ReadWriter interface.
func (t *TeeEngineReadWriter) Merge(key MVCCKey, value []byte) error {
err := t.reader1.(ReadWriter).Merge(key, value)
err2 := t.reader2.(ReadWriter).Merge(key, value)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// Put implements the ReadWriter interface.
func (t *TeeEngineReadWriter) Put(key MVCCKey, value []byte) error {
err := t.reader1.(ReadWriter).Put(key, value)
err2 := t.reader2.(ReadWriter).Put(key, value)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// LogData implements the ReadWriter interface.
func (t *TeeEngineReadWriter) LogData(data []byte) error {
err := t.reader1.(ReadWriter).LogData(data)
err2 := t.reader2.(ReadWriter).LogData(data)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// LogLogicalOp implements the ReadWriter interface.
func (t *TeeEngineReadWriter) LogLogicalOp(op MVCCLogicalOpType, details MVCCLogicalOpDetails) {
t.reader1.(ReadWriter).LogLogicalOp(op, details)
t.reader2.(ReadWriter).LogLogicalOp(op, details)
}
// TeeEngineReader implements a generic Reader on top of two underlying
// engine1/2 readers. Used for implementing snapshots.
type TeeEngineReader struct {
ctx context.Context
reader1 Reader
reader2 Reader
}
var _ Reader = &TeeEngineReader{}
// Close implements the Reader interface.
func (t *TeeEngineReader) Close() {
t.reader1.Close()
t.reader2.Close()
}
// Closed implements the Reader interface.
func (t *TeeEngineReader) Closed() bool {
closed1 := t.reader1.Closed()
closed2 := t.reader2.Closed()
if closed1 && closed2 {
return true
} else if closed1 || closed2 {
log.Fatalf(t.ctx, "only one of engines closed: %v != %v", closed1, closed2)
}
return false
}
// ExportToSst implements the Reader interface.
func (t *TeeEngineReader) ExportToSst(
startKey, endKey roachpb.Key,
startTS, endTS hlc.Timestamp,
exportAllRevisions bool,
targetSize, maxSize uint64,
io IterOptions,
) ([]byte, roachpb.BulkOpSummary, roachpb.Key, error) {
sst1, bulkOpSummary, resume1, err := t.reader1.ExportToSst(startKey, endKey, startTS, endTS, exportAllRevisions, targetSize, maxSize, io)
sst2, _, resume2, err2 := t.reader2.ExportToSst(startKey, endKey, startTS, endTS, exportAllRevisions, targetSize, maxSize, io)
if err = fatalOnErrorMismatch(t.ctx, err, err2); err != nil {
return nil, bulkOpSummary, nil, err
}
if !bytes.Equal(sst1, sst2) {
log.Fatalf(t.ctx, "mismatching SSTs returned by engines: %v != %v", sst1, sst2)
}
if !resume1.Equal(resume2) {
log.Fatalf(t.ctx, "mismatching resume key returned by engines: %v != %v", resume1, resume2)
}
return sst1, bulkOpSummary, resume1, err
}
// Get implements the Reader interface.
func (t *TeeEngineReader) Get(key MVCCKey) ([]byte, error) {
value, err := t.reader1.Get(key)
value2, err2 := t.reader2.Get(key)
if err != nil && err2 != nil {
return nil, err
} else if err != nil || err2 != nil {
// err != nil XOR err2 != nil
log.Fatalf(t.ctx, "error mismatch between engines: %s != %s", err, err2)
}
if !bytes.Equal(value, value2) {
log.Fatalf(t.ctx, "values mismatch between engines: %v != %v", value, value2)
}
return value, nil
}
// GetProto implements the Reader interface.
func (t *TeeEngineReader) GetProto(
key MVCCKey, msg protoutil.Message,
) (ok bool, keyBytes, valBytes int64, err error) {
if len(key.Key) == 0 {
return false, 0, 0, emptyKeyError()
}
// Piggyback on the value / error checks in Get.
val, err := t.Get(key)
if err != nil || val == nil {
return false, 0, 0, err
}
if msg != nil {
err = protoutil.Unmarshal(val, msg)
}
keyBytes = int64(key.Len())
valBytes = int64(len(val))
return true, keyBytes, valBytes, err
}
// Iterate implements the Reader interface.
func (t *TeeEngineReader) Iterate(
start, end roachpb.Key, f func(MVCCKeyValue) (stop bool, err error),
) error {
return iterateOnReader(t, start, end, f)
}
// NewIterator implements the Reader interface.
func (t *TeeEngineReader) NewIterator(opts IterOptions) Iterator {
return &TeeEngineIter{
ctx: t.ctx,
iter1: t.reader1.NewIterator(opts),
iter2: t.reader2.NewIterator(opts),
}
}
// TeeEngineBatch implements a Batch on top of underlying eng1 and
// eng2 batches.
type TeeEngineBatch struct {
ctx context.Context
batch1 Batch
batch2 Batch
}
var _ Batch = &TeeEngineBatch{}
// Close implements the Batch interface.
func (t *TeeEngineBatch) Close() {
t.batch1.Close()
t.batch2.Close()
}
// Closed implements the Batch interface.
func (t *TeeEngineBatch) Closed() bool {
closed1 := t.batch1.Closed()
closed2 := t.batch2.Closed()
if closed1 && closed2 {
return true
} else if closed1 || closed2 {
log.Fatalf(t.ctx, "only one of engines closed: %v != %v", closed1, closed2)
}
return false
}
// ExportToSst implements the Batch interface.
func (t *TeeEngineBatch) ExportToSst(
startKey, endKey roachpb.Key,
startTS, endTS hlc.Timestamp,
exportAllRevisions bool,
targetSize, maxSize uint64,
io IterOptions,
) ([]byte, roachpb.BulkOpSummary, roachpb.Key, error) {
sst1, bulkOpSummary, resume1, err := t.batch1.ExportToSst(startKey, endKey, startTS, endTS, exportAllRevisions, targetSize, maxSize, io)
sst2, _, resume2, err2 := t.batch2.ExportToSst(startKey, endKey, startTS, endTS, exportAllRevisions, targetSize, maxSize, io)
if err = fatalOnErrorMismatch(t.ctx, err, err2); err != nil {
return nil, bulkOpSummary, nil, err
}
if !bytes.Equal(sst1, sst2) {
log.Fatalf(t.ctx, "mismatching SSTs returned by engines: %v != %v", sst1, sst2)
}
if !resume1.Equal(resume2) {
log.Fatalf(t.ctx, "mismatching resume key returned by engines: %v != %v", resume1, resume2)
}
return sst1, bulkOpSummary, resume1, err
}
// Get implements the Batch interface.
func (t *TeeEngineBatch) Get(key MVCCKey) ([]byte, error) {
val, err := t.batch1.Get(key)
val2, err2 := t.batch2.Get(key)
if err = fatalOnErrorMismatch(t.ctx, err, err2); err != nil {
return nil, err
}
if !bytes.Equal(val, val2) {
log.Fatalf(t.ctx, "mismatching values returned in batch.Get: %v != %v", val, val2)
}
return val, err
}
// GetProto implements the Batch interface.
func (t *TeeEngineBatch) GetProto(
key MVCCKey, msg protoutil.Message,
) (ok bool, keyBytes, valBytes int64, err error) {
if len(key.Key) == 0 {
return false, 0, 0, emptyKeyError()
}
// Piggyback on the value / error checks in Get.
val, err := t.Get(key)
if err != nil || val == nil {
return false, 0, 0, err
}
if msg != nil {
err = protoutil.Unmarshal(val, msg)
}
keyBytes = int64(key.Len())
valBytes = int64(len(val))
return true, keyBytes, valBytes, err
}
// Iterate implements the Batch interface.
func (t *TeeEngineBatch) Iterate(
start, end roachpb.Key, f func(MVCCKeyValue) (stop bool, err error),
) error {
return iterateOnReader(t, start, end, f)
}
// NewIterator implements the Batch interface.
func (t *TeeEngineBatch) NewIterator(opts IterOptions) Iterator {
iter1 := t.batch1.NewIterator(opts)
iter2 := t.batch2.NewIterator(opts)
return &TeeEngineIter{
ctx: t.ctx,
iter1: iter1,
iter2: iter2,
}
}
// ApplyBatchRepr implements the Batch interface.
func (t *TeeEngineBatch) ApplyBatchRepr(repr []byte, sync bool) error {
err := t.batch1.ApplyBatchRepr(repr, sync)
err2 := t.batch2.ApplyBatchRepr(repr, sync)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// Clear implements the Batch interface.
func (t *TeeEngineBatch) Clear(key MVCCKey) error {
err := t.batch1.Clear(key)
err2 := t.batch2.Clear(key)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// SingleClear implements the Batch interface.
func (t *TeeEngineBatch) SingleClear(key MVCCKey) error {
err := t.batch1.SingleClear(key)
err2 := t.batch2.SingleClear(key)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// ClearRange implements the Batch interface.
func (t *TeeEngineBatch) ClearRange(start, end MVCCKey) error {
err := t.batch1.ClearRange(start, end)
err2 := t.batch2.ClearRange(start, end)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// ClearIterRange implements the Batch interface.
func (t *TeeEngineBatch) ClearIterRange(iter Iterator, start, end roachpb.Key) error {
err := t.batch1.ClearIterRange(iter.(*TeeEngineIter).iter1, start, end)
err2 := t.batch2.ClearIterRange(iter.(*TeeEngineIter).iter2, start, end)
return fatalOnErrorMismatch(t.ctx, err, err2)
}
// Merge implements the Batch interface.
func (t *TeeEngineBatch) Merge(key MVCCKey, value []byte) error {
err := t.batch1.Merge(key, value)
err2 := t.batch2.Merge(key, value)