/
test_http_executor.cpp
266 lines (207 loc) · 6.63 KB
/
test_http_executor.cpp
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
// 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.
#include <iostream>
#include <queue>
#include <string>
#include <mesos/http.hpp>
#include <mesos/v1/executor.hpp>
#include <mesos/v1/mesos.hpp>
#include <process/defer.hpp>
#include <process/delay.hpp>
#include <process/owned.hpp>
#include <process/process.hpp>
#include <stout/exit.hpp>
#include <stout/linkedhashmap.hpp>
#include <stout/option.hpp>
#include <stout/os.hpp>
#include <stout/uuid.hpp>
using std::cout;
using std::endl;
using std::queue;
using std::string;
using mesos::v1::ExecutorID;
using mesos::v1::FrameworkID;
using mesos::v1::TaskID;
using mesos::v1::TaskInfo;
using mesos::v1::TaskState;
using mesos::v1::TaskStatus;
using mesos::v1::executor::Call;
using mesos::v1::executor::Event;
using mesos::v1::executor::Mesos;
using process::spawn;
using process::wait;
class TestExecutor: public process::Process<TestExecutor>
{
public:
TestExecutor(const FrameworkID& _frameworkId, const ExecutorID& _executorId)
: frameworkId(_frameworkId),
executorId(_executorId),
state(DISCONNECTED) {}
void connected()
{
state = CONNECTED;
doReliableRegistration();
}
void doReliableRegistration()
{
if (state == SUBSCRIBED || state == DISCONNECTED) {
return;
}
Call call;
call.mutable_framework_id()->CopyFrom(frameworkId);
call.mutable_executor_id()->CopyFrom(executorId);
call.set_type(Call::SUBSCRIBE);
Call::Subscribe* subscribe = call.mutable_subscribe();
// Send all unacknowledged updates.
foreachvalue (const Call::Update& update, updates) {
subscribe->add_unacknowledged_updates()->MergeFrom(update);
}
// Send all unacknowledged tasks.
foreachvalue (const TaskInfo& task, tasks) {
subscribe->add_unacknowledged_tasks()->MergeFrom(task);
}
mesos->send(call);
process::delay(Seconds(1), self(), &Self::doReliableRegistration);
}
void disconnected()
{
state = DISCONNECTED;
}
void sendStatusUpdate(const TaskInfo& task, const TaskState& state)
{
id::UUID uuid = id::UUID::random();
TaskStatus status;
status.mutable_task_id()->CopyFrom(task.task_id());
status.mutable_executor_id()->CopyFrom(executorId);
status.set_state(state);
status.set_source(TaskStatus::SOURCE_EXECUTOR);
status.set_uuid(uuid.toBytes());
Call call;
call.mutable_framework_id()->CopyFrom(frameworkId);
call.mutable_executor_id()->CopyFrom(executorId);
call.set_type(Call::UPDATE);
call.mutable_update()->mutable_status()->CopyFrom(status);
// Capture the status update.
updates[uuid] = call.update();
mesos->send(call);
}
void received(queue<Event> events)
{
while (!events.empty()) {
Event event = events.front();
events.pop();
switch (event.type()) {
case Event::SUBSCRIBED: {
cout << "Received a SUBSCRIBED event" << endl;
state = SUBSCRIBED;
break;
}
case Event::LAUNCH: {
const TaskInfo& task = event.launch().task();
tasks[task.task_id()] = task;
cout << "Starting task " << task.task_id().value() << endl;
sendStatusUpdate(task, TaskState::TASK_RUNNING);
// This is where one would perform the requested task.
cout << "Finishing task " << task.task_id().value() << endl;
sendStatusUpdate(task, TaskState::TASK_FINISHED);
break;
}
case Event::LAUNCH_GROUP: {
cout << "Received a LAUNCH_GROUP event";
// TODO(vinod): Implement this.
break;
}
case Event::KILL: {
cout << "Received a KILL event" << endl;
break;
}
case Event::ACKNOWLEDGED: {
cout << "Received an ACKNOWLEDGED event" << endl;
// Remove the corresponding update.
updates.erase(id::UUID::fromBytes(event.acknowledged().uuid()).get());
// Remove the corresponding task.
tasks.erase(event.acknowledged().task_id());
break;
}
case Event::MESSAGE: {
cout << "Received a MESSAGE event" << endl;
break;
}
case Event::SHUTDOWN: {
cout << "Received a SHUTDOWN event" << endl;
break;
}
case Event::ERROR: {
cout << "Received an ERROR event" << endl;
break;
}
case Event::HEARTBEAT: {
break;
}
case Event::UNKNOWN: {
LOG(WARNING) << "Received an UNKNOWN event and ignored";
break;
}
}
}
}
protected:
void initialize() override
{
// We initialize the library here to ensure that callbacks are only invoked
// after the process has spawned.
mesos.reset(new Mesos(
mesos::ContentType::PROTOBUF,
process::defer(self(), &Self::connected),
process::defer(self(), &Self::disconnected),
process::defer(self(), &Self::received, lambda::_1)));
}
private:
const FrameworkID frameworkId;
const ExecutorID executorId;
process::Owned<Mesos> mesos;
enum State
{
CONNECTED,
DISCONNECTED,
SUBSCRIBED
} state;
LinkedHashMap<id::UUID, Call::Update> updates; // Unacknowledged updates.
LinkedHashMap<TaskID, TaskInfo> tasks; // Unacknowledged tasks.
};
int main()
{
FrameworkID frameworkId;
ExecutorID executorId;
Option<string> value;
value = os::getenv("MESOS_FRAMEWORK_ID");
if (value.isNone()) {
EXIT(EXIT_FAILURE)
<< "Expecting 'MESOS_FRAMEWORK_ID' to be set in the environment";
}
frameworkId.set_value(value.get());
value = os::getenv("MESOS_EXECUTOR_ID");
if (value.isNone()) {
EXIT(EXIT_FAILURE)
<< "Expecting 'MESOS_EXECUTOR_ID' to be set in the environment";
}
executorId.set_value(value.get());
process::Owned<TestExecutor> executor(
new TestExecutor(frameworkId, executorId));
process::spawn(executor.get());
process::wait(executor.get());
return EXIT_SUCCESS;
}