| 1 | /*
|
|---|
| 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)
|
|---|
| 13 | *
|
|---|
| 14 | * Created on: Oct 19, 2011
|
|---|
| 15 | * Author: heber
|
|---|
| 16 | */
|
|---|
| 17 |
|
|---|
| 18 | // include config.h
|
|---|
| 19 | #ifdef HAVE_CONFIG_H
|
|---|
| 20 | #include <config.h>
|
|---|
| 21 | #endif
|
|---|
| 22 |
|
|---|
| 23 | // boost asio needs specific operator new
|
|---|
| 24 | #include <boost/asio.hpp>
|
|---|
| 25 |
|
|---|
| 26 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 27 |
|
|---|
| 28 | #include <algorithm>
|
|---|
| 29 | #include <boost/bind.hpp>
|
|---|
| 30 | #include <boost/lambda/lambda.hpp>
|
|---|
| 31 | #include <boost/lexical_cast.hpp>
|
|---|
| 32 | #include <iostream>
|
|---|
| 33 | #include <vector>
|
|---|
| 34 | #include "Connection.hpp" // Must come before boost/serialization headers.
|
|---|
| 35 | #include <boost/serialization/vector.hpp>
|
|---|
| 36 | #include "CodePatterns/Info.hpp"
|
|---|
| 37 | #include "CodePatterns/Log.hpp"
|
|---|
| 38 | #include "CodePatterns/Observer/Notification.hpp"
|
|---|
| 39 | #include "ControllerChoices.hpp"
|
|---|
| 40 | #include "Operations/Servers/SendJobToWorkerOperation.hpp"
|
|---|
| 41 | #include "Operations/Workers/EnrollInPoolOperation.hpp"
|
|---|
| 42 | #include "Jobs/MPQCCommandJob.hpp"
|
|---|
| 43 | #include "Jobs/SystemCommandJob.hpp"
|
|---|
| 44 | #include "JobId.hpp"
|
|---|
| 45 |
|
|---|
| 46 | #include "FragmentScheduler.hpp"
|
|---|
| 47 |
|
|---|
| 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 | }
|
|---|
| 55 |
|
|---|
| 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 | */
|
|---|
| 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,
|
|---|
| 68 | boost::bind(&FragmentScheduler::sendJobToWorker, boost::ref(*this), _1, _2)),
|
|---|
| 69 | ControllerListener(_io_service, controllerport, JobsQueue,
|
|---|
| 70 | boost::bind(&FragmentScheduler::removeAllWorkers, boost::ref(*this)),
|
|---|
| 71 | boost::bind(&FragmentScheduler::shutdown, boost::ref(*this))),
|
|---|
| 72 | connection(_io_service)
|
|---|
| 73 | {
|
|---|
| 74 | Info info(__FUNCTION__);
|
|---|
| 75 |
|
|---|
| 76 | // sign on to idle workers and present jobs
|
|---|
| 77 | pool.signOn(this, WorkerPool::WorkerIdle);
|
|---|
| 78 | JobsQueue.signOn(this, FragmentQueue::JobAdded);
|
|---|
| 79 |
|
|---|
| 80 | // listen for controller
|
|---|
| 81 | ControllerListener.initiateSocket();
|
|---|
| 82 |
|
|---|
| 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);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /** Handle a new worker connection.
|
|---|
| 95 | *
|
|---|
| 96 | * We store the given address in the pool.
|
|---|
| 97 | *
|
|---|
| 98 | * \param e error code if something went wrong
|
|---|
| 99 | * \param conn reference with the connection
|
|---|
| 100 | */
|
|---|
| 101 | void FragmentScheduler::WorkerListener_t::handle_Accept(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 102 | {
|
|---|
| 103 | Info info(__FUNCTION__);
|
|---|
| 104 | if (!e)
|
|---|
| 105 | {
|
|---|
| 106 | // Successfully accepted a new connection.
|
|---|
| 107 | // read address
|
|---|
| 108 | conn->async_read(address,
|
|---|
| 109 | boost::bind(&FragmentScheduler::WorkerListener_t::handle_ReadAddress, this,
|
|---|
| 110 | boost::asio::placeholders::error, conn));
|
|---|
| 111 | }
|
|---|
| 112 | else
|
|---|
| 113 | {
|
|---|
| 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 | }
|
|---|
| 121 |
|
|---|
| 122 | /** Handle having received Worker's address
|
|---|
| 123 | *
|
|---|
| 124 | * \param e error code if something went wrong
|
|---|
| 125 | * \param conn reference with the connection
|
|---|
| 126 | */
|
|---|
| 127 | void FragmentScheduler::WorkerListener_t::handle_ReadAddress(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 128 | {
|
|---|
| 129 | Info info(__FUNCTION__);
|
|---|
| 130 | if (!e)
|
|---|
| 131 | {
|
|---|
| 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;
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 | // restore NoOperation choice such that choice is not read twice
|
|---|
| 223 | choice = NoWorkerOperation;
|
|---|
| 224 |
|
|---|
| 225 | initiateSocket();
|
|---|
| 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.
|
|---|
| 232 | Exitflag = ErrorFlag;
|
|---|
| 233 | ELOG(0, e.message());
|
|---|
| 234 | }
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 | /** Callback function when new worker has enrolled.
|
|---|
| 239 | *
|
|---|
| 240 | * \param e error code if something went wrong
|
|---|
| 241 | * \param conn reference with the connection
|
|---|
| 242 | */
|
|---|
| 243 | void FragmentScheduler::WorkerListener_t::handle_enrolled(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 244 | {
|
|---|
| 245 | Info info(__FUNCTION__);
|
|---|
| 246 | if (e)
|
|---|
| 247 | {
|
|---|
| 248 | // An error occurred. Log it and return. Since we are not starting a new
|
|---|
| 249 | // accept operation the io_service will run out of work to do and the
|
|---|
| 250 | // server will exit.
|
|---|
| 251 | Exitflag = ErrorFlag;
|
|---|
| 252 | ELOG(0, e.message());
|
|---|
| 253 | }
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | /** Callback function when result has been received.
|
|---|
| 257 | *
|
|---|
| 258 | * \param e error code if something went wrong
|
|---|
| 259 | * \param conn reference with the connection
|
|---|
| 260 | */
|
|---|
| 261 | void FragmentScheduler::WorkerListener_t::handle_ReceiveResultFromWorker(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 262 | {
|
|---|
| 263 | Info info(__FUNCTION__);
|
|---|
| 264 | LOG(1, "INFO: Received result for job #" << result->getId() << " ...");
|
|---|
| 265 |
|
|---|
| 266 | // and push into queue
|
|---|
| 267 | ASSERT(result->getId() != (JobId_t)JobId::NoJob,
|
|---|
| 268 | "WorkerListener_t::handle_ReceiveResultFromWorker() - result received has NoJob id.");
|
|---|
| 269 | ASSERT(result->getId() != (JobId_t)JobId::IllegalJob,
|
|---|
| 270 | "WorkerListener_t::handle_ReceiveResultFromWorker() - result received has IllegalJob id.");
|
|---|
| 271 | // place id into expected
|
|---|
| 272 | if ((result->getId() != (JobId_t)JobId::NoJob) && (result->getId() != (JobId_t)JobId::IllegalJob))
|
|---|
| 273 | JobsQueue.pushResult(result);
|
|---|
| 274 |
|
|---|
| 275 | // mark as idle
|
|---|
| 276 | pool.unmarkWorkerBusy(address);
|
|---|
| 277 |
|
|---|
| 278 | // erase result
|
|---|
| 279 | result.reset();
|
|---|
| 280 | LOG(1, "INFO: JobsQueue has " << JobsQueue.getDoneJobs() << " results.");
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | /** Callback function when result has been received.
|
|---|
| 284 | *
|
|---|
| 285 | * \param e error code if something went wrong
|
|---|
| 286 | * \param conn reference with the connection
|
|---|
| 287 | */
|
|---|
| 288 | void FragmentScheduler::WorkerListener_t::handle_RejectResultFromWorker(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 289 | {
|
|---|
| 290 | Info info(__FUNCTION__);
|
|---|
| 291 | // nothing to do
|
|---|
| 292 | LOG(1, "INFO: Rejecting result for job #" << result->getId() << ", placing back into queue.");
|
|---|
| 293 |
|
|---|
| 294 | JobsQueue.resubmitJob(result->getId());
|
|---|
| 295 |
|
|---|
| 296 | LOG(1, "INFO: JobsQueue has " << JobsQueue.getDoneJobs() << " results.");
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 | /** Handle a new controller connection.
|
|---|
| 301 | *
|
|---|
| 302 | * \sa handle_ReceiveJobs()
|
|---|
| 303 | * \sa handle_CheckResultState()
|
|---|
| 304 | * \sa handle_SendResults()
|
|---|
| 305 | *
|
|---|
| 306 | * \param e error code if something went wrong
|
|---|
| 307 | * \param conn reference with the connection
|
|---|
| 308 | */
|
|---|
| 309 | void FragmentScheduler::ControllerListener_t::handle_Accept(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 310 | {
|
|---|
| 311 | Info info(__FUNCTION__);
|
|---|
| 312 | if (!e)
|
|---|
| 313 | {
|
|---|
| 314 | conn->async_read(choice,
|
|---|
| 315 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_ReadChoice, this,
|
|---|
| 316 | boost::asio::placeholders::error, conn));
|
|---|
| 317 | }
|
|---|
| 318 | else
|
|---|
| 319 | {
|
|---|
| 320 | // An error occurred. Log it and return. Since we are not starting a new
|
|---|
| 321 | // accept operation the io_service will run out of work to do and the
|
|---|
| 322 | // server will exit.
|
|---|
| 323 | Exitflag = ErrorFlag;
|
|---|
| 324 | ELOG(0, e.message());
|
|---|
| 325 | }
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | /** Controller callback function to read the choice for next operation.
|
|---|
| 329 | *
|
|---|
| 330 | * \param e error code if something went wrong
|
|---|
| 331 | * \param conn reference with the connection
|
|---|
| 332 | */
|
|---|
| 333 | void FragmentScheduler::ControllerListener_t::handle_ReadChoice(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 334 | {
|
|---|
| 335 | Info info(__FUNCTION__);
|
|---|
| 336 | if (!e)
|
|---|
| 337 | {
|
|---|
| 338 | bool LaunchNewAcceptor = true;
|
|---|
| 339 | LOG(1, "INFO: Received request for operation " << choice << ".");
|
|---|
| 340 | // switch over the desired choice read previously
|
|---|
| 341 | switch(choice) {
|
|---|
| 342 | case NoControllerOperation:
|
|---|
| 343 | {
|
|---|
| 344 | ELOG(1, "ControllerListener_t::handle_ReadChoice() - called with NoOperation.");
|
|---|
| 345 | break;
|
|---|
| 346 | }
|
|---|
| 347 | case GetNextJobId:
|
|---|
| 348 | {
|
|---|
| 349 | LOG(1, "INFO: Receiving number of desired job ids from controller ...");
|
|---|
| 350 | conn->async_read(NumberIds,
|
|---|
| 351 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_GetNextJobIdState, this,
|
|---|
| 352 | boost::asio::placeholders::error, conn));
|
|---|
| 353 | break;
|
|---|
| 354 | }
|
|---|
| 355 | case SendJobs:
|
|---|
| 356 | {
|
|---|
| 357 | // The connection::async_write() function will automatically
|
|---|
| 358 | // serialize the data structure for us.
|
|---|
| 359 | LOG(1, "INFO: Receiving bunch of jobs from a controller ...");
|
|---|
| 360 | conn->async_read(jobs,
|
|---|
| 361 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_ReceiveJobs, this,
|
|---|
| 362 | boost::asio::placeholders::error, conn));
|
|---|
| 363 | break;
|
|---|
| 364 | }
|
|---|
| 365 | case CheckState:
|
|---|
| 366 | {
|
|---|
| 367 | // first update number
|
|---|
| 368 | jobInfo[0] = JobsQueue.getPresentJobs();
|
|---|
| 369 | jobInfo[1] = JobsQueue.getDoneJobs();
|
|---|
| 370 | // now we accept connections to check for state of calculations
|
|---|
| 371 | LOG(1, "INFO: Sending state that "+toString(jobInfo[0])+" jobs are present and "+toString(jobInfo[1])+" jobs are done to controller ...");
|
|---|
| 372 | conn->async_write(jobInfo,
|
|---|
| 373 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_CheckResultState, this,
|
|---|
| 374 | boost::asio::placeholders::error, conn));
|
|---|
| 375 | break;
|
|---|
| 376 | }
|
|---|
| 377 | case RemoveAll:
|
|---|
| 378 | {
|
|---|
| 379 | removeallWorkers();
|
|---|
| 380 | break;
|
|---|
| 381 | }
|
|---|
| 382 | case ReceiveResults:
|
|---|
| 383 | {
|
|---|
| 384 | const std::vector<FragmentResult::ptr> results = JobsQueue.getAllResults();
|
|---|
| 385 | // ... or we give the results
|
|---|
| 386 | LOG(1, "INFO: Sending "+toString(results.size())+" results to controller ...");
|
|---|
| 387 | conn->async_write(results,
|
|---|
| 388 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_SendResults, this,
|
|---|
| 389 | boost::asio::placeholders::error, conn));
|
|---|
| 390 | break;
|
|---|
| 391 | }
|
|---|
| 392 | case ShutdownControllerSocket:
|
|---|
| 393 | {
|
|---|
| 394 | LOG(1, "INFO: Received shutdown from controller ...");
|
|---|
| 395 | // only allow for shutdown when there are no more jobs in the queue
|
|---|
| 396 | if (!JobsQueue.isJobPresent()) {
|
|---|
| 397 | // we shutdown? Hence, also shutdown controller
|
|---|
| 398 | LaunchNewAcceptor = !shutdownAllSockets();
|
|---|
| 399 | } else {
|
|---|
| 400 | ELOG(2, "There are still jobs waiting in the queue.");
|
|---|
| 401 | }
|
|---|
| 402 | break;
|
|---|
| 403 | }
|
|---|
| 404 | default:
|
|---|
| 405 | Exitflag = ErrorFlag;
|
|---|
| 406 | ELOG(1, "ControllerListener_t::handle_ReadChoice() - called with no valid choice.");
|
|---|
| 407 | break;
|
|---|
| 408 | }
|
|---|
| 409 | // restore NoControllerOperation choice such that choice is not read twice
|
|---|
| 410 | choice = NoControllerOperation;
|
|---|
| 411 |
|
|---|
| 412 | if (LaunchNewAcceptor) {
|
|---|
| 413 | LOG(1, "Launching new acceptor on socket.");
|
|---|
| 414 | // Start an accept operation for a new Connection.
|
|---|
| 415 | initiateSocket();
|
|---|
| 416 | }
|
|---|
| 417 | }
|
|---|
| 418 | else
|
|---|
| 419 | {
|
|---|
| 420 | // An error occurred. Log it and return. Since we are not starting a new
|
|---|
| 421 | // accept operation the io_service will run out of work to do and the
|
|---|
| 422 | // server will exit.
|
|---|
| 423 | Exitflag = ErrorFlag;
|
|---|
| 424 | ELOG(0, e.message());
|
|---|
| 425 | }
|
|---|
| 426 | }
|
|---|
| 427 |
|
|---|
| 428 | /** Controller callback function when job has been sent.
|
|---|
| 429 | *
|
|---|
| 430 | * We check here whether the worker socket is accepting, if there
|
|---|
| 431 | * have been no jobs we re-activate it, as it is shut down after
|
|---|
| 432 | * last job.
|
|---|
| 433 | *
|
|---|
| 434 | * \param e error code if something went wrong
|
|---|
| 435 | * \param conn reference with the connection
|
|---|
| 436 | */
|
|---|
| 437 | void FragmentScheduler::ControllerListener_t::handle_ReceiveJobs(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 438 | {
|
|---|
| 439 | Info info(__FUNCTION__);
|
|---|
| 440 | // jobs are received, hence place in JobsQueue
|
|---|
| 441 | if (!jobs.empty()) {
|
|---|
| 442 | LOG(1, "INFO: Pushing " << jobs.size() << " jobs into queue.");
|
|---|
| 443 | JobsQueue.pushJobs(jobs);
|
|---|
| 444 | }
|
|---|
| 445 | jobs.clear();
|
|---|
| 446 | }
|
|---|
| 447 |
|
|---|
| 448 | /** Controller callback function when checking on state of results.
|
|---|
| 449 | *
|
|---|
| 450 | * \param e error code if something went wrong
|
|---|
| 451 | * \param conn reference with the connection
|
|---|
| 452 | */
|
|---|
| 453 | void FragmentScheduler::ControllerListener_t::handle_CheckResultState(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 454 | {
|
|---|
| 455 | Info info(__FUNCTION__);
|
|---|
| 456 | // do nothing
|
|---|
| 457 | LOG(1, "INFO: Sent that " << jobInfo << " jobs are (scheduled, done).");
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 | /** Controller callback function when checking on state of results.
|
|---|
| 461 | *
|
|---|
| 462 | * \param e error code if something went wrong
|
|---|
| 463 | * \param conn reference with the connection
|
|---|
| 464 | */
|
|---|
| 465 | void FragmentScheduler::ControllerListener_t::handle_GetNextJobIdState(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 466 | {
|
|---|
| 467 | Info info(__FUNCTION__);
|
|---|
| 468 |
|
|---|
| 469 | std::vector<JobId_t> nextids( NumberIds, JobId::IllegalJob);
|
|---|
| 470 | std::generate(nextids.begin(), nextids.end(),
|
|---|
| 471 | boost::bind(&GlobalJobId::getNextId, boost::ref(globalId)));
|
|---|
| 472 | LOG(1, "INFO: Sending next available job ids " << nextids << " to controller ...");
|
|---|
| 473 | conn->async_write(nextids,
|
|---|
| 474 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_SendIds, this,
|
|---|
| 475 | boost::asio::placeholders::error, conn));
|
|---|
| 476 | }
|
|---|
| 477 |
|
|---|
| 478 | /** Controller callback function when free job ids have been sent.
|
|---|
| 479 | *
|
|---|
| 480 | * \param e error code if something went wrong
|
|---|
| 481 | * \param conn reference with the connection
|
|---|
| 482 | */
|
|---|
| 483 | void FragmentScheduler::ControllerListener_t::handle_SendIds(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 484 | {
|
|---|
| 485 | Info info(__FUNCTION__);
|
|---|
| 486 | // do nothing
|
|---|
| 487 | LOG(1, "INFO: Ids have been sent.");
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | /** Controller callback function when result has been received.
|
|---|
| 491 | *
|
|---|
| 492 | * \param e error code if something went wrong
|
|---|
| 493 | * \param conn reference with the connection
|
|---|
| 494 | */
|
|---|
| 495 | void FragmentScheduler::ControllerListener_t::handle_SendResults(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 496 | {
|
|---|
| 497 | Info info(__FUNCTION__);
|
|---|
| 498 | // do nothing
|
|---|
| 499 | LOG(1, "INFO: Results have been sent.");
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 | /** Helper function to send a job to worker.
|
|---|
| 504 | *
|
|---|
| 505 | * Note that we do not set the worker as busy. We simply send it the job.
|
|---|
| 506 | *
|
|---|
| 507 | * @param address address of worker
|
|---|
| 508 | * @param job job to send
|
|---|
| 509 | */
|
|---|
| 510 | void FragmentScheduler::sendJobToWorker(const WorkerAddress &address, FragmentJob::ptr &job)
|
|---|
| 511 | {
|
|---|
| 512 | ASSERT( pool.isWorkerBusy(address),
|
|---|
| 513 | "FragmentScheduler::sendJobToWorker() - Worker "+toString(address)+" is not marked as busy.");
|
|---|
| 514 | LOG(1, "INFO: Sending job " << job->getId() << " to worker " << address << ".");
|
|---|
| 515 |
|
|---|
| 516 | // create op, sign on, and hand over to queue
|
|---|
| 517 | AsyncOperation *sendJobOp = new SendJobToWorkerOperation(connection,job);
|
|---|
| 518 | OpQueue.push_back(sendJobOp, address);
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | /** Helper function to shutdown a single worker.
|
|---|
| 522 | *
|
|---|
| 523 | * We send NoJob to indicate shutdown
|
|---|
| 524 | *
|
|---|
| 525 | * @param address of worker to shutdown
|
|---|
| 526 | */
|
|---|
| 527 | void FragmentScheduler::shutdownWorker(const WorkerAddress &address)
|
|---|
| 528 | {
|
|---|
| 529 | ASSERT( !pool.isWorkerBusy(address),
|
|---|
| 530 | "FragmentScheduler::sendJobToWorker() - Worker "+toString(address)+" is already busy.");
|
|---|
| 531 | LOG(2, "INFO: Shutting down worker " << address << "...");
|
|---|
| 532 | AsyncOperation *shutdownWorkerOp = new ShutdownWorkerOperation(connection);
|
|---|
| 533 | OpQueue.push_back(shutdownWorkerOp, address);
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | /** Sends shutdown to all current workers in the pool.
|
|---|
| 537 | *
|
|---|
| 538 | */
|
|---|
| 539 | void FragmentScheduler::removeAllWorkers()
|
|---|
| 540 | {
|
|---|
| 541 | // first, sign off such that no new jobs are given to workers
|
|---|
| 542 | pool.signOff(this, WorkerPool::WorkerIdle);
|
|---|
| 543 |
|
|---|
| 544 | LOG(2, "DEBUG: Waiting for busy workers to finish ...");
|
|---|
| 545 | while (pool.hasBusyWorkers())
|
|---|
| 546 | ;
|
|---|
| 547 |
|
|---|
| 548 | LOG(2, "INFO: Shutting down workers ...");
|
|---|
| 549 | // iterate until there are no more idle workers
|
|---|
| 550 | // get list of all idle workers
|
|---|
| 551 | typedef std::vector<std::pair<std::string, std::string> > WorkerList_t;
|
|---|
| 552 | WorkerList_t WorkerList = pool.getListOfIdleWorkers();
|
|---|
| 553 |
|
|---|
| 554 | // give all workers shutdown signal
|
|---|
| 555 | for (WorkerList_t::const_iterator iter = WorkerList.begin(); iter != WorkerList.end(); ++iter)
|
|---|
| 556 | shutdownWorker(WorkerAddress(iter->first, iter->second));
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 | /** Helper function to shutdown the server properly.
|
|---|
| 560 | *
|
|---|
| 561 | * \todo one should idle here until all workers have returned from
|
|---|
| 562 | * calculating stuff (or workers need to still listen while they are
|
|---|
| 563 | * calculating which is probably better).
|
|---|
| 564 | *
|
|---|
| 565 | * \note We only shutdown when there are no workers left
|
|---|
| 566 | *
|
|---|
| 567 | * @return true - doing shutdown, false - precondition not met, not shutting down
|
|---|
| 568 | */
|
|---|
| 569 | bool FragmentScheduler::shutdown()
|
|---|
| 570 | {
|
|---|
| 571 | if (!pool.presentIdleWorkers() && !pool.hasBusyWorkers()) {
|
|---|
| 572 | LOG(1, "INFO: Shutting all down ...");
|
|---|
| 573 |
|
|---|
| 574 | /// close the worker listener's socket
|
|---|
| 575 | WorkerListener.closeSocket();
|
|---|
| 576 |
|
|---|
| 577 | /// close the controller listener's socket
|
|---|
| 578 | ControllerListener.closeSocket();
|
|---|
| 579 |
|
|---|
| 580 | /// finally, stop the io_service
|
|---|
| 581 | io_service.stop();
|
|---|
| 582 | return true;
|
|---|
| 583 | } else {
|
|---|
| 584 | ELOG(2, "There are still idle or busy workers present.");
|
|---|
| 585 | return false;
|
|---|
| 586 | }
|
|---|
| 587 | }
|
|---|
| 588 |
|
|---|
| 589 | /** Internal helper to send the next available job to the next idle worker.
|
|---|
| 590 | *
|
|---|
| 591 | */
|
|---|
| 592 | void FragmentScheduler::sendAvailableJobToNextIdleWorker()
|
|---|
| 593 | {
|
|---|
| 594 | const WorkerAddress address = pool.getNextIdleWorker();
|
|---|
| 595 | FragmentJob::ptr job = JobsQueue.popJob();
|
|---|
| 596 | sendJobToWorker(address, job);
|
|---|
| 597 | }
|
|---|
| 598 |
|
|---|
| 599 | void FragmentScheduler::update(Observable *publisher)
|
|---|
| 600 | {
|
|---|
| 601 | ASSERT(0, "FragmentScheduler::update() - we are not signed on for global updates.");
|
|---|
| 602 | }
|
|---|
| 603 |
|
|---|
| 604 | void FragmentScheduler::recieveNotification(Observable *publisher, Notification_ptr notification)
|
|---|
| 605 | {
|
|---|
| 606 | if ((publisher == &pool) && (notification->getChannelNo() == WorkerPool::WorkerIdle)) {
|
|---|
| 607 | // we have an idle worker
|
|---|
| 608 | LOG(1, "INFO: We are notified of an idle worker.");
|
|---|
| 609 | // are jobs available?
|
|---|
| 610 | if (JobsQueue.isJobPresent()) {
|
|---|
| 611 | sendAvailableJobToNextIdleWorker();
|
|---|
| 612 | }
|
|---|
| 613 | } else if ((publisher == &JobsQueue) && (notification->getChannelNo() == FragmentQueue::JobAdded)) {
|
|---|
| 614 | // we have new jobs
|
|---|
| 615 | LOG(1, "INFO: We are notified of a new job.");
|
|---|
| 616 | // check for idle workers
|
|---|
| 617 | if (pool.presentIdleWorkers()) {
|
|---|
| 618 | sendAvailableJobToNextIdleWorker();
|
|---|
| 619 | }
|
|---|
| 620 | } else {
|
|---|
| 621 | ASSERT(0, "FragmentScheduler::recieveNotification() - we are not signed on for updates in channel "
|
|---|
| 622 | +toString(notification->getChannelNo())+".");
|
|---|
| 623 | }
|
|---|
| 624 | }
|
|---|
| 625 |
|
|---|
| 626 | void FragmentScheduler::subjectKilled(Observable *publisher)
|
|---|
| 627 | {}
|
|---|