forked from vitessio/vitess
/
shard.go
321 lines (276 loc) · 9.41 KB
/
shard.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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package topo
import (
"fmt"
"html/template"
"strings"
"sync"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/vt/concurrency"
"github.com/youtube/vitess/go/vt/key"
)
// Functions for dealing with shard representations in topology.
// SourceShard represents a data source for filtered replication
// accross shards. When this is used in a destination shard, the master
// of that shard will run filtered replication.
type SourceShard struct {
// Uid is the unique ID for this SourceShard object.
// It is for instance used as a unique index in blp_checkpoint
// when storing the position. It should be unique whithin a
// destination Shard, but not globally unique.
Uid uint32
// the source keyspace
Keyspace string
// the source shard
Shard string
// The source shard keyrange
// If partial, len(Tables) has to be zero
KeyRange key.KeyRange
// The source table list to replicate
// If non-empty, KeyRange must not be partial (must be KeyRange{})
Tables []string
}
// String returns a printable view of a SourceShard.
func (source *SourceShard) String() string {
return fmt.Sprintf("SourceShard(%v,%v/%v)", source.Uid, source.Keyspace, source.Shard)
}
// AsHTML returns a HTML version of the object.
func (source *SourceShard) AsHTML() template.HTML {
result := fmt.Sprintf("<b>Uid</b>: %v</br>\n<b>Source</b>: %v/%v</br>\n", source.Uid, source.Keyspace, source.Shard)
if source.KeyRange.IsPartial() {
result += fmt.Sprintf("<b>KeyRange</b>: %v-%v</br>\n",
source.KeyRange.Start.Hex(), source.KeyRange.End.Hex())
}
if len(source.Tables) > 0 {
result += fmt.Sprintf("<b>Tables</b>: %v</br>\n",
strings.Join(source.Tables, " "))
}
return template.HTML(result)
}
// A pure data struct for information stored in topology server. This
// node is used to present a controlled view of the shard, unaware of
// every management action. It also contains configuration data for a
// shard.
type Shard struct {
// There can be only at most one master, but there may be none. (0)
MasterAlias TabletAlias
// This must match the shard name based on our other conventions, but
// helpful to have it decomposed here.
KeyRange key.KeyRange
// ServedTypes is a list of all the tablet types this shard will
// serve. This is usually used with overlapping shards during
// data shuffles like shard splitting.
ServedTypes []TabletType
// SourceShards is the list of shards we're replicating from,
// using filtered replication.
SourceShards []SourceShard
// Cells is the list of cells that have tablets for this shard.
// It is populated at InitTablet time when a tabelt is added
// in a cell that is not in the list yet.
Cells []string
}
func newShard() *Shard {
return &Shard{}
}
// ValidateShardName takes a shard name and sanitizes it, and also returns
// the KeyRange.
func ValidateShardName(shard string) (string, key.KeyRange, error) {
if !strings.Contains(shard, "-") {
return shard, key.KeyRange{}, nil
}
parts := strings.Split(shard, "-")
if len(parts) != 2 {
return "", key.KeyRange{}, fmt.Errorf("Invalid shardId, can only contain one '-': %v", shard)
}
keyRange, err := key.ParseKeyRangeParts(parts[0], parts[1])
if err != nil {
return "", key.KeyRange{}, err
}
if keyRange.End != key.MaxKey && keyRange.Start >= keyRange.End {
return "", key.KeyRange{}, fmt.Errorf("Out of order keys: %v is not strictly smaller than %v", keyRange.Start.Hex(), keyRange.End.Hex())
}
return strings.ToLower(shard), keyRange, nil
}
// HasCell returns true if the cell is listed in the Cells for the shard.
func (shard *Shard) HasCell(cell string) bool {
for _, c := range shard.Cells {
if c == cell {
return true
}
}
return false
}
// ShardInfo is a meta struct that contains metadata to give the data
// more context and convenience. This is the main way we interact with a shard.
type ShardInfo struct {
keyspace string
shardName string
version int64
*Shard
}
// Keyspace returns the keyspace a shard belongs to
func (si *ShardInfo) Keyspace() string {
return si.keyspace
}
// ShardName returns the shard name for a shard
func (si *ShardInfo) ShardName() string {
return si.shardName
}
// Version returns the shard version from last time it was read or updated.
func (si *ShardInfo) Version() int64 {
return si.version
}
// NewShardInfo returns a ShardInfo basing on shard with the
// keyspace / shard. This function should be only used by Server
// implementations.
func NewShardInfo(keyspace, shard string, value *Shard, version int64) *ShardInfo {
return &ShardInfo{
keyspace: keyspace,
shardName: shard,
version: version,
Shard: value,
}
}
// UpdateShard updates the shard data, with the right version
func UpdateShard(ts Server, si *ShardInfo) error {
var version int64 = -1
if si.version != 0 {
version = si.version
}
newVersion, err := ts.UpdateShard(si, version)
if err == nil {
si.version = newVersion
}
return err
}
// CreateShard creates a new shard and tries to fill in the right information.
func CreateShard(ts Server, keyspace, shard string) error {
name, keyRange, err := ValidateShardName(shard)
if err != nil {
return err
}
s := &Shard{KeyRange: keyRange}
// start the shard with all serving types. If it overlaps with
// other shards for some serving types, remove them.
servingTypes := map[TabletType]bool{
TYPE_MASTER: true,
TYPE_REPLICA: true,
TYPE_RDONLY: true,
}
sis, err := FindAllShardsInKeyspace(ts, keyspace)
if err != nil && err != ErrNoNode {
return err
}
for _, si := range sis {
if key.KeyRangesIntersect(si.KeyRange, keyRange) {
for _, t := range si.ServedTypes {
delete(servingTypes, t)
}
}
}
s.ServedTypes = make([]TabletType, 0, len(servingTypes))
for st := range servingTypes {
s.ServedTypes = append(s.ServedTypes, st)
}
return ts.CreateShard(keyspace, name, s)
}
// InCellList returns true if the cell list is empty,
// or if the passed cell is in the cell list.
func InCellList(cell string, cells []string) bool {
if len(cells) == 0 {
return true
}
for _, c := range cells {
if c == cell {
return true
}
}
return false
}
// FindAllTabletAliasesInShard uses the replication graph to find all the
// tablet aliases in the given shard.
// It can return ErrPartialResult if some cells were not fetched,
// in which case the result only contains the cells that were fetched.
func FindAllTabletAliasesInShard(ts Server, keyspace, shard string) ([]TabletAlias, error) {
return FindAllTabletAliasesInShardByCell(ts, keyspace, shard, nil)
}
// FindAllTabletAliasesInShard uses the replication graph to find all the
// tablet aliases in the given shard.
// It can return ErrPartialResult if some cells were not fetched,
// in which case the result only contains the cells that were fetched.
func FindAllTabletAliasesInShardByCell(ts Server, keyspace, shard string, cells []string) ([]TabletAlias, error) {
// read the shard information to find the cells
si, err := ts.GetShard(keyspace, shard)
if err != nil {
return nil, err
}
resultAsMap := make(map[TabletAlias]bool)
if !si.MasterAlias.IsZero() {
if InCellList(si.MasterAlias.Cell, cells) {
resultAsMap[si.MasterAlias] = true
}
}
// read the replication graph in each cell and add all found tablets
wg := sync.WaitGroup{}
mutex := sync.Mutex{}
rec := concurrency.AllErrorRecorder{}
for _, cell := range si.Cells {
if !InCellList(cell, cells) {
continue
}
wg.Add(1)
go func(cell string) {
defer wg.Done()
sri, err := ts.GetShardReplication(cell, keyspace, shard)
if err != nil {
rec.RecordError(fmt.Errorf("GetShardReplication(%v, %v, %v) failed: %v", cell, keyspace, shard, err))
return
}
mutex.Lock()
for _, rl := range sri.ReplicationLinks {
resultAsMap[rl.TabletAlias] = true
if !rl.Parent.IsZero() && InCellList(rl.Parent.Cell, cells) {
resultAsMap[rl.Parent] = true
}
}
mutex.Unlock()
}(cell)
}
wg.Wait()
err = nil
if rec.HasErrors() {
log.Warningf("FindAllTabletAliasesInShard(%v,%v): got partial result: %v", keyspace, shard, rec.Error())
err = ErrPartialResult
}
result := make([]TabletAlias, 0, len(resultAsMap))
for a := range resultAsMap {
result = append(result, a)
}
return result, err
}
// GetTabletMapForShard returns the tablets for a shard. It can return
// ErrPartialResult if it couldn't read all the cells, or all
// the individual tablets, in which case the map is valid, but partial.
func GetTabletMapForShard(ts Server, keyspace, shard string) (map[TabletAlias]*TabletInfo, error) {
return GetTabletMapForShardByCell(ts, keyspace, shard, nil)
}
// GetTabletMapForShardByCell returns the tablets for a shard. It can return
// ErrPartialResult if it couldn't read all the cells, or all
// the individual tablets, in which case the map is valid, but partial.
func GetTabletMapForShardByCell(ts Server, keyspace, shard string, cells []string) (map[TabletAlias]*TabletInfo, error) {
// if we get a partial result, we keep going. It most likely means
// a cell is out of commission.
aliases, err := FindAllTabletAliasesInShardByCell(ts, keyspace, shard, cells)
if err != nil && err != ErrPartialResult {
return nil, err
}
// get the tablets for the cells we were able to reach, forward
// ErrPartialResult from FindAllTabletAliasesInShard
result, gerr := GetTabletMap(ts, aliases)
if gerr == nil && err != nil {
gerr = err
}
return result, gerr
}