-
Notifications
You must be signed in to change notification settings - Fork 8
/
wal_test.go
209 lines (176 loc) · 4.6 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
package wal
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/golang/snappy"
"github.com/oxtoacart/bpool"
"github.com/stretchr/testify/assert"
)
func TestFileNaming(t *testing.T) {
seq := newFileSequence()
filename := filepath.Join("folder", sequenceToFilename(seq))
assert.Equal(t, seq, filenameToSequence(filename))
filename = filename + compressedSuffix
assert.Equal(t, seq, filenameToSequence(filename))
}
func TestOffsetAfter(t *testing.T) {
assert.True(t, NewOffset(0, 1).After(nil))
assert.False(t, Offset(nil).After(NewOffset(0, 1)))
assert.True(t, NewOffset(1, 0).After(nil))
assert.False(t, Offset(nil).After(NewOffset(1, 0)))
assert.True(t, NewOffset(1, 50).After(NewOffset(1, 0)))
assert.False(t, NewOffset(1, 0).After(NewOffset(1, 50)))
assert.True(t, NewOffset(2, 0).After(NewOffset(1, 50)))
assert.False(t, NewOffset(1, 50).After(NewOffset(2, 0)))
assert.False(t, Offset(nil).After(Offset(nil)))
assert.False(t, NewOffset(1, 50).After(NewOffset(1, 50)))
}
func TestWAL(t *testing.T) {
origMaxSegmentSize := maxSegmentSize
defer func() {
maxSegmentSize = origMaxSegmentSize
}()
maxSegmentSize = 5
dir, err := ioutil.TempDir("", "waltest")
if !assert.NoError(t, err) {
return
}
// defer os.RemoveAll(dir)
wal, err := Open(&Opts{Dir: dir, SyncInterval: 1 * time.Millisecond, MaxMemoryBacklog: 1})
if !assert.NoError(t, err) {
return
}
defer wal.Close()
bufferPool := bpool.NewBytePool(1, 65536)
r, err := wal.NewReader("test", nil, bufferPool.Get)
if !assert.NoError(t, err) {
return
}
defer r.Close()
testReadWrite := func(val string) bool {
wal.log.Debugf("testReadWrite: %v", val)
readErr := wal.Write([]byte(val))
if !assert.NoError(t, readErr) {
return false
}
wal.log.Debug("Reading")
b, readErr := r.Read()
if !assert.NoError(t, readErr) {
return false
}
if !assert.Equal(t, val, string(b[:1])) {
return false
}
wal.log.Debug("Read")
return true
}
if !testReadWrite("1") {
return
}
if !testReadWrite("2") {
return
}
// Reopen WAL
wal.Close()
latest, lc, err := wal.Latest()
if !assert.NoError(t, err) {
return
}
if !assert.EqualValues(t, 9, lc.Position()) {
return
}
if !assert.Equal(t, "2", string(latest)) {
return
}
wal, err = Open(&Opts{Dir: dir, SyncInterval: 0})
if !assert.NoError(t, err) {
return
}
defer wal.Close()
r2, err := wal.NewReader("test", r.Offset(), bufferPool.Get)
if !assert.NoError(t, err) {
return
}
defer r2.Close()
// Problem is here
if !testReadWrite("3") {
return
}
// Compress item 1
err = wal.CompressBefore(r2.Offset())
if !assert.NoError(t, err) {
return
}
assertWALContents := func(entries []string) {
wal.log.Debugf("Asserting WAL contents: %v", entries)
// Read the full WAL again
r, err = wal.NewReader("test", nil, bufferPool.Get)
if !assert.NoError(t, err) {
return
}
defer r.Close()
for _, expected := range entries {
b, readErr := r.Read()
if !assert.NoError(t, readErr) {
return
}
if !assert.Equal(t, expected, string(b)) {
return
}
}
}
assertWALContents([]string{"1", "2", "3"})
// Corrupt the Snappy WAL file
files, _ := ioutil.ReadDir(dir)
for _, fi := range files {
name := filepath.Join(dir, fi.Name())
file, _ := os.OpenFile(name, os.O_RDWR, 0644)
if strings.HasSuffix(name, compressedSuffix) {
w := snappy.NewWriter(file)
lenBuf := make([]byte, 4)
encoding.PutUint32(lenBuf, 100)
_, err := w.Write(lenBuf)
if err != nil {
panic(err)
}
w.Flush()
file.Write([]byte("garbage"))
}
file.Close()
}
assertWALContents([]string{"3"})
// Reader opened at prior offset should only get "3"
wal.log.Debug(r2.Offset())
b, readErr := r2.Read()
if !assert.NoError(t, readErr) {
return
}
if !assert.Equal(t, "3", string(b)) {
return
}
err = wal.Write([]byte("data to force new WAL"))
if !assert.NoError(t, err) {
return
}
// Truncate as of known offset, should not delete any files
truncateErr := wal.TruncateBefore(r2.Offset())
testTruncate(t, wal, truncateErr, 2)
// Truncate as of now, which should remove old log segment
truncateErr = wal.TruncateBeforeTime(time.Now())
testTruncate(t, wal, truncateErr, 1)
// Truncate to size 1, which should remove remaining log segment
truncateErr = wal.TruncateToSize(1)
testTruncate(t, wal, truncateErr, 0)
}
func testTruncate(t *testing.T, wal *WAL, err error, expectedSegments int) {
if assert.NoError(t, err, "Should be able to truncate") {
segments, err := ioutil.ReadDir(wal.dir)
if assert.NoError(t, err, "Should be able to list segments") {
assert.Equal(t, expectedSegments, len(segments))
}
}
}