-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathtest_framework.py
executable file
·213 lines (170 loc) · 7.57 KB
/
test_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
#!/usr/bin/env python
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
import time
import mesos.interface
from mesos.interface import mesos_pb2
from mesos.scheduler import MesosSchedulerDriver
TOTAL_TASKS = 5
TASK_CPUS = 1
TASK_MEM = 128
class TestScheduler(mesos.interface.Scheduler):
def __init__(self, implicitAcknowledgements, executor, framework):
self.implicitAcknowledgements = implicitAcknowledgements
self.executor = executor
self.framework = framework
self.taskData = {}
self.tasksLaunched = 0
self.tasksFinished = 0
self.messagesSent = 0
self.messagesReceived = 0
def registered(self, driver, frameworkId, masterInfo):
print "Registered with framework ID %s" % frameworkId.value
self.framework.id.CopyFrom(frameworkId)
driver.updateFramework(framework, [])
def resourceOffers(self, driver, offers):
for offer in offers:
tasks = []
offerCpus = 0
offerMem = 0
for resource in offer.resources:
if resource.name == "cpus":
offerCpus += resource.scalar.value
elif resource.name == "mem":
offerMem += resource.scalar.value
print "Received offer %s with cpus: %s and mem: %s" \
% (offer.id.value, offerCpus, offerMem)
remainingCpus = offerCpus
remainingMem = offerMem
while self.tasksLaunched < TOTAL_TASKS and \
remainingCpus >= TASK_CPUS and \
remainingMem >= TASK_MEM:
tid = self.tasksLaunched
self.tasksLaunched += 1
print "Launching task %d using offer %s" \
% (tid, offer.id.value)
task = mesos_pb2.TaskInfo()
task.task_id.value = str(tid)
task.slave_id.value = offer.slave_id.value
task.name = "task %d" % tid
task.executor.MergeFrom(self.executor)
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
tasks.append(task)
self.taskData[task.task_id.value] = (
offer.slave_id, task.executor.executor_id)
remainingCpus -= TASK_CPUS
remainingMem -= TASK_MEM
operation = mesos_pb2.Offer.Operation()
operation.type = mesos_pb2.Offer.Operation.LAUNCH
operation.launch.task_infos.extend(tasks)
driver.acceptOffers([offer.id], [operation])
def statusUpdate(self, driver, update):
print "Task %s is in state %s" % \
(update.task_id.value, mesos_pb2.TaskState.Name(update.state))
# Ensure the binary data came through.
if update.data != "data with a \0 byte":
print "The update data did not match!"
print " Expected: 'data with a \\x00 byte'"
print " Actual: ", repr(str(update.data))
sys.exit(1)
if update.state == mesos_pb2.TASK_FINISHED:
self.tasksFinished += 1
if self.tasksFinished == TOTAL_TASKS:
print "All tasks done, waiting for final framework message"
slave_id, executor_id = self.taskData[update.task_id.value]
self.messagesSent += 1
driver.sendFrameworkMessage(
executor_id,
slave_id,
'data with a \0 byte')
if update.state == mesos_pb2.TASK_LOST or \
update.state == mesos_pb2.TASK_KILLED or \
update.state == mesos_pb2.TASK_FAILED:
print "Aborting because task %s is in unexpected state %s with message '%s'" \
% (update.task_id.value, mesos_pb2.TaskState.Name(update.state), update.message)
driver.abort()
# Explicitly acknowledge the update if implicit acknowledgements
# are not being used.
if not self.implicitAcknowledgements:
driver.acknowledgeStatusUpdate(update)
def frameworkMessage(self, driver, executorId, slaveId, message):
self.messagesReceived += 1
# The message bounced back as expected.
if message != "data with a \0 byte":
print "The returned message data did not match!"
print " Expected: 'data with a \\x00 byte'"
print " Actual: ", repr(str(message))
sys.exit(1)
print "Received message:", repr(str(message))
if self.messagesReceived == TOTAL_TASKS:
if self.messagesReceived != self.messagesSent:
print "Sent", self.messagesSent,
print "but received", self.messagesReceived
sys.exit(1)
print "All tasks done, and all messages received, exiting"
driver.stop()
if __name__ == "__main__":
if len(sys.argv) != 2:
print "Usage: %s master" % sys.argv[0]
sys.exit(1)
executor = mesos_pb2.ExecutorInfo()
executor.executor_id.value = "default"
executor.command.value = os.path.abspath("./test-executor")
executor.name = "Test Executor (Python)"
executor.source = "python_test"
framework = mesos_pb2.FrameworkInfo()
framework.user = "" # Have Mesos fill in the current user.
framework.name = "Test Framework (Python)"
framework.checkpoint = True
framework.role = "*"
implicitAcknowledgements = 1
if os.getenv("MESOS_EXPLICIT_ACKNOWLEDGEMENTS"):
print "Enabling explicit status update acknowledgements"
implicitAcknowledgements = 0
if os.getenv("MESOS_EXAMPLE_AUTHENTICATE"):
print "Enabling authentication for the framework"
if not os.getenv("MESOS_EXAMPLE_PRINCIPAL"):
print "Expecting authentication principal in the environment"
sys.exit(1);
credential = mesos_pb2.Credential()
credential.principal = os.getenv("MESOS_EXAMPLE_PRINCIPAL")
if os.getenv("MESOS_EXAMPLE_SECRET"):
credential.secret = os.getenv("MESOS_EXAMPLE_SECRET")
framework.principal = os.getenv("MESOS_EXAMPLE_PRINCIPAL")
else:
framework.principal = "test-framework-python"
credential = None
# Subscribe with all roles suppressed to test updateFramework() method
driver = MesosSchedulerDriver(
TestScheduler(implicitAcknowledgements, executor, framework),
framework,
sys.argv[1],
implicitAcknowledgements,
credential,
[framework.role])
status = 0 if driver.run() == mesos_pb2.DRIVER_STOPPED else 1
# Ensure that the driver process terminates.
driver.stop();
sys.exit(status)