forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
413 lines (378 loc) · 10.8 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// 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"
"fmt"
"math"
"strings"
"sync/atomic"
"testing"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble/vfs"
"github.com/cockroachdb/pebble/vfs/errorfs"
"github.com/stretchr/testify/require"
)
type panicLogger struct{}
func (l panicLogger) Infof(format string, args ...interface{}) {}
func (l panicLogger) Errorf(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 is the index of the byte which we will corrupt.
index atomic.Int32
bytesRead atomic.Int32
}
func (fs *corruptFS) maybeCorrupt(n int32, p []byte) {
newBytesRead := fs.bytesRead.Add(n)
pIdx := newBytesRead - 1 - fs.index.Load()
if pIdx >= 0 && pIdx < n {
p[pIdx]++
}
}
func (fs *corruptFS) maybeCorruptAt(n int32, p []byte, offset int64) {
pIdx := fs.index.Load() - int32(offset)
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.maybeCorruptAt(int32(n), p, off)
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().String()
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"), false); 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.ErrInjected.If(errorfs.OnIndex(i)))
err := run(fs)
if err == nil {
t.Logf("success %d\n", i)
break
}
errMsg := err.Error()
if !strings.Contains(errMsg, "injected error") {
t.Fatalf("unexpected errors: %v", err)
}
errorCounts[errMsg]++
}
expectedErrors := []string{
"fatal: MANIFEST flush failed: injected error",
"fatal: MANIFEST sync failed: injected error",
"fatal: MANIFEST set current 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(formatVersion FormatMajorVersion, index int32) (err error) {
// Perform setup with error injection disabled as it involves writes/background ops.
ii := errorfs.OnIndex(-1)
fs := errorfs.Wrap(vfs.NewMem(), errorfs.ErrInjected.If(ii))
opts := &Options{
DisableTableStats: true,
FS: fs,
Logger: panicLogger{},
FormatMajorVersion: formatVersion,
}
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, false))
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#13,SET-a2#inf,RANGEDEL]
6:
000005:[a1#10,SET-a2#11,SET]
`, d, t)
// Now perform foreground ops with error injection enabled.
ii.Store(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 {
d = 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 := ii.Load(); 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
}
versions := []FormatMajorVersion{FormatMinSupported, internalFormatNewest}
for _, version := range versions {
t.Run(fmt.Sprintf("version-%s", version), func(t *testing.T) {
for i := int32(0); ; i++ {
err := run(version, 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(formatVersion FormatMajorVersion, index int32) (err error) {
// Perform setup with corruption injection disabled as it involves writes/background ops.
fs := &corruptFS{
FS: vfs.NewMem(),
}
fs.index.Store(-1)
opts := &Options{
DisableTableStats: true,
FS: fs,
Logger: panicLogger{},
FormatMajorVersion: formatVersion,
}
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, false))
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#13,SET-a2#inf,RANGEDEL]
6:
000005:[a1#10,SET-a2#11,SET]
`, d, t)
// Now perform foreground ops with corruption injection enabled.
fs.index.Store(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 bytesRead := fs.bytesRead.Load(); bytesRead > index {
t.Errorf("corruption error injected at index %d went unreported", index)
}
if numFound != 2 {
t.Fatalf("expected 2 values; found %d", numFound)
}
return nil
}
versions := []FormatMajorVersion{FormatMinSupported, internalFormatNewest}
for _, version := range versions {
t.Run(fmt.Sprintf("version-%s", version), func(t *testing.T) {
for i := int32(0); ; i++ {
err := run(version, i)
if err == nil {
t.Logf("no failures reported at index %d", i)
break
}
}
})
}
}
func TestDBWALRotationCrash(t *testing.T) {
memfs := vfs.NewStrictMem()
var index atomic.Int32
inj := errorfs.InjectorFunc(func(op errorfs.Op) error {
if op.Kind.ReadOrWrite() == errorfs.OpIsWrite && index.Add(-1) == -1 {
memfs.SetIgnoreSyncs(true)
}
return nil
})
triggered := func() bool { return index.Load() < 0 }
run := func(fs *errorfs.FS, k int32) (err error) {
opts := &Options{
DisableTableStats: true,
FS: fs,
Logger: panicLogger{},
MemTableSize: 2048,
}
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.
index.Store(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.
index.Store(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)
index.Store(math.MaxInt32)
require.NoError(t, run(fs, k))
}
}