forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shard.go
66 lines (56 loc) · 1.67 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
package topovalidator
import (
"fmt"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/vt/topo"
)
// This file contains the Shard validator. It uses GetKeyspaces to
// find all the keyspaces, then uses GetShardNames to read all shards, then tries to read them. If any error occurs
// during the reading, it adds a fixer to either Delete or Create the
// shard.
// RegisterShardValidator registers the Shard Validator.
func RegisterShardValidator() {
RegisterValidator("Shard Validator", &ShardValidator{})
}
// ShardValidator implements Validator.
type ShardValidator struct{}
// Audit is part of the Validator interface.
func (kv *ShardValidator) Audit(ctx context.Context, ts topo.Server, w *Workflow) error {
keyspaces, err := ts.GetKeyspaces(ctx)
if err != nil {
return err
}
for _, keyspace := range keyspaces {
shards, err := ts.GetShardNames(ctx, keyspace)
if err != nil {
return err
}
for _, shard := range shards {
_, err := ts.GetShard(ctx, keyspace, shard)
if err != nil {
w.AddFixer(fmt.Sprintf("%v/%v", keyspace, shard), fmt.Sprintf("Error: %v", err), &ShardFixer{
ts: ts,
keyspace: keyspace,
shard: shard,
}, []string{"Create", "Delete"})
}
}
}
return nil
}
// ShardFixer implements Fixer.
type ShardFixer struct {
ts topo.Server
keyspace string
shard string
}
// Action is part of the Fixer interface.
func (sf *ShardFixer) Action(ctx context.Context, name string) error {
if name == "Create" {
return sf.ts.CreateShard(ctx, sf.keyspace, sf.shard)
}
if name == "Delete" {
return sf.ts.DeleteShard(ctx, sf.keyspace, sf.shard)
}
return fmt.Errorf("unknown ShardFixer action: %v", name)
}