Skip to content

Commit dfff64c

Browse files
Merge pull request #5 from harshit-soora/vivek
Vivek
2 parents dc91a58 + 2c9d36e commit dfff64c

File tree

3 files changed

+150
-87
lines changed

3 files changed

+150
-87
lines changed

monitor/backup.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import socket
2+
import pickle
3+
import sys
4+
sys.path.insert(1, '../utils/')
5+
6+
from transfer import _send_msg, _recv_msg, _wait_recv_msg
7+
from info import RECV_PRIMARY_UPDATE_PORT, MSG_SIZE
8+
9+
hashtable = {}
10+
MDS_flags = {}
11+
cluster_topology = {}
12+
13+
def recv_primary_update():
14+
recv_primary_update_socket = socket.socket()
15+
print ("recv primary update socket successfully created")
16+
recv_primary_update_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
17+
18+
# reserve a port on your computer
19+
port = RECV_PRIMARY_UPDATE_PORT
20+
21+
# Next bind to the port
22+
# we have not entered any ip in the ip field
23+
# instead we have inputted an empty string
24+
# this makes the server listen to requests
25+
# coming from other computers on the network
26+
recv_primary_update_socket.bind(('', port))
27+
print ("primary update socket bound to %s" %(port))
28+
29+
# put the socket into listening mode
30+
recv_primary_update_socket.listen(5)
31+
print ("primary update socket is listening")
32+
33+
# a forever loop until we interrupt it or
34+
# an error occurs
35+
while True:
36+
37+
# Establish connection with osd
38+
c, addr = recv_primary_update_socket.accept()
39+
print ('Got connection from', addr)
40+
41+
# recv the update
42+
update = _recv_msg(c, 1024)
43+
print(update)
44+
45+
if update["update_type"] == "hash_table":
46+
for i in range(len(update["pg_or_osd_ids_list"])):
47+
hashtable[update["pg_or_osd_ids_list"][i]] = update["osd_list"][i]
48+
else:
49+
for i in range(len(update["pg_or_osd_ids_list"])):
50+
cluster_topology[update["pg_or_osd_ids_list"][i]] = update["osd_list"][i]
51+
52+
hashtable_file = open('hashtable', 'wb')
53+
hashtable_dump = pickle.dumps(hashtable)
54+
hashtable_file.write(hashtable_dump)
55+
hashtable_file.close()
56+
57+
cluster_topology_file = open('cluster_topology', 'wb')
58+
cluster_topology_dump = pickle.dumps(cluster_topology)
59+
cluster_topology_file.write(cluster_topology_dump)
60+
cluster_topology_file.close()
61+
msg = {"status":"SUCCESS"}
62+
_send_msg(c, msg)
63+
# send the acknowledgement
64+
c.close()
65+
66+
recv_primary_update_socket.close()
67+
68+
def main():
69+
global hashtable, cluster_topology, MDS_flags
70+
71+
hashtable_file = open('hashtable', 'rb')
72+
hashtable_dump = hashtable_file.read()
73+
hashtable = pickle.loads(hashtable_dump)
74+
75+
MDS_flags_file = open('MDS_flags', 'rb')
76+
MDS_flags_dump = MDS_flags_file.read()
77+
MDS_flags = pickle.loads(MDS_flags_dump)
78+
79+
cluster_topology_file = open('cluster_topology', 'rb')
80+
cluster_topology_dump = cluster_topology_file.read()
81+
cluster_topology = pickle.loads(cluster_topology_dump)
82+
83+
hashtable_file.close()
84+
MDS_flags_file.close()
85+
cluster_topology_file.close()
86+
recv_primary_update()
87+
88+
if __name__ == '__main__':
89+
main()

monitor/main.py

+33-87
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
from transfer import _send_msg, _recv_msg, _wait_recv_msg
1717
# from monitor_gossip import heartbeat_protocol
1818
from info import MDS_IPs, MDS_PORT, WRITE_ACK_PORT, OSD_INACTIVE_STATUS_PORT, CLIENT_REQ_PORT, \
19-
RECV_PRIMARY_UPDATE_PORT, MSG_SIZE
19+
RECV_PRIMARY_UPDATE_PORT, MSG_SIZE, MONITOR_IPs
2020

2121
# hashtable = {}
2222
MDS_flags = {}
2323
cluster_topology = {}
2424
MDS_IP = MDS_IPs["primary"]["ip"]
25+
isPrimary = True
2526

2627
# pg_or_osd_list = pg_ids list, if update_type == "hashtable"
2728
# osd_ids_list, else
@@ -32,7 +33,7 @@ def update_backup_monitor(update_type, pg_or_osd_ids_list, osd_list):
3233
print ("primary update socket successfully created")
3334

3435
try:
35-
primary_update_socket.connect((MDS_IPs["backup"]["ip"], RECV_PRIMARY_UPDATE_PORT))
36+
primary_update_socket.connect((MONITOR_IPs["backup"], RECV_PRIMARY_UPDATE_PORT))
3637
msg = {"update_type": update_type, "pg_or_osd_ids_list": pg_or_osd_ids_list, \
3738
"osd_list": osd_list}
3839
_send_msg(primary_update_socket, msg)
@@ -46,62 +47,6 @@ def update_backup_monitor(update_type, pg_or_osd_ids_list, osd_list):
4647
print(e)
4748
primary_update_socket.close()
4849

49-
def recv_primary_update():
50-
recv_primary_update_socket = socket.socket()
51-
print ("recv primary update socket successfully created")
52-
recv_primary_update_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
53-
54-
# reserve a port on your computer
55-
port = RECV_PRIMARY_UPDATE_PORT
56-
57-
# Next bind to the port
58-
# we have not entered any ip in the ip field
59-
# instead we have inputted an empty string
60-
# this makes the server listen to requests
61-
# coming from other computers on the network
62-
recv_primary_update_socket.bind(('', port))
63-
print ("primary update socket bound to %s" %(port))
64-
65-
# put the socket into listening mode
66-
recv_primary_update_socket.listen(5)
67-
print ("primary update socket is listening")
68-
69-
# a forever loop until we interrupt it or
70-
# an error occurs
71-
while True:
72-
73-
# Establish connection with osd
74-
c, addr = recv_primary_update_socket.accept()
75-
print ('Got connection from', addr)
76-
77-
# recv the update
78-
update = _recv_msg(c, 1024)
79-
print(update)
80-
# hashtable = _read
81-
82-
if update["update_type"] == "hash_table":
83-
for i in range(len(update["pg_or_osd_ids_list"])):
84-
hashtable[update["pg_or_osd_ids_list"][i]] = update["osd_list"][i]
85-
else:
86-
for i in range(len(update["pg_or_osd_ids_list"])):
87-
cluster_topology[update["pg_or_osd_ids_list"][i]] = update["osd_list"][i]
88-
89-
hashtable_file = open('hashtable', 'wb')
90-
hashtable_dump = pickle.dumps(hashtable)
91-
hashtable_file.write(hashtable_dump)
92-
hashtable_file.close()
93-
94-
cluster_topology_file = open('cluster_topology', 'wb')
95-
cluster_topology_dump = pickle.dumps(cluster_topology)
96-
cluster_topology_file.write(cluster_topology_dump)
97-
cluster_topology_file.close()
98-
msg = {"status":"SUCCESS"}
99-
_send_msg(c, msg)
100-
# send the acknowledgement
101-
c.close()
102-
103-
recv_primary_update_socket.close()
104-
10550
def recv_write_acks():
10651
global hashtable, cluster_topology, MDS_IP, MDS_flags
10752

@@ -164,10 +109,11 @@ def recv_write_acks():
164109

165110
cluster_topology[osd_id]["free_space"] = free_space
166111

167-
# updating the backup
168-
update_backup_monitor("hash_table", [pg_id], [hashtable[pg_id]])
169-
update_backup_monitor("cluster_topology", [osd[0] for osd in hashtable[pg_id]], \
170-
[cluster_topology[osd[0]] for osd in hashtable[pg_id]])
112+
if isPrimary:
113+
# updating the backup
114+
update_backup_monitor("hash_table", [pg_id], [hashtable[pg_id]])
115+
update_backup_monitor("cluster_topology", [osd[0] for osd in hashtable[pg_id]], \
116+
[cluster_topology[osd[0]] for osd in hashtable[pg_id]])
171117

172118
hashtable_file = open('hashtable', 'wb')
173119
cluster_topology_file = open('cluster_topology', 'wb')
@@ -331,8 +277,9 @@ def recv_client_reqs():
331277
addrs[osd_id] = (cluster_topology[osd_id]["ip"], cluster_topology[osd_id]["port"])
332278
osd_dict = {"osd_ids": osd_ids, "addrs": addrs}
333279
response = {"osd_dict": osd_dict, "status":"SUCCESS", "msg": "written succefully"}
334-
# updating the backup(only hash_table)
335-
update_backup_monitor("hash_table", [pg_id], [hashtable[pg_id]])
280+
if isPrimary:
281+
# updating the backup(only hash_table)
282+
update_backup_monitor("hash_table", [pg_id], [hashtable[pg_id]])
336283

337284
hashtable_file = open('hashtable', 'wb')
338285
hashtable_dump = pickle.dumps(hashtable)
@@ -355,6 +302,8 @@ def recv_client_reqs():
355302

356303

357304
def main(argc, argv):
305+
global isPrimary
306+
358307
if argc < 2:
359308
print('usage: python3 main.py <monitor_type>') # monitor_type = "primary" or "backup"
360309
exit(-1)
@@ -399,7 +348,7 @@ def main(argc, argv):
399348
# }
400349
# }
401350

402-
global hashtable, cluster_topology, MDS_flags
351+
global hashtable, cluster_topology, MDS_flags, MDS_IP
403352

404353
hashtable_file = open('hashtable', 'rb')
405354
hashtable_dump = hashtable_file.read()
@@ -418,28 +367,25 @@ def main(argc, argv):
418367
MDS_flags_file.close()
419368
cluster_topology_file.close()
420369

421-
if isPrimary:
422-
## THREADS
423-
# write_acks_thread : receives write acks from osds
424-
# client_reqs_thread : receives client reqs from the client and
425-
# sends back the osds' addresses
426-
# osd_inactive_status_thread : receives the osd_ids which are inactive
427-
428-
write_acks_thread = threading.Thread(target=recv_write_acks)
429-
client_reqs_thread = threading.Thread(target=recv_client_reqs)
430-
osd_inactive_status_thread = threading.Thread(target=recv_inactive_osd)
431-
432-
# starting the threads
433-
write_acks_thread.start()
434-
client_reqs_thread.start()
435-
osd_inactive_status_thread.start()
436-
437-
# closing the threads
438-
write_acks_thread.join()
439-
client_reqs_thread.join()
440-
osd_inactive_status_thread.join()
441-
else:
442-
recv_primary_update()
370+
## THREADS
371+
# write_acks_thread : receives write acks from osds
372+
# client_reqs_thread : receives client reqs from the client and
373+
# sends back the osds' addresses
374+
# osd_inactive_status_thread : receives the osd_ids which are inactive
375+
376+
write_acks_thread = threading.Thread(target=recv_write_acks)
377+
client_reqs_thread = threading.Thread(target=recv_client_reqs)
378+
osd_inactive_status_thread = threading.Thread(target=recv_inactive_osd)
379+
380+
# starting the threads
381+
write_acks_thread.start()
382+
client_reqs_thread.start()
383+
osd_inactive_status_thread.start()
384+
385+
# closing the threads
386+
write_acks_thread.join()
387+
client_reqs_thread.join()
388+
osd_inactive_status_thread.join()
443389

444390
def _read_hash():
445391
hashtable_file = open('hashtable', 'rb')

monitor/test.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pickle
2+
3+
hashtable_file = open('hashtable', 'r+b')
4+
hashtable_dump = hashtable_file.read()
5+
hashtable = pickle.loads(hashtable_dump)
6+
7+
MDS_flags_file = open('MDS_flags', 'rb')
8+
MDS_flags_dump = MDS_flags_file.read()
9+
MDS_flags = pickle.loads(MDS_flags_dump)
10+
11+
cluster_topology_file = open('cluster_topology', 'r+b')
12+
cluster_topology_dump = cluster_topology_file.read()
13+
cluster_topology = pickle.loads(cluster_topology_dump)
14+
15+
cluster_topology[4]["status"] = 1
16+
17+
hashtable_dump = pickle.dumps(hashtable)
18+
hashtable_file.write(hashtable_dump)
19+
20+
cluster_topology_dump = pickle.dumps(cluster_topology)
21+
cluster_topology_file.write(cluster_topology_dump)
22+
23+
print(hashtable)
24+
print(cluster_topology)
25+
26+
hashtable_file.close()
27+
MDS_flags_file.close()
28+
cluster_topology_file.close()

0 commit comments

Comments
 (0)