forked from cockroachdb/cockroach
/
config.go
400 lines (361 loc) · 12 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
// 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"
"crypto/sha1"
"fmt"
"sort"
"github.com/cockroachdb/cockroach/keys"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/util"
"github.com/cockroachdb/cockroach/util/encoding"
"github.com/cockroachdb/cockroach/util/log"
)
const (
// minRangeMaxBytes is the minimum value for range max bytes.
// TODO(marc): should we revise this lower?
minRangeMaxBytes = 1 << 20
)
var (
// defaultZoneConfig is the default zone configuration used when no custom
// config has been specified.
defaultZoneConfig = ZoneConfig{
ReplicaAttrs: []roachpb.Attributes{
{},
{},
{},
},
RangeMinBytes: 1 << 20,
RangeMaxBytes: 64 << 20,
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 func(SystemConfig, uint32) (*ZoneConfig, error)
// testingLargestIDHook is a function used to bypass GetLargestObjectID
// in tests.
testingLargestIDHook func(uint32) uint32
// testingDisableTableSplits is a testing-only variable that disables
// splits of tables into separate ranges.
testingDisableTableSplits bool
)
// TestingDisableTableSplits is a testing-only function that disables
// splits of tables into separate ranges. It returns a function
// that re-enables this splitting.
func TestingDisableTableSplits() func() {
testingLock.Lock()
if testingDisableTableSplits {
log.Fatal("TestingDisableTableSplits called twice without cleaning up")
}
testingDisableTableSplits = true
testingLock.Unlock()
return func() {
testingLock.Lock()
testingDisableTableSplits = false
testingLock.Unlock()
}
}
// TestingTableSplitsDisabled is a testing-only function that returns true if table
// splits are currently disabled.
func TestingTableSplitsDisabled() bool {
testingLock.Lock()
defer testingLock.Unlock()
return testingDisableTableSplits
}
// 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 {
if len(z.ReplicaAttrs) == 0 {
return util.Errorf("attributes for at least one replica must be specified in zone config")
}
if z.RangeMaxBytes < minRangeMaxBytes {
return util.Errorf("RangeMaxBytes %d less than minimum allowed %d", z.RangeMaxBytes, minRangeMaxBytes)
}
if z.RangeMinBytes >= z.RangeMaxBytes {
return util.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
}
// Hash returns a SHA1 hash of the SystemConfig contents.
func (s SystemConfig) Hash() []byte {
sha := sha1.New()
for _, kv := range s.Values {
_, _ = sha.Write(kv.Key)
// There are all kinds of different types here, so we can't use the typed
// getters.
_, _ = sha.Write(kv.Value.RawBytes)
}
return sha.Sum(nil)
}
// 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, util.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, only user databases and tables get custom zone configs.
objectID = keys.RootNamespaceID
}
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 hook == nil {
return nil, util.Errorf("ZoneConfigHook not set, unable to lookup zone config")
}
if cfg, err := hook(s, id); cfg != nil || err != nil {
return cfg, err
}
return &defaultZoneConfig, nil
}
// ComputeSplitKeys takes a start and end key and returns an array of keys
// at which to split the span [start, end).
// The only required splits are at each user table prefix.
func (s SystemConfig) ComputeSplitKeys(startKey, endKey roachpb.RKey) []roachpb.RKey {
if TestingTableSplitsDisabled() {
return nil
}
tableStart := roachpb.RKey(keys.SystemConfigTableDataMax)
if !tableStart.Less(endKey) {
// This range is before the user tables span: no required splits.
return nil
}
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.
var splitKeys []roachpb.RKey
var key roachpb.RKey
// appendSplitKeys generates all possible split keys between the given range
// of IDs and adds them to splitKeys.
appendSplitKeys := func(startID, endID uint32) {
// endID could be smaller than startID if we don't have user tables.
for id := startID; id <= endID; id++ {
key = keys.MakeNonColumnKey(keys.MakeTablePrefix(id))
// Skip if this ID matches the startKey passed to ComputeSplitKeys.
if !startKey.Less(key) {
continue
}
// Handle the case where EndKey is already a table prefix.
if !key.Less(endKey) {
break
}
splitKeys = append(splitKeys, key)
}
}
// 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("unable to determine largest reserved object ID from system config: %s", err)
return nil
}
appendSplitKeys(startID, endID)
startID = keys.MaxReservedDescID + 1
}
// Append keys in the user space.
endID, err := s.GetLargestObjectID(0)
if err != nil {
log.Errorf("unable to determine largest object ID from system config: %s", err)
return nil
}
appendSplitKeys(startID, endID)
return splitKeys
}
// 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.ComputeSplitKeys(startKey, endKey)) > 0
}