-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaster.py
More file actions
395 lines (282 loc) · 12.8 KB
/
master.py
File metadata and controls
395 lines (282 loc) · 12.8 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
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
#!/usr/bin/env python
import os
import sys
import shutil
import uuid
import random
import json
from threading import Thread, Lock
import network
import fs
from communicator import NodeCommunicator, AsyncTask
class CartographyMasterHandler(object):
def _map(self, conn, m):
transaction_id = m['transaction_id']
input_table = m['input_table']
output_table = m['output_table']
map_command = m['map_command']
replace_mode = m['replace_mode'] if 'replace_mode' in m else False
self.master._map(transaction_id, input_table, output_table, map_command, replace_mode)
conn.send('OP_MAP OK')
def _sort(self, conn, m):
input_table = m['input_table']
output_table = m['output_table']
self.master._sort(input_table, output_table)
conn.send('OP_SORT OK')
def _remove_table(self, conn, m):
table = m['table']
self.master._remove_table(table)
conn.send('OP_REMOVE_TABLE OK')
def _rq_transaction_start(self, conn, m):
transaction_id = self.master._rq_transaction_start()
conn.send(transaction_id or '')
# responce = {'result': resu, 'transaction_id': transaction_id}
# conn.send(json.dumps(responce))
def _op_transaction_end(self, conn, m):
transaction_id = m['transaction_id']
resu = self.master._op_transaction_end(transaction_id)
responce = {'result': resu}
conn.send(json.dumps(responce))
def _op_lock_table(self, conn, m):
transaction_id = m['transaction_id']
table = m['table']
resu = self.master._op_lock_table(transaction_id, table)
conn.send('OK' if resu else 'FAILED')
def _op_unlock_table(self, conn, m):
transaction_id = m['transaction_id']
table = m['table']
resu = self.master._op_unlock_table(transaction_id, table)
conn.send('OK' if resu else 'FAILED')
def _op_upload_file(self, conn, m):
transaction_id = m['transaction_id']
filename = m['filename']
data = m['data']
resu = self.master._op_upload_file(transaction_id, filename, data)
conn.send('OK' if resu else 'FAILED')
def _rq_table_metadata(self, conn, m):
table = m['table']
table_metadata = self.master._read_table_metadata(table)
conn.send(table_metadata)
def _rq_random_holder(self, conn, m):
node_name = self.master._random_holder()
conn.send(node_name.encode('utf8'))
def _rq_node_address(self, conn, m):
node = m['node']
node_address = self.master.communicator.nodes[node]
conn.send(node_address.encode('utf8'))
def _int_chunk_written(self, conn, m):
chunk_metadata = m['chunk_metadata']
table = chunk_metadata['table']
if not self.master.fs.metadata_exists(table):
self.master.fs.create_empty_table(table)
self.master.fs.update_metadata(chunk_metadata)
conn.send('INT_CHUNK_WRITTEN OK')
def _int_node_greet(self, conn, m):
node_name = m['name']
node_port = m['port']
node_endpoint = network.endpoint_from_tuple((conn.get_peeraddress(), node_port))
# if node_endpoint.startswith('127'):
# node_endpoint = '192.168.0.101:'+str(node_port)
self.master._int_node_greet(node_name, node_endpoint)
conn.send('INT_NODE_GREET OK')
def __init__(self, master):
self.master = master
self.handlers = {
'OP_MAP' : self._map,
'OP_SORT' : self._sort,
'OP_REMOVE_TABLE' : self._remove_table,
'RQ_TRANSACTION_START' : self._rq_transaction_start,
'OP_TRANSACTION_END' : self._op_transaction_end,
'OP_LOCK_TABLE' : self._op_lock_table,
'OP_UNLOCK_TABLE' : self._op_unlock_table,
'OP_UPLOAD_FILE' : self._op_upload_file,
'RQ_TABLE_METADATA' : self._rq_table_metadata,
'RQ_RANDOM_HOLDER' : self._rq_random_holder,
'RQ_NODE_ADDRESS' : self._rq_node_address,
'INT_CHUNK_WRITTEN' : self._int_chunk_written,
'INT_NODE_GREET' : self._int_node_greet,
}
def handle_request(self, conn):
try:
message = json.loads(conn.receive())
message_type = message['operation']
print 'Message from {0} : {1}'.format(conn.get_peername(), message_type)
if message_type in self.handlers:
self.handlers[message_type](conn, message)
else:
conn.send('UNKNOWN_OPERATION')
finally:
conn.close()
class Transaction(object):
def __init__(self, t_id):
self.id = t_id
self.locked_tables = []
self.affected_nodes = []
def __hash__(self):
return self.id
def __eq__(self):
return self.id
class CartographyMaster(object):
def __init__(self, port=8090, local_mode=True, working_dir='.'):
self.port = port
self.local_mode = local_mode
self.working_dir=working_dir
self.communicator = NodeCommunicator()
self.fs = fs.GeminiFileSystemMaster(self.communicator, working_dir)
self.handler = CartographyMasterHandler(self)
self.transactions = {}
self.transactions_lock = Lock()
self.locked_tables = set()
self.locked_tables_lock = Lock()
def _transaction_dir(self, transaction_id):
return os.path.join(self.working_dir, transaction_id)
def _run_async_tasks(self, method, args):
tasks = []
for arg in args:
task = AsyncTask(target=method, args=(arg,))
tasks.append((task, arg))
for task, arg in tasks:
yield arg, task.wait()
def _rq_transaction_start(self):
try:
self.transactions_lock.acquire()
transaction = Transaction(uuid.uuid4().hex)
self.transactions[transaction.id] = transaction
os.mkdir(self._transaction_dir(transaction.id))
return transaction.id
finally:
self.transactions_lock.release()
return None
def _op_transaction_end(self, transaction_id):
try:
self.transactions_lock.acquire()
transaction = self.transactions[transaction_id]
for table in transaction.locked_tables:
self._op_unlock_table(transaction.id, table)
for node in transaction.affected_nodes:
self.communicator.request(node, 'NODE_OP_TRANSACTION_END', transaction_id)
self.transactions.pop(transaction_id)
shutil.rmtree(self._transaction_dir(transaction_id))
finally:
self.transactions_lock.release()
return True
def _op_lock_table(self, transaction_id, table):
try:
self.transactions_lock.acquire()
self.locked_tables_lock.acquire()
if transaction_id in self.transactions and table not in self.locked_tables:
self.locked_tables.add(table)
self.transactions[transaction_id].locked_tables.append(table)
return True
finally:
self.locked_tables_lock.release()
self.transactions_lock.release()
return False
def _op_unlock_table(self, transaction_id, table):
try:
self.transactions_lock.acquire()
self.locked_tables_lock.acquire()
transaction = self.transactions[transaction_id]
if table in self.locked_tables and table in transaction.locked_tables:
self.locked_tables.remove(table)
transaction.locked_tables.remove(table)
return True
finally:
self.locked_tables_lock.release()
self.transactions_lock.release()
return False
def _op_upload_file(self, transaction_id, filename, data):
# TODO: race
if transaction_id not in self.transactions:
return False
transaction = self.transactions[transaction_id]
for node, _ in self.communicator.broadcast('NODE_OP_UPLOAD_FILE', transaction_id, filename, data):
transaction.affected_nodes.append(node)
# directory = self._transaction_dir(transaction_id)
# with open(os.path.join(directory, filename), 'w') as fo:
# fo.write(data)
return True
def _map(self, transaction_id, input_table, output_table, map_command, replace_mode=False):
if not self.fs.metadata_exists(input_table):
raise Exception("Input table {0} does not exist!".format(input_table))
if self.fs.metadata_exists(output_table):
if replace_mode:
self.fs.remove_table(output_table)
else:
raise Exception("Output table {0} does exist! Set replace_mode=True to replace it.".format(output_table))
if input_table == output_table:
raise Exception("Can't map to self.")
self.fs.create_empty_table(output_table)
def run_map_on_node(node):
resu = self.communicator.request(node, 'NODE_OP_MAP', transaction_id, input_table, output_table, map_command, replace_mode)
resu = self.communicator.request(node, 'NODE_RQ_CHUNK_METADATA', output_table)
return json.loads(resu)
nodes = [chunk['node'] for _, chunk in self.fs.read_metadata(input_table)['chunks'].items()]
for node, chunk_meta in self._run_async_tasks(run_map_on_node, nodes):
self.fs.update_metadata(chunk_meta)
def _sort(self, input_table, output_table):
if not self.fs.metadata_exists(input_table):
raise Exception("Input table {0} does not exist!".format(input_table))
if self.fs.metadata_exists(output_table):
self.fs.remove_table(output_table)
nodes = [chunk['node'] for _, chunk in self.fs.read_metadata(input_table)['chunks'].items()]
def _get_sort_sample(node):
resu = self.communicator.request(node, 'NODE_RQ_SORT_SAMPLE', input_table)
return json.loads(resu)
sample = [
key
for node, chunk_sample in self._run_async_tasks(_get_sort_sample, nodes)
for key in chunk_sample['chunk']
]
sample.sort()
# random order to nodes
target_nodes = self.communicator.nodes.keys()
random.shuffle(target_nodes)
def _medians(l, n):
step = (len(l)-1)/(n+1)+1
return [l[i] for i in range(step, len(l), step)]
medians = _medians(sample, len(target_nodes)-1)
medians = [None] + medians + [None]
route_data = {}
for i, node in enumerate(target_nodes):
route_data[node] = {
'endpoint': self.communicator.nodes[node],
'lower': medians[i],
'upper': medians[i+1]
}
self.fs.create_empty_table(output_table)
def _run_partition_on_node(node):
return self.communicator.request(node, 'NODE_OP_PARTITION', input_table, output_table, route_data)
def _run_sort_on_node(node):
resu = self.communicator.request(node, 'NODE_OP_SORT_CHUNK', output_table)
resu = self.communicator.request(node, 'NODE_RQ_CHUNK_METADATA', output_table)
return json.loads(resu)
for node, chunk_meta in self._run_async_tasks(_run_partition_on_node, nodes):
print 'node {0} finished partition'.format(node)
self.fs.reset_metadata(output_table)
for node, chunk_meta in self._run_async_tasks(_run_sort_on_node, target_nodes):
print 'node {0} finished sort'.format(node)
self.fs.update_metadata(chunk_meta, sorted_append=True)
def _remove_table(self, table):
if not self.fs.metadata_exists(table):
print 'Table does not exist: {0}'.format(table)
else:
self.fs.remove_table(table)
def _read_table_metadata(self, table):
return json.dumps(self.fs.read_metadata(table))
def _random_holder(self):
return random.choice(self.communicator.nodes.values())
def _int_node_greet(self, node_name, node_endpoint):
print "node connected: {0} : {1}".format(node_name, node_endpoint)
self.communicator.register_node(node_name, node_endpoint)
def loop(self):
listener = network.ConnectionListener(self.port, self.local_mode)
try:
for conn in listener.accept_connections(10):
Thread(target=self.handler.handle_request, args=(conn,)).start()
except KeyboardInterrupt:
print 'Stopping master...'
finally:
listener.socket.close()
for node in self.communicator.nodes:
self.communicator.request(node, 'NODE_OP_SHUTDOWN')