-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusternode.go
216 lines (197 loc) · 6.47 KB
/
clusternode.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
/*
Cluster Engine management
The package 'cluster' is used by verdmell to manage the cluster.
=> Is known as a cluster a set of nodes.
-ClusterEngine
-Cluster
-ClusterNode
-ClusterService
-ClusterMessage
*/
package cluster
import (
"errors"
"strconv"
"verdmell/service"
"verdmell/utils"
)
//#
//#
//# ClusterNode struct:
//# ClusterNode is defined by a node name and its URL
type ClusterNode struct{
Name string `json:"name"`
URL string `json:"URL"`
Timestamp int64 `json:"timestamp"`
Status int `json:"status"`
NodeServices map[string] *service.ServiceObject `json:"services"`
CandidateForDetelion bool `json:"candidatefordeletion"`
}
//
//# NewClusterNode: return a CheckEngine instance to be run
func NewClusterNode(name string, url string) (error, *ClusterNode) {
env.Output.WriteChDebug("(ClusterNode::NewClusterNode)")
node := new(ClusterNode)
node.SetName(name)
node.SetURL(url)
node.SetStatus(-1)
node.SetTimestamp(0)
node.SetCandidateForDeletion(false)
node.NodeServices = make( map[string]*service.ServiceObject )
return nil, node
}
//#
//# Getters and Setters
//#----------------------------------------------------------------------------------------
//
//# SetName: attribute from ClusterNode
func (c *ClusterNode) SetName(name string) {
env.Output.WriteChDebug("(ClusterNode::SetName) Set value '"+name+"'")
c.Name = name
}
//
//# SetURL: attribute from ClusterNode
func (c *ClusterNode) SetURL(url string) {
env.Output.WriteChDebug("(ClusterNode::SetURL) Set value '"+url+"'")
c.URL = url
}
//
//# SetStatus: attribute from ClusterNode
func (c *ClusterNode) SetStatus(status int) {
env.Output.WriteChDebug("(ClusterNode::SetStatus) Set value")
c.Status = status
}
//
//# SetTimestamp: attribute from ClusterNode
func (c *ClusterNode) SetTimestamp(t int64) {
env.Output.WriteChDebug("(ClusterNode::SetTimestamp)")
c.Timestamp = t
}
//
//# SetNodeServices: attribute from ClusterNode
func (c *ClusterNode) SetNodeServices(s map[string] *service.ServiceObject) {
env.Output.WriteChDebug("(ClusterNode::SetNodeServices)")
c.NodeServices = s
}
//
//# SetCandidateForDetelion: attribute from ClusterNode
func (c *ClusterNode) SetCandidateForDeletion(d bool) {
env.Output.WriteChDebug("(ClusterNode::SetCandidateForDeletion)")
c.CandidateForDetelion = d
}
//
//# GetName: attribute from ClusterNode
func (c *ClusterNode) GetName() string {
env.Output.WriteChDebug("(ClusterNode::GetName) Get value '"+c.Name+"'")
return c.Name
}
//
//# GetURL: attribute from ClusterNode
func (c *ClusterNode) GetURL() string {
env.Output.WriteChDebug("(ClusterNode::GetName) Get value '"+c.URL+"'")
return c.URL
}
//
//# GetStatus: attribute from ClusterNode
func (c *ClusterNode) GetStatus() int {
env.Output.WriteChDebug("(ClusterNode::GetStatus) Get value")
return c.Status
}
//
//# GetTimestamp: attribute from ClusterNode
func (c *ClusterNode) GetTimestamp() int64 {
env.Output.WriteChDebug("(ClusterNode::GetTimestamp)")
return c.Timestamp
}
//
//# GetNodeServices: attribute from ClusterNode
func (c *ClusterNode) GetNodeServices() map[string] *service.ServiceObject {
env.Output.WriteChDebug("(ClusterNode::GetNodeServices)")
return c.NodeServices
}
//
//# SetCandidateForDetelion: attribute from ClusterNode
func (c *ClusterNode) GetCandidateForDeletion() bool {
env.Output.WriteChDebug("(ClusterNode::GetCandidateForDetelion)")
return c.CandidateForDetelion
}
//#
//# Specific methods
//#---------------------------------------------------------------------
//
//# CopyClusterNode: method copies a cluster node
func (c *ClusterNode) CopyClusterNode() *ClusterNode {
env.Output.WriteChDebug("(ClusterNode::CopyClusterNode)")
if c == nil {
return nil
}
node := new(ClusterNode)
node.SetName(c.GetName())
node.SetURL(c.GetURL())
node.SetStatus(c.GetStatus())
node.SetTimestamp(c.GetTimestamp())
node.SetCandidateForDeletion(c.GetCandidateForDeletion())
node.SetNodeServices(c.GetNodeServices())
return node
}
//
//# HasService: method return if a service is defined on cluster node
func (c *ClusterNode) HasService(s string) (error, *service.ServiceObject) {
env.Output.WriteChDebug("(ClusterNode::HasService) "+s)
if srv, exist := c.NodeServices[s]; !exist {
msg := "(ClusterNode::HasService) Service '"+s+"' does not exit on node "+c.GetName()
env.Output.WriteChDebug(msg)
return errors.New(msg), nil
} else {
return nil, srv
}
}
//
//# AddService: method add a service to cluster node
func (c *ClusterNode) AddService(s *service.ServiceObject) error {
env.Output.WriteChDebug("(ClusterNode::AddService) Add service to clusternode {node:'"+c.GetName()+"', service:'"+s.GetName()+"', service_status:"+strconv.Itoa(int(s.GetStatus()))+", service_timestamp:"+strconv.Itoa(int(s.GetTimestamp()))+"}")
c.NodeServices[s.GetName()] = s
if s.GetName() == c.GetName() {
c.SetStatus(s.GetStatus())
env.Output.WriteChDebug("(ClusterNode::AddService) Node has changed its status {node:"+c.GetName()+", status:"+strconv.Itoa(int(c.GetStatus()))+"}")
}
// if err,_ := c.HasService(s.GetName()); err != nil {
// c.NodeServices[s.GetName()] = s
// env.Output.WriteChDebug("(ClusterNode::AddService) Service '"+s.GetName()+"' added on node '"+c.GetName()+"'")
// if s.GetName() == c.GetName() {
// c.SetStatus(s.GetStatus())
// env.Output.WriteChDebug("(ClusterNode::AddService) Node has changed its status {node:"+c.GetName()+", status:"+strconv.Itoa(int(c.GetStatus()))+"}")
// }
// } else {
// //return errors.New("(ClusterNode::AddService) "+err.Error())
// return err
// }
return nil
}
//
//# DeleteService: method deletes a service to cluster node
func (c *ClusterNode) DeleteService(service string) error {
env.Output.WriteChDebug("(ClusterNode::DeleteService) Delete service '"+service+"' on node '"+c.GetName()+"'")
delete(c.NodeServices,service)
return nil
}
//
//# IncreaseTimestamp: method increade the timestamp to cluster node
func (c *ClusterNode) IncreaseTimestamp() error {
env.Output.WriteChDebug("(ClusterNode::IncreaseTimestamp) Node '"+c.GetName()+"'")
c.Timestamp++
return nil
}
//#
//# Common methods
//#---------------------------------------------------------------------
//
//# string: convert a ClusterService object to string
func (c *ClusterNode) String() string {
if err, str := utils.ObjectToJsonString(c); err != nil{
return err.Error()
} else{
return str
}
}
//#######################################################################################################