-
Notifications
You must be signed in to change notification settings - Fork 139
/
nodeinfo.go
147 lines (121 loc) · 3.8 KB
/
nodeinfo.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
package meta
/*
Copyright (c) 2013-2016 Errplane Inc.
This code is originally from: https://github.com/influxdata/influxdb/blob/1.7/services/meta/data.go
2022.01.23 Add status as member of NodeInfo
Copyright 2022 Huawei Cloud Computing Technologies Co., Ltd.
*/
import (
"github.com/openGemini/openGemini/lib/util/lifted/hashicorp/serf/serf"
proto2 "github.com/openGemini/openGemini/lib/util/lifted/influx/meta/proto"
"github.com/openGemini/openGemini/lib/util/lifted/protobuf/proto"
)
type NodeStatus int64
const (
StatusNone NodeStatus = iota
StatusAlive
StatusFailed
StatusRestart
StatusLeaving
StatusLeft
)
const (
NodeDefault string = "" // prioritize as writer. If no reader in cluster, NodeDeafult is both a writer and a reader.
NodeReader string = "reader"
NodeWriter string = "writer"
)
func IsNodeWriter(role string) bool {
return role == NodeWriter || role == NodeDefault
}
func IsNodeReader(role string) bool {
return role == NodeReader
}
const (
Normal uint64 = iota
Segregating
Segregated
)
// NodeInfo represents information about a single node in the cluster.
type NodeInfo struct {
ID uint64
Host string
RPCAddr string
TCPHost string
Status serf.MemberStatus
LTime uint64
GossipAddr string
SegregateStatus uint64
Role string
}
// clone returns a deep copy of ni.
func (ni NodeInfo) clone() NodeInfo { return ni }
// marshal serializes to a protobuf representation.
func (ni NodeInfo) marshal() *proto2.NodeInfo {
pb := &proto2.NodeInfo{}
pb.ID = proto.Uint64(ni.ID)
pb.Host = proto.String(ni.Host)
pb.RPCAddr = proto.String(ni.RPCAddr)
pb.TCPHost = proto.String(ni.TCPHost)
pb.Status = proto.Int64(int64(ni.Status))
pb.LTime = proto.Uint64(ni.LTime)
pb.GossipAddr = proto.String(ni.GossipAddr)
pb.SegregateStatus = proto.Uint64(ni.SegregateStatus)
pb.Role = proto.String(ni.Role)
return pb
}
// unmarshal deserializes from a protobuf representation.
func (ni *NodeInfo) unmarshal(pb *proto2.NodeInfo) {
ni.ID = pb.GetID()
ni.Host = pb.GetHost()
ni.RPCAddr = pb.GetRPCAddr()
ni.TCPHost = pb.GetTCPHost()
ni.Status = serf.MemberStatus(pb.GetStatus())
ni.LTime = pb.GetLTime()
ni.GossipAddr = pb.GetGossipAddr()
ni.SegregateStatus = pb.GetSegregateStatus()
ni.Role = pb.GetRole()
}
type DataNode struct {
NodeInfo
ConnID uint64 // after joined raft cluster. ConnID will +1 when restart
AliveConnID uint64 // after joined gossip cluster, AliveConnID will set by ConnID
}
func (n *DataNode) MarshalBinary() ([]byte, error) {
return proto.Marshal(n.marshal())
}
func (n *DataNode) UnmarshalBinary(buf []byte) error {
var pb proto2.DataNode
if err := proto.Unmarshal(buf, &pb); err != nil {
return err
}
n.unmarshal(&pb)
return nil
}
func (n *DataNode) marshal() *proto2.DataNode {
pb := &proto2.DataNode{}
pb.Ni = n.NodeInfo.marshal()
pb.ConnID = proto.Uint64(n.ConnID)
pb.AliveConnID = proto.Uint64(n.AliveConnID)
return pb
}
func (n *DataNode) unmarshal(pb *proto2.DataNode) {
n.NodeInfo.unmarshal(pb.GetNi())
n.ConnID = pb.GetConnID()
n.AliveConnID = pb.GetAliveConnID()
}
// NodeInfos is a slice of NodeInfo used for sorting
type DataNodeInfos []DataNode
// Len implements sort.Interface.
func (n DataNodeInfos) Len() int { return len(n) }
// Swap implements sort.Interface.
func (n DataNodeInfos) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
// Less implements sort.Interface.
func (n DataNodeInfos) Less(i, j int) bool { return n[i].ID < n[j].ID }
// NodeInfos is a slice of NodeInfo used for sorting
type NodeInfos []NodeInfo
// Len implements sort.Interface.
func (n NodeInfos) Len() int { return len(n) }
// Swap implements sort.Interface.
func (n NodeInfos) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
// Less implements sort.Interface.
func (n NodeInfos) Less(i, j int) bool { return n[i].ID < n[j].ID }