-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaster.py
More file actions
274 lines (224 loc) · 9.39 KB
/
master.py
File metadata and controls
274 lines (224 loc) · 9.39 KB
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
from multiprocessing import Process
import os
from multiprocessing import Queue
import json
from mapper import Mapper
from reducer import Reducer
from keyValueClient import KeyValueClient
from utility import WorkerStatus, getMapperStatusKey, getMapperFileOutputKey, getMapperCountOutputKey, getReducerStatusKey, getReducerFileOutputKey, getReducerCountOutputKey, getFileName
class Master(Process):
def __init__(self, nMappers, nReducers, filePaths, fileMaxSize, outputCountFile, outputInvertedIndexFile):
super(Master, self).__init__()
self.nMappers = nMappers
self.nReducers = nReducers
self.filePaths = filePaths
self.fileMaxSize = fileMaxSize
self.kvCountData = ""
self.kvFileData = ""
self.reducers = []
self.idleReducers = 0
self.availableReducers = set()
self.reducerJobs = {}
self.reducerCountOutputKeys = set()
self.reducerFileOutputKeys = set()
self.processReducers = []
self.reProcessReducers = []
self.mappers = []
self.idleMappers = 0
self.availableMapperQueue = Queue()
self.availableMappers = set()
self.mapperJobs = {}
self.mapperCountOutputKeys = set()
self.mapperFileOutputKeys = set()
self.reProcessFiles = []
self.keyValueClient = KeyValueClient()
self.fileDirectory = "assets"
self.outputCountFile = outputCountFile
self.outputInvertedIndexFile = outputInvertedIndexFile
pass
#preprocessing
def preprocessing(self):
self.cleanKeyValue()
self.splitFiles()
self.initializeMappers()
self.assignFilePartitionsToMapper()
while(self.idleMappers != self.nMappers):
self.checkForMappersStatus()
self.initializeReducers()
self.runReducers()
while(self.idleReducers != self.nReducers):
self.checkForReducerStatus()
self.processOutput()
'''
split files into equal size chunks, and output to a local folder
Alternative, TODO:: to store start and end pointer for each chunk,
to reduce overhead of making new files for each chunk
'''
def splitFiles(self):
maxSize = self.fileMaxSize
target = self.fileDirectory
filePaths = []
if not os.path.exists(target):
os.makedirs(target)
for inputFile in self.filePaths:
fileName = getFileName(inputFile)
with open(inputFile, 'rb') as file:
startPointer = 0
chunkNumber = 0
while True:
file.seek(startPointer)
data = file.read(maxSize)
if not data:
break
lastSpace = data.rfind(b' ')
if lastSpace != -1:
newStart = file.tell()
while(1):
file.seek(newStart)
d = file.read(maxSize)
if not d:
break
spaceIndex = d.find(b" ")
if spaceIndex != -1:
data = data+d[:spaceIndex+1]
newStart = newStart + spaceIndex+1
break
else:
data = data + d
newStart = file.tell()
startPointer = newStart
else:
startPointer = file.tell()
newFile = os.path.join(target, f'{chunkNumber}-{fileName}')
with open(newFile, 'wb') as newF:
newF.write(data)
filePaths.append(newFile)
chunkNumber = chunkNumber + 1
self.filePaths = filePaths
def cleanKeyValue(self):
self.keyValueClient.delete()
pass
def initializeMappers(self):
for i in range(self.nMappers):
self.availableMapperQueue.put(i)
self.availableMappers.add(i)
key = getMapperStatusKey(i)
value = WorkerStatus.IDLE.value
self.keyValueClient.setKey(key, value)
self.idleMappers = self.nMappers
def createMapper(self, id, file):
countOutputKey = getMapperCountOutputKey(id)
fileOutputKey = getMapperFileOutputKey(id)
self.mapperCountOutputKeys.add(countOutputKey)
self.mapperFileOutputKeys.add(fileOutputKey)
return Mapper(id,
file,
getMapperStatusKey(id),
countOutputKey,
fileOutputKey)
def assignFilePartitionsToMapper(self):
while(1):
i = 0
totalFiles = len(self.filePaths)
if not totalFiles:
break
while(i<totalFiles):
file = self.filePaths[i]
if self.idleMappers:
self.idleMappers = self.idleMappers - 1
mapperId = self.availableMapperQueue.get()
mapper = self.createMapper(mapperId,file)
self.availableMappers.remove(mapperId)
mapper.start()
self.mapperJobs[mapperId] = file
i = i+1
else:
while(not self.idleMappers):
self.checkForMappersStatus()
self.filePaths = self.reProcessFiles
self.reProcessFiles = []
def checkForMappersStatus(self):
for i in range(self.nMappers):
kv = self.keyValueClient.getKey(getMapperStatusKey(i))
#if mapper is idle, add it to queue for taking another job into consideration
if kv and kv == WorkerStatus.IDLE.value and i not in self.availableMappers:
self.availableMapperQueue.put(i)
self.availableMappers.add(i)
self.idleMappers = self.idleMappers + 1
#if mapper is failed, put it idle and puts its request to re-processing. Also sets its status to idle in key-value
elif kv and kv == WorkerStatus.FAILED.value:
print(f"Mapper {i} is faulty")
self.availableMapperQueue.put(i)
self.availableMappers.add(i)
self.idleMappers = self.idleMappers + 1
self.reProcessFiles.append(self.mapperJobs[i])
del self.mapperJobs[i]
key = getMapperStatusKey(i)
self.keyValueClient.setKey(key, WorkerStatus.IDLE.value)
def initializeReducers(self):
for i in range(self.nReducers):
self.processReducers.append(i)
self.availableReducers.add(i)
self.idleReducers = self.nReducers
pass
def runReducers(self):
while(1):
length = len(self.processReducers)
if not length:
break
for i in range(length):
reducer = self.createReducer(self.processReducers[i])
self.idleReducers = self.idleReducers - 1
self.availableReducers.remove(i)
reducer.start()
reducer.join()
self.checkForReducerStatus()
self.processReducers = self.reProcessReducers
self.reProcessReducers = []
def createReducer(self, id):
countOutputKey = getReducerCountOutputKey(id)
fileOutputKey = getReducerFileOutputKey(id)
reducerStatusKey = getReducerStatusKey(id)
self.reducerCountOutputKeys.add(countOutputKey)
self.reducerFileOutputKeys.add(fileOutputKey)
self.keyValueClient.setKey(reducerStatusKey, WorkerStatus.IDLE.value)
return Reducer(id,
self.nReducers,
reducerStatusKey,
countOutputKey,
fileOutputKey,
self.mapperFileOutputKeys,
self.mapperCountOutputKeys)
def checkForReducerStatus(self):
for i in range(self.nReducers):
kv = self.keyValueClient.getKey(getReducerStatusKey(i))
#if reducer is idle
if kv and kv == WorkerStatus.IDLE.value and i not in self.availableReducers:
self.availableReducers.add(i)
self.idleReducers = self.idleReducers + 1
#if reducer is failed, process it again
elif kv and kv == WorkerStatus.FAILED.value:
print(f"Reducer {i} is faulty")
self.availableReducers.add(i)
self.idleReducers = self.idleReducers + 1
self.reProcessReducers.append(i)
def processOutput(self):
for key in self.reducerCountOutputKeys:
value = self.keyValueClient.getKey(key)
value = json.loads(value)
for k in value:
self.kvCountData = f"{self.kvCountData} {k} {value[k]}\n"
for key in self.reducerFileOutputKeys:
value = self.keyValueClient.getKey(key)
value = json.loads(value)
for k in value:
self.kvFileData = f"{self.kvFileData} {k} {value[k]}\n"
with open(self.outputCountFile, 'w', encoding='utf-8') as filename:
filename.write(self.kvCountData)
filename.close()
with open(self.outputInvertedIndexFile, 'w', encoding='utf-8') as filename:
filename.write(self.kvFileData)
filename.close()
pass
def run(self):
self.preprocessing()