This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanagrammer_skeleton.py
324 lines (280 loc) · 11.5 KB
/
anagrammer_skeleton.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
#!/usr/bin/env python
from collections import deque
import json
import os
import signal
import sys
import time
import datetime
from threading import Thread
try:
from mesos.native import MesosExecutorDriver, MesosSchedulerDriver
from mesos.interface import Executor, Scheduler
from mesos.interface import mesos_pb2
except ImportError:
from mesos import Executor, MesosExecutorDriver, MesosSchedulerDriver, Scheduler
import mesos_pb2
import results
import task_state
import export_dot
TASK_CPUS = 0.1
TASK_MEM = 32
SHUTDOWN_TIMEOUT = 30 # in seconds
LEADING_ZEROS_COUNT = 5 # appended to task ID to facilitate lexicographical order
TASK_ATTEMPTS = 5 # how many times a task is attempted
# See the Mesos Framework Development Guide:
# http://mesos.apache.org/documentation/latest/app-framework-development-guide
#
# Scheduler, scheduler driver, executor, and executor driver definitions:
# https://github.com/apache/mesos/blob/master/src/python/src/mesos.py
# https://github.com/apache/mesos/blob/master/include/mesos/scheduler.hpp
#
# Mesos protocol buffer definitions for Python:
# https://github.com/mesosphere/deimos/blob/master/deimos/mesos_pb2.py
# https://github.com/apache/mesos/blob/master/include/mesos/mesos.proto
#
def __init__(self, seedWord, maxDefinerTasks, finderExecutor, definerExecutor):
class RenderingFinder(Scheduler):
print "Anagrammer"
print "======="
print "seedWord: [%s]\n" % seedWord
self.seedWord = seedWord
self.finderExecutor = finderExecutor
self.definerExecutor = definerExecutor
self.finderQueue = deque([seedWord])
self.definerQueue = deque([seedWord])
self.processedURLs = set([seedWord])
self.finderResults = set([])
self.definerResults = {}
self.tasksCreated = 0
self.tasksRunning = 0
self.tasksFailed = 0
self.tasksRetrying = {}
self.definerLimitReached = False
self.maxDefinerTasks = maxDefinerTasks
self.shuttingDown = False
def registered(self, driver, frameworkId, masterInfo):
"""
Invoked when the scheduler successfully registers with a Mesos master.
It is called with the frameworkId, a unique ID generated by the
master, and the masterInfo which is information about the master
itself.
"""
print "Registered with framework ID [%s]" % frameworkId.value
def reregistered(self, driver, masterInfo):
"""
Invoked when the scheduler re-registers with a newly elected Mesos
master. This is only called when the scheduler has previously been
registered. masterInfo contains information about the newly elected
master.
"""
print "Re-registered with Mesos master"
def disconnected(self, driver):
"""
Invoked when the scheduler becomes disconnected from the master, e.g.
the master fails and another is taking over.
"""
pass
def makeTaskPrototype(self, offer):
task = mesos_pb2.TaskInfo()
tid = self.tasksCreated
self.tasksCreated += 1
task.task_id.value = str(tid).zfill(LEADING_ZEROS_COUNT)
task.slave_id.value = offer.slave_id.value
cpus = task.resources.add()
cpus.name = "cpus"
cpus.type = mesos_pb2.Value.SCALAR
cpus.scalar.value = TASK_CPUS
mem = task.resources.add()
mem.name = "mem"
mem.type = mesos_pb2.Value.SCALAR
mem.scalar.value = TASK_MEM
return task
def makefinderTask(self, url, offer):
task = self.makeTaskPrototype(offer)
task.name = "finder task %s" % task.task_id.value
#
# TODO
#
#
pass
def makeDefinerTask(self, url, offer):
task = self.makeTaskPrototype(offer)
task.name = "definer task %s" % task.task_id.value
#
# TODO
#
pass
def retryTask(self, task_id, url):
if not url in self.tasksRetrying:
self.tasksRetrying[url] = 1
if self.tasksRetrying[url] < TASK_ATTEMPTS:
self.tasksRetrying[url] += 1
ordinal = lambda n: "%d%s" % (n, \
"tsnrhtdd"[(n / 10 % 10 != 1) * (n % 10 < 4) * n % 10::4])
print "%s try for \"%s\"" % \
(ordinal(self.tasksRetrying[url]), url)
if task_id.endswith(FINDER_TASK_SUFFIX):
self.finderQueue.append(url)
elif task_id.endswith(DEFINER_TASK_SUFFIX):
self.definerQueue.append(url)
else:
self.tasksFailed += 1
print "Task for \"%s\" cannot be completed, attempt limit reached" % url
def printStatistics(self):
print "Queue length: %d finder, %d definer; Tasks: %d running, %d failed" % (
len(self.finderQueue), len(self.definerQueue), self.tasksRunning, self.tasksFailed
)
def maxTasksForOffer(self, offer):
count = 0
cpus = next(rsc.scalar.value for rsc in offer.resources if rsc.name == "cpus")
mem = next(rsc.scalar.value for rsc in offer.resources if rsc.name == "mem")
#
# TODO
#
pass
def resourceOffers(self, driver, offers):
"""
Invoked when resources have been offered to this framework. A single
offer will only contain resources from a single slave. Resources
associated with an offer will not be re-offered to _this_ framework
until either (a) this framework has rejected those resources (see
SchedulerDriver.launchTasks) or (b) those resources have been
rescinded (see Scheduler.offerRescinded). Note that resources may be
concurrently offered to more than one framework at a time (depending
on the allocator being used). In that case, the first framework to
launch tasks using those resources will be able to use them while the
other frameworks will have those resources rescinded (or if a
framework has already launched tasks with those resources then those
tasks will fail with a TASK_LOST status and a message saying as much).
"""
self.printStatistics()
print "Received resource offer(s)"
#
# TODO
#
pass
def offerRescinded(self, driver, offerId):
"""
Invoked when an offer is no longer valid (e.g., the slave was lost or
another framework used resources in the offer.) If for whatever reason
an offer is never rescinded (e.g., dropped message, failing over
framework, etc.), a framwork that attempts to launch tasks using an
invalid offer will receive TASK_LOST status updats for those tasks
(see Scheduler.resourceOffers).
"""
pass
def statusUpdate(self, driver, update):
"""
Invoked when the status of a task has changed (e.g., a slave is lost
and so the task is lost, a task finishes and an executor sends a
status update saying so, etc.) Note that returning from this callback
acknowledges receipt of this status update. If for whatever reason
the scheduler aborts during this callback (or the process exits)
another status update will be delivered. Note, however, that this is
currently not true if the slave sending the status update is lost or
fails during that time.
"""
stateName = task_state.nameFor[update.state]
print "Task [%s] is in state [%s]" % (update.task_id.value, stateName)
def frameworkMessage(self, driver, executorId, slaveId, message):
"""
Invoked when an executor sends a message. These messages are best
effort; do not expect a framework message to be retransmitted in any
reliable fashion.
"""
o = json.loads(message)
if executorId.value == finderExecutor.executor_id.value:
result = results.finderResult(o['taskId'], o['url'], o['links'])
#
# TODO
#
elif executorId.value == definerExecutor.executor_id.value:
result = results.definerResult(o['taskId'], o['url'], o['imageUrl'])
#
# TODO
#
def slaveLost(self, driver, slaveId):
"""
Invoked when a slave has been determined unreachable (e.g., machine
failure, network partition.) Most frameworks will need to reschedule
any tasks launched on this slave on a new slave.
"""
pass
def executorLost(self, driver, executorId, slaveId, status):
"""
Invoked when an executor has exited/terminated. Note that any tasks
running will have TASK_LOST status updates automatically generated.
"""
pass
def error(self, driver, message):
"""
Invoked when there is an unrecoverable error in the scheduler or
scheduler driver. The driver will be aborted BEFORE invoking this
callback.
"""
print "Error from Mesos: %s " % message
def hard_shutdown():
driver.stop()
def graceful_shutdown(signal, frame):
print "anagrammer is shutting down"
anagrammer.shuttingDown = True
wait_started = datetime.datetime.now()
while (anagrammer.tasksRunning > 0) and \
(SHUTDOWN_TIMEOUT > (datetime.datetime.now() - wait_started).total_seconds()):
time.sleep(1)
if (anagrammer.tasksRunning > 0):
print "Shutdown by timeout, %d task(s) have not completed" % anagrammer.tasksRunning
hard_shutdown()
#
# Execution entry point:
#
if __name__ == "__main__":
if len(sys.argv) < 3 or len(sys.argv) > 4:
print "Usage: %s seedWord mesosMasterUrl [maxDefinerTasks]" % sys.argv[0]
sys.exit(1)
baseURI = "home/vagrant/"
suffixURI = "hostfiles"
uris = [ "anagram_finder.py",
"export_dot.py",
"definer_executor.py",
"results.py",
"task_state.py" ]
uris = [os.path.join(baseURI, suffixURI, uri) for uri in uris]
finderExecutor = mesos_pb2.ExecutorInfo()
finderExecutor.executor_id.value = "finder-executor"
finderExecutor.command.value = "python anagram_finder.py"
for uri in uris:
uri_proto = finderExecutor.command.uris.add()
uri_proto.value = uri
uri_proto.extract = False
finderExecutor.name = "Finder"
definerExecutor = mesos_pb2.ExecutorInfo()
definerExecutor.executor_id.value = "definer-executor"
definerExecutor.command.value = "python definer_executor.py --local"
for uri in uris:
uri_proto = definerExecutor.command.uris.add()
uri_proto.value = uri
uri_proto.extract = False
definerExecutor.name = "Definer"
framework = mesos_pb2.FrameworkInfo()
framework.user = "" # Have Mesos fill in the current user.
framework.name = "anagrammer"
try: maxDefinerTasks = int(sys.argv[3])
except: maxDefinerTasks = 0
anagrammer = RenderingFinder(sys.argv[1], maxDefinerTasks, finderExecutor, definerExecutor)
driver = MesosSchedulerDriver(anagrammer, framework, sys.argv[2])
# driver.run() blocks; we run it in a separate thread
def run_driver_async():
status = 0 if driver.run() == mesos_pb2.DRIVER_STOPPED else 1
driver.stop()
sys.exit(status)
framework_thread = Thread(target = run_driver_async, args = ())
framework_thread.start()
print "(Listening for Ctrl-C)"
signal.signal(signal.SIGINT, graceful_shutdown)
while framework_thread.is_alive():
time.sleep(1)
export_dot.dot(anagrammer.finderResults, anagrammer.definerResults, "result.dot")
print "Goodbye!"
sys.exit(0)