-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
291 lines (261 loc) · 11.1 KB
/
main.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
import json
import sys
import os
import multiprocessing
import namenode
import threading
import time
from subprocess import call
import argparse
parser=argparse.ArgumentParser()
parser.add_argument('--mapper')
parser.add_argument('--reducer')
parser.add_argument('--input')
parser.add_argument('--config')
parser.add_argument('--m')
parser.add_argument('--r')
parser.add_argument('--i')
parser.add_argument('--c')
parser.add_argument('--output')
parser.add_argument('--o')
args=parser.parse_args()
#TODO - initialize the datanodes so they can run independently
class IPC_Pathways():
def __init__(self, config):
self.config = config
self.mainReceiverLoop = True
self.tmpfileLock = multiprocessing.Lock()
self.mQueue = multiprocessing.SimpleQueue()
self.mLock = multiprocessing.Lock()
self.pnnQueue = multiprocessing.SimpleQueue()
self.pnnLock = multiprocessing.Lock()
self.snnQueue = multiprocessing.SimpleQueue()
self.snnLock = multiprocessing.Lock()
self.PNN = multiprocessing.Process(target=namenode.primary_namenode_thread, args=(self.mQueue, self.mLock, self.pnnQueue, self.pnnLock, self.snnQueue, self.snnLock, self.config, self.tmpfileLock))
self.PNN.start()
self.sendMsg(self.pnnQueue, self.pnnLock, [100, None])
self.SNN = multiprocessing.Process(target=namenode.secondary_namenode_thread, args=(self.mQueue, self.mLock, self.pnnQueue, self.pnnLock, self.snnQueue, self.snnLock, self.config))
self.SNN.start()
self.sendMsg(self.snnQueue, self.snnLock, [200, None])
self.mainReceiver = threading.Thread(target = self.reciever)
self.mainReceiver.start()
self.PNNReady = False
self.SNNReady = False
while(self.PNNReady == False and self.SNNReady == False):
continue
def reciever(self):
while(self.mainReceiverLoop):
self.receiveMsg()
def sendMsg(self, queue, lock, data):
# print("Main sent", data[0])
lock.acquire(block = True)
queue.put(data)
lock.release()
def receiveMsg(self):
self.mLock.acquire(block = True)
message = [1, None]
if(not self.mQueue.empty()):
message = self.mQueue.get()
# print("Main rcvd", message[0])
self.mLock.release()
if(message[0] == 0):
return 0
if(message[0] == 100):
print("Primary namenode started")
return 100
elif(message[0] == 101):
print("Primary namenode cannot access Namenode configuration. Use the 'format' command to format the namenode.")
return 101
elif(message[0] == 102):
print("Primary namenode ready to Sync with Secondary namenode")
self.PNNReady = True
return 102
elif(message[0] == 103):
return 103
elif(message[0] == 200):
print("Secondary namenode started")
return 200
elif(message[0] == 202):
print("Secondary namenode ready to Sync with Primary namenode")
self.SNNReady = True
return 202
elif(message[0] == 204):
self.PNNReady = False
self.SNNReady = False
self.sendMsg(self.pnnQueue, self.pnnLock, [100, None])
self.SNN = multiprocessing.Process(target=namenode.secondary_namenode_thread, args=(self.mQueue, self.mLock, self.pnnQueue, self.pnnLock, self.snnQueue, self.snnLock, self.config))
self.SNN.start()
else:
return 1
def formatNamenode(self):
self.sendMsg(self.pnnQueue, self.pnnLock, [101, None])
def mkdir(self, path):
self.sendMsg(self.pnnQueue, self.pnnLock, [104, path])
def mkdir_parent(self, path):
self.sendMsg(self.pnnQueue, self.pnnLock, [105, path])
def rmdir(self, path):
self.sendMsg(self.pnnQueue, self.pnnLock, [106, path])
def ls(self):
self.sendMsg(self.pnnQueue, self.pnnLock, [107, None])
def put(self, src, dest):
self.sendMsg(self.pnnQueue, self.pnnLock, [108, src, dest])
def cat(self, path, pr = True):
self.sendMsg(self.pnnQueue, self.pnnLock, [109, path, pr])
def rm(self,filepath):
self.sendMsg(self.pnnQueue,self.pnnLock,[110,filepath])
def stopAllNodes(self):
print("Stopping Secondary namenode")
self.sendMsg(self.snnQueue, self.snnLock, [0, None])
self.SNN.join()
print("Stopping Primary namenode")
self.sendMsg(self.pnnQueue, self.pnnLock, [0, None])
self.PNN.join()
self.mainReceiverLoop = False
self.mainReceiver.join()
def mapred(self,file,mapper,reducer,output,f):
try:
tmpfile_path = os.path.join(self.config['path_to_namenodes'], 'tmpfile')
try:
os.remove(tmpfile_path)
except:
pass
self.cat(file, False)
time.sleep(5)
self.tmpfileLock.acquire()
with open(tmpfile_path, 'r') as i:
with open(os.path.join(self.config['path_to_namenodes'], 'mapper'),'w') as o:
call('python3 {}'.format(mapper).split(),stdin=i,stdout=o)
with open(os.path.join(self.config['path_to_namenodes'], 'mapper'), 'r') as i:
with open(os.path.join(self.config['path_to_namenodes'], 'sorted'),'w') as o:
call('sort -k 1,1'.split(),stdin=i,stdout=o)
with open(os.path.join(self.config['path_to_namenodes'], 'sorted'), 'r') as i:
with open(os.path.join(self.config['path_to_namenodes'], os.path.basename(output)),'w') as o:
call('python3 {}'.format(reducer).split(),stdin=i,stdout=o)
self.put(os.path.join(self.config['path_to_namenodes'], os.path.basename(output)), os.path.dirname(output))
self.tmpfileLock.release()
print("MR Function completed")
if(f):
self.stopAllNodes()
exit()
except Exception as e:
print("An Error Occured: ", e)
def create_dfs():
config_path = input("Enter path for configuration file: ")
try:
config_file = open(os.path.expandvars(config_path))
config = json.load(config_file)
config_file.close()
except FileNotFoundError:
print("Path does not exist")
exit(-1)
except json.decoder.JSONDecodeError:
print("Configuration file not formatted correctly")
exit(-1)
try:
config['path_to_datanodes'] = os.path.expandvars(config['path_to_datanodes'])
config['path_to_namenodes'] = os.path.expandvars(config['path_to_namenodes'])
config['datanode_log_path'] = os.path.expandvars(config['datanode_log_path'])
config['namenode_log_path'] = os.path.expandvars(config['namenode_log_path'])
config['dfs_setup_config'] = os.path.expandvars(config['dfs_setup_config'])
config['dfs_setup_config'] = os.path.expandvars(config['dfs_setup_config'])
config['fs_path'] = os.path.expandvars(config['fs_path'])
config['namenode_checkpoints'] = os.path.expandvars(config['namenode_checkpoints'])
os.makedirs(os.path.expandvars(config['path_to_datanodes']), exist_ok=True)
os.makedirs(os.path.expandvars(config['path_to_namenodes']), exist_ok=True)
os.makedirs(os.path.expandvars(config['datanode_log_path']), exist_ok=True)
os.makedirs(os.path.expandvars(os.path.dirname(config['namenode_log_path'])), exist_ok=True)
os.makedirs(os.path.expandvars(config['namenode_checkpoints']), exist_ok=True)
os.makedirs(os.path.expandvars(config['fs_path']), exist_ok=True)
os.makedirs(os.path.expandvars(os.path.dirname(config['dfs_setup_config'])), exist_ok=True)
setup_config_path = os.path.expandvars(config['dfs_setup_config'])
setup_config_file = open(setup_config_path , 'w')
json.dump(config, setup_config_file)
setup_config_file.close()
cache_file = open('./cache_file', 'w')
print(setup_config_path, file=cache_file)
cache_file.close()
except Exception as e:
print("Error encountered in configuration file:", e)
exit(-1)
exit(0)
def cli(ipc):
while True:
cmd = input(">")
if cmd.strip().lower() == 'exit':
print("Exiting ...")
ipc.stopAllNodes()
exit(0)
if cmd.strip().lower() == 'format':
ipc.formatNamenode()
command = cmd.split()
try:
if command[0].strip() == 'put':
ipc.put(command[1], command[2])
elif command[0].strip() == 'mkdir':
if(command[1].strip() == '-p'):
ipc.mkdir_parent(command[2].strip())
else:
ipc.mkdir(command[1].strip())
elif command[0].strip() == 'rmdir':
ipc.rmdir(command[1].strip())
elif command[0].strip() == 'ls':
ipc.ls()
elif command[0].strip() == 'cat':
ipc.cat(command[1])
elif command[0].strip()=='mr':
ipc.mapred(command[1],command[2],command[3],command[4],0)
elif command[0].strip()=='rm':
ipc.rm(command[1])
except IndexError:
print('Syntax error')
continue
def main_loop(config):
ipc = IPC_Pathways(config)
cli(ipc)
if __name__ == "__main__":
try:
if(args.config is None and args.c is None):
raise IndexError
dfs_setup_config_path = args.config or args.c
dfs_setup_config_file = open(dfs_setup_config_path, 'r')
dfs_setup_config = json.load(dfs_setup_config_file)
dfs_setup_config_file.close()
cache_file = open('./cache_file', 'w')
print(args.config or args.c, file=cache_file)
except IndexError:
try:
cache_file = open('./cache_file', 'r')
dfs_setup_config_path = os.path.expandvars(cache_file.readline().strip())
dfs_setup_config_file = open(dfs_setup_config_path, 'r')
dfs_setup_config = json.load(dfs_setup_config_file)
dfs_setup_config_file.close()
cache_file.close()
except:
r = input('Would you like to create a new DFS? [y/n]: ')
if r.lower() == 'y':
create_dfs()
else:
exit(0)
except FileNotFoundError:
r = input('The specified DFS does not exist. Would you like to create a new one? [y/n]: ')
if r.lower() == 'y':
create_dfs()
else:
exit(0)
except Exception as e:
print("Unhandled exception occurred:", e)
exit(-1)
if(args.mapper is not None or args.m is not None):
ipc = IPC_Pathways(dfs_setup_config)
m=args.mapper or args.m
r=args.reducer or args.r
i=args.input or args.i
o=args.output or args.o
ipc.mapred(i,m,r,o,1)
else:
print("<---DFS DETAILS--->")
for i in dfs_setup_config:
print(i, dfs_setup_config[i], sep=':')
print("<---DFS COMMAND LINE--->")
main_loop(dfs_setup_config)
exit(0)