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