-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
store_test.go
323 lines (283 loc) · 10.2 KB
/
store_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
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package spanconfigstore
import (
"context"
"fmt"
"math/rand"
"regexp"
"strings"
"testing"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/spanconfig"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
"github.com/cockroachdb/datadriven"
"github.com/stretchr/testify/require"
)
// spanRe matches strings of the form "[start, end)", capturing both the "start"
// and "end" keys.
var spanRe = regexp.MustCompile(`^\[(\w+),\s??(\w+)\)$`)
// configRe matches a single word. It's a shorthand for declaring a unique
// config.
var configRe = regexp.MustCompile(`^(\w+)$`)
func TestSpanRe(t *testing.T) {
for _, tc := range []struct {
input string
expMatch bool
expStart, expEnd string
}{
{"[a, b)", true, "a", "b"},
{"[acd, bfg)", true, "acd", "bfg"}, // multi character keys allowed
{"[a,b)", true, "a", "b"}, // separating space is optional
{"[ a,b) ", false, "", ""}, // extraneous spaces disallowed
{"[a,b ) ", false, "", ""}, // extraneous spaces disallowed
{"[a,, b)", false, "", ""}, // only single comma allowed
{" [a, b)", false, "", ""}, // need to start with '['
{"[a,b)x", false, "", ""}, // need to end with ')'
} {
require.Equalf(t, tc.expMatch, spanRe.MatchString(tc.input), "input = %s", tc.input)
if !tc.expMatch {
continue
}
matches := spanRe.FindStringSubmatch(tc.input)
require.Len(t, matches, 3)
start, end := matches[1], matches[2]
require.Equal(t, tc.expStart, start)
require.Equal(t, tc.expEnd, end)
}
}
// parseSpan is helper function that constructs a roachpb.Span from a string of
// the form "[start, end)".
func parseSpan(t *testing.T, sp string) roachpb.Span {
if !spanRe.MatchString(sp) {
t.Fatalf("expected %s to match span regex", sp)
}
matches := spanRe.FindStringSubmatch(sp)
start, end := matches[1], matches[2]
return roachpb.Span{
Key: roachpb.Key(start),
EndKey: roachpb.Key(end),
}
}
// parseConfig is helper function that constructs a roachpb.SpanConfig with
// "tagged" with the given string (i.e. a constraint with the given string a
// required key).
func parseConfig(t *testing.T, conf string) roachpb.SpanConfig {
if !configRe.MatchString(conf) {
t.Fatalf("expected %s to match config regex", conf)
}
return roachpb.SpanConfig{
Constraints: []roachpb.ConstraintsConjunction{
{
Constraints: []roachpb.Constraint{
{
Key: conf,
},
},
},
},
}
}
// printSpan is a helper function that transforms roachpb.Span into a string of
// the form "[start,end)". The span is assumed to have been constructed by the
// parseSpan helper above.
func printSpan(sp roachpb.Span) string {
return fmt.Sprintf("[%s,%s)", string(sp.Key), string(sp.EndKey))
}
// printSpanConfig is a helper function that transforms roachpb.SpanConfig into
// a readable string. The span config is assumed to have been constructed by the
// parseSpanConfig helper above.
func printSpanConfig(conf roachpb.SpanConfig) string {
return conf.Constraints[0].Constraints[0].Key // see parseConfig for what a "tagged" roachpb.SpanConfig translates to
}
// printSpanConfigEntry is a helper function that transforms
// roachpb.SpanConfigEntry into a string of the form "[start, end):config". The
// span and config are expected to have been constructed using the
// parse{Span,Config} helpers above.
func printSpanConfigEntry(entry roachpb.SpanConfigEntry) string {
return fmt.Sprintf("%s:%s", printSpan(entry.Span), printSpanConfig(entry.Config))
}
// TestingGetAllOverlapping is a testing only helper to retrieve the set of
// overlapping entries in sorted order.
func (s *Store) TestingGetAllOverlapping(
_ context.Context, sp roachpb.Span,
) []roachpb.SpanConfigEntry {
s.mu.RLock()
defer s.mu.RUnlock()
// Iterate over all overlapping ranges and return corresponding span config
// entries.
var res []roachpb.SpanConfigEntry
for _, overlapping := range s.mu.tree.Get(sp.AsRange()) {
res = append(res, overlapping.(storeEntry).SpanConfigEntry)
}
return res
}
func TestDatadriven(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx := context.Background()
datadriven.Walk(t, "testdata", func(t *testing.T, path string) {
store := New(parseConfig(t, "FALLBACK"))
datadriven.RunTest(t, path, func(t *testing.T, d *datadriven.TestData) string {
var (
spanStr, confStr, keyStr string
)
switch d.Cmd {
case "set":
d.ScanArgs(t, "span", &spanStr)
d.ScanArgs(t, "conf", &confStr)
span, config := parseSpan(t, spanStr), parseConfig(t, confStr)
dryrun := d.HasArg("dryrun")
deleted, added := store.Apply(ctx, spanconfig.Update{Span: span, Config: config}, dryrun)
var b strings.Builder
for _, sp := range deleted {
b.WriteString(fmt.Sprintf("deleted %s\n", printSpan(sp)))
}
for _, ent := range added {
b.WriteString(fmt.Sprintf("added %s\n", printSpanConfigEntry(ent)))
}
return b.String()
case "delete":
d.ScanArgs(t, "span", &spanStr)
span := parseSpan(t, spanStr)
dryrun := d.HasArg("dryrun")
deleted, added := store.Apply(ctx, spanconfig.Update{Span: span}, dryrun)
var b strings.Builder
for _, sp := range deleted {
b.WriteString(fmt.Sprintf("deleted %s\n", printSpan(sp)))
}
for _, ent := range added {
b.WriteString(fmt.Sprintf("added %s\n", printSpanConfigEntry(ent)))
}
return b.String()
case "get":
d.ScanArgs(t, "key", &keyStr)
config, err := store.GetSpanConfigForKey(ctx, roachpb.RKey(keyStr))
require.NoError(t, err)
return fmt.Sprintf("conf=%s", printSpanConfig(config))
case "needs-split":
d.ScanArgs(t, "span", &spanStr)
span := parseSpan(t, spanStr)
start, end := roachpb.RKey(span.Key), roachpb.RKey(span.EndKey)
result := store.NeedsSplit(ctx, start, end)
return fmt.Sprintf("%t", result)
case "compute-split":
d.ScanArgs(t, "span", &spanStr)
span := parseSpan(t, spanStr)
start, end := roachpb.RKey(span.Key), roachpb.RKey(span.EndKey)
splitKey := store.ComputeSplitKey(ctx, start, end)
return fmt.Sprintf("key=%s", string(splitKey))
case "overlapping":
d.ScanArgs(t, "span", &spanStr)
span := parseSpan(t, spanStr)
entries := store.TestingGetAllOverlapping(ctx, span)
var results []string
for _, entry := range entries {
results = append(results, printSpanConfigEntry(entry))
}
return strings.Join(results, "\n")
default:
}
t.Fatalf("unknown command: %s", d.Cmd)
return ""
})
})
}
// TestRandomized randomly sets/deletes span configs for arbitrary keyspans
// within some alphabet. For a test span, it then asserts that the config we
// retrieve is what we expect to find from the store. It also ensures that all
// ranges are non-overlapping.
func TestRandomized(t *testing.T) {
defer leaktest.AfterTest(t)()
randutil.SeedForTests()
ctx := context.Background()
alphabet := "abcdefghijklmnopqrstuvwxyz"
configs := "ABCDEF"
ops := []string{"set", "del"}
genRandomSpan := func() roachpb.Span {
startIdx, endIdx := rand.Intn(len(alphabet)-1), 1+rand.Intn(len(alphabet)-1)
if startIdx == endIdx {
endIdx = (endIdx + 1) % len(alphabet)
}
if endIdx < startIdx {
startIdx, endIdx = endIdx, startIdx
}
spanStr := fmt.Sprintf("[%s, %s)", string(alphabet[startIdx]), string(alphabet[endIdx]))
sp := parseSpan(t, spanStr)
require.True(t, sp.Valid())
return sp
}
getRandomConf := func() roachpb.SpanConfig {
confStr := fmt.Sprintf("conf_%s", string(configs[rand.Intn(len(configs))]))
return parseConfig(t, confStr)
}
getRandomOp := func() string {
return ops[rand.Intn(2)]
}
testSpan := parseSpan(t, "[f,g)") // pin a single character span to test with
var expConfig roachpb.SpanConfig
var expFound bool
const numOps = 5000
store := New(roachpb.TestingDefaultSpanConfig())
for i := 0; i < numOps; i++ {
sp, conf, op := genRandomSpan(), getRandomConf(), getRandomOp()
switch op {
case "set":
store.Apply(ctx, spanconfig.Update{Span: sp, Config: conf}, false)
if testSpan.Overlaps(sp) {
expConfig, expFound = conf, true
}
case "del":
store.Apply(ctx, spanconfig.Update{Span: sp}, false)
if testSpan.Overlaps(sp) {
expConfig, expFound = roachpb.SpanConfig{}, false
}
default:
t.Fatalf("unexpected op: %s", op)
}
}
overlappingConfigs := store.TestingGetAllOverlapping(ctx, testSpan)
if !expFound {
require.Len(t, overlappingConfigs, 0)
} else {
// Check to see that the set of overlapping span configs is exactly what
// we expect.
require.Len(t, overlappingConfigs, 1)
gotSpan, gotConfig := overlappingConfigs[0].Span, overlappingConfigs[0].Config
require.Truef(t, gotSpan.Contains(testSpan),
"improper result: expected retrieved span (%s) to contain test span (%s)",
printSpan(gotSpan), printSpan(testSpan))
require.Truef(t, expConfig.Equal(gotConfig),
"mismatched configs: expected %s, got %s",
printSpanConfig(expConfig), printSpanConfig(gotConfig))
// Ensure that the config accessed through the StoreReader interface is
// the same as above.
storeReaderConfig, err := store.GetSpanConfigForKey(ctx, roachpb.RKey(testSpan.Key))
require.NoError(t, err)
require.True(t, gotConfig.Equal(storeReaderConfig))
}
var last roachpb.SpanConfigEntry
everythingSpan := parseSpan(t, fmt.Sprintf("[%s,%s)",
string(alphabet[0]), string(alphabet[len(alphabet)-1])))
for i, cur := range store.TestingGetAllOverlapping(ctx, everythingSpan) {
if i == 0 {
last = cur
continue
}
// Span configs are returned in strictly sorted order.
require.True(t, last.Span.Key.Compare(cur.Span.Key) < 0,
"expected to find spans in strictly sorted order, found %s then %s",
printSpan(last.Span), printSpan(cur.Span))
// Span configs must also be non-overlapping.
require.Falsef(t, last.Span.Overlaps(cur.Span),
"expected non-overlapping spans, found %s and %s",
printSpan(last.Span), printSpan(cur.Span))
}
}