forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 1
/
error_test.go
397 lines (362 loc) · 10 KB
/
error_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
// 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 (
"bytes"
"math"
"strings"
"sync/atomic"
"testing"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/internal/errorfs"
"github.com/cockroachdb/pebble/vfs"
"github.com/stretchr/testify/require"
)
type panicLogger struct{}
func (l panicLogger) Infof(format string, args ...interface{}) {
}
func (l panicLogger) Fatalf(format string, args ...interface{}) {
panic(errors.Errorf("fatal: "+format, args...))
}
// corruptFS injects a corruption in the `index`th byte read.
type corruptFS struct {
vfs.FS
index int32
bytesRead int32
}
func newCorruptFS(index int32) *corruptFS {
return &corruptFS{
FS: vfs.NewMem(),
index: index,
bytesRead: 0,
}
}
func (fs corruptFS) maybeCorrupt(n int32, p []byte) {
newBytesRead := atomic.AddInt32(&fs.bytesRead, n)
pIdx := newBytesRead - 1 - fs.index
if pIdx >= 0 && pIdx < n {
p[pIdx]++
}
}
func (fs *corruptFS) Open(name string, opts ...vfs.OpenOption) (vfs.File, error) {
f, err := fs.FS.Open(name)
if err != nil {
return nil, err
}
cf := corruptFile{f, fs}
for _, opt := range opts {
opt.Apply(cf)
}
return cf, nil
}
type corruptFile struct {
vfs.File
fs *corruptFS
}
func (f corruptFile) Read(p []byte) (int, error) {
n, err := f.File.Read(p)
f.fs.maybeCorrupt(int32(n), p)
return n, err
}
func (f corruptFile) ReadAt(p []byte, off int64) (int, error) {
n, err := f.File.ReadAt(p, off)
f.fs.maybeCorrupt(int32(n), p)
return n, err
}
func expectLSM(expected string, d *DB, t *testing.T) {
t.Helper()
expected = strings.TrimSpace(expected)
d.mu.Lock()
actual := d.mu.versions.currentVersion().DebugString(base.DefaultFormatter)
d.mu.Unlock()
actual = strings.TrimSpace(actual)
if expected != actual {
t.Fatalf("expected\n%s\nbut found\n%s", expected, actual)
}
}
// TestErrors repeatedly runs a short sequence of operations, injecting FS
// errors at different points, until success is achieved.
func TestErrors(t *testing.T) {
run := func(fs *errorfs.FS) (err error) {
defer func() {
if r := recover(); r != nil {
if e, ok := r.(error); ok {
err = e
} else {
t.Fatal(r)
}
}
}()
d, err := Open("", &Options{
FS: fs,
Logger: panicLogger{},
})
if err != nil {
return err
}
key := []byte("a")
value := []byte("b")
if err := d.Set(key, value, nil); err != nil {
return err
}
if err := d.Flush(); err != nil {
return err
}
if err := d.Compact(nil, []byte("\xff")); err != nil {
return err
}
iter := d.NewIter(nil)
for valid := iter.First(); valid; valid = iter.Next() {
}
if err := iter.Close(); err != nil {
return err
}
return d.Close()
}
errorCounts := make(map[string]int)
for i := int32(0); ; i++ {
fs := errorfs.Wrap(vfs.NewMem(), errorfs.OnIndex(i))
err := run(fs)
if err == nil {
t.Logf("success %d\n", i)
break
}
errorCounts[err.Error()]++
}
expectedErrors := []string{
"fatal: MANIFEST flush failed: injected error",
"fatal: MANIFEST sync failed: injected error",
"fatal: MANIFEST set current failed: injected error",
"fatal: MANIFEST dirsync failed: injected error",
}
for _, expected := range expectedErrors {
if errorCounts[expected] == 0 {
t.Errorf("expected error %q did not occur", expected)
}
}
}
// TestRequireReadError injects FS errors into read operations at successively later
// points until all operations can complete. It requires an operation fails any time
// an error was injected. This differs from the TestErrors case above as that one
// cannot require operations fail since it involves flush/compaction, which retry
// internally and succeed following an injected error.
func TestRequireReadError(t *testing.T) {
run := func(index int32) (err error) {
// Perform setup with error injection disabled as it involves writes/background ops.
inj := errorfs.OnIndex(-1)
fs := errorfs.Wrap(vfs.NewMem(), inj)
opts := &Options{
FS: fs,
Logger: panicLogger{},
}
opts.private.disableTableStats = true
d, err := Open("", opts)
require.NoError(t, err)
defer func() {
if d != nil {
require.NoError(t, d.Close())
}
}()
key1 := []byte("a1")
key2 := []byte("a2")
value := []byte("b")
require.NoError(t, d.Set(key1, value, nil))
require.NoError(t, d.Set(key2, value, nil))
require.NoError(t, d.Flush())
require.NoError(t, d.Compact(key1, key2))
require.NoError(t, d.DeleteRange(key1, key2, nil))
require.NoError(t, d.Set(key1, value, nil))
require.NoError(t, d.Flush())
expectLSM(`
0.0:
000007:[a1#4,SET-a2#72057594037927935,RANGEDEL]
6:
000005:[a1#1,SET-a2#2,SET]
`, d, t)
// Now perform foreground ops with error injection enabled.
inj.SetIndex(index)
iter := d.NewIter(nil)
if err := iter.Error(); err != nil {
return err
}
numFound := 0
expectedKeys := [][]byte{key1, key2}
for valid := iter.First(); valid; valid = iter.Next() {
if !bytes.Equal(iter.Key(), expectedKeys[numFound]) {
t.Fatalf("expected key %v; found %v", expectedKeys[numFound], iter.Key())
}
if !bytes.Equal(iter.Value(), value) {
t.Fatalf("expected value %v; found %v", value, iter.Value())
}
numFound++
}
if err := iter.Close(); err != nil {
return err
}
if err := d.Close(); err != nil {
return err
}
d = nil
// Reaching here implies all read operations succeeded. This
// should only happen when we reached a large enough index at
// which `errorfs.FS` did not return any error.
if i := inj.Index(); i < 0 {
t.Errorf("FS error injected %d ops ago went unreported", -i)
}
if numFound != 2 {
t.Fatalf("expected 2 values; found %d", numFound)
}
return nil
}
for i := int32(0); ; i++ {
err := run(i)
if err == nil {
t.Logf("no failures reported at index %d", i)
break
}
}
}
// TestCorruptReadError verifies that reads to a corrupted file detect the
// corruption and return an error. In this case the filesystem reads return
// successful status but the data they return is corrupt.
func TestCorruptReadError(t *testing.T) {
run := func(index int32) (err error) {
// Perform setup with corruption injection disabled as it involves writes/background ops.
fs := &corruptFS{
FS: vfs.NewMem(),
index: -1,
}
opts := &Options{
FS: fs,
Logger: panicLogger{},
}
opts.private.disableTableStats = true
d, err := Open("", opts)
if err != nil {
t.Fatalf("%v", err)
}
defer func() {
if d != nil {
require.NoError(t, d.Close())
}
}()
key1 := []byte("a1")
key2 := []byte("a2")
value := []byte("b")
require.NoError(t, d.Set(key1, value, nil))
require.NoError(t, d.Set(key2, value, nil))
require.NoError(t, d.Flush())
require.NoError(t, d.Compact(key1, key2))
require.NoError(t, d.DeleteRange(key1, key2, nil))
require.NoError(t, d.Set(key1, value, nil))
require.NoError(t, d.Flush())
expectLSM(`
0.0:
000007:[a1#4,SET-a2#72057594037927935,RANGEDEL]
6:
000005:[a1#1,SET-a2#2,SET]
`, d, t)
// Now perform foreground ops with corruption injection enabled.
atomic.StoreInt32(&fs.index, index)
iter := d.NewIter(nil)
if err := iter.Error(); err != nil {
return err
}
numFound := 0
expectedKeys := [][]byte{key1, key2}
for valid := iter.First(); valid; valid = iter.Next() {
if !bytes.Equal(iter.Key(), expectedKeys[numFound]) {
t.Fatalf("expected key %v; found %v", expectedKeys[numFound], iter.Key())
}
if !bytes.Equal(iter.Value(), value) {
t.Fatalf("expected value %v; found %v", value, iter.Value())
}
numFound++
}
if err := iter.Close(); err != nil {
return err
}
if err := d.Close(); err != nil {
return err
}
d = nil
// Reaching here implies all read operations succeeded. This
// should only happen when we reached a large enough index at
// which `corruptFS` did not inject any corruption.
if atomic.LoadInt32(&fs.bytesRead) > fs.index {
t.Errorf("corruption error injected at index %d went unreported", fs.index)
}
if numFound != 2 {
t.Fatalf("expected 2 values; found %d", numFound)
}
return nil
}
for i := int32(0); ; i++ {
err := run(i)
if err == nil {
t.Logf("no failures reported at index %d", i)
break
}
}
}
func TestDBWALRotationCrash(t *testing.T) {
memfs := vfs.NewStrictMem()
var index int32
inj := errorfs.InjectorFunc(func(op errorfs.Op) error {
if op == errorfs.OpWrite && atomic.AddInt32(&index, -1) == -1 {
memfs.SetIgnoreSyncs(true)
}
return nil
})
triggered := func() bool { return atomic.LoadInt32(&index) < 0 }
run := func(fs *errorfs.FS, k int32) (err error) {
opts := &Options{
FS: fs,
Logger: panicLogger{},
MemTableSize: 1024,
}
opts.private.disableTableStats = true
d, err := Open("", opts)
if err != nil || triggered() {
return err
}
// Write keys with the FS set up to simulate a crash by ignoring
// syncs on the k-th write operation.
atomic.StoreInt32(&index, k)
key := []byte("test")
for i := 0; i < 10; i++ {
v := []byte(strings.Repeat("b", i))
err = d.Set(key, v, nil)
if err != nil || triggered() {
break
}
}
err = firstError(err, d.Close())
return err
}
fs := errorfs.Wrap(memfs, inj)
for k := int32(0); ; k++ {
// Run, simulating a crash by ignoring syncs after the k-th write
// operation after Open.
atomic.StoreInt32(&index, math.MaxInt32)
err := run(fs, k)
if !triggered() {
// Stop when we reach a value of k greater than the number of
// write operations performed during `run`.
t.Logf("No crash at write operation %d\n", k)
if err != nil {
t.Fatalf("Filesystem did not 'crash', but error returned: %s", err)
}
break
}
t.Logf("Crashed at write operation % 2d, error: %v\n", k, err)
// Reset the filesystem to its state right before the simulated
// "crash", restore syncs, and run again without crashing.
memfs.ResetToSyncedState()
memfs.SetIgnoreSyncs(false)
atomic.StoreInt32(&index, math.MaxInt32)
require.NoError(t, run(fs, k))
}
}