forked from cockroachdb/cockroach
/
addressing.go
115 lines (105 loc) · 4.3 KB
/
addressing.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
// Copyright 2014 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. See the AUTHORS file
// for names of contributors.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
package storage
import (
"bytes"
"github.com/cockroachdb/cockroach/client"
"github.com/cockroachdb/cockroach/keys"
"github.com/cockroachdb/cockroach/proto"
"github.com/cockroachdb/cockroach/util"
)
type metaAction func([]client.Call, proto.Key, *proto.RangeDescriptor) []client.Call
func putMeta(calls []client.Call, key proto.Key, desc *proto.RangeDescriptor) []client.Call {
return append(calls, client.PutProto(key, desc))
}
func delMeta(calls []client.Call, key proto.Key, desc *proto.RangeDescriptor) []client.Call {
return append(calls, client.Delete(key))
}
// splitRangeAddressing creates (or overwrites if necessary) the meta1
// and meta2 range addressing records for the left and right ranges
// caused by a split.
func splitRangeAddressing(left, right *proto.RangeDescriptor) ([]client.Call, error) {
var calls []client.Call
var err error
if calls, err = rangeAddressing(calls, left, putMeta); err != nil {
return nil, err
}
if calls, err = rangeAddressing(calls, right, putMeta); err != nil {
return nil, err
}
return calls, nil
}
// mergeRangeAddressing removes subsumed meta1 and meta2 range
// addressing records caused by merging and updates the records for
// the new merged range. Left is the range descriptor for the "left"
// range before merging and merged describes the left to right merge.
func mergeRangeAddressing(left, merged *proto.RangeDescriptor) ([]client.Call, error) {
var calls []client.Call
var err error
if calls, err = rangeAddressing(calls, left, delMeta); err != nil {
return nil, err
}
if calls, err = rangeAddressing(calls, merged, putMeta); err != nil {
return nil, err
}
return calls, nil
}
// updateRangeAddressing overwrites the meta1 and meta2 range addressing
// records for the descriptor. Returns a slice of calls necessary to
// update the records on the KV database.
func updateRangeAddressing(desc *proto.RangeDescriptor) ([]client.Call, error) {
return rangeAddressing([]client.Call{}, desc, putMeta)
}
// rangeAddressing updates or deletes the range addressing metadata
// for the range specified by desc. The action to take is specified by
// the supplied metaAction function.
//
// The rules for meta1 and meta2 records are as follows:
//
// 1. If desc.StartKey or desc.EndKey is meta1:
// - ERROR
// 2. If desc.EndKey is meta2:
// - meta1(desc.EndKey)
// 3. If desc.EndKey is normal user key:
// - meta2(desc.EndKey)
// 3a. If desc.StartKey is KeyMin or meta2:
// - meta1(KeyMax)
func rangeAddressing(calls []client.Call, desc *proto.RangeDescriptor,
action metaAction) ([]client.Call, error) {
// 1. handle illegal case of start or end key being meta1.
if bytes.HasPrefix(desc.EndKey, keys.Meta1Prefix) ||
bytes.HasPrefix(desc.StartKey, keys.Meta1Prefix) {
return nil, util.Errorf("meta1 addressing records cannot be split: %+v", desc)
}
// 2. the case of the range ending with a meta2 prefix. This means
// the range is full of meta2. We must update the relevant meta1
// entry pointing to the end of this range.
if bytes.HasPrefix(desc.EndKey, keys.Meta2Prefix) {
calls = action(calls, keys.RangeMetaKey(desc.EndKey), desc)
} else {
// 3. the range ends with a normal user key, so we must update the
// relevant meta2 entry pointing to the end of this range.
calls = action(calls, keys.MakeKey(keys.Meta2Prefix, desc.EndKey), desc)
// 3a. the range starts with KeyMin or a meta2 addressing record,
// update the meta1 entry for KeyMax.
if bytes.Equal(desc.StartKey, proto.KeyMin) ||
bytes.HasPrefix(desc.StartKey, keys.Meta2Prefix) {
calls = action(calls, keys.MakeKey(keys.Meta1Prefix, proto.KeyMax), desc)
}
}
return calls, nil
}