-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathconfig_test.go
429 lines (381 loc) · 13.3 KB
/
config_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
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Marc Berhault ([email protected])
package config_test
import (
"reflect"
"sort"
"testing"
"github.com/gogo/protobuf/proto"
"gopkg.in/yaml.v2"
"github.com/cockroachdb/cockroach/config"
"github.com/cockroachdb/cockroach/keys"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/sql"
"github.com/cockroachdb/cockroach/sql/sqlbase"
"github.com/cockroachdb/cockroach/testutils"
"github.com/cockroachdb/cockroach/util/encoding"
"github.com/cockroachdb/cockroach/util/leaktest"
)
func plainKV(k, v string) roachpb.KeyValue {
return kv([]byte(k), []byte(v))
}
func sqlKV(tableID uint32, indexID, descriptorID uint64) roachpb.KeyValue {
k := keys.MakeTablePrefix(tableID)
k = encoding.EncodeUvarintAscending(k, indexID)
k = encoding.EncodeUvarintAscending(k, descriptorID)
k = encoding.EncodeUvarintAscending(k, 12345) // Column ID, but could be anything.
return kv(k, nil)
}
func descriptor(descriptorID uint64) roachpb.KeyValue {
return sqlKV(uint32(keys.DescriptorTableID), 1, descriptorID)
}
func kv(k, v []byte) roachpb.KeyValue {
return roachpb.KeyValue{
Key: k,
Value: roachpb.MakeValueFromBytes(v),
}
}
func TestObjectIDForKey(t *testing.T) {
defer leaktest.AfterTest(t)()
testCases := []struct {
key roachpb.RKey
success bool
id uint32
}{
// Before the structured span.
{roachpb.RKeyMin, false, 0},
// Boundaries of structured span.
{roachpb.RKeyMax, false, 0},
// Valid, even if there are things after the ID.
{testutils.MakeKey(keys.MakeTablePrefix(42), roachpb.RKey("\xff")), true, 42},
{keys.MakeTablePrefix(0), true, 0},
{keys.MakeTablePrefix(999), true, 999},
}
for tcNum, tc := range testCases {
id, success := config.ObjectIDForKey(tc.key)
if success != tc.success {
t.Errorf("#%d: expected success=%t", tcNum, tc.success)
continue
}
if id != tc.id {
t.Errorf("#%d: expected id=%d, got %d", tcNum, tc.id, id)
}
}
}
func TestGet(t *testing.T) {
defer leaktest.AfterTest(t)()
emptyKeys := []roachpb.KeyValue{}
someKeys := []roachpb.KeyValue{
plainKV("a", "vala"),
plainKV("c", "valc"),
plainKV("d", "vald"),
}
aVal := roachpb.MakeValueFromString("vala")
bVal := roachpb.MakeValueFromString("valc")
cVal := roachpb.MakeValueFromString("vald")
testCases := []struct {
values []roachpb.KeyValue
key string
value *roachpb.Value
}{
{emptyKeys, "a", nil},
{emptyKeys, "b", nil},
{emptyKeys, "c", nil},
{emptyKeys, "d", nil},
{emptyKeys, "e", nil},
{someKeys, "", nil},
{someKeys, "b", nil},
{someKeys, "e", nil},
{someKeys, "a0", nil},
{someKeys, "a", &aVal},
{someKeys, "c", &bVal},
{someKeys, "d", &cVal},
}
cfg := config.SystemConfig{}
for tcNum, tc := range testCases {
cfg.Values = tc.values
if val := cfg.GetValue([]byte(tc.key)); !proto.Equal(val, tc.value) {
t.Errorf("#%d: expected=%s, found=%s", tcNum, tc.value, val)
}
}
}
func TestGetLargestID(t *testing.T) {
defer leaktest.AfterTest(t)()
testCases := []struct {
values []roachpb.KeyValue
largest uint32
maxID uint32
errStr string
}{
// No data.
{nil, 0, 0, "descriptor table not found"},
// Some data, but not from the system span.
{[]roachpb.KeyValue{plainKV("a", "b")}, 0, 0, "descriptor table not found"},
// Some real data, but no descriptors.
{[]roachpb.KeyValue{
sqlKV(keys.NamespaceTableID, 1, 1),
sqlKV(keys.NamespaceTableID, 1, 2),
sqlKV(keys.UsersTableID, 1, 3),
}, 0, 0, "descriptor table not found"},
// Single correct descriptor entry.
{[]roachpb.KeyValue{sqlKV(keys.DescriptorTableID, 1, 1)}, 1, 0, ""},
// Surrounded by other data.
{[]roachpb.KeyValue{
sqlKV(keys.NamespaceTableID, 1, 20),
sqlKV(keys.NamespaceTableID, 1, 30),
sqlKV(keys.DescriptorTableID, 1, 8),
sqlKV(keys.ZonesTableID, 1, 40),
}, 8, 0, ""},
// Descriptors with holes. Index ID does not matter.
{[]roachpb.KeyValue{
sqlKV(keys.DescriptorTableID, 1, 1),
sqlKV(keys.DescriptorTableID, 2, 5),
sqlKV(keys.DescriptorTableID, 3, 8),
sqlKV(keys.DescriptorTableID, 4, 12),
}, 12, 0, ""},
// Real SQL layout.
{sqlbase.MakeMetadataSchema().GetInitialValues(), keys.MaxSystemConfigDescID + 4, 0, ""},
// Test non-zero max.
{[]roachpb.KeyValue{
sqlKV(keys.DescriptorTableID, 1, 1),
sqlKV(keys.DescriptorTableID, 2, 5),
sqlKV(keys.DescriptorTableID, 3, 8),
sqlKV(keys.DescriptorTableID, 4, 12),
}, 8, 8, ""},
// Test non-zero max.
{[]roachpb.KeyValue{
sqlKV(keys.DescriptorTableID, 1, 1),
sqlKV(keys.DescriptorTableID, 2, 5),
sqlKV(keys.DescriptorTableID, 3, 8),
sqlKV(keys.DescriptorTableID, 4, 12),
}, 5, 7, ""},
}
cfg := config.SystemConfig{}
for tcNum, tc := range testCases {
cfg.Values = tc.values
ret, err := cfg.GetLargestObjectID(tc.maxID)
if tc.errStr == "" {
if err != nil {
t.Errorf("#%d: error: %v", tcNum, err)
continue
}
} else if !testutils.IsError(err, tc.errStr) {
t.Errorf("#%d: expected err=%s, got %v", tcNum, tc.errStr, err)
continue
}
if ret != tc.largest {
t.Errorf("#%d: expected largest=%d, got %d", tcNum, tc.largest, ret)
}
}
}
func TestComputeSplits(t *testing.T) {
defer leaktest.AfterTest(t)()
const (
start = keys.MaxReservedDescID + 1
reservedStart = keys.MaxSystemConfigDescID + 1
)
schema := sqlbase.MakeMetadataSchema()
// Real SQL system tables only.
baseSql := schema.GetInitialValues()
// Real SQL system tables plus some user stuff.
userSql := append(schema.GetInitialValues(),
descriptor(start), descriptor(start+1), descriptor(start+5))
// Real SQL system with reserved non-system tables.
priv := sqlbase.NewDefaultPrivilegeDescriptor()
desc1 := sql.CreateTableDescriptor(reservedStart+1, keys.SystemDatabaseID, "CREATE TABLE system.test1 (i INT PRIMARY KEY)", priv)
schema.AddDescriptor(keys.SystemDatabaseID, &desc1)
desc2 := sql.CreateTableDescriptor(reservedStart+2, keys.SystemDatabaseID, "CREATE TABLE system.test2 (i INT PRIMARY KEY)", priv)
schema.AddDescriptor(keys.SystemDatabaseID, &desc2)
reservedSql := schema.GetInitialValues()
// Real SQL system with reserved non-system and user database.
allSql := append(schema.GetInitialValues(),
descriptor(start), descriptor(start+1), descriptor(start+5))
sort.Sort(roachpb.KeyValueByKey(allSql))
allUserSplits := []uint32{start, start + 1, start + 2, start + 3, start + 4, start + 5}
var allReservedSplits []uint32
for i := 0; i < schema.SystemDescriptorCount()-schema.SystemConfigDescriptorCount(); i++ {
allReservedSplits = append(allReservedSplits, reservedStart+uint32(i))
}
allSplits := append(allReservedSplits, allUserSplits...)
testCases := []struct {
values []roachpb.KeyValue
start, end roachpb.RKey
// Use ints in the testcase definitions, more readable.
splits []uint32
}{
// No data.
{nil, roachpb.RKeyMin, roachpb.RKeyMax, nil},
{nil, keys.MakeTablePrefix(start), roachpb.RKeyMax, nil},
{nil, keys.MakeTablePrefix(start), keys.MakeTablePrefix(start + 10), nil},
{nil, roachpb.RKeyMin, keys.MakeTablePrefix(start + 10), nil},
// No user data.
{baseSql, roachpb.RKeyMin, roachpb.RKeyMax, allReservedSplits},
{baseSql, keys.MakeTablePrefix(start), roachpb.RKeyMax, nil},
{baseSql, keys.MakeTablePrefix(start), keys.MakeTablePrefix(start + 10), nil},
{baseSql, roachpb.RKeyMin, keys.MakeTablePrefix(start + 10), allReservedSplits},
// User descriptors.
{userSql, keys.MakeTablePrefix(start - 1), roachpb.RKeyMax, allUserSplits},
{userSql, keys.MakeTablePrefix(start), roachpb.RKeyMax, allUserSplits[1:]},
{userSql, keys.MakeTablePrefix(start), keys.MakeTablePrefix(start + 10), allUserSplits[1:]},
{userSql, keys.MakeTablePrefix(start - 1), keys.MakeTablePrefix(start + 10), allUserSplits},
{userSql, keys.MakeTablePrefix(start + 4), keys.MakeTablePrefix(start + 10), allUserSplits[5:]},
{userSql, keys.MakeTablePrefix(start + 5), keys.MakeTablePrefix(start + 10), nil},
{userSql, keys.MakeTablePrefix(start + 6), keys.MakeTablePrefix(start + 10), nil},
{userSql, testutils.MakeKey(keys.MakeTablePrefix(start), roachpb.RKey("foo")),
keys.MakeTablePrefix(start + 10), allUserSplits[1:]},
{userSql, testutils.MakeKey(keys.MakeTablePrefix(start), roachpb.RKey("foo")),
keys.MakeTablePrefix(start + 5), allUserSplits[1:5]},
{userSql, testutils.MakeKey(keys.MakeTablePrefix(start), roachpb.RKey("foo")),
testutils.MakeKey(keys.MakeTablePrefix(start+5), roachpb.RKey("bar")), allUserSplits[1:5]},
{userSql, testutils.MakeKey(keys.MakeTablePrefix(start), roachpb.RKey("foo")),
testutils.MakeKey(keys.MakeTablePrefix(start), roachpb.RKey("morefoo")), nil},
// Reserved descriptors.
{reservedSql, roachpb.RKeyMin, roachpb.RKeyMax, allReservedSplits},
{reservedSql, keys.MakeTablePrefix(reservedStart), roachpb.RKeyMax, allReservedSplits[1:]},
{reservedSql, keys.MakeTablePrefix(start), roachpb.RKeyMax, nil},
{reservedSql, keys.MakeTablePrefix(reservedStart), keys.MakeTablePrefix(start + 10), allReservedSplits[1:]},
{reservedSql, roachpb.RKeyMin, keys.MakeTablePrefix(reservedStart + 2), allReservedSplits[:2]},
{reservedSql, roachpb.RKeyMin, keys.MakeTablePrefix(reservedStart + 10), allReservedSplits},
{reservedSql, keys.MakeTablePrefix(reservedStart), keys.MakeTablePrefix(reservedStart + 2), allReservedSplits[1:2]},
{reservedSql, testutils.MakeKey(keys.MakeTablePrefix(reservedStart), roachpb.RKey("foo")),
testutils.MakeKey(keys.MakeTablePrefix(start+10), roachpb.RKey("foo")), allReservedSplits[1:]},
// Reserved/User mix.
{allSql, roachpb.RKeyMin, roachpb.RKeyMax, allSplits},
{allSql, keys.MakeTablePrefix(reservedStart + 1), roachpb.RKeyMax, allSplits[2:]},
{allSql, keys.MakeTablePrefix(start), roachpb.RKeyMax, allUserSplits[1:]},
{allSql, keys.MakeTablePrefix(reservedStart), keys.MakeTablePrefix(start + 10), allSplits[1:]},
{allSql, roachpb.RKeyMin, keys.MakeTablePrefix(start + 2), allSplits[:6]},
{allSql, testutils.MakeKey(keys.MakeTablePrefix(reservedStart), roachpb.RKey("foo")),
testutils.MakeKey(keys.MakeTablePrefix(start+5), roachpb.RKey("foo")), allSplits[1:9]},
}
cfg := config.SystemConfig{}
for tcNum, tc := range testCases {
cfg.Values = tc.values
splits := cfg.ComputeSplitKeys(tc.start, tc.end)
if len(splits) == 0 && len(tc.splits) == 0 {
continue
}
// Convert ints to actual keys.
expected := []roachpb.RKey{}
for _, s := range tc.splits {
expected = append(expected, keys.MakeRowSentinelKey(keys.MakeTablePrefix(s)))
}
if !reflect.DeepEqual(splits, expected) {
t.Errorf("#%d: bad splits:\ngot: %v\nexpected: %v", tcNum, splits, expected)
}
}
}
func TestZoneConfigValidate(t *testing.T) {
defer leaktest.AfterTest(t)()
testCases := []struct {
cfg config.ZoneConfig
expected string
}{
{
config.ZoneConfig{},
"attributes for at least one replica must be specified in zone config",
},
{
config.ZoneConfig{
NumReplicas: 2,
},
"at least 3 replicas are required for multi-replica configurations",
},
{
config.ZoneConfig{
NumReplicas: 1,
},
"RangeMaxBytes 0 less than minimum allowed",
},
{
config.ZoneConfig{
NumReplicas: 1,
RangeMaxBytes: config.DefaultZoneConfig().RangeMaxBytes,
},
"",
},
{
config.ZoneConfig{
NumReplicas: 1,
RangeMinBytes: config.DefaultZoneConfig().RangeMaxBytes,
RangeMaxBytes: config.DefaultZoneConfig().RangeMaxBytes,
},
"is greater than or equal to RangeMaxBytes",
},
}
for i, c := range testCases {
err := c.cfg.Validate()
if c.expected == "" {
if err != nil {
t.Fatalf("%d: expected success, but got %v", i, err)
}
} else if !testutils.IsError(err, c.expected) {
t.Fatalf("%d: expected %s, but got %v", i, c.expected, err)
}
}
}
// TestZoneConfigMarshalYAML makes sure that ZoneConfig is correctly marshaled
// to YAML and back.
func TestZoneConfigMarshalYAML(t *testing.T) {
defer leaktest.AfterTest(t)()
original := config.ZoneConfig{
RangeMinBytes: 1,
RangeMaxBytes: 1,
GC: config.GCPolicy{
TTLSeconds: 1,
},
NumReplicas: 1,
Constraints: config.Constraints{
Constraints: []config.Constraint{
{
Type: config.Constraint_POSITIVE,
Value: "foo",
},
{
Type: config.Constraint_REQUIRED,
Key: "duck",
Value: "foo",
},
{
Type: config.Constraint_PROHIBITED,
Key: "duck",
Value: "foo",
},
},
},
}
expected := `range_min_bytes: 1
range_max_bytes: 1
gc:
ttlseconds: 1
num_replicas: 1
constraints: [foo, +duck=foo, -duck=foo]
`
body, err := yaml.Marshal(original)
if err != nil {
t.Fatal(err)
}
if string(body) != expected {
t.Fatalf("yaml.Marshal(%+v) = %s; not %s", original, body, expected)
}
var unmarshaled config.ZoneConfig
if err := yaml.Unmarshal(body, &unmarshaled); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(unmarshaled, original) {
t.Errorf("yaml.Unmarshal(%q) = %+v; not %+v", body, unmarshaled, original)
}
}