forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shard.go
166 lines (148 loc) · 4.75 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
// Package test contains utilities to test topo.Impl
// implementations. If you are testing your implementation, you will
// want to call CheckAll in your test method. For an example, look at
// the tests in github.com/youtube/vitess/go/vt/zktopo.
package test
import (
"encoding/json"
"testing"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/vt/key"
"github.com/youtube/vitess/go/vt/topo"
pb "github.com/youtube/vitess/go/vt/proto/topodata"
)
func shardEqual(left, right *pb.Shard) (bool, error) {
lj, err := json.Marshal(left)
if err != nil {
return false, err
}
rj, err := json.Marshal(right)
if err != nil {
return false, err
}
return string(lj) == string(rj), nil
}
// CheckShard verifies the Shard operations work correctly
func CheckShard(ctx context.Context, t *testing.T, ts topo.Impl) {
tts := topo.Server{Impl: ts}
if err := ts.CreateKeyspace(ctx, "test_keyspace", &pb.Keyspace{}); err != nil {
t.Fatalf("CreateKeyspace: %v", err)
}
shard := &pb.Shard{
KeyRange: newKeyRange3("b0-c0"),
}
if err := ts.CreateShard(ctx, "test_keyspace", "b0-c0", shard); err != nil {
t.Fatalf("CreateShard: %v", err)
}
if err := ts.CreateShard(ctx, "test_keyspace", "b0-c0", shard); err != topo.ErrNodeExists {
t.Errorf("CreateShard called second time, got: %v", err)
}
// Delete shard and see if we can re-create it.
if err := ts.DeleteShard(ctx, "test_keyspace", "b0-c0"); err != nil {
t.Fatalf("DeleteShard: %v", err)
}
if err := ts.CreateShard(ctx, "test_keyspace", "b0-c0", shard); err != nil {
t.Fatalf("CreateShard: %v", err)
}
// Delete ALL shards.
if err := ts.DeleteKeyspaceShards(ctx, "test_keyspace"); err != nil {
t.Fatalf("DeleteKeyspaceShards: %v", err)
}
if err := ts.CreateShard(ctx, "test_keyspace", "b0-c0", shard); err != nil {
t.Fatalf("CreateShard: %v", err)
}
if _, _, err := ts.GetShard(ctx, "test_keyspace", "666"); err != topo.ErrNoNode {
t.Errorf("GetShard(666): %v", err)
}
shard, version, err := ts.GetShard(ctx, "test_keyspace", "b0-c0")
if err != nil {
t.Errorf("GetShard: %v", err)
}
if want := newKeyRange3("b0-c0"); !key.KeyRangeEqual(shard.KeyRange, want) {
t.Errorf("shard.KeyRange: want %v, got %v", want, shard.KeyRange)
}
master := &pb.TabletAlias{Cell: "ny", Uid: 1}
shard.MasterAlias = master
shard.KeyRange = newKeyRange3("b0-c0")
shard.ServedTypes = []*pb.Shard_ServedType{
&pb.Shard_ServedType{
TabletType: pb.TabletType_MASTER,
},
&pb.Shard_ServedType{
TabletType: pb.TabletType_REPLICA,
Cells: []string{"c1"},
},
&pb.Shard_ServedType{
TabletType: pb.TabletType_RDONLY,
},
}
shard.SourceShards = []*pb.Shard_SourceShard{
&pb.Shard_SourceShard{
Uid: 1,
Keyspace: "source_ks",
Shard: "b8-c0",
KeyRange: newKeyRange3("b8-c0"),
Tables: []string{"table1", "table2"},
},
}
shard.TabletControls = []*pb.Shard_TabletControl{
&pb.Shard_TabletControl{
TabletType: pb.TabletType_MASTER,
Cells: []string{"c1", "c2"},
BlacklistedTables: []string{"black1", "black2"},
},
&pb.Shard_TabletControl{
TabletType: pb.TabletType_REPLICA,
DisableQueryService: true,
},
}
if _, err := ts.UpdateShard(ctx, "test_keyspace", "b0-c0", shard, version); err != nil {
t.Errorf("UpdateShard: %v", err)
}
other := &pb.TabletAlias{Cell: "ny", Uid: 82873}
_, err = tts.UpdateShardFields(ctx, "test_keyspace", "b0-c0", func(shard *pb.Shard) error {
shard.MasterAlias = other
return nil
})
if err != nil {
t.Fatalf("UpdateShardFields error: %v", err)
}
s, _, err := ts.GetShard(ctx, "test_keyspace", "b0-c0")
if err != nil {
t.Fatalf("GetShard: %v", err)
}
if *s.MasterAlias != *other {
t.Fatalf("shard.MasterAlias = %v, want %v", s.MasterAlias, other)
}
_, err = tts.UpdateShardFields(ctx, "test_keyspace", "b0-c0", func(shard *pb.Shard) error {
shard.MasterAlias = master
return nil
})
if err != nil {
t.Fatalf("UpdateShardFields error: %v", err)
}
updatedShard, _, err := ts.GetShard(ctx, "test_keyspace", "b0-c0")
if err != nil {
t.Fatalf("GetShard: %v", err)
}
if eq, err := shardEqual(shard, updatedShard); err != nil {
t.Errorf("cannot compare shards: %v", err)
} else if !eq {
t.Errorf("put and got shards are not identical:\n%#v\n%#v", shard, updatedShard)
}
// test GetShardNames
shards, err := ts.GetShardNames(ctx, "test_keyspace")
if err != nil {
t.Errorf("GetShardNames: %v", err)
}
if len(shards) != 1 || shards[0] != "b0-c0" {
t.Errorf(`GetShardNames: want [ "b0-c0" ], got %v`, shards)
}
if _, err := ts.GetShardNames(ctx, "test_keyspace666"); err != topo.ErrNoNode {
t.Errorf("GetShardNames(666): %v", err)
}
// test ValidateShard
if err := ts.ValidateShard(ctx, "test_keyspace", "b0-c0"); err != nil {
t.Errorf("ValidateShard(test_keyspace, b0-c0) failed: %v", err)
}
}