forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shard.go
662 lines (588 loc) · 20.9 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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
// 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 (
"encoding/hex"
"fmt"
"reflect"
"sort"
"strings"
"sync"
"golang.org/x/net/context"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/event"
"github.com/youtube/vitess/go/trace"
"github.com/youtube/vitess/go/vt/concurrency"
"github.com/youtube/vitess/go/vt/key"
"github.com/youtube/vitess/go/vt/topo/events"
"github.com/youtube/vitess/go/vt/topo/topoproto"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
)
// Functions for dealing with shard representations in topology.
// addCells will merge both cells list, settling on nil if either list is empty
func addCells(left, right []string) []string {
if len(left) == 0 || len(right) == 0 {
return nil
}
for _, cell := range right {
if !InCellList(cell, left) {
left = append(left, cell)
}
}
return left
}
// removeCells will remove the cells from the provided list. It returns
// the new list, and a boolean that indicates the returned list is empty.
func removeCells(cells, toRemove, fullList []string) ([]string, bool) {
// The assumption here is we already migrated something,
// and we're reverting that part. So we're gonna remove
// records only.
leftoverCells := make([]string, 0, len(cells))
if len(cells) == 0 {
// we migrated all the cells already, take the full list
// and remove all the ones we're not reverting
for _, cell := range fullList {
if !InCellList(cell, toRemove) {
leftoverCells = append(leftoverCells, cell)
}
}
} else {
// we migrated a subset of the cells,
// remove the ones we're reverting
for _, cell := range cells {
if !InCellList(cell, toRemove) {
leftoverCells = append(leftoverCells, cell)
}
}
}
if len(leftoverCells) == 0 {
// we don't have any cell left, we need to clear this record
return nil, true
}
return leftoverCells, false
}
// IsShardUsingRangeBasedSharding returns true if the shard name
// implies it is using range based sharding.
func IsShardUsingRangeBasedSharding(shard string) bool {
return strings.Contains(shard, "-")
}
// ValidateShardName takes a shard name and sanitizes it, and also returns
// the KeyRange.
func ValidateShardName(shard string) (string, *topodatapb.KeyRange, error) {
if !IsShardUsingRangeBasedSharding(shard) {
return shard, nil, nil
}
parts := strings.Split(shard, "-")
if len(parts) != 2 {
return "", nil, fmt.Errorf("invalid shardId, can only contain one '-': %v", shard)
}
keyRange, err := key.ParseKeyRangeParts(parts[0], parts[1])
if err != nil {
return "", nil, err
}
if len(keyRange.End) > 0 && string(keyRange.Start) >= string(keyRange.End) {
return "", nil, fmt.Errorf("out of order keys: %v is not strictly smaller than %v", hex.EncodeToString(keyRange.Start), hex.EncodeToString(keyRange.End))
}
return strings.ToLower(shard), keyRange, nil
}
// 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
*topodatapb.Shard
}
// 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 *topodatapb.Shard, version int64) *ShardInfo {
return &ShardInfo{
keyspace: keyspace,
shardName: shard,
version: version,
Shard: value,
}
}
// 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
}
// HasMaster returns true if the Shard has an assigned Master.
func (si *ShardInfo) HasMaster() bool {
return !topoproto.TabletAliasIsZero(si.Shard.MasterAlias)
}
// HasCell returns true if the cell is listed in the Cells for the shard.
func (si *ShardInfo) HasCell(cell string) bool {
return topoproto.ShardHasCell(si.Shard, cell)
}
// GetShard is a high level function to read shard data.
// It generates trace spans.
func (ts Server) GetShard(ctx context.Context, keyspace, shard string) (*ShardInfo, error) {
span := trace.NewSpanFromContext(ctx)
span.StartClient("TopoServer.GetShard")
span.Annotate("keyspace", keyspace)
span.Annotate("shard", shard)
defer span.Finish()
value, version, err := ts.Impl.GetShard(ctx, keyspace, shard)
if err != nil {
return nil, err
}
return &ShardInfo{
keyspace: keyspace,
shardName: shard,
version: version,
Shard: value,
}, nil
}
// UpdateShard masks ts.Impl.UpdateShard so nobody is tempted to use it.
func (ts Server) UpdateShard() error {
panic("do not call this function directly, use UpdateShardFields instead")
}
// updateShard updates the shard data, with the right version.
// It also creates a span, and dispatches the event.
func (ts Server) updateShard(ctx context.Context, si *ShardInfo) error {
span := trace.NewSpanFromContext(ctx)
span.StartClient("TopoServer.UpdateShard")
span.Annotate("keyspace", si.keyspace)
span.Annotate("shard", si.shardName)
defer span.Finish()
newVersion, err := ts.Impl.UpdateShard(ctx, si.keyspace, si.shardName, si.Shard, si.version)
if err != nil {
return err
}
si.version = newVersion
event.Dispatch(&events.ShardChange{
KeyspaceName: si.Keyspace(),
ShardName: si.ShardName(),
Shard: si.Shard,
Status: "updated",
})
return nil
}
// UpdateShardFields is a high level helper to read a shard record, call an
// update function on it, and then write it back. If the write fails due to
// a version mismatch, it will re-read the record and retry the update.
// If the update succeeds, it returns the updated ShardInfo.
// If the update method returns ErrNoUpdateNeeded, nothing is written,
// and nil,nil is returned.
//
// Note the callback method takes a ShardInfo, so it can get the
// keyspace and shard from it, or use all the ShardInfo methods.
func (ts Server) UpdateShardFields(ctx context.Context, keyspace, shard string, update func(*ShardInfo) error) (*ShardInfo, error) {
for {
si, err := ts.GetShard(ctx, keyspace, shard)
if err != nil {
return nil, err
}
if err = update(si); err != nil {
if err == ErrNoUpdateNeeded {
return nil, nil
}
return nil, err
}
if err = ts.updateShard(ctx, si); err != ErrBadVersion {
return si, err
}
}
}
// CreateShard creates a new shard and tries to fill in the right information.
// This will lock the Keyspace, as we may be looking at other shard servedTypes.
// Using GetOrCreateShard is probably a better idea for most use cases.
func (ts Server) CreateShard(ctx context.Context, keyspace, shard string) (err error) {
// Lock the keyspace, because we'll be looking at ServedTypes.
ctx, unlock, lockErr := ts.LockKeyspace(ctx, keyspace, "CreateShard")
if lockErr != nil {
return lockErr
}
defer unlock(&err)
// validate parameters
name, keyRange, err := ValidateShardName(shard)
if err != nil {
return err
}
// start the shard with all serving types. If it overlaps with
// other shards for some serving types, remove them.
servedTypes := map[topodatapb.TabletType]bool{
topodatapb.TabletType_MASTER: true,
topodatapb.TabletType_REPLICA: true,
topodatapb.TabletType_RDONLY: true,
}
value := &topodatapb.Shard{
KeyRange: keyRange,
}
if IsShardUsingRangeBasedSharding(name) {
// if we are using range-based sharding, we don't want
// overlapping shards to all serve and confuse the clients.
sis, err := ts.FindAllShardsInKeyspace(ctx, keyspace)
if err != nil && err != ErrNoNode {
return err
}
for _, si := range sis {
if si.KeyRange == nil || key.KeyRangesIntersect(si.KeyRange, keyRange) {
for _, st := range si.ServedTypes {
delete(servedTypes, st.TabletType)
}
}
}
}
for st := range servedTypes {
value.ServedTypes = append(value.ServedTypes, &topodatapb.Shard_ServedType{
TabletType: st,
})
}
if err := ts.Impl.CreateShard(ctx, keyspace, name, value); err != nil {
// return error as is, we need to propagate
// ErrNodeExists for instance.
return err
}
event.Dispatch(&events.ShardChange{
KeyspaceName: keyspace,
ShardName: shard,
Shard: value,
Status: "created",
})
return nil
}
// GetOrCreateShard will return the shard object, or create one if it doesn't
// already exist. Note the shard creation is protected by a keyspace Lock.
func (ts Server) GetOrCreateShard(ctx context.Context, keyspace, shard string) (si *ShardInfo, err error) {
si, err = ts.GetShard(ctx, keyspace, shard)
if err != ErrNoNode {
return
}
// create the keyspace, maybe it already exists
if err = ts.CreateKeyspace(ctx, keyspace, &topodatapb.Keyspace{}); err != nil && err != ErrNodeExists {
return nil, fmt.Errorf("CreateKeyspace(%v) failed: %v", keyspace, err)
}
// now try to create with the lock, may already exist
if err = ts.CreateShard(ctx, keyspace, shard); err != nil && err != ErrNodeExists {
return nil, fmt.Errorf("CreateShard(%v/%v) failed: %v", keyspace, shard, err)
}
// try to read the shard again, maybe someone created it
// in between the original GetShard and the LockKeyspace
return ts.GetShard(ctx, keyspace, shard)
}
// DeleteShard wraps the underlying Impl.DeleteShard
// and dispatches the event.
func (ts Server) DeleteShard(ctx context.Context, keyspace, shard string) error {
if err := ts.Impl.DeleteShard(ctx, keyspace, shard); err != nil {
return err
}
event.Dispatch(&events.ShardChange{
KeyspaceName: keyspace,
ShardName: shard,
Shard: nil,
Status: "deleted",
})
return nil
}
// GetTabletControl returns the Shard_TabletControl for the given tablet type,
// or nil if it is not in the map.
func (si *ShardInfo) GetTabletControl(tabletType topodatapb.TabletType) *topodatapb.Shard_TabletControl {
for _, tc := range si.TabletControls {
if tc.TabletType == tabletType {
return tc
}
}
return nil
}
// UpdateSourceBlacklistedTables will add or remove the listed tables
// in the shard record's TabletControl structures. Note we don't
// support a lot of the corner cases:
// - only support one table list per shard. If we encounter a different
// table list that the provided one, we error out.
// - we don't support DisableQueryService at the same time as BlacklistedTables,
// because it's not used in the same context (vertical vs horizontal sharding)
//
// This function should be called while holding the keyspace lock.
func (si *ShardInfo) UpdateSourceBlacklistedTables(ctx context.Context, tabletType topodatapb.TabletType, cells []string, remove bool, tables []string) error {
if err := CheckKeyspaceLocked(ctx, si.keyspace); err != nil {
return err
}
tc := si.GetTabletControl(tabletType)
if tc == nil {
// handle the case where the TabletControl object is new
if remove {
// we try to remove from something that doesn't exist,
// log, but we're done.
log.Warningf("Trying to remove TabletControl.BlacklistedTables for missing type %v in shard %v/%v", tabletType, si.keyspace, si.shardName)
return nil
}
// trying to add more constraints with no existing record
si.TabletControls = append(si.TabletControls, &topodatapb.Shard_TabletControl{
TabletType: tabletType,
Cells: cells,
DisableQueryService: false,
BlacklistedTables: tables,
})
return nil
}
// we have an existing record, check table lists matches and
// DisableQueryService is not set
if tc.DisableQueryService {
return fmt.Errorf("cannot safely alter BlacklistedTables as DisableQueryService is set for shard %v/%v", si.keyspace, si.shardName)
}
if remove {
si.removeCellsFromTabletControl(tc, tabletType, cells)
} else {
if !reflect.DeepEqual(tc.BlacklistedTables, tables) {
return fmt.Errorf("trying to use two different sets of blacklisted tables for shard %v/%v: %v and %v", si.keyspace, si.shardName, tc.BlacklistedTables, tables)
}
tc.Cells = addCells(tc.Cells, cells)
}
return nil
}
// UpdateDisableQueryService will make sure the disableQueryService is
// set appropriately in the shard record. Note we don't support a lot
// of the corner cases:
// - we don't support DisableQueryService at the same time as BlacklistedTables,
// because it's not used in the same context (vertical vs horizontal sharding)
// This function should be called while holding the keyspace lock.
func (si *ShardInfo) UpdateDisableQueryService(ctx context.Context, tabletType topodatapb.TabletType, cells []string, disableQueryService bool) error {
if err := CheckKeyspaceLocked(ctx, si.keyspace); err != nil {
return err
}
tc := si.GetTabletControl(tabletType)
if tc == nil {
// handle the case where the TabletControl object is new
if disableQueryService {
si.TabletControls = append(si.TabletControls, &topodatapb.Shard_TabletControl{
TabletType: tabletType,
Cells: cells,
DisableQueryService: true,
BlacklistedTables: nil,
})
} else {
log.Warningf("Trying to remove TabletControl.DisableQueryService for missing type %v for shard %v/%v", tabletType, si.keyspace, si.shardName)
}
return nil
}
// we have an existing record, check table list is empty and
// DisableQueryService is set
if len(tc.BlacklistedTables) > 0 {
return fmt.Errorf("cannot safely alter DisableQueryService as BlacklistedTables is set")
}
if !tc.DisableQueryService {
return fmt.Errorf("cannot safely alter DisableQueryService as DisableQueryService is not set, this record should not be there for shard %v/%v", si.keyspace, si.shardName)
}
if disableQueryService {
tc.Cells = addCells(tc.Cells, cells)
} else {
si.removeCellsFromTabletControl(tc, tabletType, cells)
}
return nil
}
func (si *ShardInfo) removeCellsFromTabletControl(tc *topodatapb.Shard_TabletControl, tabletType topodatapb.TabletType, cells []string) {
result, emptyList := removeCells(tc.Cells, cells, si.Cells)
if emptyList {
// we don't have any cell left, we need to clear this record
var tabletControls []*topodatapb.Shard_TabletControl
for _, tc := range si.TabletControls {
if tc.TabletType != tabletType {
tabletControls = append(tabletControls, tc)
}
}
si.TabletControls = tabletControls
} else {
tc.Cells = result
}
}
// GetServedType returns the Shard_ServedType for a TabletType, or nil
func (si *ShardInfo) GetServedType(tabletType topodatapb.TabletType) *topodatapb.Shard_ServedType {
for _, st := range si.ServedTypes {
if st.TabletType == tabletType {
return st
}
}
return nil
}
// GetServedTypesPerCell returns the list of types this shard is serving
// in the provided cell.
func (si *ShardInfo) GetServedTypesPerCell(cell string) []topodatapb.TabletType {
result := make([]topodatapb.TabletType, 0, len(si.ServedTypes))
for _, st := range si.ServedTypes {
if InCellList(cell, st.Cells) {
result = append(result, st.TabletType)
}
}
return result
}
// CheckServedTypesMigration makes sure the provided migration is possible
func (si *ShardInfo) CheckServedTypesMigration(tabletType topodatapb.TabletType, cells []string, remove bool) error {
// master is a special case with a few extra checks
if tabletType == topodatapb.TabletType_MASTER {
if len(cells) > 0 {
return fmt.Errorf("cannot migrate only some cells for master in shard %v/%v", si.keyspace, si.shardName)
}
if remove && len(si.ServedTypes) > 1 {
return fmt.Errorf("cannot migrate master away from %v/%v until everything else is migrated", si.keyspace, si.shardName)
}
}
// we can't remove a type we don't have
if si.GetServedType(tabletType) == nil && remove {
return fmt.Errorf("supplied type %v cannot be migrated out of %#v", tabletType, si)
}
return nil
}
// UpdateServedTypesMap handles ServedTypesMap. It can add or remove
// records, cells, ...
func (si *ShardInfo) UpdateServedTypesMap(tabletType topodatapb.TabletType, cells []string, remove bool) error {
// check parameters to be sure
if err := si.CheckServedTypesMigration(tabletType, cells, remove); err != nil {
return err
}
sst := si.GetServedType(tabletType)
if sst == nil {
// the record doesn't exist
if remove {
log.Warningf("Trying to remove ShardServedType for missing type %v in shard %v/%v", tabletType, si.keyspace, si.shardName)
} else {
si.ServedTypes = append(si.ServedTypes, &topodatapb.Shard_ServedType{
TabletType: tabletType,
Cells: cells,
})
}
return nil
}
if remove {
result, emptyList := removeCells(sst.Cells, cells, si.Cells)
if emptyList {
// we don't have any cell left, we need to clear this record
var servedTypes []*topodatapb.Shard_ServedType
for _, st := range si.ServedTypes {
if st.TabletType != tabletType {
servedTypes = append(servedTypes, st)
}
}
si.ServedTypes = servedTypes
} else {
sst.Cells = result
}
} else {
sst.Cells = addCells(sst.Cells, cells)
}
return nil
}
//
// Utility functions for shards
//
// 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.
//
// The tablet aliases are sorted by cell, then by UID.
func (ts Server) FindAllTabletAliasesInShard(ctx context.Context, keyspace, shard string) ([]*topodatapb.TabletAlias, error) {
return ts.FindAllTabletAliasesInShardByCell(ctx, keyspace, shard, nil)
}
// FindAllTabletAliasesInShardByCell 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.
//
// The tablet aliases are sorted by cell, then by UID.
func (ts Server) FindAllTabletAliasesInShardByCell(ctx context.Context, keyspace, shard string, cells []string) ([]*topodatapb.TabletAlias, error) {
span := trace.NewSpanFromContext(ctx)
span.StartLocal("topo.FindAllTabletAliasesInShardbyCell")
span.Annotate("keyspace", keyspace)
span.Annotate("shard", shard)
span.Annotate("num_cells", len(cells))
defer span.Finish()
ctx = trace.NewContext(ctx, span)
// read the shard information to find the cells
si, err := ts.GetShard(ctx, keyspace, shard)
if err != nil {
return nil, err
}
resultAsMap := make(map[topodatapb.TabletAlias]bool)
if si.HasMaster() {
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(ctx, cell, keyspace, shard)
if err != nil {
rec.RecordError(fmt.Errorf("GetShardReplication(%v, %v, %v) failed: %v", cell, keyspace, shard, err))
return
}
mutex.Lock()
for _, node := range sri.Nodes {
resultAsMap[*node.TabletAlias] = 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([]*topodatapb.TabletAlias, 0, len(resultAsMap))
for a := range resultAsMap {
v := a
result = append(result, &v)
}
sort.Sort(topoproto.TabletAliasList(result))
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 (ts Server) GetTabletMapForShard(ctx context.Context, keyspace, shard string) (map[topodatapb.TabletAlias]*TabletInfo, error) {
return ts.GetTabletMapForShardByCell(ctx, 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 (ts Server) GetTabletMapForShardByCell(ctx context.Context, keyspace, shard string, cells []string) (map[topodatapb.TabletAlias]*TabletInfo, error) {
// if we get a partial result, we keep going. It most likely means
// a cell is out of commission.
aliases, err := ts.FindAllTabletAliasesInShardByCell(ctx, 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 := ts.GetTabletMap(ctx, aliases)
if gerr == nil && err != nil {
gerr = err
}
return result, gerr
}