forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_recycler_test.go
135 lines (110 loc) · 4.33 KB
/
log_recycler_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
// Copyright 2019 The LevelDB-Go and Pebble Authors. All rights reserved. Use
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package pebble
import (
"testing"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/vfs"
"github.com/stretchr/testify/require"
)
func (r *logRecycler) logNums() []FileNum {
r.mu.Lock()
defer r.mu.Unlock()
return fileInfoNums(r.mu.logs)
}
func (r *logRecycler) maxLogNum() FileNum {
r.mu.Lock()
defer r.mu.Unlock()
return r.mu.maxLogNum
}
func TestLogRecycler(t *testing.T) {
r := logRecycler{limit: 3, minRecycleLogNum: 4}
// Logs below the min-recycle number are not recycled.
require.False(t, r.add(fileInfo{base.FileNum(1).DiskFileNum(), 0}))
require.False(t, r.add(fileInfo{base.FileNum(2).DiskFileNum(), 0}))
require.False(t, r.add(fileInfo{base.FileNum(3).DiskFileNum(), 0}))
// Logs are recycled up to the limit.
require.True(t, r.add(fileInfo{base.FileNum(4).DiskFileNum(), 0}))
require.EqualValues(t, []FileNum{4}, r.logNums())
require.EqualValues(t, 4, r.maxLogNum())
fi, ok := r.peek()
require.True(t, ok)
require.EqualValues(t, uint64(4), uint64(fi.fileNum.FileNum()))
require.True(t, r.add(fileInfo{base.FileNum(5).DiskFileNum(), 0}))
require.EqualValues(t, []FileNum{4, 5}, r.logNums())
require.EqualValues(t, 5, r.maxLogNum())
require.True(t, r.add(fileInfo{base.FileNum(6).DiskFileNum(), 0}))
require.EqualValues(t, []FileNum{4, 5, 6}, r.logNums())
require.EqualValues(t, 6, r.maxLogNum())
// Trying to add a file past the limit fails.
require.False(t, r.add(fileInfo{base.FileNum(7).DiskFileNum(), 0}))
require.EqualValues(t, []FileNum{4, 5, 6}, r.logNums())
require.EqualValues(t, 7, r.maxLogNum())
// Trying to add a previously recycled file returns success, but the internal
// state is unchanged.
require.True(t, r.add(fileInfo{base.FileNum(4).DiskFileNum(), 0}))
require.EqualValues(t, []FileNum{4, 5, 6}, r.logNums())
require.EqualValues(t, 7, r.maxLogNum())
// An error is returned if we try to pop an element other than the first.
require.Regexp(t, `invalid 5 vs \[4 5 6\]`, r.pop(5))
require.NoError(t, r.pop(4))
require.EqualValues(t, []FileNum{5, 6}, r.logNums())
// Log number 7 was already considered, so it won't be recycled.
require.True(t, r.add(fileInfo{base.FileNum(7).DiskFileNum(), 0}))
require.EqualValues(t, []FileNum{5, 6}, r.logNums())
require.True(t, r.add(fileInfo{base.FileNum(8).DiskFileNum(), 0}))
require.EqualValues(t, []FileNum{5, 6, 8}, r.logNums())
require.EqualValues(t, 8, r.maxLogNum())
require.NoError(t, r.pop(5))
require.EqualValues(t, []FileNum{6, 8}, r.logNums())
require.NoError(t, r.pop(6))
require.EqualValues(t, []FileNum{8}, r.logNums())
require.NoError(t, r.pop(8))
require.EqualValues(t, []FileNum(nil), r.logNums())
require.Regexp(t, `empty`, r.pop(9))
}
func TestRecycleLogs(t *testing.T) {
mem := vfs.NewMem()
d, err := Open("", &Options{
FS: mem,
})
require.NoError(t, err)
logNum := func() FileNum {
d.mu.Lock()
defer d.mu.Unlock()
return d.mu.log.queue[len(d.mu.log.queue)-1].fileNum.FileNum()
}
logCount := func() int {
d.mu.Lock()
defer d.mu.Unlock()
return len(d.mu.log.queue)
}
// Flush the memtable a few times, forcing rotation of the WAL. We should see
// the recycled logs change as expected.
require.EqualValues(t, []FileNum(nil), d.logRecycler.logNums())
curLog := logNum()
require.NoError(t, d.Flush())
require.EqualValues(t, []FileNum{curLog}, d.logRecycler.logNums())
curLog = logNum()
require.NoError(t, d.Flush())
require.EqualValues(t, []FileNum{curLog}, d.logRecycler.logNums())
require.NoError(t, d.Close())
d, err = Open("", &Options{
FS: mem,
})
require.NoError(t, err)
metrics := d.Metrics()
if n := logCount(); n != int(metrics.WAL.Files) {
t.Fatalf("expected %d WAL files, but found %d", n, metrics.WAL.Files)
}
if n, sz := d.logRecycler.stats(); n != int(metrics.WAL.ObsoleteFiles) {
t.Fatalf("expected %d obsolete WAL files, but found %d", n, metrics.WAL.ObsoleteFiles)
} else if sz != metrics.WAL.ObsoletePhysicalSize {
t.Fatalf("expected %d obsolete physical WAL size, but found %d", sz, metrics.WAL.ObsoletePhysicalSize)
}
if recycled := d.logRecycler.logNums(); len(recycled) != 0 {
t.Fatalf("expected no recycled WAL files after recovery, but found %d", recycled)
}
require.NoError(t, d.Close())
}