forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_listener_test.go
266 lines (237 loc) · 6.08 KB
/
event_listener_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
// Copyright 2018 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"
"io"
"os"
"strings"
"sync"
"testing"
"github.com/petermattis/pebble/internal/base"
"github.com/petermattis/pebble/internal/datadriven"
"github.com/petermattis/pebble/sstable"
"github.com/petermattis/pebble/vfs"
"github.com/stretchr/testify/require"
)
type syncedBuffer struct {
mu sync.Mutex
buf bytes.Buffer
}
func (b *syncedBuffer) Reset() {
b.mu.Lock()
defer b.mu.Unlock()
b.buf.Reset()
}
func (b *syncedBuffer) Write(p []byte) (n int, err error) {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.Write(p)
}
func (b *syncedBuffer) Infof(format string, args ...interface{}) {
s := fmt.Sprintf(format, args...)
b.mu.Lock()
defer b.mu.Unlock()
b.buf.Write([]byte(s))
if n := len(s); n == 0 || s[n-1] != '\n' {
b.buf.Write([]byte("\n"))
}
}
func (b *syncedBuffer) Fatalf(format string, args ...interface{}) {
panic(fmt.Sprintf(format, args...))
}
func (b *syncedBuffer) String() string {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.String()
}
type loggingFS struct {
vfs.FS
w io.Writer
}
func (fs loggingFS) Create(name string) (vfs.File, error) {
fmt.Fprintf(fs.w, "create: %s\n", name)
f, err := fs.FS.Create(name)
if err != nil {
return nil, err
}
return loggingFile{f, name, fs.w}, nil
}
func (fs loggingFS) Link(oldname, newname string) error {
fmt.Fprintf(fs.w, "link: %s -> %s\n", oldname, newname)
return fs.FS.Link(oldname, newname)
}
func (fs loggingFS) OpenDir(name string) (vfs.File, error) {
fmt.Fprintf(fs.w, "open-dir: %s\n", name)
f, err := fs.FS.OpenDir(name)
if err != nil {
return nil, err
}
return loggingFile{f, name, fs.w}, nil
}
func (fs loggingFS) Rename(oldname, newname string) error {
fmt.Fprintf(fs.w, "rename: %s -> %s\n", oldname, newname)
return fs.FS.Rename(oldname, newname)
}
func (fs loggingFS) MkdirAll(dir string, perm os.FileMode) error {
fmt.Fprintf(fs.w, "mkdir-all: %s %#o\n", dir, perm)
return fs.FS.MkdirAll(dir, perm)
}
type loggingFile struct {
vfs.File
name string
w io.Writer
}
func (f loggingFile) Sync() error {
fmt.Fprintf(f.w, "sync: %s\n", f.name)
return f.File.Sync()
}
// Verify event listener actions, as well as expected filesystem operations.
func TestEventListener(t *testing.T) {
var d *DB
var buf syncedBuffer
mem := vfs.NewMem()
err := mem.MkdirAll("ext", 0755)
if err != nil {
t.Fatal(err)
}
datadriven.RunTest(t, "testdata/event_listener", func(td *datadriven.TestData) string {
switch td.Cmd {
case "open":
buf.Reset()
var err error
d, err = Open("db", &Options{
FS: loggingFS{mem, &buf},
EventListener: MakeLoggingEventListener(&buf),
MaxManifestFileSize: 1,
WALDir: "wal",
})
if err != nil {
return err.Error()
}
return buf.String()
case "flush":
buf.Reset()
if err := d.Set([]byte("a"), nil, nil); err != nil {
return err.Error()
}
if err := d.Flush(); err != nil {
return err.Error()
}
return buf.String()
case "compact":
buf.Reset()
if err := d.Set([]byte("a"), nil, nil); err != nil {
return err.Error()
}
if err := d.Compact([]byte("a"), []byte("b")); err != nil {
return err.Error()
}
return buf.String()
case "ingest":
buf.Reset()
f, err := mem.Create("ext/0")
if err != nil {
return err.Error()
}
w := sstable.NewWriter(f, nil, LevelOptions{})
if err := w.Add(base.MakeInternalKey([]byte("a"), 0, InternalKeyKindSet), nil); err != nil {
return err.Error()
}
if err := w.Close(); err != nil {
return err.Error()
}
if err := d.Ingest([]string{"ext/0"}); err != nil {
return err.Error()
}
if err := mem.Remove("ext/0"); err != nil {
return err.Error()
}
return buf.String()
case "metrics":
return d.Metrics().String()
default:
return fmt.Sprintf("unknown command: %s", td.Cmd)
}
})
}
func TestWriteStallEvents(t *testing.T) {
const flushCount = 10
const writeStallEnd = "write stall ending"
testCases := []struct {
delayFlush bool
expected string
}{
{true, "memtable count limit reached"},
{false, "L0 file count limit exceeded"},
}
for _, c := range testCases {
t.Run("", func(t *testing.T) {
stallEnded := make(chan struct{}, 1)
createReleased := make(chan struct{}, flushCount)
var buf syncedBuffer
var delayOnce sync.Once
listener := EventListener{
TableCreated: func(info TableCreateInfo) {
if c.delayFlush == (info.Reason == "flushing") {
delayOnce.Do(func() {
<-createReleased
})
}
},
WriteStallBegin: func(info WriteStallBeginInfo) {
fmt.Fprintln(&buf, info.String())
createReleased <- struct{}{}
},
WriteStallEnd: func() {
fmt.Fprintln(&buf, writeStallEnd)
select {
case stallEnded <- struct{}{}:
default:
}
},
}
d, err := Open("db", &Options{
EventListener: listener,
FS: vfs.NewMem(),
MemTableStopWritesThreshold: 2,
L0CompactionThreshold: 2,
L0StopWritesThreshold: 2,
})
if err != nil {
t.Fatal(err)
}
defer d.Close()
for i := 0; i < flushCount; i++ {
if err := d.Set([]byte("a"), nil, NoSync); err != nil {
t.Fatal(err)
}
ch, err := d.AsyncFlush()
if err != nil {
t.Fatal(err)
}
// If we're delaying the flush (because we're testing for memtable
// write stalls), we can't wait for the flush to finish as doing so
// would deadlock. If we're not delaying the flush (because we're
// testing for L0 write stals), we wait for the flush to finish so we
// don't create too many memtables which would trigger a memtable write
// stall.
if !c.delayFlush {
<-ch
}
if strings.Contains(buf.String(), c.expected) {
break
}
}
<-stallEnded
events := buf.String()
require.Contains(t, events, c.expected)
require.Contains(t, events, writeStallEnd)
if testing.Verbose() {
t.Logf("\n%s", events)
}
})
}
}