forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_recycler_test.go
134 lines (110 loc) · 3.45 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
// 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/petermattis/pebble/vfs"
"github.com/stretchr/testify/require"
)
func (r *logRecycler) logNums() []uint64 {
r.mu.Lock()
defer r.mu.Unlock()
return append([]uint64(nil), r.mu.logNums...)
}
func (r *logRecycler) maxLogNum() uint64 {
r.mu.Lock()
defer r.mu.Unlock()
return r.mu.maxLogNum
}
func TestLogRecycler(t *testing.T) {
r := logRecycler{limit: 3}
// Logs are recycled up to the limit.
require.True(t, r.add(1))
require.EqualValues(t, []uint64{1}, r.logNums())
require.EqualValues(t, 1, r.maxLogNum())
require.EqualValues(t, 1, r.peek())
require.True(t, r.add(2))
require.EqualValues(t, []uint64{1, 2}, r.logNums())
require.EqualValues(t, 2, r.maxLogNum())
require.True(t, r.add(3))
require.EqualValues(t, []uint64{1, 2, 3}, r.logNums())
require.EqualValues(t, 3, r.maxLogNum())
// Trying to add a file past the limit fails.
require.False(t, r.add(4))
require.EqualValues(t, []uint64{1, 2, 3}, r.logNums())
require.EqualValues(t, 4, r.maxLogNum())
// Trying to add a previously recycled file returns success, but the internal
// state is unchanged.
require.True(t, r.add(1))
require.EqualValues(t, []uint64{1, 2, 3}, r.logNums())
require.EqualValues(t, 4, r.maxLogNum())
// An error is returned if we try to pop an element other than the first.
require.Regexp(t, `invalid 2 vs \[1 2 3\]`, r.pop(2))
require.NoError(t, r.pop(1))
require.EqualValues(t, []uint64{2, 3}, r.logNums())
// Log number 4 was already considered, so it won't be recycled.
require.True(t, r.add(4))
require.EqualValues(t, []uint64{2, 3}, r.logNums())
require.True(t, r.add(5))
require.EqualValues(t, []uint64{2, 3, 5}, r.logNums())
require.EqualValues(t, 5, r.maxLogNum())
require.NoError(t, r.pop(2))
require.EqualValues(t, []uint64{3, 5}, r.logNums())
require.NoError(t, r.pop(3))
require.EqualValues(t, []uint64{5}, r.logNums())
require.NoError(t, r.pop(5))
require.EqualValues(t, []uint64(nil), r.logNums())
require.Regexp(t, `empty`, r.pop(6))
}
func TestRecycleLogs(t *testing.T) {
mem := vfs.NewMem()
d, err := Open("", &Options{
FS: mem,
})
if err != nil {
t.Fatal(err)
}
logNum := func() uint64 {
d.mu.Lock()
defer d.mu.Unlock()
return d.mu.log.queue[len(d.mu.log.queue)-1]
}
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, []uint64(nil), d.logRecycler.logNums())
curLog := logNum()
if err := d.Flush(); err != nil {
t.Fatal(err)
}
require.EqualValues(t, []uint64{curLog}, d.logRecycler.logNums())
curLog = logNum()
if err := d.Flush(); err != nil {
t.Fatal(err)
}
require.EqualValues(t, []uint64{curLog}, d.logRecycler.logNums())
if err := d.Close(); err != nil {
t.Fatal(err)
}
d, err = Open("", &Options{
FS: mem,
})
if err != nil {
t.Fatal(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 := d.logRecycler.count(); n != int(metrics.WAL.ObsoleteFiles) {
t.Fatalf("expected %d obsolete WAL files, but found %d", n, metrics.WAL.ObsoleteFiles)
}
if err := d.Close(); err != nil {
t.Fatal(err)
}
}