forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
110 lines (101 loc) · 3.97 KB
/
db.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
// 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/proto"
"github.com/cockroachdb/cockroach/storage/engine"
"github.com/cockroachdb/cockroach/util"
)
type metaAction func(*client.KV, proto.Key, *proto.RangeDescriptor) error
func putMeta(db *client.KV, key proto.Key, desc *proto.RangeDescriptor) error {
return db.PreparePutProto(key, desc)
}
func delMeta(db *client.KV, key proto.Key, desc *proto.RangeDescriptor) error {
db.Prepare(proto.Delete, proto.DeleteArgs(key), &proto.DeleteResponse{})
return nil
}
// 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(db *client.KV, left, right *proto.RangeDescriptor) error {
if err := updateRangeAddressing(db, left, putMeta); err != nil {
return err
}
if err := updateRangeAddressing(db, right, putMeta); err != nil {
return err
}
return 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(db *client.KV, left, merged *proto.RangeDescriptor) error {
if err := updateRangeAddressing(db, left, delMeta); err != nil {
return err
}
if err := updateRangeAddressing(db, merged, putMeta); err != nil {
return err
}
return nil
}
// updateRangeAddressing 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 updateRangeAddressing(db *client.KV, desc *proto.RangeDescriptor, action metaAction) error {
// 1. handle illegal case of start or end key being meta1.
if bytes.HasPrefix(desc.EndKey, engine.KeyMeta1Prefix) ||
bytes.HasPrefix(desc.StartKey, engine.KeyMeta1Prefix) {
return 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, engine.KeyMeta2Prefix) {
if err := action(db, engine.RangeMetaKey(desc.EndKey), desc); err != nil {
return err
}
} 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.
if err := action(db, engine.MakeKey(engine.KeyMeta2Prefix, desc.EndKey), desc); err != nil {
return err
}
// 3a. the range starts with KeyMin or a meta2 addressing record,
// update the meta1 entry for KeyMax.
if bytes.Equal(desc.StartKey, engine.KeyMin) ||
bytes.HasPrefix(desc.StartKey, engine.KeyMeta2Prefix) {
if err := action(db, engine.MakeKey(engine.KeyMeta1Prefix, engine.KeyMax), desc); err != nil {
return err
}
}
}
return nil
}