/
HTCondorCentralManagerService.cpp
376 lines (325 loc) · 15 KB
/
HTCondorCentralManagerService.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
* Copyright (c) 2017-2018. The WRENCH Team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#include "wrench/exceptions/WorkflowExecutionException.h"
#include "wrench/logging/TerminalOutput.h"
#include "wrench/services/compute/virtualized_cluster/VirtualizedClusterComputeService.h"
#include "wrench/services/compute/cloud/CloudComputeService.h"
#include "wrench/services/compute/bare_metal/BareMetalComputeService.h"
#include "wrench/services/compute/ComputeServiceMessage.h"
#include "wrench/services/compute/htcondor/HTCondorCentralManagerService.h"
#include "wrench/services/compute/htcondor/HTCondorCentralManagerServiceMessage.h"
#include "wrench/services/compute/htcondor/HTCondorNegotiatorService.h"
#include "wrench/simgrid_S4U_util/S4U_Mailbox.h"
WRENCH_LOG_NEW_DEFAULT_CATEGORY(HTCondorCentralManager, "Log category for HTCondorCentralManagerService");
namespace wrench {
/**
* @brief Constructor
*
* @param hostname: the hostname on which to start the service
* @param compute_resources: a set of compute resources available via the HTCondor pool
* @param property_list: a property list ({} means "use all defaults")
* @param messagepayload_list: a message payload list ({} means "use all defaults")
*
* @throw std::runtime_error
*/
HTCondorCentralManagerService::HTCondorCentralManagerService(
const std::string &hostname,
std::set<ComputeService *> compute_resources,
std::map<std::string, std::string> property_list,
std::map<std::string, double> messagepayload_list)
: ComputeService(hostname, "htcondor_central_manager", "htcondor_central_manager", nullptr) {
// Check compute_resource viability
if (compute_resources.empty()) {
throw std::invalid_argument("At least one compute service should be provided");
}
for (auto const &cs : compute_resources) {
if (dynamic_cast<CloudComputeService *>(cs) and
(not dynamic_cast<VirtualizedClusterComputeService *>(cs))) {
throw std::invalid_argument(
"An HTCondorCentralManagerService cannot use a CloudComputeService - use a VirtualizedClusterComputeService instead");
}
}
this->compute_resources = compute_resources;
// Set default and specified properties
this->setProperties(this->default_property_values, std::move(property_list));
// Set default and specified message payloads
this->setMessagePayloads(this->default_messagepayload_values, std::move(messagepayload_list));
}
/**
* @brief Destructor
*/
HTCondorCentralManagerService::~HTCondorCentralManagerService() {
this->pending_jobs.clear();
this->compute_resources.clear();
this->compute_resources_map.clear();
this->running_jobs.clear();
}
/**
* @brief Submit a standard job to the HTCondor service
*
* @param job: a standard job
* @param service_specific_args: service specific arguments
*
* @throw WorkflowExecutionException
* @throw std::runtime_error
*/
void HTCondorCentralManagerService::submitStandardJob(
StandardJob *job,
std::map<std::string, std::string> &service_specific_args) {
serviceSanityCheck();
std::string answer_mailbox = S4U_Mailbox::generateUniqueMailboxName("submit_standard_job");
// send a "run a standard job" message to the daemon's mailbox_name
try {
S4U_Mailbox::putMessage(
this->mailbox_name,
new ComputeServiceSubmitStandardJobRequestMessage(
answer_mailbox, job, service_specific_args,
this->getMessagePayloadValue(
HTCondorCentralManagerServiceMessagePayload::SUBMIT_STANDARD_JOB_REQUEST_MESSAGE_PAYLOAD)));
} catch (std::shared_ptr<NetworkError> &cause) {
throw WorkflowExecutionException(cause);
}
// Get the answer
std::shared_ptr<SimulationMessage> message = nullptr;
try {
message = S4U_Mailbox::getMessage(answer_mailbox);
} catch (std::shared_ptr<NetworkError> &cause) {
throw WorkflowExecutionException(cause);
}
if (auto msg = std::dynamic_pointer_cast<ComputeServiceSubmitStandardJobAnswerMessage>(message)) {
// If no success, throw an exception
if (not msg->success) {
throw WorkflowExecutionException(msg->failure_cause);
}
} else {
throw std::runtime_error(
"ComputeService::submitStandardJob(): Received an unexpected [" + message->getName() +
"] message!");
}
}
/**
* @brief Asynchronously submit a pilot job to the cloud service
*
* @param job: a pilot job
* @param service_specific_args: service specific arguments
*
* @throw WorkflowExecutionException
* @throw std::runtime_error
*/
void HTCondorCentralManagerService::submitPilotJob(PilotJob *job,
std::map<std::string, std::string> &service_specific_args) {
throw std::runtime_error("HTCondorCentralManagerService::terminateStandardJob(): Not implemented yet!");
}
/**
* @brief Terminate a standard job to the compute service (virtual)
* @param job: the job
*
* @throw std::runtime_error
*/
void HTCondorCentralManagerService::terminateStandardJob(StandardJob *job) {
throw std::runtime_error("HTCondorCentralManagerService::terminateStandardJob(): Not implemented yet!");
}
/**
* @brief Terminate a pilot job to the compute service
* @param job: the job
*
* @throw std::runtime_error
*/
void HTCondorCentralManagerService::terminatePilotJob(PilotJob *job) {
throw std::runtime_error("HTCondorCentralManagerService::terminatePilotJob(): Not implemented yet!");
}
/**
* @brief Main method of the daemon
*
* @return 0 on termination
*/
int HTCondorCentralManagerService::main() {
TerminalOutput::setThisProcessLoggingColor(TerminalOutput::COLOR_MAGENTA);
WRENCH_INFO("HTCondor Service starting on host %s listening on mailbox_name %s",
this->hostname.c_str(), this->mailbox_name.c_str());
// start the compute resource services
try {
for (auto cs : this->compute_resources) {
auto cs_shared_ptr = this->simulation->startNewService(cs);
unsigned long sum_num_idle_cores = 0;
if (auto virtualized_cluster = dynamic_cast<VirtualizedClusterComputeService *>(cs)) {
// for cloud services, we must create/start VMs, each of which
// is a BareMetalComputeService. Right now, we greedily use the whole Cloud!
for (auto const &host : virtualized_cluster->getExecutionHosts()) {
unsigned long num_cores = Simulation::getHostNumCores(host);
double ram = Simulation::getHostMemoryCapacity(host);
auto vm_name = virtualized_cluster->createVM(num_cores, ram);
auto vm_cs = virtualized_cluster->startVM(vm_name, host);
// set the number of idle cores
sum_num_idle_cores = vm_cs->getTotalNumIdleCores();
this->compute_resources_map.insert(std::make_pair(vm_cs, sum_num_idle_cores));
}
} else if (auto cloud = dynamic_cast<CloudComputeService *>(cs)) {
throw std::runtime_error(
"An HTCondorCentralManagerService cannot use a CloudComputeService - use a VirtualizedClusterComputeService instead");
} else {
// set the number of available cores
sum_num_idle_cores = cs->getTotalNumIdleCores();
this->compute_resources_map.insert(std::make_pair(cs_shared_ptr, sum_num_idle_cores));
}
}
} catch (WorkflowExecutionException &e) {
throw std::runtime_error("Unable to acquire compute resources: " + e.getCause()->toString());
}
// main loop
while (this->processNextMessage()) {
// starting an HTCondor negotiator
if (not this->dispatching_jobs && not this->pending_jobs.empty() && not this->resources_unavailable) {
this->dispatching_jobs = true;
auto negotiator = std::shared_ptr<HTCondorNegotiatorService>(
new HTCondorNegotiatorService(this->hostname, this->compute_resources_map,
this->running_jobs,
this->pending_jobs, this->mailbox_name));
negotiator->simulation = this->simulation;
negotiator->start(negotiator, true, false); // Daemonized, no auto-restart
}
}
WRENCH_INFO("HTCondorCentralManager Service on host %s cleanly terminating!",
S4U_Simulation::getHostName().c_str());
return 0;
}
/**
* @brief Wait for and react to any incoming message
*
* @return false if the daemon should terminate, true otherwise
*
* @throw std::runtime_error
*/
bool HTCondorCentralManagerService::processNextMessage() {
// Wait for a message
std::shared_ptr<SimulationMessage> message;
try {
message = S4U_Mailbox::getMessage(this->mailbox_name);
} catch (std::shared_ptr<NetworkError> &cause) {
return true;
}
if (message == nullptr) {
WRENCH_INFO("Got a NULL message... Likely this means we're all done. Aborting");
return false;
}
WRENCH_INFO("HTCondor Central Manager got a [%s] message", message->getName().c_str());
if (auto msg = std::dynamic_pointer_cast<ServiceStopDaemonMessage>(message)) {
this->terminate();
// This is Synchronous
try {
S4U_Mailbox::putMessage(
msg->ack_mailbox,
new ServiceDaemonStoppedMessage(
this->getMessagePayloadValue(
HTCondorCentralManagerServiceMessagePayload::DAEMON_STOPPED_MESSAGE_PAYLOAD)));
} catch (std::shared_ptr<NetworkError> &cause) {
return false;
}
return false;
} else if (auto msg = std::dynamic_pointer_cast<ComputeServiceSubmitStandardJobRequestMessage>(message)) {
processSubmitStandardJob(msg->answer_mailbox, msg->job, msg->service_specific_args);
return true;
} else if (auto msg = std::dynamic_pointer_cast<ComputeServiceStandardJobDoneMessage>(message)) {
processStandardJobCompletion(msg->job);
return true;
} else if (auto msg = std::dynamic_pointer_cast<NegotiatorCompletionMessage>(message)) {
processNegotiatorCompletion(msg->scheduled_jobs);
return true;
} else {
throw std::runtime_error("Unexpected [" + message->getName() + "] message");
}
}
/**
* @brief Process a submit standard job request
*
* @param answer_mailbox: the mailbox to which the answer message should be sent
* @param job: the job
* @param service_specific_args: service specific arguments
*
* @throw std::runtime_error
*/
void HTCondorCentralManagerService::processSubmitStandardJob(
const std::string &answer_mailbox, StandardJob *job,
std::map<std::string, std::string> &service_specific_args) {
this->pending_jobs.push_back(job);
this->resources_unavailable = false;
try {
S4U_Mailbox::dputMessage(
answer_mailbox,
new ComputeServiceSubmitStandardJobAnswerMessage(
job, this->getSharedPtr<HTCondorCentralManagerService>(), true, nullptr,
this->getMessagePayloadValue(
HTCondorCentralManagerServiceMessagePayload::SUBMIT_STANDARD_JOB_ANSWER_MESSAGE_PAYLOAD)));
} catch (std::shared_ptr<NetworkError> &cause) {
}
}
/**
* @brief Process a standard job completion
*
* @param job: the job
*
* @throw std::runtime_error
*/
void HTCondorCentralManagerService::processStandardJobCompletion(StandardJob *job) {
WRENCH_INFO("A standard job has completed job %s", job->getName().c_str());
std::string callback_mailbox = job->popCallbackMailbox();
for (auto task : job->getTasks()) {
WRENCH_INFO(" Task completed: %s (%s)", task->getID().c_str(), callback_mailbox.c_str());
}
// Send the callback to the originator
try {
S4U_Mailbox::dputMessage(
callback_mailbox, new ComputeServiceStandardJobDoneMessage(
job, this->getSharedPtr<HTCondorCentralManagerService>(), this->getMessagePayloadValue(
HTCondorCentralManagerServiceMessagePayload::STANDARD_JOB_DONE_MESSAGE_PAYLOAD)));
this->resources_unavailable = false;
} catch (std::shared_ptr<NetworkError> &cause) {
}
auto cs = this->running_jobs.find(job);
auto cs_map = this->compute_resources_map.find(cs->second);
cs_map->second = cs_map->second + job->getMinimumRequiredNumCores();
this->running_jobs.erase(job);
}
/**
* @brief Process a negotiator cycle completion
*
* @param scheduled_jobs: list of scheduled jobs upon negotiator cycle completion
*/
void HTCondorCentralManagerService::processNegotiatorCompletion(
std::vector<wrench::StandardJob *> scheduled_jobs) {
if (scheduled_jobs.empty()) {
this->resources_unavailable = true;
this->dispatching_jobs = false;
return;
}
for (auto sjob : scheduled_jobs) {
for (auto it = this->pending_jobs.begin(); it != this->pending_jobs.end(); ++it) {
auto pjob = *it;
if (sjob == pjob) {
this->pending_jobs.erase(it);
break;
}
}
}
this->dispatching_jobs = false;
}
/**
* @brief Terminate the daemon.
*/
void HTCondorCentralManagerService::terminate() {
this->setStateToDown();
for (auto cs : this->compute_resources) {
cs->stop();
}
this->compute_resources.clear();
this->compute_resources_map.clear();
this->pending_jobs.clear();
this->running_jobs.clear();
}
}