forked from golang/leveldb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_cache_test.go
296 lines (268 loc) · 7.44 KB
/
table_cache_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
// Copyright 2013 The LevelDB-Go 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 leveldb
import (
"bytes"
"fmt"
"math/rand"
"sync"
"testing"
"time"
"github.com/golang/leveldb/db"
"github.com/golang/leveldb/memfs"
"github.com/golang/leveldb/table"
)
type tableCacheTestFile struct {
db.File
fs *tableCacheTestFS
name string
}
func (f *tableCacheTestFile) Close() error {
f.fs.mu.Lock()
if f.fs.closeCounts != nil {
f.fs.closeCounts[f.name]++
}
f.fs.mu.Unlock()
return f.File.Close()
}
type tableCacheTestFS struct {
db.FileSystem
mu sync.Mutex
openCounts map[string]int
closeCounts map[string]int
}
func (fs *tableCacheTestFS) Open(name string) (db.File, error) {
fs.mu.Lock()
if fs.openCounts != nil {
fs.openCounts[name]++
}
fs.mu.Unlock()
f, err := fs.FileSystem.Open(name)
if err != nil {
return nil, err
}
return &tableCacheTestFile{f, fs, name}, nil
}
func (fs *tableCacheTestFS) validate(t *testing.T, c *tableCache, f func(i, gotO, gotC int) error) {
if err := fs.validateOpenTables(f); err != nil {
t.Error(err)
return
}
c.Close()
if err := fs.validateNoneStillOpen(); err != nil {
t.Error(err)
return
}
}
// validateOpenTables validates that no tables in the cache are open twice, and
// the number still open is no greater than tableCacheTestCacheSize.
func (fs *tableCacheTestFS) validateOpenTables(f func(i, gotO, gotC int) error) error {
// try backs off to let any clean-up goroutines do their work.
return try(100*time.Microsecond, 20*time.Second, func() error {
fs.mu.Lock()
defer fs.mu.Unlock()
numStillOpen := 0
for i := 0; i < tableCacheTestNumTables; i++ {
filename := dbFilename("", fileTypeTable, uint64(i))
gotO, gotC := fs.openCounts[filename], fs.closeCounts[filename]
if gotO > gotC {
numStillOpen++
}
if gotC != gotO && gotC != gotO-1 {
return fmt.Errorf("i=%d: table closed too many or too few times: opened %d times, closed %d times",
i, gotO, gotC)
}
if f != nil {
if err := f(i, gotO, gotC); err != nil {
return err
}
}
}
if numStillOpen > tableCacheTestCacheSize {
return fmt.Errorf("numStillOpen is %d, want <= %d", numStillOpen, tableCacheTestCacheSize)
}
return nil
})
}
// validateNoneStillOpen validates that no tables in the cache are open.
func (fs *tableCacheTestFS) validateNoneStillOpen() error {
// try backs off to let any clean-up goroutines do their work.
return try(100*time.Microsecond, 20*time.Second, func() error {
fs.mu.Lock()
defer fs.mu.Unlock()
for i := 0; i < tableCacheTestNumTables; i++ {
filename := dbFilename("", fileTypeTable, uint64(i))
gotO, gotC := fs.openCounts[filename], fs.closeCounts[filename]
if gotO != gotC {
return fmt.Errorf("i=%d: opened %d times, closed %d times", i, gotO, gotC)
}
}
return nil
})
}
const (
tableCacheTestNumTables = 300
tableCacheTestCacheSize = 100
)
func newTableCache() (*tableCache, *tableCacheTestFS, error) {
xxx := bytes.Repeat([]byte("x"), tableCacheTestNumTables)
fs := &tableCacheTestFS{
FileSystem: memfs.New(),
}
for i := 0; i < tableCacheTestNumTables; i++ {
f, err := fs.Create(dbFilename("", fileTypeTable, uint64(i)))
if err != nil {
return nil, nil, fmt.Errorf("fs.Create: %v", err)
}
tw := table.NewWriter(f, &db.Options{
Comparer: internalKeyComparer{userCmp: db.DefaultComparer},
})
if err := tw.Set(makeIkey(fmt.Sprintf("k.SET.%d", i)), xxx[:i], nil); err != nil {
return nil, nil, fmt.Errorf("tw.Set: %v", err)
}
if err := tw.Close(); err != nil {
return nil, nil, fmt.Errorf("tw.Close: %v", err)
}
}
fs.mu.Lock()
fs.openCounts = map[string]int{}
fs.closeCounts = map[string]int{}
fs.mu.Unlock()
c := &tableCache{}
c.init("", fs, nil, tableCacheTestCacheSize)
return c, fs, nil
}
func testTableCacheRandomAccess(t *testing.T, concurrent bool) {
const N = 2000
c, fs, err := newTableCache()
if err != nil {
t.Fatal(err)
}
rngMu := sync.Mutex{}
rng := rand.New(rand.NewSource(1))
errc := make(chan error, N)
for i := 0; i < N; i++ {
go func(i int) {
rngMu.Lock()
fileNum, sleepTime := rng.Intn(tableCacheTestNumTables), rng.Intn(1000)
rngMu.Unlock()
iter, err := c.find(uint64(fileNum), []byte("k"))
if err != nil {
errc <- fmt.Errorf("i=%d, fileNum=%d: find: %v", i, fileNum, err)
return
}
if concurrent {
time.Sleep(time.Duration(sleepTime) * time.Microsecond)
}
if !iter.Next() {
errc <- fmt.Errorf("i=%d, fileNum=%d: next.0: got false, want true", i, fileNum)
return
}
if got := len(iter.Value()); got != fileNum {
errc <- fmt.Errorf("i=%d, fileNum=%d: value: got %d bytes, want %d", i, fileNum, got, fileNum)
return
}
if iter.Next() {
errc <- fmt.Errorf("i=%d, fileNum=%d: next.1: got true, want false", i, fileNum)
return
}
if err := iter.Close(); err != nil {
errc <- fmt.Errorf("i=%d, fileNum=%d: close: %v", i, fileNum, err)
return
}
errc <- nil
}(i)
if !concurrent {
if err := <-errc; err != nil {
t.Fatal(err)
}
}
}
if concurrent {
for i := 0; i < N; i++ {
if err := <-errc; err != nil {
t.Fatal(err)
}
}
}
fs.validate(t, c, nil)
}
func TestTableCacheRandomAccessSequential(t *testing.T) { testTableCacheRandomAccess(t, false) }
func TestTableCacheRandomAccessConcurrent(t *testing.T) { testTableCacheRandomAccess(t, true) }
func TestTableCacheFrequentlyUsed(t *testing.T) {
const (
N = 1000
pinned0 = 7
pinned1 = 11
)
c, fs, err := newTableCache()
if err != nil {
t.Fatal(err)
}
for i := 0; i < N; i++ {
for _, j := range [...]int{pinned0, i % tableCacheTestNumTables, pinned1} {
iter, err := c.find(uint64(j), nil)
if err != nil {
t.Fatalf("i=%d, j=%d: find: %v", i, j, err)
}
if err := iter.Close(); err != nil {
t.Fatalf("i=%d, j=%d: close: %v", i, j, err)
}
}
}
fs.validate(t, c, func(i, gotO, gotC int) error {
if i == pinned0 || i == pinned1 {
if gotO != 1 || gotC != 0 {
return fmt.Errorf("i=%d: pinned table: got %d, %d, want %d, %d", i, gotO, gotC, 1, 0)
}
} else if gotO == 1 {
return fmt.Errorf("i=%d: table only opened once", i)
}
return nil
})
}
func TestTableCacheEvictions(t *testing.T) {
const (
N = 1000
lo, hi = 10, 20
)
c, fs, err := newTableCache()
if err != nil {
t.Fatal(err)
}
rng := rand.New(rand.NewSource(2))
for i := 0; i < N; i++ {
j := rng.Intn(tableCacheTestNumTables)
iter, err := c.find(uint64(j), nil)
if err != nil {
t.Fatalf("i=%d, j=%d: find: %v", i, j, err)
}
if err := iter.Close(); err != nil {
t.Fatalf("i=%d, j=%d: close: %v", i, j, err)
}
c.evict(uint64(lo + rng.Intn(hi-lo)))
}
sumEvicted, nEvicted := 0, 0
sumSafe, nSafe := 0, 0
fs.validate(t, c, func(i, gotO, gotC int) error {
if lo <= i && i < hi {
sumEvicted += gotO
nEvicted++
} else {
sumSafe += gotO
nSafe++
}
return nil
})
fEvicted := float64(sumEvicted) / float64(nEvicted)
fSafe := float64(sumSafe) / float64(nSafe)
// The magic 1.25 number isn't derived from formal modeling. It's just a guess. For
// (lo, hi, tableCacheTestCacheSize, tableCacheTestNumTables) = (10, 20, 100, 300),
// the ratio seems to converge on roughly 1.5 for large N, compared to 1.0 if we do
// not evict any cache entries.
if ratio := fEvicted / fSafe; ratio < 1.25 {
t.Errorf("evicted tables were opened %.3f times on average, safe tables %.3f, ratio %.3f < 1.250",
fEvicted, fSafe, ratio)
}
}