-
Notifications
You must be signed in to change notification settings - Fork 10
/
wal_stubs_test.go
660 lines (583 loc) · 17 KB
/
wal_stubs_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
// Copyright (c) HashiCorp, Inc
// SPDX-License-Identifier: MPL-2.0
package wal
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"os"
"sort"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/benbjohnson/immutable"
"github.com/hashicorp/raft"
"github.com/hashicorp/raft-wal/types"
"github.com/stretchr/testify/require"
)
func testOpenWAL(t *testing.T, tsOpts []testStorageOpt, walOpts []walOpt, allowInvalidMeta bool) (*testStorage, *WAL, error) {
t.Helper()
ts := makeTestStorage(tsOpts...)
w, err := testOpenWALWithStorage(t, ts, walOpts, allowInvalidMeta)
return ts, w, err
}
func testOpenWALWithStorage(t *testing.T, ts *testStorage, walOpts []walOpt, allowInvalidMeta bool) (*WAL, error) {
t.Helper()
// Make sure "persisted" state is setup in a valid way
sort.Slice(ts.metaState.Segments, func(i, j int) bool {
si, sj := ts.metaState.Segments[i], ts.metaState.Segments[j]
if si.BaseIndex == sj.BaseIndex {
return si.ID < sj.ID
}
return si.BaseIndex < sj.BaseIndex
})
if !allowInvalidMeta {
ts.assertValidMetaState(t)
}
opts := append(walOpts, stubStorage(ts))
w, err := Open("test", opts...)
return w, err
}
type testStorageOpt func(ts *testStorage)
// firstIndex is a helper for setting the initial index
func firstIndex(idx uint64) testStorageOpt {
return func(ts *testStorage) {
ts.setupMaxIndex = idx
}
}
// segFull adds a full segment starting where the last one left off.
func segFull() testStorageOpt {
return func(ts *testStorage) {
seg := makeTestSegment(ts.setupMaxIndex)
es := makeLogEntries(seg.info().BaseIndex, seg.limit)
if err := seg.Append(es); err != nil {
panic(err)
}
// Seal "full" segments
seg.mutate(func(newState *testSegmentState) error {
newState.info.SealTime = time.Now()
newState.info.IndexStart = 12345
newState.info.MaxIndex = newState.info.BaseIndex + uint64(len(es)) - 1
return nil
})
ts.setupMaxIndex += uint64(seg.numLogs())
ts.segments[seg.info().ID] = seg
// Also need to represent this in committed state
ts.metaState.Segments = append(ts.metaState.Segments, seg.info())
ts.metaState.NextSegmentID = seg.info().ID + 1
}
}
// segTail adds an unsealed segment with n entries
func segTail(n int) testStorageOpt {
return func(ts *testStorage) {
seg := makeTestSegment(ts.setupMaxIndex)
es := makeLogEntries(seg.info().BaseIndex, n)
if err := seg.Append(es); err != nil {
panic(err)
}
ts.setupMaxIndex += uint64(seg.numLogs())
ts.segments[seg.info().ID] = seg
// Also need to represent this in committed state
ts.metaState.Segments = append(ts.metaState.Segments, seg.info())
ts.metaState.NextSegmentID = seg.info().ID + 1
}
}
// seg is a helper for defining a stored segment
func makeTestSegment(baseIndex uint64) *testSegment {
ts := &testSegment{
// Just need some records for this level of testing since we are not
// actually bothering about blocks and encoding etc.
limit: 100,
}
info := types.SegmentInfo{
BaseIndex: baseIndex,
ID: baseIndex, // for now just use 1:1 baseIndex and ID
MinIndex: baseIndex,
Codec: CodecBinaryV1,
CreateTime: time.Now(),
}
ts.s.Store(testSegmentState{
info: info,
logs: &immutable.SortedMap[uint64, types.LogEntry]{},
})
return ts
}
// stable is a helper for setting up stable store state with byte values.
func stable(key, val string) testStorageOpt {
return func(ts *testStorage) {
ts.stable[key] = []byte(val)
}
}
// stableInt is a helper for setting up stable store state with uint64 encoded
// values.
func stableInt(key string, val uint64) testStorageOpt {
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], val)
return func(ts *testStorage) {
ts.stable[key] = buf[:]
}
}
func makeLogEntries(startIdx uint64, num int) []types.LogEntry {
codec := &BinaryCodec{}
entries := make([]types.LogEntry, 0, num)
for _, log := range makeRaftLogs(startIdx, num) {
// Allocate a new buffer every time otherwise we end up returning slices to
// the same underlying buffer that is mutated in the next iteration.
var buf bytes.Buffer
if err := codec.Encode(log, &buf); err != nil {
panic(err)
}
// need to copy the buffer since the next iteration may mutate the same
// underlying byteslice returned by Bytes
entries = append(entries, types.LogEntry{
Index: log.Index,
Data: buf.Bytes(),
})
}
return entries
}
func makeRaftLogs(startIdx uint64, num int) []*raft.Log {
logs := make([]*raft.Log, 0, num)
for i := uint64(0); i < uint64(num); i++ {
log := raft.Log{
Index: startIdx + i,
Term: 1,
Data: []byte(fmt.Sprintf("Log entry %d", startIdx+i)),
AppendedAt: time.Now(),
}
logs = append(logs, &log)
}
return logs
}
func makeRaftLogsSparse(idxs ...uint64) []*raft.Log {
logs := make([]*raft.Log, 0, len(idxs))
for _, idx := range idxs {
log := raft.Log{
Index: idx,
Term: 1,
Data: []byte(fmt.Sprintf("Log entry %d", idx)),
AppendedAt: time.Now(),
}
logs = append(logs, &log)
}
return logs
}
func validateLogEntry(t *testing.T, log *raft.Log) {
expectBytes := []byte(fmt.Sprintf("Log entry %d", log.Index))
require.Equal(t, string(expectBytes), string(log.Data))
}
func makeTestStorage(opts ...testStorageOpt) *testStorage {
ts := &testStorage{
segments: make(map[uint64]*testSegment),
stable: make(map[string][]byte),
setupMaxIndex: 1, // Start from index 1 like raft
}
for _, fn := range opts {
fn(ts)
}
return ts
}
// stubStorage is a helper to stub out the metaDB segmentFiler interfaces in a
// WAL instance with ts.
func stubStorage(ts *testStorage) walOpt {
return func(w *WAL) {
w.metaDB = ts
w.sf = ts
}
}
// testStorage allows us to stub all interaction with segment files and MetaDB
// while testing WAL logic. It implements both segmentFiler and MetaStore
// interfaces.
type testStorage struct {
mu sync.Mutex
segments map[uint64]*testSegment
deleted []*testSegment
calls map[string]int
// setupMaxIndex is used just during construction to keep track of what
// segments have been added.
setupMaxIndex uint64
// lastDir stores the last dir argument passed to any method that accepts it
lastDir string
// lastName stores the last name argument passed to any method that accepts it
lastName string
metaState types.PersistentState
stable map[string][]byte
// errors that can be set by test to force subsequent calls to return the
// error.
loadErr, commitErr, getStableErr, setStableErr,
listErr, createErr, deleteErr, openErr, recoverErr error
}
func (ts *testStorage) Close() error {
return nil
}
func (ts *testStorage) debugDump() string {
var sb strings.Builder
ts.mu.Lock()
defer ts.mu.Unlock()
// We want to dump them in order so copy to an array first and sort!
sorted := make([]*testSegment, 0, len(ts.segments))
for _, s := range ts.segments {
sorted = append(sorted, s)
}
sort.Slice(sorted, func(i, j int) bool {
ii, ij := sorted[i].info(), sorted[j].info()
return ii.BaseIndex < ij.BaseIndex
})
// Makes it easier to read in test log output
sb.WriteRune('\n')
for _, s := range sorted {
info := s.info()
sealed, _, _ := s.Sealed()
fmt.Fprintf(&sb, "Seg[BaseIndex=%d ID=%d Logs=[%d..%d](%d) %v]",
info.BaseIndex, info.ID,
info.MinIndex, s.LastIndex(), s.numLogs(), sealed,
)
sb.WriteRune('\n')
}
return sb.String()
}
func (ts *testStorage) assertValidMetaState(t *testing.T) {
t.Helper()
ts.mu.Lock()
defer ts.mu.Unlock()
// must be an unsealed final segment or empty
n := len(ts.metaState.Segments)
for i, seg := range ts.metaState.Segments {
isTail := (i == n-1)
if isTail && !seg.SealTime.IsZero() {
t.Fatalf("final segment in committed state is sealed")
}
if !isTail && seg.SealTime.IsZero() {
t.Fatalf("unsealed segment not at tail in committed state")
}
// Make sure that the first log in the segment is the same as its base index
// (if the segment exists already, it might not right after meta updated but
// segment not created yet which is exercised in some tests).
if ts, ok := ts.segments[seg.ID]; ok {
tss := ts.loadState()
require.Equal(t, seg.BaseIndex, tss.info.BaseIndex)
it := tss.logs.Iterator()
it.First()
if !it.Done() {
_, log, ok := it.Next()
require.True(t, ok)
require.Equal(t, seg.BaseIndex, log.Index)
}
// Verify that if it's meant to be sealed in metadata that it actually is
// and has an index block. Note that we don't test that the actual segment
// sealed status matches meta since that is not always true e.g. just
// after an append that caused segment to seal but before the rotate has
// updated metadata. The thing we need to ensure is that if metadata says
// it's sealed that the actual segment is actually sealed and has an index
// block.
if !seg.SealTime.IsZero() {
sealed, indexStart, err := ts.Sealed()
require.NoError(t, err)
require.True(t, sealed)
require.NotEqual(t, 0, int(indexStart))
require.Equal(t, indexStart, seg.IndexStart)
}
}
}
}
func (ts *testStorage) recordCall(name string) {
if ts.calls == nil {
ts.calls = make(map[string]int)
}
ts.calls[name] = ts.calls[name] + 1
}
// Load implements MetaStore
func (ts *testStorage) Load(dir string) (types.PersistentState, error) {
ts.mu.Lock()
defer ts.mu.Unlock()
ts.recordCall("Load")
ts.lastDir = dir
return ts.metaState, ts.loadErr
}
// CommitState implements MetaStore
func (ts *testStorage) CommitState(ps types.PersistentState) error {
ts.mu.Lock()
defer ts.mu.Unlock()
ts.recordCall("CommitState")
ts.metaState = ps
// For the sake of not being super confusing, lets also update all the
// types.SegmentInfos in the testSegments e.g. if Min/Max were set due to a
// truncation or the segment was sealed.
for _, seg := range ps.Segments {
ts, ok := ts.segments[seg.ID]
if !ok {
// Probably a impossible/a test bug but lets ignore it here as other
// places should fail and it wouldn't be a realistic error to return here.
continue
}
ts.mutate(func(newState *testSegmentState) error {
newState.info = seg
return nil
})
}
return ts.commitErr
}
// GetStable implements MetaStore
func (ts *testStorage) GetStable(key []byte) ([]byte, error) {
ts.mu.Lock()
defer ts.mu.Unlock()
ts.recordCall("GetStable")
if ts.getStableErr != nil {
return nil, ts.getStableErr
}
return ts.stable[string(key)], nil
}
// SetStable implements MetaStore
func (ts *testStorage) SetStable(key []byte, value []byte) error {
ts.mu.Lock()
defer ts.mu.Unlock()
ts.recordCall("SetStable")
if ts.stable == nil {
ts.stable = make(map[string][]byte)
}
ts.stable[string(key)] = value
return ts.setStableErr
}
// Create implements segmentFiler
func (ts *testStorage) Create(info types.SegmentInfo) (types.SegmentWriter, error) {
ts.mu.Lock()
defer ts.mu.Unlock()
ts.recordCall("Create")
_, ok := ts.segments[info.ID]
if ok {
return nil, fmt.Errorf("segment ID %d already exists", info.ID)
}
sw := &testSegment{
limit: 100, // Set a size limit or it will be immediately full!
}
sw.s.Store(testSegmentState{
info: info,
logs: &immutable.SortedMap[uint64, types.LogEntry]{},
})
ts.segments[info.ID] = sw
return sw, ts.createErr
}
// RecoverTail implements segmentFiler
func (ts *testStorage) RecoverTail(info types.SegmentInfo) (types.SegmentWriter, error) {
ts.mu.Lock()
defer ts.mu.Unlock()
ts.recordCall("RecoverTail")
// Safety checks
sw, ok := ts.segments[info.ID]
if !ok {
// Simulate the right error - if this segment file isn't here then it's an
// os.ErrNotExist
return nil, fmt.Errorf("%w: can't recover unknown segment with ID %d", os.ErrNotExist, info.ID)
}
if ts.recoverErr != nil {
return nil, ts.recoverErr
}
return sw, nil
}
// Open implements segmentFiler
func (ts *testStorage) Open(info types.SegmentInfo) (types.SegmentReader, error) {
ts.mu.Lock()
defer ts.mu.Unlock()
ts.recordCall("Open")
sw, ok := ts.segments[info.ID]
if !ok {
return nil, fmt.Errorf("segment %d does not exist", info.ID)
}
if ts.openErr != nil {
return nil, ts.openErr
}
return sw, nil
}
// List implements segmentFiler
func (ts *testStorage) List() (map[uint64]uint64, error) {
ts.mu.Lock()
defer ts.mu.Unlock()
ts.recordCall("List")
if ts.listErr != nil {
return nil, ts.listErr
}
set := make(map[uint64]uint64)
for _, seg := range ts.segments {
info := seg.info()
set[info.ID] = info.BaseIndex
}
return set, nil
}
// Delete implements segmentFiler
func (ts *testStorage) Delete(baseIndex uint64, ID uint64) error {
ts.mu.Lock()
defer ts.mu.Unlock()
ts.recordCall("Delete")
if ts.deleteErr != nil {
return ts.deleteErr
}
old, ok := ts.segments[ID]
delete(ts.segments, ID)
if ok {
ts.deleted = append(ts.deleted, old)
}
return nil
}
func (ts *testStorage) assertDeletedAndClosed(t *testing.T, baseIndexes ...uint64) {
t.Helper()
ts.mu.Lock()
defer ts.mu.Unlock()
deletedIndexes := make([]uint64, 0, len(baseIndexes))
for _, s := range ts.deleted {
info := s.info()
deletedIndexes = append(deletedIndexes, info.BaseIndex)
require.True(t, s.closed(), "segment with BaseIndex=%d was deleted but not Closed", info.BaseIndex)
}
// We don't actually care about ordering as long as the right things are closed
require.ElementsMatch(t, baseIndexes, deletedIndexes)
}
func (ts *testStorage) assertAllClosed(t *testing.T, wantClosed bool) {
t.Helper()
ts.mu.Lock()
defer ts.mu.Unlock()
for _, s := range ts.segments {
closed := s.closed()
if wantClosed {
require.True(t, closed, "segment with BaseIndex=%d was not closed", s.info().BaseIndex)
} else {
require.False(t, closed, "segment with BaseIndex=%d was closed", s.info().BaseIndex)
}
}
}
// testSegment is a testing mock that implements segmentReader and segmentWriter
// but just stores the "file" contents in memory.
type testSegment struct {
writeLock sync.Mutex
s atomic.Value // testSegmentState
// limit can be set to test rolling logs
limit int
}
type testSegmentState struct {
info types.SegmentInfo
logs *immutable.SortedMap[uint64, types.LogEntry]
closed bool
indexStart uint64
}
func (s *testSegment) loadState() testSegmentState {
return s.s.Load().(testSegmentState)
}
func (s *testSegment) Close() error {
return s.mutate(func(newState *testSegmentState) error {
newState.closed = true
return nil
})
}
func (s *testSegment) GetLog(idx uint64) (*types.PooledBuffer, error) {
state := s.loadState()
if state.closed {
return nil, errors.New("closed")
}
if idx < state.info.MinIndex || (state.info.MaxIndex > 0 && idx > state.info.MaxIndex) {
return nil, ErrNotFound
}
log, ok := state.logs.Get(idx)
if !ok {
return nil, ErrNotFound
}
// We make a pooled buffer with it's own copy of the log which we then
// invalidate when it's Closed to make sure that any test code that reads data
// from a buffer that was freed fails to read what it expected. This ensures
// our logic about returning slices to users that we might re-use is correct.
buf := make([]byte, len(log.Data))
copy(buf, log.Data)
pb := &types.PooledBuffer{
Bs: buf,
CloseFn: func() {
closed := []byte{'c', 'l', 'o', 's', 'e', 'd', ' ', 'b', 'u', 'f', 'f', 'e', 'r', '!'}
for i := 0; i < len(buf); i++ {
buf[i] = closed[i%len(closed)]
}
},
}
return pb, nil
}
func (s *testSegment) Append(entries []types.LogEntry) error {
sealed, _, err := s.Sealed()
if err != nil {
return err
}
if sealed {
return ErrSealed
}
return s.mutate(func(newState *testSegmentState) error {
if newState.closed {
return errors.New("closed")
}
for _, e := range entries {
if e.Index != (newState.info.BaseIndex + uint64(newState.logs.Len())) {
return fmt.Errorf("non-monotonic append! BaseIndex=%d len=%d appended=%d",
newState.info.BaseIndex, newState.logs.Len(), e.Index)
}
newState.logs = newState.logs.Set(e.Index, e)
}
// Maybe seal
if newState.logs.Len() >= s.limit {
newState.indexStart = 12345
}
return nil
})
}
func (s *testSegment) Sealed() (bool, uint64, error) {
state := s.loadState()
if state.closed {
panic("sealed on closed segment")
}
return state.indexStart > 0, state.indexStart, nil
}
func (s *testSegment) ForceSeal() (uint64, error) {
err := s.mutate(func(newState *testSegmentState) error {
if newState.closed {
return errors.New("closed")
}
newState.indexStart = 12345
return nil
})
if err != nil {
return 0, err
}
return 12345, nil
}
func (s *testSegment) LastIndex() uint64 {
state := s.loadState()
if state.closed {
panic("lastIndex on closed segment")
}
if state.logs.Len() == 0 {
return 0
}
it := state.logs.Iterator()
it.Last()
_, log, _ := it.Next()
return log.Index
}
func (s *testSegment) closed() bool {
state := s.loadState()
return state.closed
}
func (s *testSegment) info() types.SegmentInfo {
state := s.loadState()
return state.info
}
func (s *testSegment) numLogs() int {
state := s.loadState()
return state.logs.Len()
}
func (s *testSegment) mutate(tx func(newState *testSegmentState) error) error {
s.writeLock.Lock()
defer s.writeLock.Unlock()
newState := s.loadState()
err := tx(&newState)
if err != nil {
return err
}
s.s.Store(newState)
return nil
}