/
naming_logging.py
executable file
·208 lines (171 loc) · 6.8 KB
/
naming_logging.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
#!/usr/bin/env python3
'''
@shekharpalit
'''
import sys
import struct
import socket
from xmlrpc.server import SimpleXMLRPCServer
from threading import Thread
import concurrent.futures
import proxy
import datetime
import json
import time
naming_proxy = proxy.TupleSpaceAdapter('http://localhost:8004')
MAX_UDP_PAYLOAD = 65507
server = SimpleXMLRPCServer(("localhost", 5000), allow_none = True)
map_tuple_uri = {}
ack_track = {}
def replicate_tuple_to_all(name, data): # method to replicate the data to every node in teh cluster
global map_tuple_uri, ack_track
for key, val in map_tuple_uri.items():
if key != name:
replicate_proxy = proxy.TupleSpaceAdapter(val[0])
replicate_proxy._out(data)
return "Sucessfully replicated\n"
def delete_from_file(data): #helper to delete the data from the file
with open("log_data.txt","r+") as f:
new_f = f.readlines()
f.seek(0)
str_data = str(data[:len(data)-1])[1:-1]
str_data.split(",")
str_data = "[" + str_data + ","
str_data.replace("None,", "")
for line in new_f:
if str_data not in line:
f.write(line)
f.truncate()
return True
def delete_from_all(name, data): # helper to delete data from everywhere to maintain consistency
global map_tuple_uri
for key, val in map_tuple_uri.items():
if key != name:
delete_proxy = proxy.TupleSpaceAdapter(val[0])
delete_proxy._in(data)
if delete_from_file(data):
return True
def logging(data): # method to log the data in file
with open('log_data.txt', 'a') as f:
f.write(str(data))
f.write("\n")
return True
def write_to_TupleSpace(data_from_RPC): #helper to Write the value from the TS
global map_tuple_uri, ack_track
if map_tuple_uri[data_from_RPC[0]][1] == True:
URI = map_tuple_uri[data_from_RPC[0]][0]
operation_on_Ts = proxy.TupleSpaceAdapter(URI)
ts = datetime.datetime.now().timestamp()
data_from_RPC.append(str(ts))
operation_on_Ts._out(data_from_RPC)
t3 = Thread(target=replicate_tuple_to_all, args=(data_from_RPC[0], data_from_RPC))
t3.start()
t3.join(2)
if ack_track[data_from_RPC[-1]] >= 2:
if logging(data_from_RPC):
del ack_track[data_from_RPC[-1]]
def Read_from_TupleSpace(data_from_RPC): #helper to read the value from the TS
global map_tuple_uri
if map_tuple_uri[data_from_RPC[0]][1] == True:
URI = map_tuple_uri[data_from_RPC[0]][0]
operation_on_Ts = proxy.TupleSpaceAdapter(URI)
res = operation_on_Ts._rd([data_from_RPC[1],data_from_RPC[2], data_from_RPC[3], None])
return str(res)
def Delete_from_TupleSpace(data_from_RPC): #helper to delete the value from the TS
global map_tuple_uri
if map_tuple_uri[data_from_RPC[0]][1] == True:
URI = map_tuple_uri[data_from_RPC[0]][0]
operation_on_Ts = proxy.TupleSpaceAdapter(URI)
res = operation_on_Ts._in([data_from_RPC[1],data_from_RPC[2], data_from_RPC[3], None])
if delete_from_all(data_from_RPC[0], [data_from_RPC[1],data_from_RPC[2], data_from_RPC[3], None]):
return str(res)
def async_check_helper(URI): #helper fucntion to check the node status async
check_stats = proxy.TupleSpaceAdapter(URI)
try:
active_node = check_stats._rd([None, None, None, None])
if active_node:
return True
except Exception as e:
print("Node is not active")
return False
def check_status_for_nodes(): #method to check the node status
global map_tuple_uri
for key, val in map_tuple_uri.items():
with concurrent.futures.ThreadPoolExecutor() as executor:
results = [executor.submit(async_check_helper, val[0])]
for f in concurrent.futures.as_completed(results):
if f.result():
map_tuple_uri[key][1] = True
else:
map_tuple_uri[key][1] = False
def recover_TupleSpace(URI): #method to recover the TupleSpaces
file = open("log_data.txt","r")
content = file.readlines()
results = []
if content ==['\n']:
print("Log file is empty")
else:
for line in range(len(content)):
results.append([content[line].split("'")[1], content[line].split("'")[3],content[line].split("'")[5],
json.loads(content[line].split("'")[-2])])
for result in results:
URI._out(result)
file.close()
def main(address = '224.0.0.1', port = '54322'):
server_address = ('', int(port))
global map_tuple_uri, ack_track
size = 100
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(server_address)
group = socket.inet_aton(address)
mreq = struct.pack('4sL', group, socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
print(f"Listening on udp://{address}:{port}")
try:
while True:
data, _ = sock.recvfrom(MAX_UDP_PAYLOAD)
notification = data.decode()
namecheck = None
notifications_string = notification.split(' ')
notifications_string_tuple = ' '.join(notifications_string[2:])[1:-1]
notifications_string_tuple_value = notifications_string_tuple.split(',')
res = []
for char in notifications_string_tuple_value:
res.append(char[1:-1])
if notifications_string[1] == 'adapter':
get_URI_proxy = proxy.TupleSpaceAdapter(str(notifications_string[2]))
if notifications_string[0] not in map_tuple_uri:
map_tuple_uri[notifications_string[0]] = [notifications_string[2], True]
recover_TupleSpace(get_URI_proxy)
elif notifications_string[1] == 'write':
ack_track[res[-1]] = 1
ack_track[res[-1]] += 1
except Exception as e:
print(e)
sock.close()
def usage(program):
print(f'Usage: {program} ADDRESS PORT', file=sys.stderr)
sys.exit(1)
def server_handler_for_write():
server.register_function(write_to_TupleSpace, 'write_to_TupleSpace')
server.serve_forever()
def server_handler_for_delete():
server.register_function(Delete_from_TupleSpace, 'Delete_from_TupleSpace')
server.serve_forever()
def server_handler_for_read():
server.register_function(Read_from_TupleSpace, 'Read_from_TupleSpace')
server.serve_forever()
t0 = Thread(target=server_handler_for_write)
t0.start()
t1 = Thread(target=server_handler_for_read)
t1.start()
t2 = Thread(target=server_handler_for_delete)
t2.start()
def heartbeat(): #method to check the heartbeat of the nodes in the cluster
while True:
time.sleep(30)
check_status_for_nodes()
if __name__ == '__main__':
t = Thread(target=heartbeat)
t.start()
main()