[72eaf7f] | 1 | /*
|
---|
[cd4a6e] | 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2011 University of Bonn. All rights reserved.
|
---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | /*
|
---|
| 9 | * \file FragmentScheduler.cpp
|
---|
| 10 | *
|
---|
| 11 | * This file strongly follows the Serialization example from the boost::asio
|
---|
| 12 | * library (see server.cpp)
|
---|
[72eaf7f] | 13 | *
|
---|
[cd4a6e] | 14 | * Created on: Oct 19, 2011
|
---|
[72eaf7f] | 15 | * Author: heber
|
---|
| 16 | */
|
---|
| 17 |
|
---|
[f93842] | 18 | // include config.h
|
---|
| 19 | #ifdef HAVE_CONFIG_H
|
---|
| 20 | #include <config.h>
|
---|
| 21 | #endif
|
---|
| 22 |
|
---|
[c6bcd0] | 23 | // boost asio needs specific operator new
|
---|
[72eaf7f] | 24 | #include <boost/asio.hpp>
|
---|
[c6bcd0] | 25 |
|
---|
| 26 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 27 |
|
---|
[c4f43e] | 28 | #include <algorithm>
|
---|
[72eaf7f] | 29 | #include <boost/bind.hpp>
|
---|
[9a6b895] | 30 | #include <boost/lambda/lambda.hpp>
|
---|
[72eaf7f] | 31 | #include <boost/lexical_cast.hpp>
|
---|
| 32 | #include <iostream>
|
---|
| 33 | #include <vector>
|
---|
[af3aed] | 34 | #include "Connection.hpp" // Must come before boost/serialization headers.
|
---|
[72eaf7f] | 35 | #include <boost/serialization/vector.hpp>
|
---|
[af3aed] | 36 | #include "CodePatterns/Info.hpp"
|
---|
[b0b64c] | 37 | #include "CodePatterns/Log.hpp"
|
---|
[2344a3] | 38 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
| 39 | #include "ControllerChoices.hpp"
|
---|
[9a6b895] | 40 | #include "Operations/Servers/SendJobToWorkerOperation.hpp"
|
---|
[50d095] | 41 | #include "Operations/Workers/EnrollInPoolOperation.hpp"
|
---|
[ff60cfa] | 42 | #include "Jobs/MPQCCommandJob.hpp"
|
---|
[d920b9] | 43 | #include "Jobs/SystemCommandJob.hpp"
|
---|
[ef2767] | 44 | #include "JobId.hpp"
|
---|
[72eaf7f] | 45 |
|
---|
[cd4a6e] | 46 | #include "FragmentScheduler.hpp"
|
---|
[72eaf7f] | 47 |
|
---|
[ff60cfa] | 48 | /** Helper function to enforce binding of FragmentWorker to possible derived
|
---|
| 49 | * FragmentJob classes.
|
---|
| 50 | */
|
---|
| 51 | void dummyInit() {
|
---|
| 52 | SystemCommandJob("/bin/false", "something", JobId::IllegalJob);
|
---|
| 53 | MPQCCommandJob("nofile", JobId::IllegalJob);
|
---|
| 54 | }
|
---|
[c7deca] | 55 |
|
---|
[db03d9] | 56 | /** Constructor of class FragmentScheduler.
|
---|
| 57 | *
|
---|
| 58 | * We setup both acceptors to accept connections from workers and Controller.
|
---|
| 59 | *
|
---|
| 60 | * \param io_service io_service of the asynchronous communications
|
---|
| 61 | * \param workerport port to listen for worker connections
|
---|
| 62 | * \param controllerport port to listen for controller connections.
|
---|
| 63 | */
|
---|
[2344a3] | 64 | FragmentScheduler::FragmentScheduler(boost::asio::io_service& _io_service, unsigned short workerport, unsigned short controllerport) :
|
---|
| 65 | Observer("FragmentScheduler"),
|
---|
| 66 | io_service(_io_service),
|
---|
| 67 | WorkerListener(_io_service, workerport, JobsQueue, pool,
|
---|
[41c1b7] | 68 | boost::bind(&FragmentScheduler::sendJobToWorker, boost::ref(*this), _1, _2)),
|
---|
[2344a3] | 69 | ControllerListener(_io_service, controllerport, JobsQueue,
|
---|
[b15c4f] | 70 | boost::bind(&FragmentScheduler::removeAllWorkers, boost::ref(*this)),
|
---|
[2344a3] | 71 | boost::bind(&FragmentScheduler::shutdown, boost::ref(*this))),
|
---|
[ba995d] | 72 | connection(_io_service)
|
---|
[ed2c5b] | 73 | {
|
---|
[b0b64c] | 74 | Info info(__FUNCTION__);
|
---|
[72eaf7f] | 75 |
|
---|
[2344a3] | 76 | // sign on to idle workers and present jobs
|
---|
| 77 | pool.signOn(this, WorkerPool::WorkerIdle);
|
---|
| 78 | JobsQueue.signOn(this, FragmentQueue::JobAdded);
|
---|
| 79 |
|
---|
[41c1b7] | 80 | // listen for controller
|
---|
| 81 | ControllerListener.initiateSocket();
|
---|
| 82 |
|
---|
[2344a3] | 83 | // listen for workers
|
---|
| 84 | WorkerListener.initiateSocket();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | FragmentScheduler::~FragmentScheduler()
|
---|
| 88 | {
|
---|
| 89 | // sign off
|
---|
| 90 | pool.signOff(this, WorkerPool::WorkerIdle);
|
---|
| 91 | JobsQueue.signOff(this, FragmentQueue::JobAdded);
|
---|
[402bde] | 92 | }
|
---|
| 93 |
|
---|
[db03d9] | 94 | /** Handle a new worker connection.
|
---|
| 95 | *
|
---|
[41c1b7] | 96 | * We store the given address in the pool.
|
---|
[db03d9] | 97 | *
|
---|
| 98 | * \param e error code if something went wrong
|
---|
| 99 | * \param conn reference with the connection
|
---|
| 100 | */
|
---|
[8036b7] | 101 | void FragmentScheduler::WorkerListener_t::handle_Accept(const boost::system::error_code& e, connection_ptr conn)
|
---|
[ed2c5b] | 102 | {
|
---|
[cd4a6e] | 103 | Info info(__FUNCTION__);
|
---|
[ed2c5b] | 104 | if (!e)
|
---|
[72eaf7f] | 105 | {
|
---|
[b0b64c] | 106 | // Successfully accepted a new connection.
|
---|
[41c1b7] | 107 | // read address
|
---|
| 108 | conn->async_read(address,
|
---|
[9a3f84] | 109 | boost::bind(&FragmentScheduler::WorkerListener_t::handle_ReadAddress, this,
|
---|
[41c1b7] | 110 | boost::asio::placeholders::error, conn));
|
---|
[9a3f84] | 111 | }
|
---|
| 112 | else
|
---|
| 113 | {
|
---|
[41c1b7] | 114 | // An error occurred. Log it and return. Since we are not starting a new
|
---|
| 115 | // accept operation the io_service will run out of work to do and the
|
---|
| 116 | // server will exit.
|
---|
| 117 | Exitflag = ErrorFlag;
|
---|
| 118 | ELOG(0, e.message());
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
[0bdd51b] | 121 |
|
---|
[9a3f84] | 122 | /** Handle having received Worker's address
|
---|
[41c1b7] | 123 | *
|
---|
| 124 | * \param e error code if something went wrong
|
---|
| 125 | * \param conn reference with the connection
|
---|
| 126 | */
|
---|
[9a3f84] | 127 | void FragmentScheduler::WorkerListener_t::handle_ReadAddress(const boost::system::error_code& e, connection_ptr conn)
|
---|
[41c1b7] | 128 | {
|
---|
| 129 | Info info(__FUNCTION__);
|
---|
| 130 | if (!e)
|
---|
| 131 | {
|
---|
[9a3f84] | 132 | // Successfully accepted a new connection.
|
---|
| 133 | // read address
|
---|
| 134 | conn->async_read(choice,
|
---|
| 135 | boost::bind(&FragmentScheduler::WorkerListener_t::handle_ReadChoice, this,
|
---|
| 136 | boost::asio::placeholders::error, conn));
|
---|
| 137 | }
|
---|
| 138 | else
|
---|
| 139 | {
|
---|
| 140 | // An error occurred. Log it and return. Since we are not starting a new
|
---|
| 141 | // accept operation the io_service will run out of work to do and the
|
---|
| 142 | // server will exit.
|
---|
| 143 | Exitflag = ErrorFlag;
|
---|
| 144 | ELOG(0, e.message());
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /** Controller callback function to read the choice for next operation.
|
---|
| 149 | *
|
---|
| 150 | * \param e error code if something went wrong
|
---|
| 151 | * \param conn reference with the connection
|
---|
| 152 | */
|
---|
| 153 | void FragmentScheduler::WorkerListener_t::handle_ReadChoice(const boost::system::error_code& e, connection_ptr conn)
|
---|
| 154 | {
|
---|
| 155 | Info info(__FUNCTION__);
|
---|
| 156 | if (!e)
|
---|
| 157 | {
|
---|
| 158 | LOG(1, "INFO: Received request for operation " << choice << ".");
|
---|
| 159 | // switch over the desired choice read previously
|
---|
| 160 | switch(choice) {
|
---|
| 161 | case NoWorkerOperation:
|
---|
| 162 | {
|
---|
| 163 | ELOG(1, "WorkerListener_t::handle_ReadChoice() - called with NoOperation.");
|
---|
| 164 | break;
|
---|
| 165 | }
|
---|
| 166 | case EnrollInPool:
|
---|
| 167 | {
|
---|
| 168 | if (pool.presentInPool(address)) {
|
---|
| 169 | ELOG(1, "INFO: worker "+toString(address)+" is already contained in pool.");
|
---|
| 170 | enum EnrollInPoolOperation::EnrollFlag flag = EnrollInPoolOperation::Fail;
|
---|
| 171 | conn->async_write(flag,
|
---|
| 172 | boost::bind(&FragmentScheduler::WorkerListener_t::handle_enrolled, this,
|
---|
| 173 | boost::asio::placeholders::error, conn));
|
---|
| 174 | } else {
|
---|
| 175 | // insert as its new worker
|
---|
| 176 | LOG(1, "INFO: Adding " << address << " to pool ...");
|
---|
| 177 | pool.addWorker(address);
|
---|
| 178 | enum EnrollInPoolOperation::EnrollFlag flag = EnrollInPoolOperation::Success;
|
---|
| 179 | conn->async_write(flag,
|
---|
| 180 | boost::bind(&FragmentScheduler::WorkerListener_t::handle_enrolled, this,
|
---|
| 181 | boost::asio::placeholders::error, conn));
|
---|
| 182 | break;
|
---|
| 183 | }
|
---|
| 184 | case SendResult:
|
---|
| 185 | {
|
---|
| 186 | if (pool.presentInPool(address)) {
|
---|
| 187 | // check whether its priority is busy_priority
|
---|
| 188 | if (pool.isWorkerBusy(address)) {
|
---|
| 189 | conn->async_read(result,
|
---|
| 190 | boost::bind(&FragmentScheduler::WorkerListener_t::handle_ReceiveResultFromWorker, this,
|
---|
| 191 | boost::asio::placeholders::error, conn));
|
---|
| 192 | } else {
|
---|
| 193 | ELOG(1, "Worker " << address << " trying to send result who is not marked as busy.");
|
---|
| 194 | conn->async_read(result,
|
---|
| 195 | boost::bind(&FragmentScheduler::WorkerListener_t::handle_RejectResultFromWorker, this,
|
---|
| 196 | boost::asio::placeholders::error, conn));
|
---|
| 197 | }
|
---|
| 198 | } else {
|
---|
| 199 | ELOG(1, "Worker " << address << " trying to send result who is not in pool.");
|
---|
| 200 | conn->async_read(result,
|
---|
| 201 | boost::bind(&FragmentScheduler::WorkerListener_t::handle_RejectResultFromWorker, this,
|
---|
| 202 | boost::asio::placeholders::error, conn));
|
---|
| 203 | }
|
---|
| 204 | break;
|
---|
| 205 | }
|
---|
| 206 | case RemoveFromPool:
|
---|
| 207 | {
|
---|
| 208 | if (pool.presentInPool(address)) {
|
---|
| 209 | // removing present worker
|
---|
| 210 | pool.removeWorker(address);
|
---|
| 211 | } else {
|
---|
| 212 | ELOG(1, "Shutting down Worker " << address << " not contained in pool.");
|
---|
| 213 | }
|
---|
| 214 | break;
|
---|
| 215 | }
|
---|
| 216 | default:
|
---|
| 217 | Exitflag = ErrorFlag;
|
---|
| 218 | ELOG(1, "WorkerListener_t::handle_ReadChoice() - called with no valid choice.");
|
---|
| 219 | break;
|
---|
[41c1b7] | 220 | }
|
---|
[b0b64c] | 221 | }
|
---|
[9a3f84] | 222 | // restore NoOperation choice such that choice is not read twice
|
---|
| 223 | choice = NoWorkerOperation;
|
---|
[2344a3] | 224 |
|
---|
| 225 | initiateSocket();
|
---|
[cd4a6e] | 226 | }
|
---|
| 227 | else
|
---|
| 228 | {
|
---|
| 229 | // An error occurred. Log it and return. Since we are not starting a new
|
---|
| 230 | // accept operation the io_service will run out of work to do and the
|
---|
| 231 | // server will exit.
|
---|
[8036b7] | 232 | Exitflag = ErrorFlag;
|
---|
[b0b64c] | 233 | ELOG(0, e.message());
|
---|
[cd4a6e] | 234 | }
|
---|
[ed2c5b] | 235 | }
|
---|
[72eaf7f] | 236 |
|
---|
[9a3f84] | 237 |
|
---|
[41c1b7] | 238 | /** Callback function when new worker has enrolled.
|
---|
[db03d9] | 239 | *
|
---|
| 240 | * \param e error code if something went wrong
|
---|
| 241 | * \param conn reference with the connection
|
---|
| 242 | */
|
---|
[41c1b7] | 243 | void FragmentScheduler::WorkerListener_t::handle_enrolled(const boost::system::error_code& e, connection_ptr conn)
|
---|
[ed2c5b] | 244 | {
|
---|
[41c1b7] | 245 | Info info(__FUNCTION__);
|
---|
[d9373b] | 246 | if (!e) {
|
---|
| 247 | LOG(2, "DEBUG: Successfully enrolled.");
|
---|
| 248 | LOG(1, "INFO: There are " << pool.getNoTotalWorkers() << " workers in the queue, "
|
---|
| 249 | << pool.getNoIdleWorkers() << " of which are idle.");
|
---|
| 250 | } else {
|
---|
[41c1b7] | 251 | // An error occurred. Log it and return. Since we are not starting a new
|
---|
| 252 | // accept operation the io_service will run out of work to do and the
|
---|
| 253 | // server will exit.
|
---|
| 254 | Exitflag = ErrorFlag;
|
---|
| 255 | ELOG(0, e.message());
|
---|
| 256 | }
|
---|
[ef2767] | 257 | }
|
---|
| 258 |
|
---|
[db03d9] | 259 | /** Callback function when result has been received.
|
---|
| 260 | *
|
---|
| 261 | * \param e error code if something went wrong
|
---|
| 262 | * \param conn reference with the connection
|
---|
| 263 | */
|
---|
[8036b7] | 264 | void FragmentScheduler::WorkerListener_t::handle_ReceiveResultFromWorker(const boost::system::error_code& e, connection_ptr conn)
|
---|
[ef2767] | 265 | {
|
---|
[db03d9] | 266 | Info info(__FUNCTION__);
|
---|
[35f587] | 267 | LOG(1, "INFO: Received result for job #" << result->getId() << " ...");
|
---|
[41c1b7] | 268 |
|
---|
[35f587] | 269 | // and push into queue
|
---|
| 270 | ASSERT(result->getId() != (JobId_t)JobId::NoJob,
|
---|
[41c1b7] | 271 | "WorkerListener_t::handle_ReceiveResultFromWorker() - result received has NoJob id.");
|
---|
[35f587] | 272 | ASSERT(result->getId() != (JobId_t)JobId::IllegalJob,
|
---|
[41c1b7] | 273 | "WorkerListener_t::handle_ReceiveResultFromWorker() - result received has IllegalJob id.");
|
---|
[778abb] | 274 | // place id into expected
|
---|
[35f587] | 275 | if ((result->getId() != (JobId_t)JobId::NoJob) && (result->getId() != (JobId_t)JobId::IllegalJob))
|
---|
[db03d9] | 276 | JobsQueue.pushResult(result);
|
---|
[41c1b7] | 277 |
|
---|
| 278 | // mark as idle
|
---|
| 279 | pool.unmarkWorkerBusy(address);
|
---|
[d9373b] | 280 | LOG(1, "INFO: There are " << pool.getNoTotalWorkers() << " workers in the queue, "
|
---|
| 281 | << pool.getNoIdleWorkers() << " of which are idle.");
|
---|
[41c1b7] | 282 |
|
---|
[db03d9] | 283 | // erase result
|
---|
[35f587] | 284 | result.reset();
|
---|
[778abb] | 285 | LOG(1, "INFO: JobsQueue has " << JobsQueue.getDoneJobs() << " results.");
|
---|
[db03d9] | 286 | }
|
---|
| 287 |
|
---|
[9a3f84] | 288 | /** Callback function when result has been received.
|
---|
| 289 | *
|
---|
| 290 | * \param e error code if something went wrong
|
---|
| 291 | * \param conn reference with the connection
|
---|
| 292 | */
|
---|
| 293 | void FragmentScheduler::WorkerListener_t::handle_RejectResultFromWorker(const boost::system::error_code& e, connection_ptr conn)
|
---|
| 294 | {
|
---|
| 295 | Info info(__FUNCTION__);
|
---|
| 296 | // nothing to do
|
---|
| 297 | LOG(1, "INFO: Rejecting result for job #" << result->getId() << ", placing back into queue.");
|
---|
| 298 |
|
---|
| 299 | JobsQueue.resubmitJob(result->getId());
|
---|
| 300 |
|
---|
| 301 | LOG(1, "INFO: JobsQueue has " << JobsQueue.getDoneJobs() << " results.");
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[41c1b7] | 304 |
|
---|
[db03d9] | 305 | /** Handle a new controller connection.
|
---|
| 306 | *
|
---|
| 307 | * \sa handle_ReceiveJobs()
|
---|
| 308 | * \sa handle_CheckResultState()
|
---|
| 309 | * \sa handle_SendResults()
|
---|
| 310 | *
|
---|
| 311 | * \param e error code if something went wrong
|
---|
| 312 | * \param conn reference with the connection
|
---|
| 313 | */
|
---|
[8036b7] | 314 | void FragmentScheduler::ControllerListener_t::handle_Accept(const boost::system::error_code& e, connection_ptr conn)
|
---|
[db03d9] | 315 | {
|
---|
| 316 | Info info(__FUNCTION__);
|
---|
| 317 | if (!e)
|
---|
| 318 | {
|
---|
[778abb] | 319 | conn->async_read(choice,
|
---|
[8036b7] | 320 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_ReadChoice, this,
|
---|
[778abb] | 321 | boost::asio::placeholders::error, conn));
|
---|
| 322 | }
|
---|
| 323 | else
|
---|
| 324 | {
|
---|
| 325 | // An error occurred. Log it and return. Since we are not starting a new
|
---|
| 326 | // accept operation the io_service will run out of work to do and the
|
---|
| 327 | // server will exit.
|
---|
[8036b7] | 328 | Exitflag = ErrorFlag;
|
---|
[778abb] | 329 | ELOG(0, e.message());
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | /** Controller callback function to read the choice for next operation.
|
---|
| 334 | *
|
---|
| 335 | * \param e error code if something went wrong
|
---|
| 336 | * \param conn reference with the connection
|
---|
| 337 | */
|
---|
[8036b7] | 338 | void FragmentScheduler::ControllerListener_t::handle_ReadChoice(const boost::system::error_code& e, connection_ptr conn)
|
---|
[778abb] | 339 | {
|
---|
| 340 | Info info(__FUNCTION__);
|
---|
| 341 | if (!e)
|
---|
| 342 | {
|
---|
[0196c6] | 343 | bool LaunchNewAcceptor = true;
|
---|
[d1dbfc] | 344 | LOG(1, "INFO: Received request for operation " << choice << ".");
|
---|
[778abb] | 345 | // switch over the desired choice read previously
|
---|
| 346 | switch(choice) {
|
---|
[38032a] | 347 | case NoControllerOperation:
|
---|
[778abb] | 348 | {
|
---|
[9a3f84] | 349 | ELOG(1, "ControllerListener_t::handle_ReadChoice() - called with NoOperation.");
|
---|
[778abb] | 350 | break;
|
---|
| 351 | }
|
---|
[d1dbfc] | 352 | case GetNextJobId:
|
---|
| 353 | {
|
---|
[c4f43e] | 354 | LOG(1, "INFO: Receiving number of desired job ids from controller ...");
|
---|
| 355 | conn->async_read(NumberIds,
|
---|
[8036b7] | 356 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_GetNextJobIdState, this,
|
---|
[d1dbfc] | 357 | boost::asio::placeholders::error, conn));
|
---|
| 358 | break;
|
---|
| 359 | }
|
---|
[425fc6] | 360 | case SendJobs:
|
---|
[d1dbfc] | 361 | {
|
---|
| 362 | // The connection::async_write() function will automatically
|
---|
| 363 | // serialize the data structure for us.
|
---|
| 364 | LOG(1, "INFO: Receiving bunch of jobs from a controller ...");
|
---|
| 365 | conn->async_read(jobs,
|
---|
[8036b7] | 366 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_ReceiveJobs, this,
|
---|
[d1dbfc] | 367 | boost::asio::placeholders::error, conn));
|
---|
| 368 | break;
|
---|
| 369 | }
|
---|
[778abb] | 370 | case CheckState:
|
---|
| 371 | {
|
---|
[3c4a5e] | 372 | // first update number
|
---|
[6f2bc7] | 373 | jobInfo[0] = JobsQueue.getPresentJobs();
|
---|
| 374 | jobInfo[1] = JobsQueue.getDoneJobs();
|
---|
[3c4a5e] | 375 | // now we accept connections to check for state of calculations
|
---|
[6f2bc7] | 376 | LOG(1, "INFO: Sending state that "+toString(jobInfo[0])+" jobs are present and "+toString(jobInfo[1])+" jobs are done to controller ...");
|
---|
| 377 | conn->async_write(jobInfo,
|
---|
[8036b7] | 378 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_CheckResultState, this,
|
---|
[3c4a5e] | 379 | boost::asio::placeholders::error, conn));
|
---|
[778abb] | 380 | break;
|
---|
| 381 | }
|
---|
[b15c4f] | 382 | case RemoveAll:
|
---|
| 383 | {
|
---|
| 384 | removeallWorkers();
|
---|
| 385 | break;
|
---|
| 386 | }
|
---|
[9d14c3] | 387 | case ReceiveResults:
|
---|
[778abb] | 388 | {
|
---|
[35f587] | 389 | const std::vector<FragmentResult::ptr> results = JobsQueue.getAllResults();
|
---|
[778abb] | 390 | // ... or we give the results
|
---|
| 391 | LOG(1, "INFO: Sending "+toString(results.size())+" results to controller ...");
|
---|
| 392 | conn->async_write(results,
|
---|
[8036b7] | 393 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_SendResults, this,
|
---|
[778abb] | 394 | boost::asio::placeholders::error, conn));
|
---|
[0196c6] | 395 | break;
|
---|
| 396 | }
|
---|
[38032a] | 397 | case ShutdownControllerSocket:
|
---|
[0196c6] | 398 | {
|
---|
[9a3f84] | 399 | LOG(1, "INFO: Received shutdown from controller ...");
|
---|
| 400 | // only allow for shutdown when there are no more jobs in the queue
|
---|
| 401 | if (!JobsQueue.isJobPresent()) {
|
---|
[668b55] | 402 | // we shutdown? Hence, also shutdown controller
|
---|
| 403 | LaunchNewAcceptor = !shutdownAllSockets();
|
---|
[9a3f84] | 404 | } else {
|
---|
| 405 | ELOG(2, "There are still jobs waiting in the queue.");
|
---|
| 406 | }
|
---|
[778abb] | 407 | break;
|
---|
[db03d9] | 408 | }
|
---|
[778abb] | 409 | default:
|
---|
[8036b7] | 410 | Exitflag = ErrorFlag;
|
---|
[9a3f84] | 411 | ELOG(1, "ControllerListener_t::handle_ReadChoice() - called with no valid choice.");
|
---|
[778abb] | 412 | break;
|
---|
| 413 | }
|
---|
[38032a] | 414 | // restore NoControllerOperation choice such that choice is not read twice
|
---|
| 415 | choice = NoControllerOperation;
|
---|
[778abb] | 416 |
|
---|
[0196c6] | 417 | if (LaunchNewAcceptor) {
|
---|
| 418 | LOG(1, "Launching new acceptor on socket.");
|
---|
| 419 | // Start an accept operation for a new Connection.
|
---|
[8036b7] | 420 | initiateSocket();
|
---|
[0196c6] | 421 | }
|
---|
[db03d9] | 422 | }
|
---|
| 423 | else
|
---|
| 424 | {
|
---|
| 425 | // An error occurred. Log it and return. Since we are not starting a new
|
---|
| 426 | // accept operation the io_service will run out of work to do and the
|
---|
| 427 | // server will exit.
|
---|
[8036b7] | 428 | Exitflag = ErrorFlag;
|
---|
[db03d9] | 429 | ELOG(0, e.message());
|
---|
| 430 | }
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | /** Controller callback function when job has been sent.
|
---|
[778abb] | 434 | *
|
---|
| 435 | * We check here whether the worker socket is accepting, if there
|
---|
| 436 | * have been no jobs we re-activate it, as it is shut down after
|
---|
| 437 | * last job.
|
---|
[db03d9] | 438 | *
|
---|
| 439 | * \param e error code if something went wrong
|
---|
| 440 | * \param conn reference with the connection
|
---|
| 441 | */
|
---|
[8036b7] | 442 | void FragmentScheduler::ControllerListener_t::handle_ReceiveJobs(const boost::system::error_code& e, connection_ptr conn)
|
---|
[db03d9] | 443 | {
|
---|
| 444 | Info info(__FUNCTION__);
|
---|
| 445 | // jobs are received, hence place in JobsQueue
|
---|
| 446 | if (!jobs.empty()) {
|
---|
| 447 | LOG(1, "INFO: Pushing " << jobs.size() << " jobs into queue.");
|
---|
| 448 | JobsQueue.pushJobs(jobs);
|
---|
| 449 | }
|
---|
| 450 | jobs.clear();
|
---|
[ed2c5b] | 451 | }
|
---|
[cd4a6e] | 452 |
|
---|
[3c4a5e] | 453 | /** Controller callback function when checking on state of results.
|
---|
| 454 | *
|
---|
| 455 | * \param e error code if something went wrong
|
---|
| 456 | * \param conn reference with the connection
|
---|
| 457 | */
|
---|
[8036b7] | 458 | void FragmentScheduler::ControllerListener_t::handle_CheckResultState(const boost::system::error_code& e, connection_ptr conn)
|
---|
[3c4a5e] | 459 | {
|
---|
| 460 | Info info(__FUNCTION__);
|
---|
| 461 | // do nothing
|
---|
[6f2bc7] | 462 | LOG(1, "INFO: Sent that " << jobInfo << " jobs are (scheduled, done).");
|
---|
[3c4a5e] | 463 | }
|
---|
[778abb] | 464 |
|
---|
[d1dbfc] | 465 | /** Controller callback function when checking on state of results.
|
---|
| 466 | *
|
---|
| 467 | * \param e error code if something went wrong
|
---|
| 468 | * \param conn reference with the connection
|
---|
| 469 | */
|
---|
[8036b7] | 470 | void FragmentScheduler::ControllerListener_t::handle_GetNextJobIdState(const boost::system::error_code& e, connection_ptr conn)
|
---|
[c4f43e] | 471 | {
|
---|
| 472 | Info info(__FUNCTION__);
|
---|
| 473 |
|
---|
| 474 | std::vector<JobId_t> nextids( NumberIds, JobId::IllegalJob);
|
---|
| 475 | std::generate(nextids.begin(), nextids.end(),
|
---|
| 476 | boost::bind(&GlobalJobId::getNextId, boost::ref(globalId)));
|
---|
| 477 | LOG(1, "INFO: Sending next available job ids " << nextids << " to controller ...");
|
---|
| 478 | conn->async_write(nextids,
|
---|
| 479 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_SendIds, this,
|
---|
| 480 | boost::asio::placeholders::error, conn));
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | /** Controller callback function when free job ids have been sent.
|
---|
| 484 | *
|
---|
| 485 | * \param e error code if something went wrong
|
---|
| 486 | * \param conn reference with the connection
|
---|
| 487 | */
|
---|
| 488 | void FragmentScheduler::ControllerListener_t::handle_SendIds(const boost::system::error_code& e, connection_ptr conn)
|
---|
[d1dbfc] | 489 | {
|
---|
| 490 | Info info(__FUNCTION__);
|
---|
| 491 | // do nothing
|
---|
[c4f43e] | 492 | LOG(1, "INFO: Ids have been sent.");
|
---|
[d1dbfc] | 493 | }
|
---|
| 494 |
|
---|
[778abb] | 495 | /** Controller callback function when result has been received.
|
---|
| 496 | *
|
---|
| 497 | * \param e error code if something went wrong
|
---|
| 498 | * \param conn reference with the connection
|
---|
| 499 | */
|
---|
[8036b7] | 500 | void FragmentScheduler::ControllerListener_t::handle_SendResults(const boost::system::error_code& e, connection_ptr conn)
|
---|
[778abb] | 501 | {
|
---|
| 502 | Info info(__FUNCTION__);
|
---|
| 503 | // do nothing
|
---|
| 504 | LOG(1, "INFO: Results have been sent.");
|
---|
| 505 | }
|
---|
| 506 |
|
---|
[41c1b7] | 507 |
|
---|
| 508 | /** Helper function to send a job to worker.
|
---|
[9a3f84] | 509 | *
|
---|
| 510 | * Note that we do not set the worker as busy. We simply send it the job.
|
---|
[41c1b7] | 511 | *
|
---|
| 512 | * @param address address of worker
|
---|
| 513 | * @param job job to send
|
---|
| 514 | */
|
---|
| 515 | void FragmentScheduler::sendJobToWorker(const WorkerAddress &address, FragmentJob::ptr &job)
|
---|
| 516 | {
|
---|
[9a3f84] | 517 | ASSERT( pool.isWorkerBusy(address),
|
---|
| 518 | "FragmentScheduler::sendJobToWorker() - Worker "+toString(address)+" is not marked as busy.");
|
---|
[41c1b7] | 519 | LOG(1, "INFO: Sending job " << job->getId() << " to worker " << address << ".");
|
---|
[9a6b895] | 520 |
|
---|
| 521 | // create op, sign on, and hand over to queue
|
---|
| 522 | AsyncOperation *sendJobOp = new SendJobToWorkerOperation(connection,job);
|
---|
| 523 | OpQueue.push_back(sendJobOp, address);
|
---|
[41c1b7] | 524 | }
|
---|
| 525 |
|
---|
[2344a3] | 526 | /** Helper function to shutdown a single worker.
|
---|
| 527 | *
|
---|
| 528 | * We send NoJob to indicate shutdown
|
---|
| 529 | *
|
---|
| 530 | * @param address of worker to shutdown
|
---|
| 531 | */
|
---|
| 532 | void FragmentScheduler::shutdownWorker(const WorkerAddress &address)
|
---|
| 533 | {
|
---|
[6ea7f4] | 534 | ASSERT( !pool.isWorkerBusy(address),
|
---|
| 535 | "FragmentScheduler::sendJobToWorker() - Worker "+toString(address)+" is already busy.");
|
---|
| 536 | LOG(2, "INFO: Shutting down worker " << address << "...");
|
---|
[ba995d] | 537 | AsyncOperation *shutdownWorkerOp = new ShutdownWorkerOperation(connection);
|
---|
| 538 | OpQueue.push_back(shutdownWorkerOp, address);
|
---|
[2344a3] | 539 | }
|
---|
| 540 |
|
---|
| 541 | /** Sends shutdown to all current workers in the pool.
|
---|
| 542 | *
|
---|
| 543 | */
|
---|
| 544 | void FragmentScheduler::removeAllWorkers()
|
---|
| 545 | {
|
---|
[6b3a37] | 546 | // first, sign off such that no new jobs are given to workers
|
---|
| 547 | pool.signOff(this, WorkerPool::WorkerIdle);
|
---|
[befcf8] | 548 |
|
---|
| 549 | LOG(2, "DEBUG: Waiting for busy workers to finish ...");
|
---|
[6b3a37] | 550 | while (pool.hasBusyWorkers())
|
---|
| 551 | ;
|
---|
| 552 |
|
---|
[befcf8] | 553 | LOG(2, "INFO: Shutting down workers ...");
|
---|
| 554 | // iterate until there are no more idle workers
|
---|
[270364] | 555 | // get list of all idle workers
|
---|
| 556 | typedef std::vector<std::pair<std::string, std::string> > WorkerList_t;
|
---|
| 557 | WorkerList_t WorkerList = pool.getListOfIdleWorkers();
|
---|
| 558 |
|
---|
| 559 | // give all workers shutdown signal
|
---|
| 560 | for (WorkerList_t::const_iterator iter = WorkerList.begin(); iter != WorkerList.end(); ++iter)
|
---|
| 561 | shutdownWorker(WorkerAddress(iter->first, iter->second));
|
---|
[2344a3] | 562 | }
|
---|
| 563 |
|
---|
[267b8d] | 564 | /** Function to shutdown server properly, e.g. for use as signal handler.
|
---|
| 565 | *
|
---|
| 566 | * @param sig signal number
|
---|
| 567 | */
|
---|
| 568 | void FragmentScheduler::shutdown(int sig)
|
---|
| 569 | {
|
---|
| 570 | LOG(0, "STATUS: Shutting down due to signal " << sig << ".");
|
---|
| 571 |
|
---|
| 572 | if (!pool.presentIdleWorkers() && !pool.hasBusyWorkers()) {
|
---|
| 573 | shutdown();
|
---|
| 574 | } else {
|
---|
| 575 | removeAllWorkers();
|
---|
| 576 | }
|
---|
| 577 | }
|
---|
| 578 |
|
---|
[2344a3] | 579 | /** Helper function to shutdown the server properly.
|
---|
| 580 | *
|
---|
| 581 | * \todo one should idle here until all workers have returned from
|
---|
[b15c4f] | 582 | * calculating stuff (or workers need to still listen while they are
|
---|
[2344a3] | 583 | * calculating which is probably better).
|
---|
| 584 | *
|
---|
[668b55] | 585 | * \note We only shutdown when there are no workers left
|
---|
| 586 | *
|
---|
| 587 | * @return true - doing shutdown, false - precondition not met, not shutting down
|
---|
[2344a3] | 588 | */
|
---|
[668b55] | 589 | bool FragmentScheduler::shutdown()
|
---|
[2344a3] | 590 | {
|
---|
[668b55] | 591 | if (!pool.presentIdleWorkers() && !pool.hasBusyWorkers()) {
|
---|
| 592 | LOG(1, "INFO: Shutting all down ...");
|
---|
[2344a3] | 593 |
|
---|
[668b55] | 594 | /// close the worker listener's socket
|
---|
| 595 | WorkerListener.closeSocket();
|
---|
[2344a3] | 596 |
|
---|
[668b55] | 597 | /// close the controller listener's socket
|
---|
| 598 | ControllerListener.closeSocket();
|
---|
[2344a3] | 599 |
|
---|
[668b55] | 600 | /// finally, stop the io_service
|
---|
| 601 | io_service.stop();
|
---|
| 602 | return true;
|
---|
| 603 | } else {
|
---|
| 604 | ELOG(2, "There are still idle or busy workers present.");
|
---|
| 605 | return false;
|
---|
| 606 | }
|
---|
[2344a3] | 607 | }
|
---|
| 608 |
|
---|
| 609 | /** Internal helper to send the next available job to the next idle worker.
|
---|
| 610 | *
|
---|
| 611 | */
|
---|
| 612 | void FragmentScheduler::sendAvailableJobToNextIdleWorker()
|
---|
| 613 | {
|
---|
| 614 | const WorkerAddress address = pool.getNextIdleWorker();
|
---|
| 615 | FragmentJob::ptr job = JobsQueue.popJob();
|
---|
| 616 | sendJobToWorker(address, job);
|
---|
| 617 | }
|
---|
| 618 |
|
---|
| 619 | void FragmentScheduler::update(Observable *publisher)
|
---|
| 620 | {
|
---|
[a40c85] | 621 | ASSERT(0, "FragmentScheduler::update() - we are not signed on for global updates.");
|
---|
[2344a3] | 622 | }
|
---|
| 623 |
|
---|
| 624 | void FragmentScheduler::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
| 625 | {
|
---|
| 626 | if ((publisher == &pool) && (notification->getChannelNo() == WorkerPool::WorkerIdle)) {
|
---|
[e032b4] | 627 | // we have an idle worker
|
---|
[2344a3] | 628 | LOG(1, "INFO: We are notified of an idle worker.");
|
---|
| 629 | // are jobs available?
|
---|
| 630 | if (JobsQueue.isJobPresent()) {
|
---|
| 631 | sendAvailableJobToNextIdleWorker();
|
---|
| 632 | }
|
---|
[e032b4] | 633 | } else if ((publisher == &JobsQueue) && (notification->getChannelNo() == FragmentQueue::JobAdded)) {
|
---|
| 634 | // we have new jobs
|
---|
[2344a3] | 635 | LOG(1, "INFO: We are notified of a new job.");
|
---|
| 636 | // check for idle workers
|
---|
| 637 | if (pool.presentIdleWorkers()) {
|
---|
| 638 | sendAvailableJobToNextIdleWorker();
|
---|
| 639 | }
|
---|
[e032b4] | 640 | } else {
|
---|
| 641 | ASSERT(0, "FragmentScheduler::recieveNotification() - we are not signed on for updates in channel "
|
---|
| 642 | +toString(notification->getChannelNo())+".");
|
---|
[2344a3] | 643 | }
|
---|
| 644 | }
|
---|
| 645 |
|
---|
| 646 | void FragmentScheduler::subjectKilled(Observable *publisher)
|
---|
| 647 | {}
|
---|