-
Notifications
You must be signed in to change notification settings - Fork 8
/
node.go
232 lines (198 loc) · 4.49 KB
/
node.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
package model
import (
"encoding/gob"
"errors"
"fmt"
"reflect"
"sync"
"time"
)
func init() {
n1 := NodeInfo{}
gob.RegisterName(reflect.TypeOf(n1).String(), n1)
n2 := Node{}
gob.RegisterName(reflect.TypeOf(n2).String(), n2)
}
type NodeStatus int
const (
PkgPrefix string = "github.com/huajiao-tv/gokeeper/model."
StatusStop NodeStatus = 0
StatusRunning NodeStatus = 1
eventChanSize = 10
)
type NodeInfo struct {
ID string `json:"id"`
KeeperAddr string `json:"keeper_addr"`
Domain string `json:"domain"`
Component string `json:"component"`
Hostname string `json:"hostname"`
StartTime int64 `json:"start_time"`
UpdateTime int64 `json:"update_time"`
RawSubscription []string `json:"raw_subscription"`
Status NodeStatus `json:"status"`
Version int `json:"version"`
mu sync.RWMutex `json:"-"`
ComponentTags map[string]string `json:"component_tags"`
}
func NewNodeInfo(ID, hostname, keeperAddr, domain, component string, rawSubscription []string, tag map[string]string) *NodeInfo {
info := &NodeInfo{
ID: ID,
KeeperAddr: keeperAddr,
Domain: domain,
Component: component,
StartTime: time.Now().Unix(),
Status: StatusRunning,
Hostname: hostname,
RawSubscription: rawSubscription,
ComponentTags: tag,
}
return info
}
func (n *NodeInfo) GetID() string {
return n.ID
}
func (n *NodeInfo) GetDomain() string {
return n.Domain
}
func (n *NodeInfo) GetRawSubscription() []string {
n.mu.RLock()
rawSubscription := n.RawSubscription
n.mu.RUnlock()
return rawSubscription
}
func (n *NodeInfo) GetVersion() int {
n.mu.RLock()
v := n.Version
n.mu.RUnlock()
return v
}
func (n *NodeInfo) GetKeeperAddr() string {
n.mu.RLock()
s := n.KeeperAddr
n.mu.RUnlock()
return s
}
func (n *NodeInfo) GetComponent() string {
n.mu.RLock()
s := n.Component
n.mu.RUnlock()
return s
}
func (n *NodeInfo) GetUpdateTime() int64 {
n.mu.RLock()
t := n.UpdateTime
n.mu.RUnlock()
return t
}
func (n *NodeInfo) SetStatus(status NodeStatus) {
n.mu.Lock()
n.Status = status
n.mu.Unlock()
}
func (n *NodeInfo) SetVersion(version int) {
n.mu.Lock()
n.Version = version
n.mu.Unlock()
}
func (n *NodeInfo) SetComponentTag(tag map[string]string) {
n.mu.Lock()
n.ComponentTags = tag
n.mu.Unlock()
}
func (n *NodeInfo) SetStartTime(t int64) {
n.mu.Lock()
n.StartTime = t
n.mu.Unlock()
}
func (n *NodeInfo) SetUpdateTime(t int64) {
n.mu.Lock()
n.UpdateTime = t
n.mu.Unlock()
}
func (n *NodeInfo) SetRawSubscription(rawSubscription []string) {
n.mu.Lock()
n.RawSubscription = rawSubscription
n.mu.Unlock()
}
// Node stores the information about a node
type Node struct {
*NodeInfo
Subscription []string `json:"subscription"`
StructDatas []StructData `json:"struct_datas"`
Proc *ProcInfo `json:"proc"`
Event chan Event `json:"-"`
}
func NewNode(nodeInfo NodeInfo) *Node {
node := &Node{
NodeInfo: NewNodeInfo(nodeInfo.ID, nodeInfo.Hostname, nodeInfo.KeeperAddr, nodeInfo.Domain, nodeInfo.Component, nodeInfo.RawSubscription, nodeInfo.ComponentTags),
Event: make(chan Event, eventChanSize),
}
return node
}
func (n *Node) GetSubscription() []string {
n.mu.RLock()
s := n.Subscription
n.mu.RUnlock()
return s
}
func (n *Node) GetStructDatas() []StructData {
n.mu.RLock()
structDatas := n.StructDatas
n.mu.RUnlock()
return structDatas
}
func (n *Node) GetProc() *ProcInfo {
n.mu.RLock()
p := n.Proc
n.mu.RUnlock()
return p
}
func (n *Node) SetSubscription(subscription []string) {
n.mu.Lock()
n.Subscription = subscription
n.mu.Unlock()
}
func (n *Node) SetStructDatas(structDatas []StructData, version int) {
for k := range structDatas {
structDatas[k].Version = version
}
n.mu.Lock()
n.StructDatas = structDatas
n.mu.Unlock()
}
func (n *Node) SetProc(proc *ProcInfo) {
n.mu.Lock()
n.Proc = proc
n.mu.Unlock()
}
func (n *Node) AddEvent(evt Event) error {
n.mu.RLock()
defer n.mu.RUnlock()
if n.Status == StatusStop {
return errors.New("node has stopped")
}
select {
case n.Event <- evt:
default:
return fmt.Errorf("%s event chan full", n.ID)
}
return nil
}
func (n *Node) Info() NodeInfo {
n.mu.RLock()
info := n.NodeInfo
n.mu.RUnlock()
return *info
}
func (n *Node) Exit() {
n.mu.Lock()
defer n.mu.Unlock()
n.Status = StatusStop
for {
select {
case <-n.Event:
default:
return
}
}
}