/
wal.go
171 lines (155 loc) · 5.14 KB
/
wal.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
// 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 tool
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"github.com/cockroachdb/pebble"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/rangekey"
"github.com/cockroachdb/pebble/record"
"github.com/cockroachdb/pebble/sstable"
"github.com/spf13/cobra"
)
// walT implements WAL-level tools, including both configuration state and the
// commands themselves.
type walT struct {
Root *cobra.Command
Dump *cobra.Command
opts *pebble.Options
fmtKey keyFormatter
fmtValue valueFormatter
defaultComparer string
comparers sstable.Comparers
verbose bool
}
func newWAL(opts *pebble.Options, comparers sstable.Comparers, defaultComparer string) *walT {
w := &walT{
opts: opts,
}
w.fmtKey.mustSet("quoted")
w.fmtValue.mustSet("size")
w.comparers = comparers
w.defaultComparer = defaultComparer
w.Root = &cobra.Command{
Use: "wal",
Short: "WAL introspection tools",
}
w.Dump = &cobra.Command{
Use: "dump <wal-files>",
Short: "print WAL contents",
Long: `
Print the contents of the WAL files.
`,
Args: cobra.MinimumNArgs(1),
Run: w.runDump,
}
w.Root.AddCommand(w.Dump)
w.Root.PersistentFlags().BoolVarP(&w.verbose, "verbose", "v", false, "verbose output")
w.Dump.Flags().Var(
&w.fmtKey, "key", "key formatter")
w.Dump.Flags().Var(
&w.fmtValue, "value", "value formatter")
return w
}
func (w *walT) runDump(cmd *cobra.Command, args []string) {
stdout, stderr := cmd.OutOrStdout(), cmd.OutOrStderr()
w.fmtKey.setForComparer(w.defaultComparer, w.comparers)
w.fmtValue.setForComparer(w.defaultComparer, w.comparers)
for _, arg := range args {
func() {
// Parse the filename in order to extract the file number. This is
// necessary in case WAL recycling was used (which it is usually is). If
// we can't parse the filename or it isn't a log file, we'll plow ahead
// anyways (which will likely fail when we try to read the file).
_, fileNum, ok := base.ParseFilename(w.opts.FS, arg)
if !ok {
fileNum = base.FileNum(0).DiskFileNum()
}
f, err := w.opts.FS.Open(arg)
if err != nil {
fmt.Fprintf(stderr, "%s\n", err)
return
}
defer f.Close()
fmt.Fprintf(stdout, "%s\n", arg)
var b pebble.Batch
var buf bytes.Buffer
rr := record.NewReader(f, fileNum.FileNum())
for {
offset := rr.Offset()
r, err := rr.Next()
if err == nil {
buf.Reset()
_, err = io.Copy(&buf, r)
}
if err != nil {
// It is common to encounter a zeroed or invalid chunk due to WAL
// preallocation and WAL recycling. We need to distinguish these
// errors from EOF in order to recognize that the record was
// truncated, but want to otherwise treat them like EOF.
switch err {
case record.ErrZeroedChunk:
fmt.Fprintf(stdout, "EOF [%s] (may be due to WAL preallocation)\n", err)
case record.ErrInvalidChunk:
fmt.Fprintf(stdout, "EOF [%s] (may be due to WAL recycling)\n", err)
default:
fmt.Fprintf(stdout, "%s\n", err)
}
return
}
b = pebble.Batch{}
if err := b.SetRepr(buf.Bytes()); err != nil {
fmt.Fprintf(stdout, "corrupt batch within log file %q: %v", arg, err)
return
}
fmt.Fprintf(stdout, "%d(%d) seq=%d count=%d\n",
offset, len(b.Repr()), b.SeqNum(), b.Count())
for r, idx := b.Reader(), 0; ; idx++ {
kind, ukey, value, ok, err := r.Next()
if !ok {
if err != nil {
fmt.Fprintf(stdout, "corrupt batch within log file %q: %v", arg, err)
}
break
}
fmt.Fprintf(stdout, " %s(", kind)
switch kind {
case base.InternalKeyKindDelete:
fmt.Fprintf(stdout, "%s", w.fmtKey.fn(ukey))
case base.InternalKeyKindSet:
fmt.Fprintf(stdout, "%s,%s", w.fmtKey.fn(ukey), w.fmtValue.fn(ukey, value))
case base.InternalKeyKindMerge:
fmt.Fprintf(stdout, "%s,%s", w.fmtKey.fn(ukey), w.fmtValue.fn(ukey, value))
case base.InternalKeyKindLogData:
fmt.Fprintf(stdout, "<%d>", len(value))
case base.InternalKeyKindIngestSST:
fileNum, _ := binary.Uvarint(ukey)
fmt.Fprintf(stdout, "%s", base.FileNum(fileNum))
case base.InternalKeyKindSingleDelete:
fmt.Fprintf(stdout, "%s", w.fmtKey.fn(ukey))
case base.InternalKeyKindSetWithDelete:
fmt.Fprintf(stdout, "%s", w.fmtKey.fn(ukey))
case base.InternalKeyKindRangeDelete:
fmt.Fprintf(stdout, "%s,%s", w.fmtKey.fn(ukey), w.fmtKey.fn(value))
case base.InternalKeyKindRangeKeySet, base.InternalKeyKindRangeKeyUnset, base.InternalKeyKindRangeKeyDelete:
ik := base.MakeInternalKey(ukey, b.SeqNum()+uint64(idx), kind)
s, err := rangekey.Decode(ik, value, nil)
if err != nil {
fmt.Fprintf(stdout, "%s: error decoding %s", w.fmtKey.fn(ukey), err)
} else {
fmt.Fprintf(stdout, "%s", s.Pretty(w.fmtKey.fn))
}
case base.InternalKeyKindDeleteSized:
v, _ := binary.Uvarint(value)
fmt.Fprintf(stdout, "%s,%d", w.fmtKey.fn(ukey), v)
}
fmt.Fprintf(stdout, ")\n")
}
}
}()
}
}