-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMaster_Server.py
117 lines (89 loc) · 3.53 KB
/
Master_Server.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
import rpyc
import uuid
import math
import random
import ConfigParser
import signal
import sys
import json
from rpyc.utils.server import ThreadedServer
def int_handler(signal, frame):
content = MasterService.exposed_Master.file_table
try:
con = rpyc.connect("127.0.0.1", port=8100)
back_up_server = con.root.BackUpServer()
file_table_string = json.dumps(content)
back_up_server.updateFileTable(file_table_string)
con.close()
except:
print "\n -----Info: Primary backup Server not found !!! ------- "
print " ----- Master Server memory lost ------- \n "
sys.exit(0)
def set_conf():
conf = ConfigParser.ConfigParser()
conf.readfp(open('GFS.conf'))
MasterService.exposed_Master.block_size = int(conf.get('master', 'block_size'))
minions = conf.get('master', 'chunkServers').split(',')
for m in minions:
id, host, port = m.split(":")
MasterService.exposed_Master.minions[id] = (host, port)
try:
con = rpyc.connect("127.0.0.1", port=8100)
print " ----- Connected to Primary back-up Server ------"
back_up_server = con.root.BackUpServer()
file_table_backup = back_up_server.getFileTable()
MasterService.exposed_Master.file_table = json.loads(file_table_backup)
con.close()
except:
print "\n -----Info: Primary backup Server not found !!! ------- "
print " -----Start the primary_backup_server ------- \n \n "
class MasterService(rpyc.Service):
class exposed_Master():
file_table = {}
minions = {}
block_size = 0
def exposed_read(self, fname):
mapping = self.__class__.file_table[fname]
return mapping
def exposed_write(self, dest, size):
if self.exists(dest):
pass
self.__class__.file_table[dest] = []
num_blocks = self.calc_num_blocks(size)
blocks = self.alloc_blocks(dest, num_blocks)
return blocks
def exposed_delete(self, fname):
if not self.exists(fname): return False
mapping_to_be_deleted = self.__class__.file_table.pop(fname, None)
return mapping_to_be_deleted
def exposed_get_file_table_entry(self, fname):
if fname in self.__class__.file_table:
return self.__class__.file_table[fname]
else:
return None
def exposed_get_list_of_files(self):
return self.__class__.file_table.keys()
def exposed_get_block_size(self):
return self.__class__.block_size
def exposed_get_minions(self):
return self.__class__.minions
def calc_num_blocks(self, size):
return int(math.ceil(float(size) / self.__class__.block_size))
def exists(self, file):
return file in self.__class__.file_table
def alloc_blocks(self, dest, num):
blocks = []
for i in range(0, num):
block_uuid = uuid.uuid1()
block_uuid = str(block_uuid)
nodes_id = random.choice(self.__class__.minions.keys())
blocks.append((block_uuid, nodes_id))
# append block_id , Chunk_server_id, index_of_block
self.__class__.file_table[dest].append((block_uuid, nodes_id, i))
return blocks
if __name__ == "__main__":
set_conf()
signal.signal(signal.SIGINT, int_handler)
print "Master Server running"
t = ThreadedServer(MasterService, port=2131)
t.start()