This repository has been archived by the owner on Mar 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgasc.cpp
268 lines (218 loc) · 6.93 KB
/
gasc.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
267
268
#include <iostream>
#include <sstream>
#include <fstream>
#include <gasc.hpp>
using std::cerr;
using std::cout;
using std::endl;
using std::map;
using std::string;
using std::vector;
using namespace mesos;
GascDaemon::GascDaemon(int state, string hostname, int port)
: state_(state),
hostname(hostname),
port(port) {
}
void GascDaemon::changeState(int state) {
if (debug) {
cout << "Daemon transitioned to " << state << endl;
}
state_ = state;
}
string GascDaemon::hostnamePort() const {
string portString = std::to_string(port);
return hostname + ":" + portString;
}
GascScheduler::GascScheduler(
double cpusPerInstance,
long memoryPerInstance,
long instances,
string command = "")
: tasksToLaunch(instances),
tasksFinished(0),
cpusPerInstance(cpusPerInstance),
memoryPerInstance(memoryPerInstance),
instances(instances),
command(command),
nextTaskId(0) {}
void GascScheduler::registered(SchedulerDriver*,
const FrameworkID&,
const MasterInfo&)
{
if (debug) {
cout << "GASC registered" << endl;
}
}
void GascScheduler::reregistered(SchedulerDriver*, const MasterInfo& masterInfo) {
cerr << "GASC reregistered" << endl;
}
void GascScheduler::disconnected(SchedulerDriver* driver) {
cerr << "GASC disconnected" << endl;
}
void GascScheduler::resourceOffers(SchedulerDriver* driver,
const vector<Offer>& offers)
{
for (size_t i = 0; i < offers.size(); i++) {
const Offer& offer = offers[i];
double availableCpus = 0;
double availableMemory = 0;
for (size_t j = 0; j < offer.resources_size(); j++) {
const Resource& resource = offer.resources(j);
if (resource.has_name()) {
if (resource.has_scalar()) {
double value = resource.scalar().value();
if (resource.name() == "cpus") {
availableCpus = value;
} else if (resource.name() == "mem") {
availableMemory = value;
}
}
// TODO(nnielsen): Parse port range and disk.
}
}
if (availableCpus < cpusPerInstance) {
if (debug) {
cerr << "Declining offer: need " << cpusPerInstance << " cpus"
<< " but was offered " << availableCpus << endl;
}
driver->declineOffer(offer.id());
continue;
}
if (availableMemory < memoryPerInstance) {
if (debug) {
cerr << "Declining offer: need " << memoryPerInstance << " mb"
<< " memory but was offered "
<< availableMemory << " mb " << endl;
}
driver->declineOffer(offer.id());
continue;
}
Resource cpuResource;
cpuResource.set_name("cpus");
cpuResource.set_type(Value_Type_SCALAR);
cpuResource.mutable_scalar()->set_value(cpusPerInstance);
Resource memoryResource;
memoryResource.set_name("mem");
memoryResource.set_type(Value_Type_SCALAR);
memoryResource.mutable_scalar()->set_value(memoryPerInstance);
// Launch tasks.
vector<TaskInfo> tasks;
while ((availableCpus >= cpusPerInstance) &&
(availableMemory >= memoryPerInstance) &&
(tasksToLaunch > 0)) {
int taskId = nextTaskId++;
tasksToLaunch--;
availableCpus -= cpusPerInstance;
availableMemory -= memoryPerInstance;
string taskIdString = std::to_string(taskId);
int port = 8000 + taskId;
string portString = std::to_string(port);
TaskInfo task;
task.set_name("GSAC daemon #" + taskIdString);
task.mutable_task_id()->set_value(taskIdString);
task.mutable_slave_id()->CopyFrom(offer.slave_id());
task.mutable_command()->set_value(
"/usr/sbin/sshd -p " + portString +
" -D -f /etc/ssh/sshd_config");
task.add_resources()->MergeFrom(cpuResource);
task.add_resources()->MergeFrom(memoryResource);
if (debug) {
cout << "Starting daemon #" << taskId << " on "
<< offer.hostname() << ":" << portString << endl;
}
daemons.insert(std::pair<int, GascDaemon>(
taskId,
GascDaemon(TASK_STAGING, offer.hostname(), port)));
tasks.push_back(task);
}
driver->launchTasks(offer.id(), tasks);
}
}
void GascScheduler::offerRescinded(SchedulerDriver* driver,
const OfferID& offerId) {
// We don't care as we don't keep offers around; we accept or
// decline up frount.
}
void GascScheduler::statusUpdate(SchedulerDriver* driver, const TaskStatus& status)
{
const char* statusString[7] = {
"TASK_STARTING",
"TASK_RUNNING",
"TASK_FINISHED",
"TASK_FAILED",
"TASK_KILLED",
"TASK_LOST",
"TASK_STAGING"};
int taskId;
std::istringstream(status.task_id().value()) >> taskId;
if (debug) {
cout << "Task " << taskId << " is in state "
<< statusString[status.state()] << endl;
}
map<int, GascDaemon>::iterator daemonIterator = daemons.find(taskId);
if (daemonIterator != daemons.end()) {
GascDaemon& daemon = daemonIterator->second;
daemon.changeState(status.state());
}
if ((status.state() == TASK_FAILED) &&
(status.state() == TASK_LOST) &&
(status.state() == TASK_KILLED)) {
tasksToLaunch++;
if (daemonIterator != daemons.end()) {
daemons.erase(daemonIterator);
}
} else if (status.state() == TASK_RUNNING) {
tasksRunning++;
if (tasksRunning == instances) {
runTool(driver);
}
} else if (status.state() == TASK_FINISHED) {
tasksFinished++;
if (tasksFinished == instances)
driver->stop();
}
}
void GascScheduler::frameworkMessage(SchedulerDriver* driver,
const ExecutorID& executorId,
const SlaveID& slaveId,
const string& data) {
// This should not happen as we are using the mesos-executor.
}
void GascScheduler::slaveLost(SchedulerDriver* driver, const SlaveID& sid) {
cerr << "Lost slave " << sid.value() << endl
<< "Aborting run: GASC can't recover!" << endl;
driver->stop();
}
void GascScheduler::executorLost(SchedulerDriver* driver,
const ExecutorID& executorID,
const SlaveID& slaveID,
int status) {
cerr << "Lost executor " << executorID.value() << endl
<< "Aborting run: GASC can't recover!" << endl;
driver->stop();
}
void GascScheduler::error(SchedulerDriver* driver, const string& message)
{
cerr << "Detected framework error: " << message << endl;
}
void GascScheduler::runTool(SchedulerDriver* driver) {
// TODO(nnielsen): Make host name file configurable or
// auto-generated.
std::ofstream hosts("hosts.txt");
map<int, GascDaemon>::iterator daemonIterator = daemons.begin();
for (; daemonIterator != daemons.end(); daemonIterator++) {
const GascDaemon& daemon = daemonIterator->second;
hosts << daemon.hostnamePort() << endl;
}
hosts.close();
if (debug) {
cout << "Host file written in 'hosts.txt'" << endl
<< "" << endl;
}
FILE* toolStream = popen(command.c_str(), "w");
// TODO(nnielsen): Do this in thread instead of blocking callback!
pclose(toolStream);
// TODO(nnielsen): close pipe.
driver->stop();
}