forked from hashicorp/raft-wal
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wal_test.go
844 lines (778 loc) · 21.9 KB
/
wal_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
838
839
840
841
842
843
844
// Copyright (c) HashiCorp, Inc
// SPDX-License-Identifier: MPL-2.0
package wal
import (
"context"
"fmt"
"os"
"sort"
"testing"
"time"
"github.com/polarsignals/wal/types"
"github.com/stretchr/testify/require"
)
func TestWALOpen(t *testing.T) {
cases := []struct {
name string
tsOpts []testStorageOpt
walOpts []walOpt
expectErr string
expectSegmentBases []uint64
expectCalls map[string]int
ignoreInvalidMeta bool
// validate recovery of data
expectFirstIndex uint64
expectLastIndex uint64
}{
{
name: "empty dir creates new log",
// Should end up with one segment created with baseIndex 1
expectSegmentBases: []uint64{1},
expectCalls: map[string]int{
"Create": 1,
"CommitState": 1,
"List": 1,
"Load": 1,
},
expectFirstIndex: 0,
expectLastIndex: 0,
},
{
name: "single segment recovery",
tsOpts: []testStorageOpt{
segTail(10), // Single tail segment with 10 entries
},
expectSegmentBases: []uint64{1},
expectCalls: map[string]int{
"List": 1,
"Load": 1,
"RecoverTail": 1,
},
expectFirstIndex: 1,
expectLastIndex: 10,
},
{
name: "multiple segment recovery",
tsOpts: []testStorageOpt{
segFull(),
segFull(),
segTail(10),
},
expectSegmentBases: []uint64{1, 101, 201},
expectCalls: map[string]int{
"List": 1,
"Load": 1,
"RecoverTail": 1,
"Open": 2,
},
expectFirstIndex: 1,
expectLastIndex: 210,
},
{
name: "metadb load fails",
tsOpts: []testStorageOpt{
func(ts *testStorage) {
ts.loadErr = os.ErrNotExist
},
},
expectErr: "file does not exist",
},
{
// This is kinda far-fetched since meta db is typically in same Dir, but
// it _might_ not be with a custom MetaStore.
name: "list fails",
tsOpts: []testStorageOpt{
func(ts *testStorage) {
ts.listErr = os.ErrNotExist
},
},
expectErr: "file does not exist",
},
{
name: "recover tail fails",
tsOpts: []testStorageOpt{
segTail(10),
func(ts *testStorage) {
ts.recoverErr = ErrCorrupt
},
},
expectErr: "corrupt",
},
{
name: "open fails",
tsOpts: []testStorageOpt{
segFull(),
segTail(10),
func(ts *testStorage) {
ts.openErr = os.ErrNotExist
},
},
expectErr: "file does not exist",
},
{
name: "commit fails",
tsOpts: []testStorageOpt{
func(ts *testStorage) {
ts.commitErr = os.ErrPermission
},
},
expectErr: "permission denied",
},
{
name: "commit fails",
tsOpts: []testStorageOpt{
func(ts *testStorage) {
ts.createErr = os.ErrPermission
},
},
expectErr: "permission denied",
},
{
name: "recover tampered meta store",
tsOpts: []testStorageOpt{
segFull(),
segTail(10), // multiple tails is invalid
segTail(10),
func(ts *testStorage) {
ts.createErr = os.ErrPermission
},
},
expectErr: "unsealed segment is not at tail",
ignoreInvalidMeta: true, // disable the test's setup checks on validity
},
{
name: "ALICE fail: recovering with a missing tail file should work",
tsOpts: []testStorageOpt{
segFull(),
segTail(0),
func(ts *testStorage) {
// Simulate the tail file not being created yet after a crash.
ts.Delete(101, 101)
},
},
expectSegmentBases: []uint64{1, 101},
expectCalls: map[string]int{
"List": 1,
"Load": 1,
"RecoverTail": 1,
"Open": 1,
"Create": 1,
"Delete": 1, // This one is called by us above not the WAL
},
expectFirstIndex: 1,
expectLastIndex: 100,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
ts, w, err := testOpenWAL(t, tc.tsOpts, tc.walOpts, tc.ignoreInvalidMeta)
// Error or not we should never commit an invalid set of segments to meta.
if !tc.ignoreInvalidMeta {
ts.assertValidMetaState(t)
}
if tc.expectErr != "" {
require.ErrorContains(t, err, tc.expectErr)
return
}
require.NoError(t, err)
// Check we ended up with the segments we expected
segmentBases := make([]uint64, 0)
for _, seg := range ts.segments {
segmentBases = append(segmentBases, seg.info().BaseIndex)
}
sort.Slice(segmentBases, func(i, j int) bool {
return segmentBases[i] < segmentBases[j]
})
committedSegmentBases := make([]uint64, 0)
for _, seg := range ts.metaState.Segments {
committedSegmentBases = append(committedSegmentBases, seg.BaseIndex)
}
require.Equal(t, tc.expectSegmentBases, segmentBases)
require.Equal(t, tc.expectSegmentBases, committedSegmentBases)
if tc.expectCalls != nil {
require.Equal(t, tc.expectCalls, ts.calls)
}
// Validate the data was recovered and is readable
first, err := w.FirstIndex()
require.NoError(t, err)
require.Equal(t, int(tc.expectFirstIndex), int(first))
last, err := w.LastIndex()
require.NoError(t, err)
require.Equal(t, int(tc.expectLastIndex), int(last))
// Every index between first and last inclusive should be readable
var log, log2 types.LogEntry
if last > 0 {
for idx := first; idx <= last; idx++ {
err := w.GetLog(idx, &log)
require.NoError(t, err)
require.Equal(t, idx, log.Index, "WAL returned wrong entry for index")
validateLogEntry(t, log)
}
}
// We should _not_ be able to get logs outside the expected range.
if first > 0 {
err := w.GetLog(first-1, &log)
require.ErrorIs(t, err, ErrNotFound)
}
err = w.GetLog(last+1, &log)
require.ErrorIs(t, err, ErrNotFound)
// Basic test that appending after recovery works
log.Index = last + 1
log.Data = []byte("appended")
err = w.StoreLogs([]types.LogEntry{log})
require.NoError(t, err)
err = w.GetLog(last+1, &log2)
require.NoError(t, err)
require.Equal(t, last+1, log2.Index)
require.Equal(t, "appended", string(log2.Data))
})
}
}
func TestStoreLogs(t *testing.T) {
cases := []struct {
name string
tsOpts []testStorageOpt
walOpts []walOpt
store []types.LogEntry
store2 []types.LogEntry
expectErr string
// validate recovery of data
expectFirstIndex uint64
expectLastIndex uint64
expectFirstBaseIndex uint64
}{
{
name: "empty log append",
store: makeLogEntries(1, 5),
expectFirstIndex: 1,
expectLastIndex: 5,
},
{
name: "empty log append, start from non-zero",
store: makeLogEntries(10000, 5),
expectFirstIndex: 10000,
expectLastIndex: 10004,
// Ensure we actually re-created the first segment with the right base index.
expectFirstBaseIndex: 10000,
},
{
name: "existing segment log append",
tsOpts: []testStorageOpt{
segTail(10),
},
store: makeLogEntries(11, 5),
expectFirstIndex: 1,
expectLastIndex: 15,
},
{
name: "existing multi segment log append",
tsOpts: []testStorageOpt{
segFull(),
segFull(),
segTail(10),
},
store: makeLogEntries(211, 5),
expectFirstIndex: 1,
expectLastIndex: 215,
},
{
name: "out of order",
tsOpts: []testStorageOpt{
segTail(10),
},
// Append logs starting from 100 when tail is currently 10
store: makeLogEntries(100, 5),
expectErr: "non-monotonic log entries",
},
{
name: "out of sequence",
tsOpts: []testStorageOpt{
segTail(10),
},
// Append logs starting from 10 but not with gaps internally
store: makeLogEntriesSparse(10, 11, 14, 15),
expectErr: "non-monotonic log entries",
},
{
name: "rotate when full",
tsOpts: []testStorageOpt{
segTail(99),
},
store: makeLogEntries(100, 5),
expectFirstIndex: 1,
expectLastIndex: 104,
},
{
name: "rotate and append more",
tsOpts: []testStorageOpt{
segTail(99),
},
store: makeLogEntries(100, 5),
store2: makeLogEntries(105, 5),
expectFirstIndex: 1,
expectLastIndex: 109,
},
{
name: "empty rotate and append more",
// Start from empty. This had a bug initially since open didn't
// initialize the reader but that is only noticed after the initial
// segment is no longer the tail and you attempt to fetch.
tsOpts: []testStorageOpt{},
store: makeLogEntries(100, 101),
store2: makeLogEntries(201, 5),
expectFirstIndex: 100,
expectLastIndex: 205,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
ts, w, err := testOpenWAL(t, tc.tsOpts, tc.walOpts, false)
require.NoError(t, err)
err = w.StoreLogs(tc.store)
if tc.expectErr != "" {
require.ErrorContains(t, err, tc.expectErr)
return
}
require.NoError(t, err)
if len(tc.store2) > 0 {
err = w.StoreLogs(tc.store2)
require.NoError(t, err)
}
t.Log(ts.debugDump())
// Validate the data was recovered and is readable
first, err := w.FirstIndex()
require.NoError(t, err)
require.Equal(t, int(tc.expectFirstIndex), int(first))
last, err := w.LastIndex()
require.NoError(t, err)
require.Equal(t, int(tc.expectLastIndex), int(last))
// Check all the internal meta/segment state meets our invariants
ts.assertValidMetaState(t)
// Check all log entries exist that are meant to
if tc.expectLastIndex > 0 {
firstAppended := tc.expectLastIndex - uint64(len(tc.store)+len(tc.store2)) + 1
// Start one _before_ the appended logs (assuming there was one before)
// to verify that we didn't accidentally overwrite the previous tail.
start := firstAppended - 1
if start < tc.expectFirstIndex {
start = tc.expectFirstIndex
}
var log types.LogEntry
for idx := start; idx <= tc.expectLastIndex; idx++ {
err := w.GetLog(idx, &log)
require.NoError(t, err, "failed to find log %d", idx)
require.Equal(t, int(idx), int(log.Index))
if idx >= firstAppended {
appendedOffset := int(idx - firstAppended)
var want types.LogEntry
if appendedOffset < len(tc.store) {
want = tc.store[appendedOffset]
} else {
want = tc.store2[appendedOffset-len(tc.store)]
}
require.Equal(t, int(want.Index), int(log.Index))
require.Equal(t, want.Data, log.Data)
}
}
}
// Check the actual base index of the first segment is the one we expect.
// This is an internal detail, but it's important as the contract with the
// Segment layer is broken if we assume we can start logs at a higher
// index than the BaseIndex.
if tc.expectFirstBaseIndex > 0 {
segments, err := ts.List()
require.NoError(t, err)
// First segment would have been ID 0, abse index 1 but we should have
// replaced it with a segment with the correct base index which would
// have ID 1
require.Equal(t, map[uint64]uint64{1: tc.expectFirstBaseIndex}, segments)
// Ensure that the old file was closed and deleted
ts.assertDeletedAndClosed(t, 1)
}
})
}
}
func TestDeleteRange(t *testing.T) {
cases := []struct {
name string
tsOpts []testStorageOpt
walOpts []walOpt
deleteMin uint64
deleteMax uint64
expectErr string
// validate recovery of data
expectFirstIndex uint64
expectLastIndex uint64
expectDeleted []uint64
expectNHeadTruncations uint64
expectNTailTruncations uint64
}{
{
name: "no-op truncate from head",
tsOpts: []testStorageOpt{
firstIndex(1000),
segFull(),
segTail(10),
},
// Delete a range before the log starts
deleteMin: 0,
deleteMax: 100,
expectFirstIndex: 1000,
expectLastIndex: 1000 + 100 + 10 - 1,
},
{
name: "no-op truncate from after tail",
tsOpts: []testStorageOpt{
firstIndex(1000),
segFull(),
segTail(10),
},
// Delete a range before the log starts
deleteMin: 5000,
deleteMax: 5100,
expectFirstIndex: 1000,
expectLastIndex: 1000 + 100 + 10 - 1,
},
{
name: "in-segment head truncation",
tsOpts: []testStorageOpt{
firstIndex(1000),
segFull(),
segTail(10),
},
// Delete a range before the log starts
deleteMin: 0,
deleteMax: 1010,
expectFirstIndex: 1011,
expectLastIndex: 1000 + 100 + 10 - 1,
expectNHeadTruncations: 11,
},
{
name: "segment deleting head truncation",
tsOpts: []testStorageOpt{
firstIndex(1000),
segFull(),
segTail(10),
},
// Delete a range before the log starts start exactly on firstIndex to
// test boundary conditions.
deleteMin: 1000,
deleteMax: 1101, // Leave some entries in the tail
expectFirstIndex: 1102,
expectLastIndex: 1000 + 100 + 10 - 1,
expectDeleted: []uint64{1000},
expectNHeadTruncations: 102,
},
{
name: "head truncation deleting all segments",
tsOpts: []testStorageOpt{
firstIndex(1000),
segFull(),
segTail(10),
},
// Delete a range before the log starts start exactly on firstIndex to
// test boundary conditions.
deleteMin: 1000,
deleteMax: 1000 + 100 + 10 - 1,
expectFirstIndex: 0,
expectLastIndex: 0,
expectDeleted: []uint64{1000, 1100},
expectNHeadTruncations: 110,
},
{
name: "non-deleting tail-truncation",
tsOpts: []testStorageOpt{
firstIndex(1000),
segFull(),
segTail(10),
},
// Delete the last two entries from the log
deleteMin: 1000 + 100 + 10 - 2,
deleteMax: 1000 + 100 + 10 - 1,
expectFirstIndex: 1000,
expectLastIndex: 1107,
expectDeleted: []uint64{},
expectNTailTruncations: 2,
},
{
name: "tail-deleting tail-truncation",
tsOpts: []testStorageOpt{
firstIndex(1000),
segFull(),
segTail(10),
},
deleteMin: 1051,
deleteMax: 1000 + 100 + 10 - 1,
expectFirstIndex: 1000,
expectLastIndex: 1050,
expectDeleted: []uint64{1100},
expectNTailTruncations: 59,
},
{
name: "multi-segment deleting tail-truncation",
tsOpts: []testStorageOpt{
firstIndex(1000),
segFull(),
segFull(),
segTail(10),
},
deleteMin: 1051,
deleteMax: 1000 + 100 + 100 + 10 - 1,
expectFirstIndex: 1000,
expectLastIndex: 1050,
expectDeleted: []uint64{1100, 1200},
expectNTailTruncations: 159,
},
{
name: "everything deleting truncation",
tsOpts: []testStorageOpt{
firstIndex(1000),
segFull(),
segFull(),
segTail(10),
},
deleteMin: 1,
deleteMax: 1000 + 100 + 100 + 10 - 1,
expectFirstIndex: 0,
expectLastIndex: 0,
expectDeleted: []uint64{1000, 1100, 1200},
// This is technically neither tail nor head since all entries are being
// removed but since head deletions are simpler we treat it as a head
// deletion.
expectNHeadTruncations: 210,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
opts := tc.walOpts
ts, w, err := testOpenWAL(t, tc.tsOpts, opts, false)
require.NoError(t, err)
firstIndex, _ := w.FirstIndex()
if tc.deleteMin <= firstIndex {
err = w.TruncateFront(tc.deleteMax + 1)
} else {
err = w.TruncateBack(tc.deleteMin - 1)
}
if tc.expectErr != "" {
require.ErrorContains(t, err, tc.expectErr)
return
}
require.NoError(t, err)
t.Log(ts.debugDump())
// Validate the data was recovered and is readable
first, err := w.FirstIndex()
require.NoError(t, err)
require.Equal(t, int(tc.expectFirstIndex), int(first), "unexpected first index")
last, err := w.LastIndex()
require.NoError(t, err)
require.Equal(t, int(tc.expectLastIndex), int(last), "unexpected last index")
// Check all log entries exist that are meant to
var log types.LogEntry
if tc.expectLastIndex > 0 {
for idx := tc.expectFirstIndex; idx <= tc.expectLastIndex; idx++ {
err := w.GetLog(idx, &log)
require.NoError(t, err, "failed to find log %d", idx)
require.Equal(t, int(idx), int(log.Index))
validateLogEntry(t, log)
}
}
// We should _not_ be able to get logs outside of the expected range
if first > 0 {
err := w.GetLog(first-1, &log)
require.ErrorIs(t, err, ErrNotFound)
}
err = w.GetLog(last+1, &log)
require.ErrorIs(t, err, ErrNotFound)
if len(tc.expectDeleted) > 0 {
ts.assertDeletedAndClosed(t, tc.expectDeleted...)
}
// Now validate we can continue to append as expected.
nextIdx := tc.expectLastIndex + 1
err = w.StoreLogs(makeLogEntriesSparse(nextIdx))
require.NoError(t, err)
last, err = w.LastIndex()
require.NoError(t, err)
require.Equal(t, int(nextIdx), int(last))
// and read it back
err = w.GetLog(nextIdx, &log)
require.NoError(t, err, "failed to find appended log %d", nextIdx)
require.Equal(t, int(nextIdx), int(log.Index))
validateLogEntry(t, log)
})
}
}
// TestConcurrentReadersAndWriter is designed to be run with race detector
// enabled to validate the concurrent behavior of the WAL.
func TestConcurrentReadersAndWriter(t *testing.T) {
_, w, err := testOpenWAL(t, nil, nil, false)
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Start writers, we logically only support a single writer since that is
// typical in raft but validate that even if appending and truncation are
// triggered in separate threads there is no race.
appender := func() {
index := uint64(1000)
for {
if ctx.Err() != nil {
return
}
err := w.StoreLogs(makeLogEntriesSparse(index))
if err != nil {
panic("error during append: " + err.Error())
}
if index%1009 == 0 {
// Every 1009 (prime so it doesn't always fall on segment boundary)
// entries, truncate the last one and rewrite it to simulate head
// truncations too (which are always done synchronously with appends in
// raft).
err = w.TruncateBack(index - 1) // delete the last one (include min/max)
if err != nil {
panic("error during delete range: " + err.Error())
}
// Validate that we actually truncated
last, err := w.LastIndex()
if err != nil {
panic("error fetching last: " + err.Error())
}
if last != index-1 {
panic(fmt.Sprintf("didn't truncate tail expected last=%d got last=%d", index-1, last))
}
// Write it back again so we don't get stuck on this index forever on the next loop or leave a gap!
err = w.StoreLogs(makeLogEntriesSparse(index))
if err != nil {
panic("error during append after truncate: " + err.Error())
}
t.Logf("truncated tail and appended to %d", index)
}
index++
// Tiny sleep to stop CPUs burning
time.Sleep(250 * time.Microsecond)
}
}
truncator := func() {
for {
select {
case <-ctx.Done():
return
case <-time.After(100 * time.Millisecond):
// continue below
}
last, err := w.LastIndex()
if err != nil {
panic("error fetching last: " + err.Error())
}
err = w.TruncateFront(last - 99) // Keep the last 100 entries
if err != nil {
panic("error during delete range: " + err.Error())
}
first, err := w.FirstIndex()
if err != nil {
panic("error fetching first: " + err.Error())
}
expectFirst := last - 99
if expectFirst < 1000 {
// The writer starts at 1000 so if we happen to run a truncate before
// 100 entries are appended (which happened in CI due to timing
// differences) then our append would not have done anything and first
// will still be 1000.
expectFirst = 1000
}
if first != expectFirst {
panic(fmt.Sprintf("head truncate didn't work, expected first=%d got first=%d", expectFirst, first))
}
t.Logf("truncated head to %d", last-99)
}
}
reader := func() {
var log types.LogEntry
var total int
var good int
for {
select {
case <-ctx.Done():
// On completion, validate that we had at least _some_ percentage of
// succesful reads and didn't just get NotFound on every iteration!
if total > 0 && good == 0 {
panic("reader only every got ErrNotFound")
}
return
case <-time.After(1 * time.Millisecond):
// continue below
}
// Read first entry, middle entry and last entry to make sure we hit all
// segment types.
first, err := w.FirstIndex()
if err != nil {
panic("error fetching first: " + err.Error())
}
last, err := w.LastIndex()
if err != nil {
panic("error fetching last: " + err.Error())
}
middle := (last - first) / 2
get := func(name string, idx uint64) int {
err = w.GetLog(idx, &log)
if err == ErrNotFound {
return 0
}
if err != nil {
panic("error fetching log: " + err.Error())
}
if log.Index != idx {
panic(fmt.Errorf("wrong %s log want=%d got=%d", name, idx, log.Index))
}
if string(log.Data) != fmt.Sprintf("Log entry %d", log.Index) {
panic(fmt.Errorf("wrong data in log %d: %s", log.Index, log.Data))
}
return 1
}
total += 3
good += get("first", first)
good += get("middle", middle)
good += get("last", last)
}
}
// Run writers
go appender()
go truncator()
// Run 10 readers
for i := 0; i < 1; /*0*/ i++ {
go reader()
}
// Wait for timeout
<-ctx.Done()
}
func TestClose(t *testing.T) {
// Multiple "files" to open
opts := []testStorageOpt{
segFull(),
segFull(),
segFull(),
segTail(1),
}
ts, w, err := testOpenWAL(t, opts, nil, false)
require.NoError(t, err)
// ensure files are actually open
ts.assertAllClosed(t, false)
// Close and check
require.NoError(t, w.Close())
ts.assertAllClosed(t, true)
// Ensure all public methods now return an error
_, err = w.FirstIndex()
require.ErrorIs(t, err, ErrClosed)
_, err = w.LastIndex()
require.ErrorIs(t, err, ErrClosed)
var log types.LogEntry
err = w.GetLog(1, &log)
require.ErrorIs(t, err, ErrClosed)
err = w.StoreLogs([]types.LogEntry{log})
require.ErrorIs(t, err, ErrClosed)
err = w.TruncateFront(2)
require.ErrorIs(t, err, ErrClosed)
err = w.TruncateBack(2)
require.ErrorIs(t, err, ErrClosed)
}