-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.py
588 lines (521 loc) · 24.6 KB
/
peer.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
"""
Peer
Authors: Ana Gomes, Andre Freitas
Description: This class describes a peer computer. The peer have a home directory
that is where the backups are store and the temporary files, ie. chunks
that were generated to send to another peer in the network. It's used by the daemon
"""
import os
import socket
import struct
import random
import time
import re
import shutil
from file import File
from threading import Thread,Timer
from datetime import datetime
from datetime import timedelta
from data import Data
from chunks_monitor import ChunksMonitor
BACKUP_DIR="backup"
TEMP_DIR="temp"
RESTORE_DIR="restore"
SHELL_PORT=8383
VERSION="1.0"
CRLF="\r\n"
MAX_MESSAGE_SIZE=65565
TTL=1
MAX_ATTEMPTS=5
TIMEOUT=0.5
LOOPBACK=0
MAX_DELETE_WAITING_TIME=60
waiting=False
stop_restore_waiting=False
restore_waiting_improved_protocol=True
subscriptions={}
restored={}
def print_message(message):
print "[" + datetime.now().strftime("%d/%m/%y %H:%M") + "] " + message
class Peer:
def __init__(self, home_dir,mc_address,mc_port,mdb_address,mdb_port,mdr_address,mdr_port,backup_size,shell_port=SHELL_PORT):
self.home_dir=home_dir
self.init_home_dir()
self.mc=self.create_multicast_socket(mc_address, mc_port)
self.mc_address=mc_address
self.mc_port=mc_port
self.mdb_address=mdb_address
self.mdb_port=mdb_port
self.mdr_address=mdr_address
self.mdr_port=mdr_port
self.mdb=self.create_multicast_socket(mdb_address, mdb_port)
self.mdr=self.create_multicast_socket(mdr_address, mdr_port)
self.shell_port=shell_port
self.shell=self.create_socket(self.shell_port)
self.db_path = self.home_dir + "/data.db"
self.db_path = self.db_path.decode("latin1")
self.can_send_removed=True
self.reject_putchunks={}
self.pending_deletes={}
self.backup_size = backup_size
self.needs_space_reclaiming()
def needs_space_reclaiming(self):
if (self.backup_size < self.check_directory_size(self.backup_dir)):
self.remove_chunks_higher_replication_degree()
return True
return False
def remove_chunks_higher_replication_degree(self):
data = Data(self.db_path)
chunks = data.get_ordered_chunks_difference_replication_degree()
el = 0
while (self.backup_size < self.check_directory_size(self.backup_dir)):
modification_id = chunks[el][4]
chunk_number = chunks[el][1]
sha256 = data.get_chunk_sha256(modification_id)
data.delete_chunk_removed(chunk_number, sha256)
file_name = self.backup_dir+str(sha256)+"_"+str(chunk_number)+".chunk"
os.remove(file_name)
message = "REMOVED " + VERSION + " " + sha256 + " " + str(chunk_number)
self.shell.sendto(message, ("127.0.0.1", self.shell_port))
el +=1
def init_home_dir(self):
self.backup_dir=self.home_dir+"/"+BACKUP_DIR+"/"
if(not os.path.exists(self.backup_dir)):
os.makedirs(self.backup_dir)
self.temp_dir=self.home_dir+"/"+TEMP_DIR+"/"
if(not os.path.exists(self.temp_dir)):
os.makedirs(self.temp_dir)
self.restore_dir=self.home_dir+"/"+RESTORE_DIR+"/"
if(not os.path.exists(self.restore_dir)):
os.makedirs(self.restore_dir)
if(not os.path.exists(self.home_dir+"/data.db")):
shutil.copy2("data.db", self.home_dir)
def listen_shell(self):
pending_deletes_cron= Thread(target=self.pending_deletes_cron, args=())
pending_deletes_cron.start()
while True:
message,addr = self.shell.recvfrom(MAX_MESSAGE_SIZE)
message=message.strip(' \t\n\r')
print_message("Shell received \"" + message+"\"")
self.handle_shell_request(message,addr)
pending_deletes_cron.join()
def pending_deletes_cron(self):
while(True):
modifications=self.pending_deletes.keys()
print_message("CRON found "+str(len(modifications))+ " modifications pending delete")
for file_id in modifications:
message="DELETE2 "+str(file_id)+CRLF+CRLF
self.mc.sendto(message, (self.mc_address, self.mc_port))
self.pending_deletes[file_id][0]-=1
if(self.pending_deletes[file_id][0]==0):
del self.pending_deletes[file_id]
time.sleep(60)
def listen(self,sock,channel):
while True:
global subscriptions
message,addr = sock.recvfrom(MAX_MESSAGE_SIZE)
if message in subscriptions:
value = subscriptions[message]
subscriptions[message] = int(value)+1
operation=message.split(" ")[0].strip(' \t\n\r')
print_message(channel+" received \"" + operation + "\" from " + str(addr) )
self.handle_request(message,addr)
def listen_all(self):
pid=str(os.getpid())
print_message("Starting EasyBackup Peer Daemon with PID " + pid + " ...")
f=open(self.home_dir+"/pid.txt","w")
f.write(pid)
f.close()
shell = Thread(target=self.listen_shell, args=())
mc = Thread(target=self.listen, args=(self.mc,"MC"))
mdb = Thread(target=self.listen, args=(self.mdb,"MDB"))
mdr = Thread(target=self.listen, args=(self.mdr,"MDR"))
monitor_chunks=Thread(target=self.monitor_chunks, args=())
shell.start()
print_message("Shell listening at UDP port " + str(self.shell_port))
mdb.start()
print_message("MDB listening at Multicast group "+ self.mdb_address + " and port " + str(self.mdb_port))
mc.start()
print_message("MC listening at Multicast group "+ self.mc_address + " and port " + str(self.mc_port))
mdr.start()
print_message("MDR listening at Multicast group "+ self.mdr_address + " and port " + str(self.mdr_port))
monitor_chunks.start()
shell.join()
mdb.join()
mc.join()
mdr.join()
monitor_chunks.join()
def monitor_chunks(self):
monitor=ChunksMonitor(self.backup_dir, self.shell_port,self.db_path)
monitor.start()
monitor.join()
def create_multicast_socket(self,multicast_address,multicast_port):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("", multicast_port))
mreq = struct.pack("4sl", socket.inet_aton(multicast_address), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, TTL)
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, LOOPBACK)
return sock
def create_socket(self,port):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("", port))
return s
def handle_shell_request(self, message,addr):
args=message.split(" ")
operation=args[0]
data = Data(self.db_path)
if(operation=="backup"):
file_path=args[1]
replication_degree=args[2]
if(self.send_chunks(file_path,int(replication_degree))):
self.shell.sendto("ok\n", addr)
else:
self.shell.sendto("fail\n", addr)
elif(operation=="restore"):
file_path=args[1]
modifications=data.get_file_modifications(file_path)
message="found "+ str(len(modifications))
for modification in modifications:
modification_date=modification[2]
modification_date=modification_date[:10]+"T"+modification_date[11:19]
message+= " " + modification_date
self.shell.sendto(message,addr)
elif(operation=="restoremodification"):
file_name = args[1]
option = int(args[2])
modification = data.get_file_modifications(file_name)[option-1]
sha256 = modification[1]
chunks = int(modification[3])
self.restore_file_modification(sha256, chunks)
self.get_file(sha256, file_name,chunks)
elif(operation=="delete"):
file_name=args[1]
self.request_file_deletion(file_name)
elif(operation=="delete2"):
file_name=args[1]
self.request_file_deletion2(file_name)
elif(operation=="REMOVED"):
if(self.can_send_removed):
file_id=message.split(" ")[2]
chunk_number=message.split(" ")[3]
self.reject_putchunks[file_id+chunk_number]=datetime.now() + timedelta(0,60)
message+=CRLF+CRLF
self.mc.sendto(message,(self.mc_address,self.mc_port))
elif(operation=="deletechunk"):
data= Data(self.db_path)
file_id=message.split(" ")[1]
chunk_number=message.split(" ")[2]
filepath=self.backup_dir+file_id+"_"+chunk_number+".chunk"
if (os.path.exists(filepath)):
os.remove(filepath)
data.delete_chunk_removed(chunk_number, file_id)
elif(operation=="deleted"):
file_id=message.split(" ")[1]
if (file_id in self.pending_deletes):
host=addr[0]
try:
self.pending_deletes[file_id][1].remove(host)
if(len(self.pending_deletes[file_id][1])==0):
del self.pending_deletes[file_id]
except:
pass
def handle_request(self, message,addr):
operation=message.split(" ")[0].strip(' \t\n\r')
message=message.strip(' \t\n\r')
if (operation == "PUTCHUNK"):
data = Data(self.db_path)
file_id=message.split(" ")[2]
chunk_number=message.split(" ")[3]
now=datetime.now()
try:
body = message.split(CRLF+CRLF)[1]
except:
print_message("Invalid message from "+str(addr))
if(len(body)<2):
print_message("The PUTCHUNK split failed")
return False
can_store=True
if(data.chunk_owner(file_id)):
can_store=False
if((file_id+chunk_number) in self.reject_putchunks and now<self.reject_putchunks[file_id+chunk_number]):
can_store=False
if ((self.backup_size - self.check_directory_size(self.backup_dir)) <= len(body)):
can_store=False
if(can_store):
self.backup_chunk(message)
else:
print_message("PUTCHUNK rejected")
elif(operation == "GETCHUNK"):
self.create_restore_subscription(message)
self.get_and_send_chunk(message)
elif(operation == "CHUNK"):
print message.split(CRLF+CRLF)[0]
self.save_chunk_to_restore(message)
elif(operation=="DELETE"):
self.can_send_removed=False
self.delete_chunks(message)
time.sleep(1)
self.can_send_removed=True
elif(operation=="DELETE2"):
self.can_send_removed=False
self.delete_chunks(message)
file_id=message.split(" ")[1].strip(CRLF+CRLF)
self.mc.sendto("deleted "+str(file_id),(addr[0],SHELL_PORT))
time.sleep(1)
self.can_send_removed=True
elif(operation=="STORED"):
if (not self.check_bigger_replication_degree(message, addr)):
self.increment_chunk_replication_degree(message,addr)
elif(operation=="REMOVED"):
self.update_chunk_replication_degree(message,addr)
def create_restore_subscription(self, getchunk_message):
args = getchunk_message.split(" ")
file_id = args[2]
chunk_no = args[3]
message_expected = "CHUNK " + VERSION + " " + file_id + " " + chunk_no
restored[message_expected] = False
def check_bigger_replication_degree(self, message, addr):
data = Data(self.db_path)
file_id = message.split(" ")[2]
chunk_number = message.split(" ")[3]
replication_degree = data.get_chunk_replication_degree(file_id, chunk_number)
minimum_replication_degree = data.get_chunk_minimum_replication_degree(file_id, chunk_number)
if (data.chunk_owner(file_id) and replication_degree >= minimum_replication_degree and not data.chunk_have_replication_host(file_id, chunk_number, addr[0])):
print_message("Sending a chunk delete to "+str(addr[0])+" because the replication degree was already enough")
message="deletechunk " + file_id + " "+ chunk_number
self.shell.sendto(message, (addr[0], SHELL_PORT))
return True
return False
def update_chunk_replication_degree(self,message,addr):
file_id=message.split(" ")[2]
chunk_number=message.split(" ")[3].strip(CRLF+CRLF)
host=addr[0]
data = Data(self.db_path)
if(data.get_chunk_id(chunk_number, file_id)):
data.remove_chunk_replication_degree(file_id, chunk_number, host)
# check replication degree
replication_degree=data.get_chunk_replication_degree(file_id, chunk_number)
minimum_replication_degree=data.get_chunk_minimum_replication_degree(file_id , chunk_number)
print_message("Chunk replication degree is "+str(replication_degree) + " and minimum is "+str(minimum_replication_degree))
if(replication_degree<minimum_replication_degree):
print_message("Chunk replication degree is not enough. Sending a PUTCHUNK")
self.put_chunk(file_id,chunk_number,replication_degree)
def increment_chunk_replication_degree(self,message,addr):
ip=addr[0]
data = Data(self.db_path)
file_id=message.split(" ")[2]
chunk_number=message.split(" ")[3]
print "file_id: *"+file_id+"*"
print "chunk_number: *"+chunk_number+"*"
if(data.increment_replication_degree(file_id, chunk_number,ip)):
print "replication degree: " + str(data.get_chunk_replication_degree(file_id, chunk_number))
def create_chunk_data(self,message):
data = Data(self.db_path)
file_id=message.split(" ")[2]
chunk_number=message.split(" ")[3]
minimum_replication_degree=message.split(" ")[4].split(CRLF+CRLF)[0]
data.add_only_modification(file_id)
data.add_chunk(file_id, chunk_number, minimum_replication_degree)
data.increment_replication_degree(file_id, chunk_number,"localhost")
def backup_chunk(self, message):
message_list=message.split(" ")
file_id=message_list[2]
chunk_no=message_list[3]
chunk = message.split(CRLF+CRLF)[1]
chunk_file = open(self.backup_dir+file_id+"_"+chunk_no+".chunk", "wb")
chunk_file.write(chunk)
chunk_file.close()
self.create_chunk_data(message)
delay=random.randint(0,400)
d = delay/1000.0
time.sleep(d)
message="STORED " + VERSION + " " + file_id + " " + chunk_no + CRLF + CRLF
self.mc.sendto(message, (self.mc_address, self.mc_port))
def get_file(self, sha256, file_name,chunks):
f = File(file_name,sha256)
while(not f.restore_file(self.temp_dir, self.restore_dir,chunks)):
pass
self.remove_chunks_from_directory(f.get_file_id(), self.temp_dir)
return True
def request_file_deletion(self, file_name):
data = Data(self.db_path)
modifications=data.get_file_modifications(file_name)
for modification in modifications:
file_id=modification[1]
message="DELETE "+str(file_id)+CRLF+CRLF
self.mc.sendto(message, (self.mc_address, self.mc_port))
def request_file_deletion2(self, file_name):
data = Data(self.db_path)
modifications=data.get_file_modifications(file_name)
for modification in modifications:
file_id=modification[1]
message="DELETE2 "+str(file_id)+CRLF+CRLF
self.mc.sendto(message, (self.mc_address, self.mc_port))
chunks=modification[3]
hosts_set=set([])
for chunk in range(chunks):
hosts=data.get_chunk_hosts(file_id, chunk)
hosts=filter(lambda a:a!="localhost",hosts)
for host in hosts:
host=host[0]
hosts_set.add(host)
self.pending_deletes[str(file_id)]=[MAX_DELETE_WAITING_TIME,hosts_set]
def delete_chunks(self,message):
file_id=message.split(" ")[1].strip(CRLF+CRLF)
self.remove_chunks_from_directory(file_id, self.backup_dir)
def restore_file_modification(self, file_id, chunks):
total_chunks=0
for i in range(chunks):
message = "GETCHUNK " + VERSION + " " + file_id + " " + str(i) + CRLF + CRLF
message_expected = "CHUNK " + VERSION + " " + file_id + " " + str(i)
restored[message_expected] = False
chunk_received = False
attempts=0
timeout = TIMEOUT
while (not chunk_received and attempts < MAX_ATTEMPTS):
self.mc.sendto(message, (self.mc_address, self.mc_port))
if (self.check_received_chunk(message_expected, timeout)):
chunk_received = True
total_chunks+=1
else:
timeout*=2
attempts+=1
return total_chunks == chunks
def check_received_chunk(self, message_expected, timeout):
global stop_restore_waiting
chunk_received=False
timeout_check = Timer(timeout, self.restore_waiting)
timeout_check.start()
while (not chunk_received and not stop_restore_waiting):
chunk_received = restored[message_expected]
timeout_check.cancel()
if (not chunk_received):
print "Timeout while getting chunk."
else:
del restored[message_expected]
return chunk_received
def restore_waiting(self):
global stop_restore_waiting
stop_restore_waiting = True
def save_chunk_to_restore(self, message):
original_message = message
check_message = message.split(CRLF+CRLF)[0]
if (check_message in restored):
restored[check_message] = True
message = message.split(" ")
file_id = message[2]
splited = original_message.split(CRLF+CRLF)
chunk_no = splited[0].split(" ")[3]
body = splited[1]
chunk = open(self.temp_dir+file_id+"_"+chunk_no+".chunk", "wb")
chunk.write(body)
chunk.close()
def put_chunk(self,file_id,chunk_number,replication_degree):
full_path = self.backup_dir+file_id+"_"+chunk_number+".chunk"
if (os.path.exists(full_path)):
chunk = open(full_path, "rb")
chunk_content = chunk.read()
chunk.close()
message="PUTCHUNK " + VERSION + " " + str(file_id) + " " + str(chunk_number)+" "+str(replication_degree) + CRLF + CRLF + chunk_content
delay=random.randint(0,400)/1000.0
time.sleep(delay)
self.mdb.sendto(message, (self.mdb_address, self.mdb_port))
def get_and_send_chunk(self, message):
message_list=message.split(" ")
file_id=message_list[2]
chunk_no=message_list[3]
full_path = self.backup_dir+file_id+"_"+chunk_no+".chunk"
if (os.path.exists(full_path)):
chunk = open(full_path, "rb")
chunk_content = chunk.read()
chunk.close()
message_expected = "CHUNK " + VERSION + " " + file_id + " " + chunk_no
delay=random.randint(0,400)/1000.0
global restore_waiting_improved_protocol
restore_waiting_improved_protocol=True
timeout_restore = Timer(delay, self.quit_waiting_chunk)
timeout_restore.start()
chunk_already_sent=False
while (not chunk_already_sent and restore_waiting_improved_protocol):
if (message_expected in restored):
chunk_already_sent = restored[message_expected]
if (not chunk_already_sent):
message="CHUNK " + VERSION + " " + file_id + " " + chunk_no + CRLF + CRLF + chunk_content
self.mdr.sendto(message, (self.mdr_address, self.mdr_port))
timeout_restore.cancel()
else:
print "Chunk received before!"
timeout_restore.cancel()
def quit_waiting_chunk(self):
global restore_waiting_improved_protocol
restore_waiting_improved_protocol=False
def send_chunks(self, path,replication_degree):
f = File(path)
data = Data(self.db_path)
if (not data.get_file_id(path)):
data.add_file(path)
chunks_number = f.generate_chunks(self.temp_dir)
file_id=f.generate_file_id()
date = f.get_modification_date()
data.add_modification(path, file_id, chunks_number, date)
chunks=f.fetch_chunks(self.temp_dir)
acks_chunks=0
for j in range(len(chunks)):
chunk_file = open(self.temp_dir +chunks[j], "rb")
body=chunk_file.read()
chunk_file.close()
replication_degree=str(replication_degree)
chunk_no=str(j)
message="PUTCHUNK " + VERSION + " " + file_id + " " + chunk_no + " " + replication_degree + CRLF + CRLF + body
acks=False
attempts=0
timeout=TIMEOUT
while(not acks and attempts<MAX_ATTEMPTS):
self.mdb.sendto(message, (self.mdb_address, self.mdb_port))
data.add_chunk(file_id, chunk_no, replication_degree)
if(self.check_replication_degree(file_id,chunk_no,replication_degree,timeout)):
acks=True
timeout*=2
attempts+=1
if(acks): acks_chunks+=1
self.remove_chunks_from_directory(f.get_name(),self.temp_dir)
return (acks_chunks>=chunks_number)
def check_replication_degree(self, file_id, chunk_no, replication_degree,timeout):
replication_degree=int(replication_degree)
acks=0
global waiting
waiting=False
timeout_check = Timer(timeout, self.quit_waiting)
timeout_check.start()
message_expected="STORED " + VERSION + " " + file_id + " " + chunk_no + CRLF + CRLF
global subscriptions
subscriptions[message_expected] = 0
while (acks < replication_degree and not waiting):
acks = subscriptions[message_expected]
if(waiting and acks < replication_degree):
print_message("Timeout getting the desired replication degree")
timeout_check.cancel()
return acks==replication_degree
def quit_waiting(self):
global waiting
waiting = True
def remove_chunks_from_directory(self,file_id,directory):
file_id = file_id.split(".")[0]
list_dir = os.listdir(directory)
for file_name in list_dir:
match = re.search(file_id, file_name)
if (match):
os.remove(directory+file_name)
# Formato retornado em Bytes
def check_directory_size(self, directory):
TotalSize = 0
for item in os.walk(directory):
for file_name in item[2]:
try:
TotalSize = TotalSize + os.path.getsize(os.path.join(item[0], file_name))
except:
print("error with file: " + os.path.join(item[0], file_name))
return TotalSize