-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
config.go
508 lines (462 loc) · 15.4 KB
/
config.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// 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 (marc@cockroachlabs.com)
package config
import (
"bytes"
"fmt"
"sort"
"strings"
yaml "gopkg.in/yaml.v2"
"golang.org/x/net/context"
"github.com/pkg/errors"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
const (
// minRangeMaxBytes is the minimum value for range max bytes.
minRangeMaxBytes = 64 << 10 // 64 KB
)
type zoneConfigHook func(SystemConfig, uint32) (ZoneConfig, bool, error)
var (
// defaultZoneConfig is the default zone configuration used when no custom
// config has been specified.
defaultZoneConfig = ZoneConfig{
NumReplicas: 3,
RangeMinBytes: 1 << 20, // 1 MB
RangeMaxBytes: 64 << 20, // 64 MB
GC: GCPolicy{
TTLSeconds: 24 * 60 * 60, // 1 day
},
}
// ZoneConfigHook is a function used to lookup a zone config given a table
// or database ID.
// This is also used by testing to simplify fake configs.
ZoneConfigHook zoneConfigHook
// testingLargestIDHook is a function used to bypass GetLargestObjectID
// in tests.
testingLargestIDHook func(uint32) uint32
)
func (c Constraint) String() string {
var str string
switch c.Type {
case Constraint_REQUIRED:
str += "+"
case Constraint_PROHIBITED:
str += "-"
}
if len(c.Key) > 0 {
str += c.Key + "="
}
str += c.Value
return str
}
// FromString populates the constraint from the constraint shorthand notation.
func (c *Constraint) FromString(short string) error {
switch short[0] {
case '+':
c.Type = Constraint_REQUIRED
short = short[1:]
case '-':
c.Type = Constraint_PROHIBITED
short = short[1:]
default:
c.Type = Constraint_POSITIVE
}
parts := strings.Split(short, "=")
if len(parts) == 1 {
c.Value = parts[0]
} else if len(parts) == 2 {
c.Key = parts[0]
c.Value = parts[1]
} else {
return errors.Errorf("constraint needs to be in the form \"(key=)value\", not %q", short)
}
return nil
}
var _ yaml.Marshaler = Constraints{}
var _ yaml.Unmarshaler = &Constraints{}
// MarshalYAML implements yaml.Marshaler.
func (c Constraints) MarshalYAML() (interface{}, error) {
short := make([]string, len(c.Constraints))
for i, c := range c.Constraints {
short[i] = c.String()
}
return short, nil
}
// UnmarshalYAML implements yaml.Unmarshaler.
func (c *Constraints) UnmarshalYAML(unmarshal func(interface{}) error) error {
var shortConstraints []string
if err := unmarshal(&shortConstraints); err != nil {
return err
}
constraints := make([]Constraint, len(shortConstraints))
for i, short := range shortConstraints {
if err := constraints[i].FromString(short); err != nil {
return err
}
}
c.Constraints = constraints
return nil
}
// DefaultZoneConfig is the default zone configuration used when no custom
// config has been specified.
func DefaultZoneConfig() ZoneConfig {
testingLock.Lock()
defer testingLock.Unlock()
return defaultZoneConfig
}
// TestingSetDefaultZoneConfig is a testing-only function that changes the
// default zone config and returns a function that reverts the change.
func TestingSetDefaultZoneConfig(cfg ZoneConfig) func() {
testingLock.Lock()
oldConfig := defaultZoneConfig
defaultZoneConfig = cfg
testingLock.Unlock()
return func() {
testingLock.Lock()
defaultZoneConfig = oldConfig
testingLock.Unlock()
}
}
// Validate verifies some ZoneConfig fields.
// This should be used to validate user input when setting a new zone config.
func (z ZoneConfig) Validate() error {
switch z.NumReplicas {
case 0:
return fmt.Errorf("attributes for at least one replica must be specified in zone config")
case 2:
return fmt.Errorf("at least 3 replicas are required for multi-replica configurations")
}
if z.RangeMaxBytes < minRangeMaxBytes {
return fmt.Errorf("RangeMaxBytes %d less than minimum allowed %d",
z.RangeMaxBytes, minRangeMaxBytes)
}
if z.RangeMinBytes >= z.RangeMaxBytes {
return fmt.Errorf("RangeMinBytes %d is greater than or equal to RangeMaxBytes %d",
z.RangeMinBytes, z.RangeMaxBytes)
}
return nil
}
// ObjectIDForKey returns the object ID (table or database) for 'key',
// or (_, false) if not within the structured key space.
func ObjectIDForKey(key roachpb.RKey) (uint32, bool) {
if key.Equal(roachpb.RKeyMax) {
return 0, false
}
if encoding.PeekType(key) != encoding.Int {
// TODO(marc): this should eventually return SystemDatabaseID.
return 0, false
}
// Consume first encoded int.
_, id64, err := encoding.DecodeUvarintAscending(key)
return uint32(id64), err == nil
}
// Equal checks for equality.
//
// It assumes that s.Values and other.Values are sorted in key order.
func (s SystemConfig) Equal(other SystemConfig) bool {
if len(s.Values) != len(other.Values) {
return false
}
for i := range s.Values {
leftKV, rightKV := s.Values[i], other.Values[i]
if !leftKV.Key.Equal(rightKV.Key) {
return false
}
leftVal, rightVal := leftKV.Value, rightKV.Value
if !bytes.Equal(leftVal.RawBytes, rightVal.RawBytes) {
return false
}
if leftVal.Timestamp != rightVal.Timestamp {
return false
}
}
return true
}
// GetValue searches the kv list for 'key' and returns its
// roachpb.Value if found.
func (s SystemConfig) GetValue(key roachpb.Key) *roachpb.Value {
if kv := s.get(key); kv != nil {
return &kv.Value
}
return nil
}
// get searches the kv list for 'key' and returns its roachpb.KeyValue
// if found.
func (s SystemConfig) get(key roachpb.Key) *roachpb.KeyValue {
if index, found := s.GetIndex(key); found {
// TODO(marc): I'm pretty sure a Value returned by MVCCScan can
// never be nil. Should check.
return &s.Values[index]
}
return nil
}
// GetIndex searches the kv list for 'key' and returns its index if found.
func (s SystemConfig) GetIndex(key roachpb.Key) (int, bool) {
l := len(s.Values)
index := sort.Search(l, func(i int) bool {
return bytes.Compare(s.Values[i].Key, key) >= 0
})
if index == l || !key.Equal(s.Values[index].Key) {
return 0, false
}
return index, true
}
func decodeDescMetadataID(key roachpb.Key) (uint64, error) {
// Extract object ID from key.
// TODO(marc): move sql/keys.go to keys (or similar) and use a DecodeDescMetadataKey.
// We should also check proper encoding.
remaining, tableID, err := keys.DecodeTablePrefix(key)
if err != nil {
return 0, err
}
if tableID != keys.DescriptorTableID {
return 0, errors.Errorf("key is not a descriptor table entry: %v", key)
}
// DescriptorTable.PrimaryIndex.ID
remaining, _, err = encoding.DecodeUvarintAscending(remaining)
if err != nil {
return 0, err
}
// descID
_, id, err := encoding.DecodeUvarintAscending(remaining)
if err != nil {
return 0, err
}
return id, nil
}
// GetLargestObjectID returns the largest object ID found in the config which is
// less than or equal to maxID. If maxID is 0, returns the largest ID in the
// config.
func (s SystemConfig) GetLargestObjectID(maxID uint32) (uint32, error) {
testingLock.Lock()
hook := testingLargestIDHook
testingLock.Unlock()
if hook != nil {
return hook(maxID), nil
}
// Search for the descriptor table entries within the SystemConfig.
highBound := roachpb.Key(keys.MakeTablePrefix(keys.DescriptorTableID + 1))
highIndex := sort.Search(len(s.Values), func(i int) bool {
return bytes.Compare(s.Values[i].Key, highBound) >= 0
})
lowBound := roachpb.Key(keys.MakeTablePrefix(keys.DescriptorTableID))
lowIndex := sort.Search(len(s.Values), func(i int) bool {
return bytes.Compare(s.Values[i].Key, lowBound) >= 0
})
if highIndex == lowIndex {
return 0, fmt.Errorf("descriptor table not found in system config of %d values", len(s.Values))
}
// No maximum specified; maximum ID is the last entry in the descriptor
// table.
if maxID == 0 {
id, err := decodeDescMetadataID(s.Values[highIndex-1].Key)
if err != nil {
return 0, err
}
return uint32(id), nil
}
// Maximum specified: need to search the descriptor table. Binary search
// through all descriptor table values to find the first descriptor with ID
// >= maxID.
searchSlice := s.Values[lowIndex:highIndex]
var err error
maxIdx := sort.Search(len(searchSlice), func(i int) bool {
var id uint64
id, err = decodeDescMetadataID(searchSlice[i].Key)
if err != nil {
return false
}
return uint32(id) >= maxID
})
if err != nil {
return 0, err
}
// If we found an index within the list, maxIdx might point to a descriptor
// with exactly maxID.
if maxIdx < len(searchSlice) {
id, err := decodeDescMetadataID(searchSlice[maxIdx].Key)
if err != nil {
return 0, err
}
if uint32(id) == maxID {
return uint32(id), nil
}
}
if maxIdx == 0 {
return 0, fmt.Errorf("no descriptors present with ID < %d", maxID)
}
// Return ID of the immediately preceding descriptor.
id, err := decodeDescMetadataID(searchSlice[maxIdx-1].Key)
if err != nil {
return 0, err
}
return uint32(id), nil
}
// GetZoneConfigForKey looks up the zone config for the range containing 'key'.
// It is the caller's responsibility to ensure that the range does not need to be split.
func (s SystemConfig) GetZoneConfigForKey(key roachpb.RKey) (ZoneConfig, error) {
objectID, ok := ObjectIDForKey(key)
if !ok {
// Not in the structured data namespace.
objectID = keys.RootNamespaceID
} else if objectID <= keys.MaxReservedDescID {
// For now, you can only set a zone config on the system database as a whole,
// not on any of its constituent tables. This is largely because all the
// "system config" tables are colocated in the same range by default and
// thus couldn't be managed separately.
objectID = keys.SystemDatabaseID
}
// Special-case known system ranges to their special zone configs.
if key.Equal(roachpb.RKeyMin) || bytes.HasPrefix(key, keys.Meta1Prefix) || bytes.HasPrefix(key, keys.Meta2Prefix) {
objectID = keys.MetaRangesID
} else if bytes.HasPrefix(key, keys.TimeseriesPrefix) {
objectID = keys.TimeseriesRangesID
} else if bytes.HasPrefix(key, keys.SystemPrefix) {
objectID = keys.SystemRangesID
}
return s.getZoneConfigForID(objectID)
}
// getZoneConfigForID looks up the zone config for the object (table or database)
// with 'id'.
func (s SystemConfig) getZoneConfigForID(id uint32) (ZoneConfig, error) {
testingLock.Lock()
hook := ZoneConfigHook
testingLock.Unlock()
if cfg, found, err := hook(s, id); err != nil || found {
return cfg, err
}
return DefaultZoneConfig(), nil
}
// StaticSplits is the list of pre-defined split points in the beginning of
// the keyspace that are there to support separate zone configs for different
// parts of the system / system config ranges.
// Exposed publicly so that its ordering can be tested.
var StaticSplits = []struct {
SplitPoint roachpb.RKey
SplitKey roachpb.RKey
}{
// End of meta records / start of system ranges
{
SplitPoint: roachpb.RKey(keys.SystemPrefix),
SplitKey: roachpb.RKey(keys.SystemPrefix),
},
// Start of timeseries ranges (within system ranges)
{
SplitPoint: roachpb.RKey(keys.TimeseriesPrefix),
SplitKey: roachpb.RKey(keys.TimeseriesPrefix),
},
// End of timeseries ranges (continuation of system ranges)
{
SplitPoint: roachpb.RKey(keys.TimeseriesPrefix.PrefixEnd()),
SplitKey: roachpb.RKey(keys.TimeseriesPrefix.PrefixEnd()),
},
// System config tables (end of system ranges)
{
SplitPoint: roachpb.RKey(keys.TableDataMin),
SplitKey: keys.SystemConfigSplitKey,
},
}
// ComputeSplitKey takes a start and end key and returns the first key at which
// to split the span [start, end). Returns nil if no splits are required.
//
// Splits are required between user tables (i.e. /table/<id>), at the start
// of the system-config tables (i.e. /table/0), and at certain points within the
// system ranges that come before the system tables. The system-config range is
// somewhat special in that it can contain multiple SQL tables
// (/table/0-/table/<max-system-config-desc>) within a single range.
func (s SystemConfig) ComputeSplitKey(startKey, endKey roachpb.RKey) roachpb.RKey {
// Before dealing with splits necessitated by SQL tables, handle all of the
// static splits earlier in the keyspace. Note that this list must be kept in
// the proper order (ascending in the keyspace) for the logic below to work.
for _, split := range StaticSplits {
if startKey.Less(split.SplitPoint) {
if split.SplitPoint.Less(endKey) {
// The split point is contained within [startKey, endKey), so we need to
// create the split.
return split.SplitKey
}
// [startKey, endKey) is contained between the previous split point and
// this split point.
return nil
}
// [startKey, endKey) is somewhere greater than this split point. Continue.
}
// If the above iteration over the static split points didn't decide anything,
// the key range must be somewhere in the SQL table part of the keyspace.
startID, ok := ObjectIDForKey(startKey)
if !ok || startID <= keys.MaxSystemConfigDescID {
// The start key is either:
// - not part of the structured data span
// - part of the system span
// In either case, start looking for splits at the first ID usable
// by the user data span.
startID = keys.MaxSystemConfigDescID + 1
} else {
// The start key is either already a split key, or after the split
// key for its ID. We can skip straight to the next one.
startID++
}
// Build key prefixes for sequential table IDs until we reach endKey. Note
// that there are two disjoint sets of sequential keys: non-system reserved
// tables have sequential IDs, as do user tables, but the two ranges contain a
// gap.
// findSplitKey returns the first possible split key between the given range
// of IDs.
findSplitKey := func(startID, endID uint32) roachpb.RKey {
// endID could be smaller than startID if we don't have user tables.
for id := startID; id <= endID; id++ {
key := roachpb.RKey(keys.MakeRowSentinelKey(keys.MakeTablePrefix(id)))
// Skip if this ID matches the provided startKey.
if !startKey.Less(key) {
continue
}
// Handle the case where EndKey is already a table prefix.
if !key.Less(endKey) {
break
}
return key
}
return nil
}
// If the startKey falls within the non-system reserved range, compute those
// keys first.
if startID <= keys.MaxReservedDescID {
endID, err := s.GetLargestObjectID(keys.MaxReservedDescID)
if err != nil {
log.Errorf(context.TODO(), "unable to determine largest reserved object ID from system config: %s", err)
return nil
}
if splitKey := findSplitKey(startID, endID); splitKey != nil {
return splitKey
}
startID = keys.MaxReservedDescID + 1
}
// Find the split key in the user space.
endID, err := s.GetLargestObjectID(0)
if err != nil {
log.Errorf(context.TODO(), "unable to determine largest object ID from system config: %s", err)
return nil
}
return findSplitKey(startID, endID)
}
// NeedsSplit returns whether the range [startKey, endKey) needs a split due
// to zone configs.
func (s SystemConfig) NeedsSplit(startKey, endKey roachpb.RKey) bool {
return len(s.ComputeSplitKey(startKey, endKey)) > 0
}