forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_stats_test.go
230 lines (210 loc) · 5.57 KB
/
table_stats_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
// Copyright 2020 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"
"strings"
"testing"
"github.com/cockroachdb/datadriven"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/internal/keyspan"
"github.com/cockroachdb/pebble/internal/rangekey"
"github.com/cockroachdb/pebble/internal/testkeys"
"github.com/cockroachdb/pebble/sstable"
"github.com/cockroachdb/pebble/vfs"
"github.com/stretchr/testify/require"
)
func TestTableStats(t *testing.T) {
fs := vfs.NewMem()
// loadedInfo is protected by d.mu.
var loadedInfo *TableStatsInfo
opts := &Options{
FS: fs,
EventListener: &EventListener{
TableStatsLoaded: func(info TableStatsInfo) {
loadedInfo = &info
},
},
}
opts.DisableAutomaticCompactions = true
opts.Comparer = testkeys.Comparer
opts.FormatMajorVersion = FormatRangeKeys
d, err := Open("", opts)
require.NoError(t, err)
defer func() {
if d != nil {
require.NoError(t, closeAllSnapshots(d))
require.NoError(t, d.Close())
}
}()
datadriven.RunTest(t, "testdata/table_stats", func(t *testing.T, td *datadriven.TestData) string {
switch td.Cmd {
case "disable":
d.mu.Lock()
d.opts.private.disableTableStats = true
d.mu.Unlock()
return ""
case "enable":
d.mu.Lock()
d.opts.private.disableTableStats = false
d.maybeCollectTableStatsLocked()
d.mu.Unlock()
return ""
case "define":
require.NoError(t, closeAllSnapshots(d))
require.NoError(t, d.Close())
loadedInfo = nil
d, err = runDBDefineCmd(td, opts)
if err != nil {
return err.Error()
}
d.mu.Lock()
s := d.mu.versions.currentVersion().String()
d.mu.Unlock()
return s
case "reopen":
require.NoError(t, d.Close())
loadedInfo = nil
// Open using existing file system.
d, err = Open("", opts)
require.NoError(t, err)
return ""
case "batch":
b := d.NewBatch()
if err := runBatchDefineCmd(td, b); err != nil {
return err.Error()
}
b.Commit(nil)
return ""
case "flush":
if err := d.Flush(); err != nil {
return err.Error()
}
d.mu.Lock()
s := d.mu.versions.currentVersion().String()
d.mu.Unlock()
return s
case "ingest":
if err = runBuildCmd(td, d, d.opts.FS); err != nil {
return err.Error()
}
if err = runIngestCmd(td, d, d.opts.FS); err != nil {
return err.Error()
}
d.mu.Lock()
s := d.mu.versions.currentVersion().String()
d.mu.Unlock()
return s
case "wait-pending-table-stats":
return runTableStatsCmd(td, d)
case "wait-loaded-initial":
d.mu.Lock()
for d.mu.tableStats.loading || !d.mu.tableStats.loadedInitial {
d.mu.tableStats.cond.Wait()
}
s := loadedInfo.String()
d.mu.Unlock()
return s
case "compact":
if err := runCompactCmd(td, d); err != nil {
return err.Error()
}
d.mu.Lock()
// Disable the "dynamic base level" code for this test.
d.mu.versions.picker.forceBaseLevel1()
s := d.mu.versions.currentVersion().String()
d.mu.Unlock()
return s
default:
return fmt.Sprintf("unknown command: %s", td.Cmd)
}
})
}
func TestTableRangeDeletionIter(t *testing.T) {
var m *fileMetadata
cmp := base.DefaultComparer.Compare
fs := vfs.NewMem()
datadriven.RunTest(t, "testdata/table_stats_deletion_iter", func(t *testing.T, td *datadriven.TestData) string {
switch cmd := td.Cmd; cmd {
case "build":
f, err := fs.Create("tmp.sst")
if err != nil {
return err.Error()
}
w := sstable.NewWriter(f, sstable.WriterOptions{
TableFormat: sstable.TableFormatMax,
})
m = &fileMetadata{}
for _, line := range strings.Split(td.Input, "\n") {
s := keyspan.ParseSpan(line)
// Range dels can be written sequentially. Range keys must be collected.
rKeySpan := &keyspan.Span{Start: s.Start, End: s.End}
for _, k := range s.Keys {
if rangekey.IsRangeKey(k.Kind()) {
rKeySpan.Keys = append(rKeySpan.Keys, k)
} else {
k := base.InternalKey{UserKey: s.Start, Trailer: k.Trailer}
if err = w.Add(k, s.End); err != nil {
return err.Error()
}
}
}
err = rangekey.Encode(rKeySpan, func(k base.InternalKey, v []byte) error {
return w.AddRangeKey(k, v)
})
if err != nil {
return err.Error()
}
}
if err = w.Close(); err != nil {
return err.Error()
}
meta, err := w.Metadata()
if err != nil {
return err.Error()
}
if meta.HasPointKeys {
m.ExtendPointKeyBounds(cmp, meta.SmallestPoint, meta.LargestPoint)
}
if meta.HasRangeDelKeys {
m.ExtendPointKeyBounds(cmp, meta.SmallestRangeDel, meta.LargestRangeDel)
}
if meta.HasRangeKeys {
m.ExtendRangeKeyBounds(cmp, meta.SmallestRangeKey, meta.LargestRangeKey)
}
return m.DebugString(base.DefaultFormatter, false /* verbose */)
case "spans":
f, err := fs.Open("tmp.sst")
if err != nil {
return err.Error()
}
var r *sstable.Reader
readable, err := sstable.NewSimpleReadable(f)
if err != nil {
return err.Error()
}
r, err = sstable.NewReader(readable, sstable.ReaderOptions{})
if err != nil {
return err.Error()
}
defer r.Close()
iter, err := newCombinedDeletionKeyspanIter(base.DefaultComparer, r, m)
if err != nil {
return err.Error()
}
defer iter.Close()
var buf bytes.Buffer
for s := iter.First(); s != nil; s = iter.Next() {
buf.WriteString(s.String() + "\n")
}
if buf.Len() == 0 {
return "(none)"
}
return buf.String()
default:
return fmt.Sprintf("unknown command: %s", cmd)
}
})
}