forked from ziyuang/mincemeatpy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdistmm.py
354 lines (292 loc) · 11 KB
/
distmm.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
import mincemeat
from mincemeat import Protocol
import socket
import time
import sys
import logging
import logging.handlers
import multiprocessing
from multiprocessing import Pool, Process
import optparse
import collections
import fileiter
import pickle
from mincemeatpy.registry import Registry
import re
import string
MINIMUM_CLIENT_SLEEP_SECONDS = 1
DEFAULT_HOSTNAME = 'localhost'
DEFAULT_PASSWORD = 'changeme'
VERSION = '0.0.1'
DEFAULT_PORT = mincemeat.DEFAULT_PORT
READ_STEP = 500
DELIMITER = ' '
class Client(mincemeat.Client):
def __init__(self, id=None):
mincemeat.Client.__init__(self)
self.key = ''
self.input_file = ''
self.command = b''
if id is not None:
self.id = id
def validate(self, command, data):
task_id, input_file = data
self.key = ''
self.command = command
self.input_file = input_file
if command == b'map':
logging.info("Validate map task")
self.key = Registry.get_instance().generate_key(self.mapfn, input_file)
else:
logging.info("Validate reduce task for %s" % input_file[0])
# self.key = ''
self.key = Registry.get_instance().generate_key_from_files(self.reducefn, input_file)
self.send_command(b'keyurl', (task_id, (self.key, None)))
def start_map(self, command, data):
logging.info("Mapping %s at client %s" % (str(data[0]), self.id))
file = dict(enumerate(fileiter.read(data[1], READ_STEP)))
results = {}
''' running map func on assigned split '''
for key, lines in file.items():
self.call_mapfn(results, (key, lines))
output_file = "%s_map_output" % data[0]
pickle.dump(results, open(output_file, 'wb'))
logging.info("generate map results at %s" % output_file)
self.send_command(b'mapdone', (data[0], (self.key, [output_file])))
def call_mapfn(self, results, data):
for k, v in self.mapfn(data[0], data[1]):
if k not in results:
results[k] = []
results[k].append(v)
if self.collectfn:
for k in results:
results[k] = [self.collectfn(k, results[k])]
''' TODO: add partition function '''
def start_reduce(self, command, data):
logging.info("Reducing %s at client %s" % (str(data[0]), self.id))
input_files = data[1]
results = {}
for file in input_files:
input_file = pickle.load(open(file, 'rb'))
for k, v in input_file.items():
if k not in results:
results[k] = v
results[k].extend(v)
output_file = "%s_reduce_output" % data[0]
file = open(output_file, 'w')
for k, v in results.items():
file.write("%s, %s\n" % (k, str(self.call_reducefn((k, v)))))
file.close()
self.send_command(b'reducedone', (data[0], (self.key, output_file)))
def call_reducefn(self, data):
return self.reducefn(data[0], data[1])
def start_task(self, command, data):
task_id, url = data
if url is None:
commands = {
b'map': self.start_map,
b'reduce': self.start_reduce
}
commands[self.command](self.command, (task_id, self.input_file))
else:
commands = {
b'map': b'mapdone',
b'reduce': b'reducedone'
}
if self.command == b'map':
url = [url]
self.send_command(commands[self.command], (task_id, (self.key, url)))
def process_command(self, command, data=None):
commands = {
b'mapfn': self.set_mapfn,
b'collectfn': self.set_collectfn,
b'reducefn': self.set_reducefn,
b'map': self.validate,
b'reduce': self.validate,
b'url': self.start_task
}
if command in commands:
commands[command](command, data)
else:
Protocol.process_command(self, command, data)
def run(self, options):
client_sleep_seconds = None
if options.client_sleep_seconds is not None:
client_sleep_seconds = float(options.client_sleep_seconds)
while True:
try:
if type(options.password) == str:
options.password = bytes(options.password, "utf-8")
self.password = options.password
self.conn(options.hostname, options.port)
break
except socket.error:
exc_info = sys.exc_info()
logging.debug("%s:{hostname=%s, port=%s}:%s",
exc_info[0],
options.hostname,
options.port,
exc_info[1])
if client_sleep_seconds is None:
time.sleep(MINIMUM_CLIENT_SLEEP_SECONDS)
break
else:
time.sleep(client_sleep_seconds)
print('socket error')
self.__init__()
except KeyboardInterrupt:
break
except:
exc_info = sys.exc_info()
logging.exception("%s:%s", exc_info[0], exc_info[1])
break
def run_client(queue=None, options=None):
h = logging.handlers.QueueHandler(queue)
root = logging.getLogger()
root.addHandler(h)
if options.verbose:
root.setLevel(logging.INFO)
if options.loud:
root.setLevel(logging.DEBUG)
if options.quiet:
root.setLevel(logging.FATAL)
while True:
try:
client = Client(0)
client.run(options)
except KeyboardInterrupt:
break
except:
exc_info = sys.exc_info()
logging.exception("%s:%s", exc_info[0], exc_info[1])
break
finally:
print('end client')
if not options.run_forever:
break
def client_options_parser():
parser = optparse.OptionParser(usage='%prog [options]', version='%%prog %s' % VERSION)
parser.add_option('-p', '--password', dest='password', default=DEFAULT_PASSWORD, help='password')
parser.add_option('-H', '--hostname', dest='hostname', default=DEFAULT_HOSTNAME, help='hostname')
parser.add_option('-P', '--port', dest='port', type='int', default=DEFAULT_PORT, help='port')
parser.add_option('-v', '--verbose', dest='verbose', action='store_true')
parser.add_option('-V', '--loud', dest='loud', action='store_true')
parser.add_option('-q', '--quiet', dest='quiet', action='store_true')
parser.add_option('-n', '--number_of_clients', dest='number_of_clients', default='1',
help='number of client processes')
parser.add_option('-s', '--sleep', dest='client_sleep_seconds', default=None, help='client sleep seconds')
parser.add_option('-t', '--client_timeout', dest='client_timeout_seconds', default=None,
help='worker timeout seconds')
parser.add_option('-8', '--run_forever', dest='run_forever', action='store_true')
parser.add_option('-i', '--input_filename', dest='input_filename', default='', help='input filename')
return parser
def run_clients(queue, options=None):
parser = client_options_parser()
(default_options, args) = parser.parse_args([])
if options is not None:
try:
default_options.__dict__.update(options.__dict__)
except:
default_options.__dict__.update(options)
options = default_options
number_of_clients = int(options.number_of_clients)
pool = Pool(processes=number_of_clients)
try:
for i in range(number_of_clients):
pool.apply_async(run_client, kwds=dict(options=options, queue=queue))
except KeyboardInterrupt:
exc_info = sys.exc_info()
logging.debug("%s:%s", exc_info[0], exc_info[1])
pool.terminate()
pool.join()
except:
exc_info = sys.exc_info()
logging.exception("%s:%s", exc_info[0], exc_info[1])
pool.terminate()
else:
pool.close()
finally:
print('end pool')
pool.join()
def map_default(k, v):
yield k, v
def reduce_default(k, vs):
if len(vs) == 1:
return vs[0]
else:
return vs
class Server(mincemeat.Server):
def __init__(self, datasource=None):
mincemeat.Server.__init__(self)
self.datasource = datasource
self.mapfn = map_default
self.reducefn = reduce_default
def run_server(options):
parser = client_options_parser()
(default_options, args) = parser.parse_args([])
if options is not None:
try:
default_options.__dict__.update(options.__dict__)
except:
default_options.__dict__.update(options)
options = default_options
logging.debug(options)
''' initialize server data and assign map/reduce function '''
datasource = None
if isinstance(options.datasource, collections.Mapping):
datasource = options.datasource
else:
datasource = dict(enumerate(options.datasource))
server = None
if 'server' in options.__dict__:
server = options.server(datasource)
else:
server = Server(datasource)
if 'mapfn' in options.__dict__:
server.mapfn = options.mapfn
if 'reducefn' in options.__dict__:
server.reducefn = options.reducefn
if 'cache' in options.__dict__:
server.cache_on = options.cache
return server.run_server(password=options.password)
def listener_configurer():
root = logging.getLogger()
h = logging.FileHandler('debug.log', 'a')
f = logging.Formatter('%(asctime)s %(processName)-10s %(name)s %(levelname)-8s %(message)s')
h.setFormatter(f)
root.addHandler(h)
def listener_process(queue, configurer):
configurer()
while True:
try:
record = queue.get()
if record is None: # We send this as a sentinel to tell the listener to quit.
break
logger = logging.getLogger(record.name)
logger.handle(record) # No level or filter logic applied - just do it!
except Exception:
import sys, traceback
print('Whoops! Problem:', file=sys.stderr)
traceback.print_exc(file=sys.stderr)
if __name__ == '__main__':
parser = client_options_parser()
(options, args) = parser.parse_args()
if options.verbose:
logging.basicConfig(level=logging.INFO)
if options.loud:
logging.basicConfig(level=logging.DEBUG)
if options.quiet:
logging.basicConfig(level=logging.FATAL)
if len(args) > 0:
options.hostname = args[0]
logging.debug('options: %s', options)
queue = multiprocessing.Manager().Queue(-1)
listener = Process(target=listener_process, args=(queue, listener_configurer))
listener.start()
# p = Process(target=run_clients, args=(queue, options))
# p.start()
# p.join()
run_clients(queue, options)
queue.put_nowait(None)
listener.join()
print('end')