-
Notifications
You must be signed in to change notification settings - Fork 11
/
framework.py
312 lines (244 loc) · 11.1 KB
/
framework.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
#!/usr/bin/env python
# _ __ _
# _____ ____ _ _ __ ___ _ __ | | ___ / _|_ __ __ _ _ __ ___ _____ _____ _ __| | __
# / _ \ \/ / _` | '_ ` _ \| '_ \| |/ _ \_____| |_| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
# | __/> < (_| | | | | | | |_) | | __/_____| _| | | (_| | | | | | | __/\ V V / (_) | | | <
# \___/_/\_\__,_|_| |_| |_| .__/|_|\___| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
# |_|
#
import Queue
import argparse
import os
import threading
import time
import logging
import pesos.api
import pesos.scheduler
from pesos.vendor.mesos import mesos_pb2
logger = logging.getLogger(__name__)
class ExampleScheduler(pesos.api.Scheduler):
"""Example scheduler that launches tasks that don't do a whole lot.
"""
TASK_CPU = 0.1
TASK_MEM = 256
def __init__(self, taskQueue):
# Maintain a queue of the tasks to launch
self.tasks = taskQueue
self.terminal = 0
self.total_tasks = taskQueue.qsize()
def registered(self, driver, frameworkId, masterInfo):
"""
Invoked when the scheduler successfully registers with a Mesos
master. A unique ID (generated by the master) used for
distinguishing this framework from others and MasterInfo
with the ip and port of the current master are provided as arguments.
"""
logger.info("Registered framework %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 containing the updated information about the elected master
is provided as an argument.
"""
logger.info("Connected with master %s" % (masterInfo.ip))
def disconnected(self, driver):
"""
Invoked when the scheduler becomes "disconnected" from the master
(e.g., the master fails and another is taking over).
"""
logger.info("Disconnected from master")
def resource_offers(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).
"""
logger.info("Received %d offers" % len(offers))
def handle_offers():
declined = []
# Loop over the offers and see if there's anything that looks good
for offer in offers:
offer_cpu = 0
offer_mem = 0
if self.tasks.empty():
declined.append(offer.id)
continue
# Collect up the CPU and Memory resources from the offer
for resource in offer.resources:
if resource.name == "cpus":
offer_cpu = resource.scalar.value
if resource.name == "mem":
offer_mem = resource.scalar.value
tasks = []
# Keep looking for tasks until any of the following criteria are met
# - No more CPU left in the offer
# - No more Memory left in the offer
# - No more tasks left to launch
while offer_mem >= self.TASK_MEM and offer_cpu >= self.TASK_CPU \
and not self.tasks.empty(): \
offer_cpu -= self.TASK_CPU
offer_mem -= self.TASK_MEM
# Pop a task off the queue
executor_id, task_id, args = self.tasks.get()
self.tasks.task_done() # Mark it as done immediately
logger.info("Queue task %d:%d" % (executor_id, task_id))
tasks.append(self._build_task(offer, executor_id, task_id, args))
# If we have any tasks to launch, ask the driver to launch them.
if tasks:
driver.launch_tasks(offer.id, tasks)
# Decline the offers in batch
if declined:
driver.decline_offer(declined)
t = threading.Thread(target=handle_offers)
t.start()
def _build_task(self, offer, executor_id, task_id, args):
"""
Create a TaskInfo object for an offer, executor_id and task_id.
"""
# Create the initial TaskInfo object
task = mesos_pb2.TaskInfo()
task.name = "Test Framework Task"
task.task_id.value = "%d:%d" % (executor_id, task_id)
task.slave_id.value = offer.slave_id.value
# Configure the executor
task.executor.executor_id.value = str(executor_id)
task.executor.framework_id.value = offer.framework_id.value
uri = task.executor.command.uris.add()
uri.value = args.executor_uri
task.executor.command.value = "./%s/bin/executor" % os.path.basename(uri.value).split(".")[0]
# Add the task resource
cpus = task.resources.add()
cpus.name = "cpus"
cpus.type = mesos_pb2.Value.SCALAR
cpus.scalar.value = self.TASK_CPU
mem = task.resources.add()
mem.name = "mem"
mem.type = mesos_pb2.Value.SCALAR
mem.scalar.value = self.TASK_MEM
return task
def offer_rescinded(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 framework that attempts
to launch tasks using an invalid offer will receive TASK_LOST
status updates for those tasks (see Scheduler::resourceOffers).
"""
logger.info("Offer rescinded %s" % (offerId.value))
def status_update(self, driver, taskStatus):
"""
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/fails during that time).
"""
statuses = {
mesos_pb2.TASK_STAGING: "STAGING",
mesos_pb2.TASK_STARTING: "STARTING",
mesos_pb2.TASK_RUNNING: "RUNNING",
mesos_pb2.TASK_FINISHED: "FINISHED",
mesos_pb2.TASK_FAILED: "FAILED",
mesos_pb2.TASK_KILLED: "KILLED",
mesos_pb2.TASK_LOST: "LOST",
}
logger.info("Received status update for task %s (%s)" % (
taskStatus.task_id.value,
statuses[taskStatus.state]
))
if taskStatus.state == mesos_pb2.TASK_FINISHED or \
taskStatus.state == mesos_pb2.TASK_FAILED or \
taskStatus.state == mesos_pb2.TASK_KILLED or \
taskStatus.state == mesos_pb2.TASK_LOST: \
# Mark this task as terminal
self.terminal += 1
if self.terminal == self.total_tasks:
driver.stop()
def framework_message(self, driver, executorId, slaveId, data):
"""
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.
"""
logger.info("Message from executor %s and slave %s: %s" % (
executorId.value,
slaveId.value,
data
))
def slave_lost(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.
"""
logger.info("Slave %s has been lost. Y U DO DIS." % (slaveId.value))
def executor_lost(self, driver, executorId, slaveId, exitCode):
"""
Invoked when an executor has exited/terminated. Note that any
tasks running will have TASK_LOST status updates automagically
generated.
"""
logger.info("Executor %s has been lost on slave %s with exit code %d" % (
executorId.value,
slaveId.value,
exitCode
))
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.
"""
logger.info("There was an error: %s" % (message))
if __name__ == "__main__":
for l in ('pesos', 'compactor', 'tornado', '__main__'):
l = logging.getLogger(l)
l.setLevel(logging.DEBUG)
parser = argparse.ArgumentParser(prog="docker-launcher")
parser.add_argument("-m", "--master", required=True, type=str,
help="IP/Port of mesos master")
parser.add_argument("--num-tasks", default=1, type=int,
help="Number of tasks to launch per executor (default: 1)")
parser.add_argument("--num-executors", default=1, type=int,
help="Number of executors to launch (default: 1)")
parser.add_argument("--executor-uri", required=True, type=str,
help="URL to download a version of this code.")
args = parser.parse_args()
# Setup the loggers
loggers = (__name__, "tornado", "pesos", "compactor")
for log in loggers:
logging.getLogger(log).setLevel(logging.DEBUG)
# Create the queue of tasks
tasks = Queue.Queue()
for task in xrange(args.num_tasks):
for executor in xrange(args.num_executors):
tasks.put((executor, task, args))
# Launch the mesos framework
framework = mesos_pb2.FrameworkInfo()
framework.name = "Test Python Framework"
framework.user = "root"
driver = pesos.scheduler.MesosSchedulerDriver(
ExampleScheduler(tasks),
framework,
args.master
)
t = threading.Thread(target=driver.run)
t.setDaemon(True)
t.start()
while t.isAlive():
time.sleep(0.5)