forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsm_view.go
277 lines (256 loc) · 7.31 KB
/
lsm_view.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
// Copyright 2024 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 (
"context"
"fmt"
"slices"
"strings"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/internal/humanize"
"github.com/cockroachdb/pebble/internal/lsmview"
"github.com/cockroachdb/pebble/objstorage"
)
// LSMViewURL returns an URL which shows a diagram of the LSM.
func (d *DB) LSMViewURL() string {
v := func() *version {
d.mu.Lock()
defer d.mu.Unlock()
v := d.mu.versions.currentVersion()
v.Ref()
return v
}()
defer v.Unref()
b := lsmViewBuilder{
cmp: d.opts.Comparer.Compare,
fmtKey: d.opts.Comparer.FormatKey,
}
if b.fmtKey == nil {
b.fmtKey = DefaultComparer.FormatKey
}
b.InitLevels(v)
b.PopulateKeys()
data := b.Build(d.objProvider, d.newIters)
url, err := lsmview.GenerateURL(data)
if err != nil {
return fmt.Sprintf("error: %s", err)
}
return url.String()
}
type lsmViewBuilder struct {
cmp base.Compare
fmtKey base.FormatKey
levelNames []string
levels [][]*fileMetadata
// The keys that appear as Smallest/Largest, sorted and formatted.
sortedKeys []string
// keys[k] is the position of key k in the sortedKeys list.
keys map[string]int
// scanTables is set during Build. If we don't have too many tables, we will
// create iterators and show some of the keys.
scanTables bool
}
// InitLevels gets the metadata for the tables in the LSM and populates
// levelNames and levels.
func (b *lsmViewBuilder) InitLevels(v *version) {
var levelNames []string
var levels [][]*fileMetadata
for sublevel := len(v.L0SublevelFiles) - 1; sublevel >= 0; sublevel-- {
var files []*fileMetadata
v.L0SublevelFiles[sublevel].Each(func(f *fileMetadata) {
files = append(files, f)
})
levelNames = append(levelNames, fmt.Sprintf("L0.%d", sublevel))
levels = append(levels, files)
}
if len(levels) == 0 {
levelNames = append(levelNames, "L0")
levels = append(levels, nil)
}
for level := 1; level < len(v.Levels); level++ {
var files []*fileMetadata
v.Levels[level].Slice().Each(func(f *fileMetadata) {
files = append(files, f)
})
levelNames = append(levelNames, fmt.Sprintf("L%d", level))
levels = append(levels, files)
}
b.levelNames = levelNames
b.levels = levels
}
// PopulateKeys initializes the sortedKeys and keys fields.
func (b *lsmViewBuilder) PopulateKeys() {
// keys[k] will hold the position of k into sortedKeys.
keys := make(map[string]int)
for _, l := range b.levels {
for _, f := range l {
keys[string(f.Smallest.UserKey)] = -1
keys[string(f.Largest.UserKey)] = -1
}
}
sortedKeys := make([]string, 0, len(keys))
for s := range keys {
sortedKeys = append(sortedKeys, s)
}
slices.SortFunc(sortedKeys, func(k1, k2 string) int {
return b.cmp([]byte(k1), []byte(k2))
})
sortedKeys = slices.CompactFunc(sortedKeys, func(k1, k2 string) bool {
return b.cmp([]byte(k1), []byte(k2)) == 0
})
for i, k := range sortedKeys {
keys[k] = i
}
for i := range sortedKeys {
sortedKeys[i] = fmt.Sprintf("%v", b.fmtKey([]byte(sortedKeys[i])))
}
b.sortedKeys = sortedKeys
b.keys = keys
}
func (b *lsmViewBuilder) Build(
objProvider objstorage.Provider, newIters tableNewIters,
) lsmview.Data {
n := 0
for _, l := range b.levels {
n += len(l)
}
const scanTablesThreshold = 100
b.scanTables = n <= scanTablesThreshold
var data lsmview.Data
data.Keys = b.sortedKeys
data.Levels = make([]lsmview.Level, len(b.levels))
for i, files := range b.levels {
l := &data.Levels[i]
l.Name = b.levelNames[i]
l.Tables = make([]lsmview.Table, len(files))
for j, f := range files {
t := &l.Tables[j]
if !f.Virtual {
t.Label = fmt.Sprintf("%d", f.FileNum)
} else {
t.Label = fmt.Sprintf("%d (%d)", f.FileNum, f.FileBacking.DiskFileNum)
}
t.Size = f.Size
t.SmallestKey = b.keys[string(f.Smallest.UserKey)]
t.LargestKey = b.keys[string(f.Largest.UserKey)]
t.Details = b.tableDetails(f, objProvider, newIters)
}
}
return data
}
func (b *lsmViewBuilder) tableDetails(
m *fileMetadata, objProvider objstorage.Provider, newIters tableNewIters,
) []string {
res := make([]string, 0, 10)
outf := func(format string, args ...any) {
res = append(res, fmt.Sprintf(format, args...))
}
outf("%s: %s - %s", m.FileNum, m.Smallest.Pretty(b.fmtKey), m.Largest.Pretty(b.fmtKey))
outf("size: %s", humanize.Bytes.Uint64(m.Size))
if m.Virtual {
meta, err := objProvider.Lookup(base.FileTypeTable, m.FileBacking.DiskFileNum)
var backingInfo string
switch {
case err != nil:
backingInfo = fmt.Sprintf(" (error looking up object: %v)", err)
case meta.IsShared():
backingInfo = "shared; "
case meta.IsExternal():
backingInfo = "external; "
}
outf("virtual; backed by %s (%ssize: %s)", m.FileBacking.DiskFileNum, backingInfo, humanize.Bytes.Uint64(m.FileBacking.Size))
}
outf("seqnums: %d - %d", m.SmallestSeqNum, m.LargestSeqNum)
if m.SyntheticPrefix.IsSet() {
// Note: we are abusing the key formatter by passing just the prefix.
outf("synthetic prefix: %s", b.fmtKey(m.SyntheticPrefix))
}
if m.SyntheticSuffix.IsSet() {
// Note: we are abusing the key formatter by passing just the suffix.
outf("synthetic suffix: %s", b.fmtKey(m.SyntheticSuffix))
}
var iters iterSet
if b.scanTables {
var err error
iters, err = newIters(context.Background(), m, nil /* opts */, internalIterOpts{}, iterPointKeys|iterRangeDeletions|iterRangeKeys)
if err != nil {
outf("error opening table: %v", err)
} else {
defer iters.CloseAll()
}
}
const maxPoints = 14
const maxRangeDels = 10
const maxRangeKeys = 10
if m.HasPointKeys {
outf("points: %s - %s", m.SmallestPointKey.Pretty(b.fmtKey), m.LargestPointKey.Pretty(b.fmtKey))
if b.scanTables {
n := 0
if it := iters.point; it != nil {
for kv := it.First(); kv != nil; kv = it.Next() {
if n == maxPoints {
outf(" ...")
break
}
outf(" %s", kv.K.Pretty(b.fmtKey))
n++
}
if err := it.Error(); err != nil {
outf(" error scanning points: %v", err)
}
}
if n == 0 {
outf(" no points")
}
n = 0
if it := iters.rangeDeletion; it != nil {
span, err := it.First()
for ; span != nil; span, err = it.Next() {
if n == maxRangeDels {
outf(" ...")
break
}
seqNums := make([]string, len(span.Keys))
for i, k := range span.Keys {
seqNums[i] = fmt.Sprintf("#%d", k.SeqNum())
}
outf(" [%s - %s): %s", b.fmtKey(span.Start), b.fmtKey(span.End), strings.Join(seqNums, ","))
n++
}
if err != nil {
outf("error scanning range dels: %v", err)
}
}
if n == 0 {
outf(" no range dels")
}
}
}
if m.HasRangeKeys {
outf("range keys: %s - %s", m.SmallestRangeKey.Pretty(b.fmtKey), m.LargestRangeKey.Pretty(b.fmtKey))
n := 0
if it := iters.rangeKey; it != nil {
span, err := it.First()
for ; span != nil; span, err = it.Next() {
if n == maxRangeKeys {
outf(" ...")
break
}
keys := make([]string, len(span.Keys))
for i, k := range span.Keys {
keys[i] = k.String()
}
outf(" [%s, %s): {%s}", b.fmtKey(span.Start), b.fmtKey(span.End), strings.Join(keys, " "))
n++
}
if err != nil {
outf("error scanning range keys: %v", err)
}
}
if n == 0 {
outf(" no range keys")
}
}
return res
}