-
Notifications
You must be signed in to change notification settings - Fork 460
/
keyspan_probe_test.go
392 lines (340 loc) · 10.1 KB
/
keyspan_probe_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
// Copyright 2023 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"
"go/token"
"io"
"reflect"
"strconv"
"strings"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble/internal/dsl"
"github.com/cockroachdb/pebble/internal/keyspan"
"github.com/cockroachdb/pebble/internal/treeprinter"
)
// This file contains testing facilities for Spans and FragmentIterators. It's
// a copy of the facilities defined in internal/keyspan/datadriven_test.go.
//
// TODO(jackson): Move keyspan.{Span,Key,FragmentIterator} into internal/base,
// and then move the testing facilities to an independent package, eg
// internal/itertest, where it may be shared by both uses.
// keyspanProbe defines an interface for probes that may inspect or mutate internal
// span iterator behavior.
type keyspanProbe interface {
// probe inspects, and possibly manipulates, iterator operations' results.
probe(*keyspanProbeContext)
}
func parseKeyspanProbes(probeDSLs ...string) []keyspanProbe {
probes := make([]keyspanProbe, len(probeDSLs))
var err error
for i := range probeDSLs {
probes[i], err = probeParser.Parse(probeDSLs[i])
if err != nil {
panic(err)
}
}
return probes
}
func attachKeyspanProbes(
iter keyspan.FragmentIterator, pctx keyspanProbeContext, probes ...keyspanProbe,
) keyspan.FragmentIterator {
if pctx.log == nil {
pctx.log = io.Discard
}
for i := range probes {
iter = &probeKeyspanIterator{
iter: iter,
probe: probes[i],
probeCtx: pctx,
}
}
return iter
}
// keyspanProbeContext provides the context within which a probe is run. It includes
// information about the iterator operation in progress.
type keyspanProbeContext struct {
keyspanOp
log io.Writer
}
type keyspanOp struct {
Kind keyspanOpKind
SeekKey []byte
Span *keyspan.Span
Err error
}
// errInjected is an error artificially injected for testing.
var errInjected = &errorProbe{name: "ErrInjected", err: errors.New("injected error")}
var probeParser = func() *dsl.Parser[keyspanProbe] {
valuerParser := dsl.NewParser[valuer]()
valuerParser.DefineConstant("StartKey", func() valuer { return startKey{} })
valuerParser.DefineConstant("SeekKey", func() valuer { return seekKey{} })
valuerParser.DefineFunc("Bytes",
func(p *dsl.Parser[valuer], s *dsl.Scanner) valuer {
v := bytesConstant{bytes: []byte(s.ConsumeString())}
s.Consume(token.RPAREN)
return v
})
predicateParser := dsl.NewPredicateParser[*keyspanProbeContext]()
predicateParser.DefineFunc("Equal",
func(p *dsl.Parser[dsl.Predicate[*keyspanProbeContext]], s *dsl.Scanner) dsl.Predicate[*keyspanProbeContext] {
eq := equal{
valuerParser.ParseFromPos(s, s.Scan()),
valuerParser.ParseFromPos(s, s.Scan()),
}
s.Consume(token.RPAREN)
return eq
})
for i, name := range opNames {
opKind := keyspanOpKind(i)
predicateParser.DefineConstant(name, func() dsl.Predicate[*keyspanProbeContext] {
// An OpKind implements dsl.Predicate[*probeContext].
return opKind
})
}
probeParser := dsl.NewParser[keyspanProbe]()
probeParser.DefineConstant("ErrInjected", func() keyspanProbe { return errInjected })
probeParser.DefineConstant("noop", func() keyspanProbe { return noop{} })
probeParser.DefineFunc("If",
func(p *dsl.Parser[keyspanProbe], s *dsl.Scanner) keyspanProbe {
probe := ifProbe{
predicateParser.ParseFromPos(s, s.Scan()),
probeParser.ParseFromPos(s, s.Scan()),
probeParser.ParseFromPos(s, s.Scan()),
}
s.Consume(token.RPAREN)
return probe
})
probeParser.DefineFunc("Return",
func(p *dsl.Parser[keyspanProbe], s *dsl.Scanner) (ret keyspanProbe) {
switch tok := s.Scan(); tok.Kind {
case token.STRING:
str, err := strconv.Unquote(tok.Lit)
if err != nil {
panic(err)
}
span := keyspan.ParseSpan(str)
ret = returnSpan{s: &span}
case token.IDENT:
switch tok.Lit {
case "nil":
ret = returnSpan{s: nil}
default:
panic(errors.Newf("unrecognized return value %q", tok.Lit))
}
}
s.Consume(token.RPAREN)
return ret
})
probeParser.DefineFunc("Log",
func(p *dsl.Parser[keyspanProbe], s *dsl.Scanner) (ret keyspanProbe) {
ret = loggingProbe{prefix: s.ConsumeString()}
s.Consume(token.RPAREN)
return ret
})
return probeParser
}()
// probe implementations
type errorProbe struct {
name string
err error
}
func (p *errorProbe) String() string { return p.name }
func (p *errorProbe) Error() error { return p.err }
func (p *errorProbe) probe(pctx *keyspanProbeContext) {
pctx.keyspanOp.Err = p.err
pctx.keyspanOp.Span = nil
}
// ifProbe is a conditional probe. If its predicate evaluates to true, it probes
// using its Then probe. If its predicate evalutes to false, it probes using its
// Else probe.
type ifProbe struct {
Predicate dsl.Predicate[*keyspanProbeContext]
Then keyspanProbe
Else keyspanProbe
}
func (p ifProbe) String() string { return fmt.Sprintf("(If %s %s %s)", p.Predicate, p.Then, p.Else) }
func (p ifProbe) probe(pctx *keyspanProbeContext) {
if p.Predicate.Evaluate(pctx) {
p.Then.probe(pctx)
} else {
p.Else.probe(pctx)
}
}
type returnSpan struct {
s *keyspan.Span
}
func (p returnSpan) String() string {
if p.s == nil {
return "(Return nil)"
}
return fmt.Sprintf("(Return %q)", p.s.String())
}
func (p returnSpan) probe(pctx *keyspanProbeContext) {
pctx.keyspanOp.Span = p.s
pctx.keyspanOp.Err = nil
}
type noop struct{}
func (noop) String() string { return "Noop" }
func (noop) probe(pctx *keyspanProbeContext) {}
type loggingProbe struct {
prefix string
}
func (lp loggingProbe) String() string { return fmt.Sprintf("(Log %q)", lp.prefix) }
func (lp loggingProbe) probe(pctx *keyspanProbeContext) {
opStr := strings.TrimPrefix(pctx.keyspanOp.Kind.String(), "Op")
fmt.Fprintf(pctx.log, "%s%s(", lp.prefix, opStr)
if pctx.keyspanOp.SeekKey != nil {
fmt.Fprintf(pctx.log, "%q", pctx.keyspanOp.SeekKey)
}
fmt.Fprint(pctx.log, ") = ")
if pctx.keyspanOp.Span == nil {
fmt.Fprint(pctx.log, "nil")
if pctx.keyspanOp.Err != nil {
fmt.Fprintf(pctx.log, " <err=%q>", pctx.keyspanOp.Err)
}
} else {
fmt.Fprint(pctx.log, pctx.keyspanOp.Span.String())
}
fmt.Fprintln(pctx.log)
}
// dsl.Predicate[*probeContext] implementations.
type equal struct {
a, b valuer
}
func (e equal) String() string { return fmt.Sprintf("(Equal %s %s)", e.a, e.b) }
func (e equal) Evaluate(pctx *keyspanProbeContext) bool {
return reflect.DeepEqual(e.a.value(pctx), e.b.value(pctx))
}
// keyspanOpKind indicates the type of iterator operation being performed.
type keyspanOpKind int8
const (
opSpanSeekGE keyspanOpKind = iota
opSpanSeekLT
opSpanFirst
opSpanLast
opSpanNext
opSpanPrev
opSpanClose
numKeyspanOpKinds
)
func (o keyspanOpKind) String() string { return opNames[o] }
func (o keyspanOpKind) Evaluate(pctx *keyspanProbeContext) bool { return pctx.keyspanOp.Kind == o }
var opNames = [numKeyspanOpKinds]string{
opSpanSeekGE: "opSpanSeekGE",
opSpanSeekLT: "opSpanSeekLT",
opSpanFirst: "opSpanFirst",
opSpanLast: "opSpanLast",
opSpanNext: "opSpanNext",
opSpanPrev: "opSpanPrev",
opSpanClose: "opSpanClose",
}
// valuer implementations
type valuer interface {
fmt.Stringer
value(pctx *keyspanProbeContext) any
}
type bytesConstant struct {
bytes []byte
}
func (b bytesConstant) String() string { return fmt.Sprintf("%q", string(b.bytes)) }
func (b bytesConstant) value(pctx *keyspanProbeContext) any { return b.bytes }
type startKey struct{}
func (s startKey) String() string { return "StartKey" }
func (s startKey) value(pctx *keyspanProbeContext) any {
if pctx.keyspanOp.Span == nil {
return nil
}
return pctx.keyspanOp.Span.Start
}
type seekKey struct{}
func (s seekKey) String() string { return "SeekKey" }
func (s seekKey) value(pctx *keyspanProbeContext) any {
if pctx.keyspanOp.SeekKey == nil {
return nil
}
return pctx.keyspanOp.SeekKey
}
type probeKeyspanIterator struct {
iter keyspan.FragmentIterator
probe keyspanProbe
probeCtx keyspanProbeContext
}
// Assert that probeIterator implements the fragment iterator interface.
var _ keyspan.FragmentIterator = (*probeKeyspanIterator)(nil)
func (p *probeKeyspanIterator) handleOp(preProbeOp keyspanOp) (*keyspan.Span, error) {
p.probeCtx.keyspanOp = preProbeOp
p.probe.probe(&p.probeCtx)
return p.probeCtx.keyspanOp.Span, p.probeCtx.keyspanOp.Err
}
func (p *probeKeyspanIterator) SeekGE(key []byte) (*keyspan.Span, error) {
op := keyspanOp{
Kind: opSpanSeekGE,
SeekKey: key,
}
if p.iter != nil {
op.Span, op.Err = p.iter.SeekGE(key)
}
return p.handleOp(op)
}
func (p *probeKeyspanIterator) SeekLT(key []byte) (*keyspan.Span, error) {
op := keyspanOp{
Kind: opSpanSeekLT,
SeekKey: key,
}
if p.iter != nil {
op.Span, op.Err = p.iter.SeekLT(key)
}
return p.handleOp(op)
}
func (p *probeKeyspanIterator) First() (*keyspan.Span, error) {
op := keyspanOp{Kind: opSpanFirst}
if p.iter != nil {
op.Span, op.Err = p.iter.First()
}
return p.handleOp(op)
}
func (p *probeKeyspanIterator) Last() (*keyspan.Span, error) {
op := keyspanOp{Kind: opSpanLast}
if p.iter != nil {
op.Span, op.Err = p.iter.Last()
}
return p.handleOp(op)
}
func (p *probeKeyspanIterator) Next() (*keyspan.Span, error) {
op := keyspanOp{Kind: opSpanNext}
if p.iter != nil {
op.Span, op.Err = p.iter.Next()
}
return p.handleOp(op)
}
func (p *probeKeyspanIterator) Prev() (*keyspan.Span, error) {
op := keyspanOp{Kind: opSpanPrev}
if p.iter != nil {
op.Span, op.Err = p.iter.Prev()
}
return p.handleOp(op)
}
func (p *probeKeyspanIterator) WrapChildren(wrap keyspan.WrapFn) {
p.iter = wrap(p.iter)
}
// DebugTree is part of the FragmentIterator interface.
func (p *probeKeyspanIterator) DebugTree(tp treeprinter.Node) {
n := tp.Childf("%T(%p)", p, p)
if p.iter != nil {
p.iter.DebugTree(n)
}
}
// SetContext is part of the FragmentIterator interface.
func (p *probeKeyspanIterator) SetContext(ctx context.Context) {
p.iter.SetContext(ctx)
}
func (p *probeKeyspanIterator) Close() {
op := keyspanOp{Kind: opSpanClose}
if p.iter != nil {
p.iter.Close()
}
_, _ = p.handleOp(op)
}