forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
toporeader.go
73 lines (62 loc) · 2.21 KB
/
toporeader.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
package main
import (
"encoding/json"
"fmt"
"path"
"github.com/youtube/vitess/go/rpcwrap/proto"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/zk"
)
type TopoReader struct {
zkr zk.ZkReader
}
// FIXME(ryszard): These methods are kinda copy-and-pasted from
// zktopo.Server. In the long-term, the TopoReader should just take a
// topo.Server, which would be backed by a caching ZooKeeper
// connection.
func zkPathForVt(cell string) string {
return fmt.Sprintf("/zk/%v/vt/ns", cell)
}
func zkPathForVtKeyspace(cell, keyspace string) string {
return path.Join(zkPathForVt(cell), keyspace)
}
func zkPathForVtType(cell, keyspace, shard string, tabletType topo.TabletType) string {
return path.Join(zkPathForVt(cell), keyspace, shard, string(tabletType))
}
func (tr *TopoReader) GetSrvKeyspaceNames(context *proto.Context, req *topo.GetSrvKeyspaceNamesArgs, reply *topo.SrvKeyspaceNames) error {
vtPath := zkPathForVt(req.Cell)
zkrReply := &zk.ZkNode{}
if err := tr.zkr.Children(&zk.ZkPath{Path: vtPath}, zkrReply); err != nil {
return err
}
reply.Entries = zkrReply.Children
return nil
}
func (tr *TopoReader) GetSrvKeyspace(context *proto.Context, req *topo.GetSrvKeyspaceArgs, reply *topo.SrvKeyspace) (err error) {
keyspacePath := zkPathForVtKeyspace(req.Cell, req.Keyspace)
zkrReply := &zk.ZkNode{}
if err := tr.zkr.Get(&zk.ZkPath{Path: keyspacePath}, zkrReply); err != nil {
return err
}
keyspace := topo.NewSrvKeyspace(int64(zkrReply.Stat.Version()))
if len(zkrReply.Data) > 0 {
if err := json.Unmarshal([]byte(zkrReply.Data), keyspace); err != nil {
return fmt.Errorf("SrvKeyspace unmarshal failed: %v %v", zkrReply.Data, err)
}
}
*reply = *keyspace
return
}
func (tr *TopoReader) GetEndPoints(context *proto.Context, req *topo.GetEndPointsArgs, reply *topo.EndPoints) (err error) {
tabletTypePath := zkPathForVtType(req.Cell, req.Keyspace, req.Shard, req.TabletType)
zkrReply := &zk.ZkNode{}
if err := tr.zkr.Get(&zk.ZkPath{Path: tabletTypePath}, zkrReply); err != nil {
return err
}
if len(zkrReply.Data) > 0 {
if err := json.Unmarshal([]byte(zkrReply.Data), reply); err != nil {
return fmt.Errorf("EndPoints unmarshal failed: %v %v", zkrReply.Data, err)
}
}
return nil
}