forked from dolthub/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toporeader.go
85 lines (76 loc) · 2.66 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
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
log "github.com/golang/glog"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/vtgate"
"golang.org/x/net/context"
pb "github.com/youtube/vitess/go/vt/proto/topodata"
)
// TopoReader implements topo.TopoReader.
type TopoReader struct {
// the server to get data from
ts vtgate.SrvTopoServer
// stats
queryCount *stats.Counters
errorCount *stats.Counters
}
// NewTopoReader creates a new TopoReader.
func NewTopoReader(ts vtgate.SrvTopoServer) *TopoReader {
return &TopoReader{
ts: ts,
queryCount: stats.NewCounters("TopoReaderRpcQueryCount"),
errorCount: stats.NewCounters("TopoReaderRpcErrorCount"),
}
}
// GetSrvKeyspaceNames returns the names of all keyspaces for the cell.
func (tr *TopoReader) GetSrvKeyspaceNames(ctx context.Context, req *topo.GetSrvKeyspaceNamesArgs, reply *topo.SrvKeyspaceNames) error {
tr.queryCount.Add(req.Cell, 1)
var err error
reply.Entries, err = tr.ts.GetSrvKeyspaceNames(ctx, req.Cell)
if err != nil {
log.Warningf("GetSrvKeyspaceNames(%v) failed: %v", req.Cell, err)
tr.errorCount.Add(req.Cell, 1)
return err
}
return nil
}
// GetSrvKeyspace returns information about a keyspace
// in a particular cell.
func (tr *TopoReader) GetSrvKeyspace(ctx context.Context, req *topo.GetSrvKeyspaceArgs, reply *pb.SrvKeyspace) (err error) {
tr.queryCount.Add(req.Cell, 1)
keyspace, err := tr.ts.GetSrvKeyspace(ctx, req.Cell, req.Keyspace)
if err != nil {
log.Warningf("GetSrvKeyspace(%v,%v) failed: %v", req.Cell, req.Keyspace, err)
tr.errorCount.Add(req.Cell, 1)
return err
}
*reply = *keyspace
return nil
}
// GetSrvShard returns information about a shard for a keyspace
// in a particular cell.
func (tr *TopoReader) GetSrvShard(ctx context.Context, req *topo.GetSrvShardArgs, reply *pb.SrvShard) (err error) {
tr.queryCount.Add(req.Cell, 1)
shard, err := tr.ts.GetSrvShard(ctx, req.Cell, req.Keyspace, req.Shard)
if err != nil {
log.Warningf("GetSrvShard(%v,%v,%v) failed: %v", req.Cell, req.Keyspace, req.Shard, err)
tr.errorCount.Add(req.Cell, 1)
return err
}
*reply = *shard
return nil
}
// GetEndPoints returns addresses for a tablet type in a shard
// in a keyspace.
func (tr *TopoReader) GetEndPoints(ctx context.Context, req *topo.GetEndPointsArgs, reply *pb.EndPoints) (err error) {
tr.queryCount.Add(req.Cell, 1)
addrs, _, err := tr.ts.GetEndPoints(ctx, req.Cell, req.Keyspace, req.Shard, topo.TabletTypeToProto(req.TabletType))
if err != nil {
log.Warningf("GetEndPoints(%v,%v,%v,%v) failed: %v", req.Cell, req.Keyspace, req.Shard, req.TabletType, err)
tr.errorCount.Add(req.Cell, 1)
return err
}
*reply = *addrs
return nil
}