-
Notifications
You must be signed in to change notification settings - Fork 3
/
Monitoring_Node.py
341 lines (281 loc) · 13.7 KB
/
Monitoring_Node.py
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# Author: Ritesh G, Shreyas M
from xmlrpc.server import SimpleXMLRPCServer
from collections import defaultdict
from gossip_server import get_arguments, start_gossip_node, stop_gossip_node
from gossip_rpc.config import Configuration
import os
import time
import copy
from utilities.utils import *
import utilities.Constants as Constants
class MonitoringNode:
"""
Author: Ritesh G, Shreyas M
Independent Node to passively receive information from
other nodes in cluster.
1. Keeps track of nodes joining the network.
2. Shows the state of cluster.
3. Implementation of Consensus Algorithm
NOTE: This node is not a part of cluster and it doesn't participate
or provide information to any node in the cluster.
"""
def __init__(self):
# Author: Ritesh G
self.node_index = 0 # order of node joining
self.IP_to_Node_Index = {}
self.Index_to_IP = {}
self.suspect_matrix = [[]] # matrix of fault vectors of every node.
self.global_fault_vector = [] # overall state of network. 1 is FAIL.
self.global_state_map = defaultdict(defaultdict) # State of cluster
self.start_time = None
self.total_msg_count = 0
self.consensusMap = defaultdict(defaultdict)
self.ip_generation = defaultdict() # records node's generation at which failure occured.
self.heartbeatTime = {}
self.false_failure_count = 0
def setheartbeatTime(self, ip):
self.heartbeatTime[ip] = getTimeStamp()
def setMapping(self,ip):
"""
Author: Ritesh G
:param ip: new IP joining the network
Intializes Fault vector for this IP and updates global fault vector.
"""
if ip in self.IP_to_Node_Index:
self.suspect_matrix[self.IP_to_Node_Index[ip]] = list(self.global_fault_vector)
return
self.global_fault_vector.extend([0])
self.IP_to_Node_Index[ip] = self.node_index
self.Index_to_IP[self.node_index] = ip
for row in self.suspect_matrix:
row.extend([0])
if(len(self.suspect_matrix[0]) > 1):
self.suspect_matrix.append(list(self.global_fault_vector))
self.node_index += 1
def getMapping(self):
"""
Author: Ritesh G
Used in front-end of Flask.
"""
return self.IP_to_Node_Index
def updateSuspectMatrix(self, ip, fault_vector, generation):
"""
Author: Ritesh G
:param ip : IP who is telling to update status for nodes is fault_vector
:param fault_vector: new status of nodes according to IP
"param generation : used in false failure detection.
"""
try:
if self.global_fault_vector[self.IP_to_Node_Index[ip]] == 1:
self.global_fault_vector[self.IP_to_Node_Index[ip]] = 0
except Exception as e:
pass
for k,v in fault_vector.items():
if v==0:
try:
if(self.IP_to_Node_Index[k] in self.ip_generation and generation == self.ip_generation[self.IP_to_Node_Index[k]]):
# if generation for a node which was previously detected FAIL is same as
# current generation in alive state then it was a FALSE FAILURE DETECTION
print('False failure detection happened for ', k)
self.false_failure_count += 1
except Exception as e:
pass
self.suspect_matrix[self.IP_to_Node_Index[ip]][self.IP_to_Node_Index[k]] = v
self.doConsensus()
def getFaultVector(self):
"""
Author: Ritesh G
Used in front-end of Flask.
"""
return self.global_fault_vector
def showAliveDeadNode(self):
# Author: Ritesh G
live_nodes = [self.Index_to_IP[ip] for ip,status in enumerate(self.global_fault_vector) if status!=1]
dead_nodes = [self.Index_to_IP[ip] for ip,status in enumerate(self.global_fault_vector) if status==1]
return {'live': live_nodes, 'dead': dead_nodes}
def sendEpStateMap(self, ip, epStateMap, msg_count):
# Author: Ritesh G
self.receiveStateMap(ip, epStateMap, msg_count)
def receiveStateMap(self, ip, epStateMap, msg_count):
"""
Author: Ritesh G
Receives information from nodes passively.
It includes:
what a node thinks about the cluster and
total messages exchanged by the node up until this point.
"""
self.global_state_map[ip] = epStateMap
self.total_msg_count += msg_count
def getSuspectMatrix(self):
"""
Author: Ritesh G
Used in front-end of Flask.
"""
return self.suspect_matrix
def doConsensus(self):
"""
Author: Shreyas M
Consensus Algorithm.
"""
print('---------------------------------------')
for j in range(len(self.suspect_matrix[0])):
state = 1
for i in range(len(self.suspect_matrix)):
if i != j and ((self.global_fault_vector[i]) != 1):
try:
if getDiffInSeconds(self.heartbeatTime[self.Index_to_IP[i]]) < Constants.WAIT_SECONDS_FAIL:
state &= self.suspect_matrix[i][j]
except Exception as e:
pass
if(state == 1):
print("Node %s is failed" % (self.Index_to_IP[j]))
else:
print("Node %s is alive" % (self.Index_to_IP[j]))
self.global_fault_vector[j] = state
if(state == 1):
try:
self.ip_generation[j] = self.global_state_map[self.Index_to_IP[j]][self.Index_to_IP[j]]['heartBeat']['generation']
except Exception as e:
pass
print('---------------------------------------')
def doConsensusCheck(self):
"""
Author: Shreyas M
Check Consensus.
NOTE: This doesn't provide any external information to the nodes about
the status of nodes. It is just for presentation purpose to know
what is happening inside the cluster.
"""
res = True
currentEpStateMap = copy.deepcopy(self.global_state_map)
for k,v in currentEpStateMap.items() :
for k1, v1 in v.items():
temp = {}
temp['generation'] = v1['heartBeat']['generation']
temp['App_status'] = v1['appState']['App_status']
temp['App_version'] = v1['appState']['App_version']
temp['fault_vector'] = self.suspect_matrix[self.IP_to_Node_Index[k]]
self.consensusMap[k][k1] = temp
# structure of consensus map
# consensusMap = {k:{'App_version':v[], 'App_status':, 'generation':, 'fault_vector':} }
keyList = list(self.consensusMap.keys())
temp = None
for i in range(len(keyList)):
if self.global_fault_vector[self.IP_to_Node_Index[keyList[i]]] != 1:
if temp == None:
temp = self.consensusMap[keyList[i]]
else:
res = res & (temp == self.consensusMap[keyList[i]])
temp = self.consensusMap[keyList[i]]
return res
def readFile(self):
"""
Author: Ritesh G
Used in front-end of Flask.
"""
data = ""
with open('results.txt', 'r') as fp:
data = fp.read()
data = data.replace('\n', '<br>')
return data
if __name__ == "__main__":
import socket
import socket, dns.resolver,dns.reversename
configuration_file,_ = get_arguments()
os.environ["GOSSIP_CONFIG"] = configuration_file
# ConfigurationManager.reset_configuration()
configuration = Configuration(configuration_file)
# Author: Tanvi P
'''
Configuring the nodes to pickup their ip and ports from respective hostnames and also resolve dns for the other hosts in the network
'''
host_ip = socket.gethostbyname(socket.gethostname())
server_ip = str(dns.resolver.resolve_address(host_ip).rrset[0]).split('.')[0]
server_port = configuration.get_gossip_port()
# Author: Ritesh G
node = MonitoringNode()
start_gossip_node(node)
import json
while True:
console_input = input("1. \"Test Case for Single Node failure\"\
\n2. \"Test Case for Simultaneous Failure\"\
\n3. \"Test Case for Dead Node becomes Alive\"\
\n4. \"stop\"\nEnter your input:")
if console_input.strip() == "collect":
with open('states.json','w') as fp:
json.dump(node.global_state_map,fp)
with open('ip_mapping.json', 'w') as fp:
json.dump(node.IP_to_Node_Index, fp)
if console_input.strip() == "1":
# single node failure case
node.start_time = time.perf_counter()
node.total_msg_count = 0
node.false_failure_count = 0
flag = 0
while(1):
for ip,status in enumerate(node.global_fault_vector):
ip = node.Index_to_IP[ip]
if status == 1 and (getDiffInSeconds(node.heartbeatTime[ip]) > Constants.WAIT_SECONDS_FAIL):
flag = 1 #consensus
break
if flag:
run_time = time.perf_counter() - node.start_time
with open('results.txt', 'a+') as file_pointer:
file_pointer.write('\n\n------------- Consensus Achieved --------------\n')
file_pointer.write('Test Case for Single Node failure\n')
file_pointer.write("Run time for detection: "+str(run_time)+'\n')
file_pointer.write("Total Messages exchanged for consensus: "+str(node.total_msg_count)+'\n')
file_pointer.write("False Failure Detection Count: "+str(node.false_failure_count)+'\n')
file_pointer.write("Total number of nodes in cluster: "+str(len(node.Index_to_IP))+'\n')
file_pointer.close()
print('------------- Consensus Achieved --------------')
print("Run time for detection: ", run_time)
print("Total Messages exchanged for consensus: ", node.total_msg_count)
print("False Failure Detection Count: ",node.false_failure_count)
break
if console_input.strip() == "2":
node.start_time = time.perf_counter()
node.total_msg_count = 0
node.false_failure_count = 0
while(1):
if(node.doConsensusCheck()):
run_time = time.perf_counter() - node.start_time
with open('results.txt', 'a+') as file_pointer:
file_pointer.write('\n\n------------- Consensus Achieved --------------\n')
file_pointer.write('Test Case for Simultaneous Failure\n')
file_pointer.write("Run time for detection: "+str(run_time)+'\n')
file_pointer.write("Total Messages exchanged for consensus"+str(node.total_msg_count)+'\n')
file_pointer.write("False Failure Detection Count: "+str(node.false_failure_count)+'\n')
file_pointer.write("Total number of nodes in cluster: "+str(len(node.Index_to_IP))+'\n')
file_pointer.close()
print('------------- Consensus Achieved --------------')
print("Run time for detection: ", run_time)
print("Total Messages exchanged for consensus", node.total_msg_count)
print("False Failure Detection Count: ",node.false_failure_count)
break
# exit(0)
if console_input.strip() == "3":
node.start_time = time.perf_counter()
node.total_msg_count = 0
node.false_failure_count = 0
while(1):
# if(len(list(set(list(result_dict.values())))) == 1):
if(node.doConsensusCheck()):
run_time = time.perf_counter() - node.start_time
with open('results.txt', 'a+') as file_pointer:
file_pointer.write('\n\n------------- Consensus Achieved --------------\n')
file_pointer.write('Test Case for Dead Node becomes Alive\n')
file_pointer.write("Run time for detection: "+str(run_time)+'\n')
file_pointer.write("Total Messages exchanged for consensus"+str(node.total_msg_count)+'\n')
file_pointer.write("False Failure Detection Count: "+str(node.false_failure_count)+'\n')
file_pointer.write("Total number of nodes in cluster: "+str(len(node.Index_to_IP))+'\n')
file_pointer.close()
print('------------- Consensus Achieved --------------')
print("Run time for detection: ", run_time)
print("Total Messages exchanged for consensus", node.total_msg_count)
print("False Failure Detection Count: ",node.false_failure_count)
break
# exit(0)
if console_input.strip() == "4":
stop_gossip_node()
break