-
Notifications
You must be signed in to change notification settings - Fork 21
/
configsnapshot.go
126 lines (100 loc) · 3.05 KB
/
configsnapshot.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
package gocbcore
// ConfigSnapshot is a snapshot of the underlying configuration currently in use.
type ConfigSnapshot struct {
state *kvMuxState
}
// RevID returns the config revision for this snapshot.
func (pi ConfigSnapshot) RevID() int64 {
return pi.state.RevID()
}
// KeyToVbucket translates a particular key to its assigned vbucket.
func (pi ConfigSnapshot) KeyToVbucket(key []byte) (uint16, error) {
if pi.state.VBMap() == nil {
return 0, errUnsupportedOperation
}
return pi.state.VBMap().VbucketByKey(key), nil
}
// KeyToServer translates a particular key to its assigned server index.
func (pi ConfigSnapshot) KeyToServer(key []byte, replicaIdx uint32) (int, error) {
if pi.state.VBMap() != nil {
serverIdx, err := pi.state.VBMap().NodeByKey(key, replicaIdx)
if err != nil {
return 0, err
}
return serverIdx, nil
}
if pi.state.KetamaMap() != nil {
serverIdx, err := pi.state.KetamaMap().NodeByKey(key)
if err != nil {
return 0, err
}
return serverIdx, nil
}
return 0, errCliInternalError
}
// VbucketToServer returns the server index for a particular vbucket.
func (pi ConfigSnapshot) VbucketToServer(vbID uint16, replicaIdx uint32) (int, error) {
if pi.state.VBMap() == nil {
return 0, errUnsupportedOperation
}
serverIdx, err := pi.state.VBMap().NodeByVbucket(vbID, replicaIdx)
if err != nil {
return 0, err
}
return serverIdx, nil
}
// VbucketsOnServer returns the list of VBuckets for a server.
func (pi ConfigSnapshot) VbucketsOnServer(index int) ([]uint16, error) {
if pi.state.VBMap() == nil {
return nil, errUnsupportedOperation
}
return pi.state.VBMap().VbucketsOnServer(index)
}
// NumVbuckets returns the number of VBuckets configured on the
// connected cluster.
func (pi ConfigSnapshot) NumVbuckets() (int, error) {
if pi.state.VBMap() == nil {
return 0, errUnsupportedOperation
}
return pi.state.VBMap().NumVbuckets(), nil
}
// NumReplicas returns the number of replicas configured on the
// connected cluster.
func (pi ConfigSnapshot) NumReplicas() (int, error) {
if pi.state.VBMap() == nil {
return 0, errUnsupportedOperation
}
return pi.state.VBMap().NumReplicas(), nil
}
// NumServers returns the number of servers accessible for K/V.
func (pi ConfigSnapshot) NumServers() (int, error) {
return pi.state.NumPipelines(), nil
}
// BucketUUID returns the UUID of the bucket we are connected to.
func (pi ConfigSnapshot) BucketUUID() string {
return pi.state.UUID()
}
func (pi ConfigSnapshot) KeyToServersByServerGroup(key []byte) (map[string][]int, error) {
vbID, err := pi.KeyToVbucket(key)
if err != nil {
return nil, err
}
numReplicas, err := pi.NumReplicas()
if err != nil {
return nil, err
}
numServers := numReplicas + 1
serverMap := make(map[string][]int)
for i := 0; i < numServers; i++ {
srvIndex, err := pi.VbucketToServer(vbID, uint32(i))
if err != nil {
return nil, err
}
group := pi.state.pipelines[srvIndex].ServerGroup()
if _, ok := serverMap[group]; !ok {
serverMap[group] = make([]int, 0)
}
serverMap[group] = append(serverMap[group], i)
}
return serverMap, nil
}