-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroutingclient_topology.go
112 lines (96 loc) · 2.83 KB
/
routingclient_topology.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
package gocbcoreps
import (
"context"
"time"
"github.com/couchbase/goprotostellar/genproto/routing_v1"
"go.uber.org/zap"
)
func (p *RoutingClient) translateTopology(t *routing_v1.WatchRoutingResponse) *Topology {
nodes := make([]*Node, len(t.Endpoints))
for psEpIdx, psEp := range t.Endpoints {
node := &Node{
NodeID: psEp.Id,
ServerGroup: psEp.ServerGroup,
}
nodes[psEpIdx] = node
}
var vbRouting *VbucketRouting
psVbRouting := t.GetVbucketDataRouting()
if vbRouting != nil {
dataNodes := make([]*DataNode, len(psVbRouting.Endpoints))
for psDataEpIdx, psDataEp := range psVbRouting.Endpoints {
dataNode := &DataNode{
Node: nodes[psDataEp.EndpointIdx],
LocalVbuckets: psDataEp.LocalVbuckets,
GroupVbuckets: psDataEp.GroupVbuckets,
}
dataNodes[psDataEpIdx] = dataNode
}
vbRouting = &VbucketRouting{
NumVbuckets: uint(vbRouting.NumVbuckets),
Nodes: dataNodes,
}
}
return &Topology{
Revision: t.Revision,
Nodes: nodes,
VbucketRouting: vbRouting,
}
}
func (p *RoutingClient) watchTopology(ctx context.Context, bucketName *string) (<-chan *Topology, error) {
// TODO(brett19): PSProvider.watchTopology is something that probably belongs in the client.
// Putting it in the client will be helpful because it's likely used in multipled places, and the
// client itself needs to maintain knowledge of the topology anyways, we can perform coalescing
// of those watchers in the client (to reduce the number of watchers).
b := exponentialBackoff(0, 0, 0)
var numRetries uint32
routingStream, err := p.RoutingV1().WatchRouting(ctx, &routing_v1.WatchRoutingRequest{
BucketName: bucketName,
})
if err != nil {
return nil, err
}
routingResp, err := routingStream.Recv()
if err != nil {
return nil, err
}
outputCh := make(chan *Topology)
go func() {
outputCh <- p.translateTopology(routingResp)
MainLoop:
for {
routingStream, err := p.RoutingV1().WatchRouting(ctx, &routing_v1.WatchRoutingRequest{
BucketName: bucketName,
})
if err != nil {
// TODO(brett19): Implement better error handling here...
p.logger.Error("failed to watch routing", zap.Error(err))
numRetries++
select {
case <-time.After(b(numRetries)):
continue
case <-ctx.Done():
break MainLoop
}
}
numRetries = 0
for {
routingResp, err := routingStream.Recv()
if err != nil {
p.logger.Error("failed to recv updated topology", zap.Error(err))
break
}
outputCh <- p.translateTopology(routingResp)
}
}
}()
return outputCh, nil
}
func (p *RoutingClient) WatchTopology(ctx context.Context, bucketName string) (<-chan *Topology, error) {
// TODO(brett19): Remove this pointer shenanigans
var bucketNamePtr *string
if bucketName != "" {
bucketNamePtr = &bucketName
}
return p.watchTopology(ctx, bucketNamePtr)
}