forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serving_graph.go
356 lines (306 loc) · 10.3 KB
/
serving_graph.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
// Copyright 2014, 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 etcdtopo
import (
"encoding/json"
"fmt"
"path"
"time"
"github.com/coreos/go-etcd/etcd"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/topo/topoproto"
"golang.org/x/net/context"
pb "github.com/youtube/vitess/go/vt/proto/topodata"
)
// WatchSleepDuration is how many seconds interval to poll for in case
// we get an error from the Watch method. It is exported so individual
// test and main programs can change it.
var WatchSleepDuration = 30 * time.Second
// GetSrvTabletTypesPerShard implements topo.Server.
func (s *Server) GetSrvTabletTypesPerShard(ctx context.Context, cellName, keyspace, shard string) ([]pb.TabletType, error) {
cell, err := s.getCell(cellName)
if err != nil {
return nil, err
}
resp, err := cell.Get(srvShardDirPath(keyspace, shard), false /* sort */, false /* recursive */)
if err != nil {
return nil, convertError(err)
}
if resp.Node == nil {
return nil, ErrBadResponse
}
tabletTypes := make([]pb.TabletType, 0, len(resp.Node.Nodes))
for _, n := range resp.Node.Nodes {
strType := path.Base(n.Key)
if tt, err := topoproto.ParseTabletType(strType); err == nil {
tabletTypes = append(tabletTypes, tt)
}
}
return tabletTypes, nil
}
// CreateEndPoints implements topo.Server.
func (s *Server) CreateEndPoints(ctx context.Context, cellName, keyspace, shard string, tabletType pb.TabletType, addrs *pb.EndPoints) error {
cell, err := s.getCell(cellName)
if err != nil {
return err
}
data, err := json.MarshalIndent(addrs, "", " ")
if err != nil {
return err
}
// Set only if it doesn't exist.
_, err = cell.Create(endPointsFilePath(keyspace, shard, tabletType), string(data), 0 /* ttl */)
return convertError(err)
}
// UpdateEndPoints implements topo.Server.
func (s *Server) UpdateEndPoints(ctx context.Context, cellName, keyspace, shard string, tabletType pb.TabletType, addrs *pb.EndPoints, existingVersion int64) error {
cell, err := s.getCell(cellName)
if err != nil {
return err
}
data, err := json.MarshalIndent(addrs, "", " ")
if err != nil {
return err
}
if existingVersion == -1 {
// Set unconditionally.
_, err := cell.Set(endPointsFilePath(keyspace, shard, tabletType), string(data), 0 /* ttl */)
return convertError(err)
}
// Update only if version matches.
return s.updateEndPoints(cellName, keyspace, shard, tabletType, addrs, existingVersion)
}
// updateEndPoints updates the EndPoints file only if the version matches.
func (s *Server) updateEndPoints(cellName, keyspace, shard string, tabletType pb.TabletType, addrs *pb.EndPoints, version int64) error {
cell, err := s.getCell(cellName)
if err != nil {
return err
}
data, err := json.MarshalIndent(addrs, "", " ")
if err != nil {
return err
}
_, err = cell.CompareAndSwap(endPointsFilePath(keyspace, shard, tabletType), string(data), 0, /* ttl */
"" /* prevValue */, uint64(version))
return convertError(err)
}
// GetEndPoints implements topo.Server.
func (s *Server) GetEndPoints(ctx context.Context, cellName, keyspace, shard string, tabletType pb.TabletType) (*pb.EndPoints, int64, error) {
cell, err := s.getCell(cellName)
if err != nil {
return nil, -1, err
}
resp, err := cell.Get(endPointsFilePath(keyspace, shard, tabletType), false /* sort */, false /* recursive */)
if err != nil {
return nil, -1, convertError(err)
}
if resp.Node == nil {
return nil, -1, ErrBadResponse
}
value := &pb.EndPoints{}
if resp.Node.Value != "" {
if err := json.Unmarshal([]byte(resp.Node.Value), value); err != nil {
return nil, -1, fmt.Errorf("bad end points data (%v): %q", err, resp.Node.Value)
}
}
return value, int64(resp.Node.ModifiedIndex), nil
}
// DeleteEndPoints implements topo.Server.
func (s *Server) DeleteEndPoints(ctx context.Context, cellName, keyspace, shard string, tabletType pb.TabletType, existingVersion int64) error {
cell, err := s.getCell(cellName)
if err != nil {
return err
}
dirPath := endPointsDirPath(keyspace, shard, tabletType)
if existingVersion == -1 {
// Delete unconditionally.
_, err := cell.Delete(dirPath, true /* recursive */)
return convertError(err)
}
// Delete EndPoints file only if version matches.
if _, err := cell.CompareAndDelete(endPointsFilePath(keyspace, shard, tabletType), "" /* prevValue */, uint64(existingVersion)); err != nil {
return convertError(err)
}
// Delete the parent dir only if it's empty.
_, err = cell.DeleteDir(dirPath)
err = convertError(err)
if err == topo.ErrNotEmpty {
// Someone else recreated the EndPoints file after we deleted it,
// but before we got around to removing the parent dir.
// This is fine, because whoever recreated it has already seen our delete,
// and we're not at risk of overwriting their change.
err = nil
}
return err
}
// UpdateSrvShard implements topo.Server.
func (s *Server) UpdateSrvShard(ctx context.Context, cellName, keyspace, shard string, srvShard *pb.SrvShard) error {
cell, err := s.getCell(cellName)
if err != nil {
return err
}
data, err := json.MarshalIndent(srvShard, "", " ")
if err != nil {
return err
}
_, err = cell.Set(srvShardFilePath(keyspace, shard), string(data), 0 /* ttl */)
return convertError(err)
}
// GetSrvShard implements topo.Server.
func (s *Server) GetSrvShard(ctx context.Context, cellName, keyspace, shard string) (*pb.SrvShard, error) {
cell, err := s.getCell(cellName)
if err != nil {
return nil, err
}
resp, err := cell.Get(srvShardFilePath(keyspace, shard), false /* sort */, false /* recursive */)
if err != nil {
return nil, convertError(err)
}
if resp.Node == nil {
return nil, ErrBadResponse
}
value := &pb.SrvShard{}
if err := json.Unmarshal([]byte(resp.Node.Value), value); err != nil {
return nil, fmt.Errorf("bad serving shard data (%v): %q", err, resp.Node.Value)
}
return value, nil
}
// DeleteSrvShard implements topo.Server.
func (s *Server) DeleteSrvShard(ctx context.Context, cellName, keyspace, shard string) error {
cell, err := s.getCell(cellName)
if err != nil {
return err
}
_, err = cell.Delete(srvShardDirPath(keyspace, shard), true /* recursive */)
return convertError(err)
}
// UpdateSrvKeyspace implements topo.Server.
func (s *Server) UpdateSrvKeyspace(ctx context.Context, cellName, keyspace string, srvKeyspace *pb.SrvKeyspace) error {
cell, err := s.getCell(cellName)
if err != nil {
return err
}
data, err := json.MarshalIndent(srvKeyspace, "", " ")
if err != nil {
return err
}
_, err = cell.Set(srvKeyspaceFilePath(keyspace), string(data), 0 /* ttl */)
return convertError(err)
}
// DeleteSrvKeyspace implements topo.Server.
func (s *Server) DeleteSrvKeyspace(ctx context.Context, cellName, keyspace string) error {
cell, err := s.getCell(cellName)
if err != nil {
return err
}
_, err = cell.Delete(srvKeyspaceDirPath(keyspace), true /* recursive */)
return convertError(err)
}
// GetSrvKeyspace implements topo.Server.
func (s *Server) GetSrvKeyspace(ctx context.Context, cellName, keyspace string) (*pb.SrvKeyspace, error) {
cell, err := s.getCell(cellName)
if err != nil {
return nil, err
}
resp, err := cell.Get(srvKeyspaceFilePath(keyspace), false /* sort */, false /* recursive */)
if err != nil {
return nil, convertError(err)
}
if resp.Node == nil {
return nil, ErrBadResponse
}
value := &pb.SrvKeyspace{}
if err := json.Unmarshal([]byte(resp.Node.Value), value); err != nil {
return nil, fmt.Errorf("bad serving keyspace data (%v): %q", err, resp.Node.Value)
}
return value, nil
}
// GetSrvKeyspaceNames implements topo.Server.
func (s *Server) GetSrvKeyspaceNames(ctx context.Context, cellName string) ([]string, error) {
cell, err := s.getCell(cellName)
if err != nil {
return nil, err
}
resp, err := cell.Get(servingDirPath, true /* sort */, false /* recursive */)
if err != nil {
return nil, convertError(err)
}
return getNodeNames(resp)
}
// WatchSrvKeyspace is part of the topo.Server interface
func (s *Server) WatchSrvKeyspace(ctx context.Context, cellName, keyspace string) (<-chan *pb.SrvKeyspace, chan<- struct{}, error) {
cell, err := s.getCell(cellName)
if err != nil {
return nil, nil, fmt.Errorf("WatchSrvKeyspace cannot get cell: %v", err)
}
filePath := srvKeyspaceFilePath(keyspace)
notifications := make(chan *pb.SrvKeyspace, 10)
stopWatching := make(chan struct{})
// The watch go routine will stop if the 'stop' channel is closed.
// Otherwise it will try to watch everything in a loop, and send events
// to the 'watch' channel.
watch := make(chan *etcd.Response)
stop := make(chan bool)
go func() {
var srvKeyspace *pb.SrvKeyspace
var modifiedVersion int64
resp, err := cell.Get(filePath, false /* sort */, false /* recursive */)
if err != nil || resp.Node == nil {
// node doesn't exist
} else {
if resp.Node.Value != "" {
srvKeyspace = &pb.SrvKeyspace{}
if err := json.Unmarshal([]byte(resp.Node.Value), srvKeyspace); err != nil {
log.Warningf("bad SrvKeyspace data (%v): %q", err, resp.Node.Value)
} else {
modifiedVersion = int64(resp.Node.ModifiedIndex)
}
}
}
// re-check for stop here to be safe, in case the
// getEndPoints took a long time
select {
case <-stop:
return
case notifications <- srvKeyspace:
}
for {
if _, err := cell.Client.Watch(filePath, uint64(modifiedVersion+1), false /* recursive */, watch, stop); err != nil {
log.Errorf("Watch on %v failed, waiting for %v to retry: %v", filePath, WatchSleepDuration, err)
timer := time.After(WatchSleepDuration)
select {
case <-stop:
return
case <-timer:
}
}
}
}()
// This go routine is the main event handling routine:
// - it will stop if stopWatching is closed.
// - if it receives a notification from the watch, it will forward it
// to the notifications channel.
go func() {
for {
select {
case resp := <-watch:
var srvKeyspace *pb.SrvKeyspace
if resp.Node != nil && resp.Node.Value != "" {
srvKeyspace = &pb.SrvKeyspace{}
if err := json.Unmarshal([]byte(resp.Node.Value), srvKeyspace); err != nil {
log.Errorf("failed to Unmarshal EndPoints for %v: %v", filePath, err)
continue
}
}
notifications <- srvKeyspace
case <-stopWatching:
close(stop)
close(notifications)
return
}
}
}()
return notifications, stopWatching, nil
}