/
clusterinfo.go
283 lines (255 loc) · 8.89 KB
/
clusterinfo.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
package redis
import (
"fmt"
"net"
"reflect"
"sort"
"strconv"
"strings"
"time"
"github.com/golang/glog"
)
const (
// ClusterInfosUnset status of the cluster info: no data set
ClusterInfosUnset = "Unset"
// ClusterInfosPartial status of the cluster info: data is not complete (some nodes didn't respond)
ClusterInfosPartial = "Partial"
// ClusterInfosInconsistent status of the cluster info: nodesinfos is not consistent between nodes
ClusterInfosInconsistent = "Inconsistent"
// ClusterInfosConsistent status of the cluster info: nodeinfos is complete and consistent between nodes
ClusterInfosConsistent = "Consistent"
)
// NodeInfos representation of a node info, i.e. data returned by the CLUSTER NODE redis command
// Node is the information of the targetted node
// Friends are the view of the other nodes from the targetted node
type NodeInfos struct {
Node *Node
Friends Nodes
}
// ClusterInfos represents the node infos for all nodes of the cluster
type ClusterInfos struct {
Infos map[string]*NodeInfos
Status string
}
// NewNodeInfos returns an instance of NodeInfo
func NewNodeInfos() *NodeInfos {
return &NodeInfos{
Node: NewDefaultNode(),
Friends: Nodes{},
}
}
// NewClusterInfos returns an instance of ClusterInfos
func NewClusterInfos() *ClusterInfos {
return &ClusterInfos{
Infos: make(map[string]*NodeInfos),
Status: ClusterInfosUnset,
}
}
// DecodeNodeStartTime decode from the cmd output the Redis instance info. Second argument is the node on which we are connected to request info
func DecodeNodeStartTime(input *string) (time.Time, error) {
lines := strings.Split(*input, "\n")
for _, line := range lines {
values := strings.Split(line, ":")
if values[0] == "uptime_in_seconds" {
uptimeInSeconds, err := strconv.Atoi(strings.TrimSpace(values[1]))
if err != nil {
glog.Errorf("Error while decoding redis instance uptime in seconds. String : %s Error: %v", values[1], err)
return time.Now(), err
}
return time.Now().Add(-time.Duration(uptimeInSeconds) * time.Second), nil
}
}
glog.Errorf("Error while decoding redis instance uptime in seconds. No data found")
return time.Now(), fmt.Errorf("Error while decoding redis instance uptime in seconds. No data found")
}
// DecodeNodeInfos decode from the cmd output the Redis nodes info. Second argument is the node on which we are connected to request info
func DecodeNodeInfos(input *string, addr string) *NodeInfos {
infos := NewNodeInfos()
lines := strings.Split(*input, "\n")
for _, line := range lines {
values := strings.Split(line, " ")
if len(values) < 8 {
// last line is always empty
glog.V(7).Infof("Not enough values in line split, ignoring line: '%s'", line)
continue
} else {
node := NewDefaultNode()
node.ID = values[0]
//remove trailing port for cluster internal protocol
ipPort := strings.Split(values[1], "@")
if ip, port, err := net.SplitHostPort(ipPort[0]); err == nil {
node.IP = ip
node.Port = port
if ip == "" {
// ip of the node we are connecting to is sometime empty
node.IP, _, _ = net.SplitHostPort(addr)
}
} else {
glog.Errorf("Error while decoding node info for node '%s', cannot split ip:port ('%s'): %v", node.ID, values[1], err)
}
node.SetRole(values[2])
node.SetFailureStatus(values[2])
node.SetReferentMaster(values[3])
if i, err := strconv.ParseInt(values[4], 10, 64); err == nil {
node.PingSent = i
}
if i, err := strconv.ParseInt(values[5], 10, 64); err == nil {
node.PongRecv = i
}
if i, err := strconv.ParseInt(values[6], 10, 64); err == nil {
node.ConfigEpoch = i
}
node.SetLinkStatus(values[7])
for _, slot := range values[8:] {
if s, importing, migrating, err := DecodeSlotRange(slot); err == nil {
node.Slots = append(node.Slots, s...)
if importing != nil {
node.ImportingSlots[importing.SlotID] = importing.FromNodeID
}
if migrating != nil {
node.MigratingSlots[migrating.SlotID] = migrating.ToNodeID
}
}
}
if strings.HasPrefix(values[2], "myself") {
infos.Node = node
glog.V(7).Infof("Getting node info for node: '%s'", node)
} else {
infos.Friends = append(infos.Friends, node)
glog.V(7).Infof("Adding node to slice: '%s'", node)
}
}
}
return infos
}
// ComputeStatus check the ClusterInfos status based on the current data
// the status ClusterInfosPartial is set while building the clusterinfos
// if already set, do nothing
// returns true if contistent or if another error
func (c *ClusterInfos) ComputeStatus() bool {
if c.Status != ClusterInfosUnset {
return false
}
consistencyStatus := false
consolidatedView := c.GetNodes().SortByFunc(LessByID)
consolidatedSignature := getConfigSignature(consolidatedView)
glog.V(7).Infof("Consolidated view:\n%s", consolidatedSignature)
for addr, nodeinfos := range c.Infos {
nodesView := append(nodeinfos.Friends, nodeinfos.Node).SortByFunc(LessByID)
nodeSignature := getConfigSignature(nodesView)
glog.V(7).Infof("Node view from %s (ID: %s):\n%s", addr, nodeinfos.Node.ID, nodeSignature)
if !reflect.DeepEqual(consolidatedSignature, nodeSignature) {
glog.V(4).Info("Temporary inconsistency between nodes is possible. If the following inconsistency message persists for more than 20 mins, any cluster operation (scale, rolling update) should be avoided before the message is gone")
glog.V(4).Infof("Inconsistency from %s: \n%s\nVS\n%s", addr, consolidatedSignature, nodeSignature)
c.Status = ClusterInfosInconsistent
}
}
if c.Status == ClusterInfosUnset {
c.Status = ClusterInfosConsistent
consistencyStatus = true
}
return consistencyStatus
}
// GetNodes returns a nodeSlice view of the cluster
// the slice if formed from how each node see itself
// you should check the Status before doing it, to wait for a consistent view
func (c *ClusterInfos) GetNodes() Nodes {
nodes := Nodes{}
for _, nodeinfos := range c.Infos {
nodes = append(nodes, nodeinfos.Node)
}
return nodes
}
// ConfigSignature Represents the slots of each node
type ConfigSignature map[string]SlotSlice
// String representation of a ConfigSignaure
func (c ConfigSignature) String() string {
s := "map["
sc := make([]string, 0, len(c))
for i := range c {
sc = append(sc, i)
}
sort.Strings(sc)
for _, i := range sc {
s += fmt.Sprintf("%s:%s\n", i, c[i])
}
s += "]"
return s
}
// getConfigSignature returns a way to identify a cluster view, to check consistency
func getConfigSignature(nodes Nodes) ConfigSignature {
signature := ConfigSignature{}
for _, node := range nodes {
if node.Role == redisMasterRole {
signature[node.ID] = SlotSlice(node.Slots)
}
}
return signature
}
// OwnerWithStatus represents a node owner and the way it sees the slot
type OwnerWithStatus struct {
Addr string
Status string
}
// OwneshipView map representing who owns a slot and who sees it
type OwneshipView map[OwnerWithStatus][]string
// ClusterInconsistencies structure representing inconsistencies in the cluster
type ClusterInconsistencies map[Slot]OwneshipView
// String
func (ci ClusterInconsistencies) String() string {
output := ""
for slot, ownership := range ci {
output += fmt.Sprintf("%d: %s\n", slot, ownership)
}
return output
}
// GetInconsistencies returns a view of the inconsistent configuration per slot
func (c *ClusterInfos) GetInconsistencies() *ClusterInconsistencies {
ci := ClusterInconsistencies{}
for addr, nodeinfo := range c.Infos {
allSlots := []Slot{}
for _, node := range append(nodeinfo.Friends, nodeinfo.Node) {
// owned slots
allSlots = append(allSlots, node.Slots...)
for _, slot := range node.Slots {
if _, ok := ci[slot]; !ok {
ci[slot] = OwneshipView{}
}
viewers := ci[slot][OwnerWithStatus{Addr: node.IPPort(), Status: "owned"}]
ci[slot][OwnerWithStatus{Addr: node.IPPort(), Status: "owned"}] = append(viewers, addr)
}
// migrating slots
for slot := range node.MigratingSlots {
if _, ok := ci[slot]; !ok {
ci[slot] = OwneshipView{}
}
viewers := ci[slot][OwnerWithStatus{Addr: node.IPPort(), Status: "migrating"}]
ci[slot][OwnerWithStatus{Addr: node.IPPort(), Status: "migrating"}] = append(viewers, addr)
}
// importing slots
for slot := range node.ImportingSlots {
if _, ok := ci[slot]; !ok {
ci[slot] = OwneshipView{}
}
viewers := ci[slot][OwnerWithStatus{Addr: node.IPPort(), Status: "importing"}]
ci[slot][OwnerWithStatus{Addr: node.IPPort(), Status: "importing"}] = append(viewers, addr)
}
}
// slots that are not owned according to this node
missing := RemoveSlots(BuildSlotSlice(0, 16383), allSlots)
for _, slot := range missing {
if _, ok := ci[slot]; !ok {
ci[slot] = OwneshipView{}
}
viewers := ci[slot][OwnerWithStatus{Addr: "", Status: ""}]
ci[slot][OwnerWithStatus{Addr: "", Status: ""}] = append(viewers, addr)
}
}
// now cleaning all consistent data
for slot, ownership := range ci {
if len(ownership) <= 1 {
delete(ci, slot)
}
}
return &ci
}