forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_test.go
503 lines (464 loc) · 13.7 KB
/
metrics_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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// 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/rand"
"runtime"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/cockroachdb/datadriven"
"github.com/cockroachdb/pebble/internal/cache"
"github.com/cockroachdb/pebble/internal/humanize"
"github.com/cockroachdb/pebble/internal/testkeys"
"github.com/cockroachdb/pebble/objstorage/remote"
"github.com/cockroachdb/pebble/sstable"
"github.com/cockroachdb/pebble/vfs"
"github.com/cockroachdb/pebble/vfs/errorfs"
"github.com/cockroachdb/redact"
"github.com/stretchr/testify/require"
)
func exampleMetrics() Metrics {
var m Metrics
m.BlockCache.Size = 1
m.BlockCache.Count = 2
m.BlockCache.Hits = 3
m.BlockCache.Misses = 4
m.Compact.Count = 5
m.Compact.DefaultCount = 27
m.Compact.DeleteOnlyCount = 28
m.Compact.ElisionOnlyCount = 29
m.Compact.MoveCount = 30
m.Compact.ReadCount = 31
m.Compact.TombstoneDensityCount = 16
m.Compact.RewriteCount = 32
m.Compact.CopyCount = 33
m.Compact.MultiLevelCount = 34
m.Compact.EstimatedDebt = 6
m.Compact.InProgressBytes = 7
m.Compact.NumInProgress = 2
m.Flush.Count = 8
m.Flush.AsIngestBytes = 34
m.Flush.AsIngestTableCount = 35
m.Flush.AsIngestCount = 36
m.Filter.Hits = 9
m.Filter.Misses = 10
m.MemTable.Size = 11
m.MemTable.Count = 12
m.MemTable.ZombieSize = 13
m.MemTable.ZombieCount = 14
m.Snapshots.Count = 4
m.Snapshots.EarliestSeqNum = 1024
m.Table.ZombieSize = 15
m.Table.BackingTableCount = 1
m.Table.BackingTableSize = 2 << 20
m.Table.ZombieCount = 16
m.TableCache.Size = 17
m.TableCache.Count = 18
m.TableCache.Hits = 19
m.TableCache.Misses = 20
m.TableIters = 21
m.WAL.Files = 22
m.WAL.ObsoleteFiles = 23
m.WAL.Size = 24
m.WAL.BytesIn = 25
m.WAL.BytesWritten = 26
m.Ingest.Count = 27
m.Table.Local.LiveSize = 28
m.Table.Local.ObsoleteSize = 29
m.Table.Local.ZombieSize = 30
for i := range m.Levels {
l := &m.Levels[i]
base := uint64((i + 1) * 100)
l.Sublevels = int32(i + 1)
l.NumFiles = int64(base) + 1
l.NumVirtualFiles = uint64(base) + 1
l.VirtualSize = base + 3
l.Size = int64(base) + 2
l.Score = float64(base) + 3
l.BytesIn = base + 4
l.BytesIngested = base + 4
l.BytesMoved = base + 6
l.BytesRead = base + 7
l.BytesCompacted = base + 8
l.BytesFlushed = base + 9
l.TablesCompacted = base + 10
l.TablesFlushed = base + 11
l.TablesIngested = base + 12
l.TablesMoved = base + 13
l.MultiLevel.BytesInTop = base + 4
l.MultiLevel.BytesIn = base + 4
l.MultiLevel.BytesRead = base + 4
}
return m
}
func TestMetrics(t *testing.T) {
if runtime.GOARCH == "386" {
t.Skip("skipped on 32-bit due to slightly varied output")
}
defer sstable.DeterministicReadBlockDurationForTesting()()
var d *DB
var iters map[string]*Iterator
var closeFunc func()
var memFS *vfs.MemFS
var remoteStorage remote.Storage
defer func() {
if closeFunc != nil {
closeFunc()
}
}()
init := func(t *testing.T, createOnSharedLower bool, reopen bool) {
if closeFunc != nil {
closeFunc()
}
if !reopen {
memFS = vfs.NewMem()
remoteStorage = remote.NewInMem()
}
c := cache.New(cacheDefaultSize)
defer c.Unref()
opts := &Options{
Cache: c,
Comparer: testkeys.Comparer,
FormatMajorVersion: FormatNewest,
FS: memFS,
L0CompactionThreshold: 8,
// Large value for determinism.
MaxOpenFiles: 10000,
}
opts.Experimental.EnableValueBlocks = func() bool { return true }
opts.Levels = append(opts.Levels, LevelOptions{TargetFileSize: 50})
// Prevent foreground flushes and compactions from triggering asynchronous
// follow-up compactions. This avoids asynchronously-scheduled work from
// interfering with the expected metrics output and reduces test flakiness.
opts.DisableAutomaticCompactions = true
// Increase the threshold for memtable stalls to allow for more flushable
// ingests.
opts.MemTableStopWritesThreshold = 4
opts.Experimental.RemoteStorage = remote.MakeSimpleFactory(map[remote.Locator]remote.Storage{
"": remoteStorage,
})
if createOnSharedLower {
opts.Experimental.CreateOnShared = remote.CreateOnSharedLower
} else {
opts.Experimental.CreateOnShared = remote.CreateOnSharedNone
}
var err error
d, err = Open("", opts)
require.NoError(t, err)
if createOnSharedLower {
require.NoError(t, d.SetCreatorID(1))
}
if reopen {
// Stats population is eventually consistent, and happens in the
// background when a DB is re-opened. To avoid races, wait synchronously
// for all tables to have their stats fully populated, which requires
// opening each SST.
d.mu.Lock()
for !d.mu.tableStats.loadedInitial {
d.mu.tableStats.cond.Wait()
}
d.mu.Unlock()
}
iters = make(map[string]*Iterator)
closeFunc = func() {
for _, i := range iters {
require.NoError(t, i.Close())
}
require.NoError(t, d.Close())
}
}
datadriven.RunTest(t, "testdata/metrics", func(t *testing.T, td *datadriven.TestData) string {
switch td.Cmd {
case "init":
createOnSharedLower := false
if td.HasArg("shared-lower") {
createOnSharedLower = true
}
reopen := false
if td.HasArg("reopen") {
reopen = true
}
init(t, createOnSharedLower, reopen)
return ""
case "example":
m := exampleMetrics()
res := m.String()
// Nothing in the metrics should be redacted.
redacted := string(redact.Sprintf("%s", &m).Redact())
if redacted != res {
td.Fatalf(t, "redacted metrics don't match\nunredacted:\n%s\nredacted:%s\n", res, redacted)
}
return res
case "batch":
b := d.NewBatch()
if err := runBatchDefineCmd(td, b); err != nil {
return err.Error()
}
b.Commit(nil)
return ""
case "build":
if err := runBuildCmd(td, d, d.opts.FS); err != nil {
return err.Error()
}
return ""
case "compact":
if err := runCompactCmd(td, d); err != nil {
return err.Error()
}
d.mu.Lock()
s := d.mu.versions.currentVersion().String()
d.mu.Unlock()
return s
case "delay-flush":
d.mu.Lock()
defer d.mu.Unlock()
switch td.Input {
case "enable":
d.mu.compact.flushing = true
case "disable":
d.mu.compact.flushing = false
default:
return fmt.Sprintf("unknown directive %q (expected 'enable'/'disable')", td.Input)
}
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 := runIngestCmd(td, d, d.opts.FS); err != nil {
return err.Error()
}
return ""
case "lsm":
d.mu.Lock()
s := d.mu.versions.currentVersion().String()
d.mu.Unlock()
return s
case "ingest-and-excise":
if err := runIngestAndExciseCmd(td, d, d.opts.FS); err != nil {
return err.Error()
}
return ""
case "iter-close":
if len(td.CmdArgs) != 1 {
return "iter-close <name>"
}
name := td.CmdArgs[0].String()
if iter := iters[name]; iter != nil {
if err := iter.Close(); err != nil {
return err.Error()
}
delete(iters, name)
} else {
return fmt.Sprintf("%s: not found", name)
}
// The deletion of obsolete files happens asynchronously when an iterator
// is closed. Wait for the obsolete tables to be deleted.
d.cleanupManager.Wait()
return ""
case "iter-new":
if len(td.CmdArgs) < 1 {
return "iter-new <name>"
}
name := td.CmdArgs[0].String()
if iter := iters[name]; iter != nil {
if err := iter.Close(); err != nil {
return err.Error()
}
}
var categoryAndQoS sstable.CategoryAndQoS
if td.HasArg("category") {
var s string
td.ScanArgs(t, "category", &s)
categoryAndQoS.Category = sstable.Category(s)
}
if td.HasArg("qos") {
var qos string
td.ScanArgs(t, "qos", &qos)
categoryAndQoS.QoSLevel = sstable.StringToQoSForTesting(qos)
}
iter, _ := d.NewIter(&IterOptions{CategoryAndQoS: categoryAndQoS})
// Some iterators (eg. levelIter) do not instantiate the underlying
// iterator until the first positioning call. Position the iterator
// so that levelIters will have loaded an sstable.
iter.First()
iters[name] = iter
return ""
case "metrics":
// The asynchronous loading of table stats can change metrics, so
// wait for all the tables' stats to be loaded.
d.mu.Lock()
d.waitTableStats()
d.mu.Unlock()
m := d.Metrics()
// Some subset of cases show non-determinism in cache hits/misses.
if td.HasArg("zero-cache-hits-misses") {
// Avoid non-determinism.
m.TableCache = cache.Metrics{}
m.BlockCache = cache.Metrics{}
// Empirically, the unknown stats are also non-deterministic.
if len(m.CategoryStats) > 0 && m.CategoryStats[0].Category == "_unknown" {
m.CategoryStats[0].CategoryStats = sstable.CategoryStats{}
}
}
var buf strings.Builder
fmt.Fprintf(&buf, "%s", m.StringForTests())
if len(m.CategoryStats) > 0 {
fmt.Fprintf(&buf, "Iter category stats:\n")
for _, stats := range m.CategoryStats {
fmt.Fprintf(&buf, "%20s, %11s: %+v\n", stats.Category,
redact.StringWithoutMarkers(stats.QoSLevel), stats.CategoryStats)
}
}
return buf.String()
case "metrics-value":
// metrics-value confirms the value of a given metric. Note that there
// are some metrics which aren't deterministic and behave differently
// for invariant/non-invariant builds. An example of this is cache
// hit rates. Under invariant builds, the excising code will try
// to create iterators and confirm that the virtual sstable bounds
// are accurate. Reads on these iterators will change the cache hit
// rates.
lines := strings.Split(td.Input, "\n")
m := d.Metrics()
// TODO(bananabrick): Use reflection to pull the values associated
// with the metrics fields.
var buf bytes.Buffer
for i := range lines {
line := lines[i]
if line == "num-backing" {
buf.WriteString(fmt.Sprintf("%d\n", m.Table.BackingTableCount))
} else if line == "backing-size" {
buf.WriteString(fmt.Sprintf("%s\n", humanize.Bytes.Uint64(m.Table.BackingTableSize)))
} else if line == "virtual-size" {
buf.WriteString(fmt.Sprintf("%s\n", humanize.Bytes.Uint64(m.VirtualSize())))
} else if strings.HasPrefix(line, "num-virtual") {
splits := strings.Split(line, " ")
if len(splits) == 1 {
buf.WriteString(fmt.Sprintf("%d\n", m.NumVirtual()))
continue
}
// Level is specified.
l, err := strconv.Atoi(splits[1])
if err != nil {
panic(err)
}
if l >= numLevels {
panic(fmt.Sprintf("invalid level %d", l))
}
buf.WriteString(fmt.Sprintf("%d\n", m.Levels[l].NumVirtualFiles))
} else {
panic(fmt.Sprintf("invalid field: %s", line))
}
}
return buf.String()
case "disk-usage":
return humanize.Bytes.Uint64(d.Metrics().DiskSpaceUsage()).String()
case "additional-metrics":
// The asynchronous loading of table stats can change metrics, so
// wait for all the tables' stats to be loaded.
d.mu.Lock()
d.waitTableStats()
d.mu.Unlock()
m := d.Metrics()
var b strings.Builder
fmt.Fprintf(&b, "block bytes written:\n")
fmt.Fprintf(&b, " __level___data-block__value-block\n")
for i := range m.Levels {
fmt.Fprintf(&b, "%7d ", i)
fmt.Fprintf(&b, "%12s %12s\n",
humanize.Bytes.Uint64(m.Levels[i].Additional.BytesWrittenDataBlocks),
humanize.Bytes.Uint64(m.Levels[i].Additional.BytesWrittenValueBlocks))
}
return b.String()
default:
return fmt.Sprintf("unknown command: %s", td.Cmd)
}
})
}
func TestMetricsWAmpDisableWAL(t *testing.T) {
d, err := Open("", &Options{FS: vfs.NewMem(), DisableWAL: true})
require.NoError(t, err)
ks := testkeys.Alpha(2)
wo := WriteOptions{Sync: false}
for i := 0; i < 5; i++ {
v := []byte(strconv.Itoa(i))
for j := int64(0); j < ks.Count(); j++ {
require.NoError(t, d.Set(testkeys.Key(ks, j), v, &wo))
}
require.NoError(t, d.Flush())
require.NoError(t, d.Compact([]byte("a"), []byte("z"), false /* parallelize */))
}
m := d.Metrics()
tot := m.Total()
require.Greater(t, tot.WriteAmp(), 1.0)
require.NoError(t, d.Close())
}
// TestMetricsWALBytesWrittenMonotonicity tests that the
// Metrics.WAL.BytesWritten metric is always nondecreasing.
// It's a regression test for issue #3505.
func TestMetricsWALBytesWrittenMonotonicity(t *testing.T) {
fs := errorfs.Wrap(vfs.NewMem(), errorfs.RandomLatency(
nil, 100*time.Microsecond, time.Now().UnixNano(), 0 /* no limit */))
d, err := Open("", &Options{
FS: fs,
// Use a tiny memtable size so that we get frequent flushes. While a
// flush is in-progress or completing, the WAL bytes written should
// remain nondecreasing.
MemTableSize: 1 << 20, /* 20 KiB */
})
require.NoError(t, err)
stopCh := make(chan struct{})
ks := testkeys.Alpha(3)
var wg sync.WaitGroup
const concurrentWriters = 5
wg.Add(concurrentWriters)
for w := 0; w < concurrentWriters; w++ {
go func() {
defer wg.Done()
data := make([]byte, 1<<10) // 1 KiB
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
_, err := rng.Read(data)
require.NoError(t, err)
buf := make([]byte, ks.MaxLen())
for i := 0; ; i++ {
select {
case <-stopCh:
return
default:
}
n := testkeys.WriteKey(buf, ks, int64(i)%ks.Count())
require.NoError(t, d.Set(buf[:n], data, NoSync))
}
}()
}
func() {
defer func() { close(stopCh) }()
abort := time.After(time.Second)
var prevWALBytesWritten uint64
for {
select {
case <-abort:
return
default:
}
m := d.Metrics()
if m.WAL.BytesWritten < prevWALBytesWritten {
t.Fatalf("WAL bytes written decreased: %d -> %d", prevWALBytesWritten, m.WAL.BytesWritten)
}
prevWALBytesWritten = m.WAL.BytesWritten
}
}()
wg.Wait()
}