| 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 <boost/bind.hpp>
|
|---|
| 29 | #include <boost/lexical_cast.hpp>
|
|---|
| 30 | #include <iostream>
|
|---|
| 31 | #include <vector>
|
|---|
| 32 | #include "Connection.hpp" // Must come before boost/serialization headers.
|
|---|
| 33 | #include <boost/serialization/vector.hpp>
|
|---|
| 34 | #include "CodePatterns/Info.hpp"
|
|---|
| 35 | #include "CodePatterns/Log.hpp"
|
|---|
| 36 | #include "Jobs/MPQCCommandJob.hpp"
|
|---|
| 37 | #include "Jobs/SystemCommandJob.hpp"
|
|---|
| 38 | #include "JobId.hpp"
|
|---|
| 39 |
|
|---|
| 40 | #include "FragmentScheduler.hpp"
|
|---|
| 41 |
|
|---|
| 42 | FragmentJob::ptr FragmentScheduler::WorkerListener_t::NoJob(new SystemCommandJob(std::string("/bin/true"), std::string("dosomething"), JobId::NoJob));
|
|---|
| 43 |
|
|---|
| 44 | /** Helper function to enforce binding of FragmentWorker to possible derived
|
|---|
| 45 | * FragmentJob classes.
|
|---|
| 46 | */
|
|---|
| 47 | void dummyInit() {
|
|---|
| 48 | SystemCommandJob("/bin/false", "something", JobId::IllegalJob);
|
|---|
| 49 | MPQCCommandJob("nofile", JobId::IllegalJob);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /** Constructor of class FragmentScheduler.
|
|---|
| 53 | *
|
|---|
| 54 | * We setup both acceptors to accept connections from workers and Controller.
|
|---|
| 55 | *
|
|---|
| 56 | * \param io_service io_service of the asynchronous communications
|
|---|
| 57 | * \param workerport port to listen for worker connections
|
|---|
| 58 | * \param controllerport port to listen for controller connections.
|
|---|
| 59 | */
|
|---|
| 60 | FragmentScheduler::FragmentScheduler(boost::asio::io_service& io_service, unsigned short workerport, unsigned short controllerport) :
|
|---|
| 61 | WorkerListener(io_service, workerport, JobsQueue),
|
|---|
| 62 | ControllerListener(io_service, controllerport, JobsQueue,
|
|---|
| 63 | boost::bind(&Listener::initiateSocket, boost::ref(WorkerListener)))
|
|---|
| 64 | {
|
|---|
| 65 | Info info(__FUNCTION__);
|
|---|
| 66 |
|
|---|
| 67 | // only initiate socket if jobs are already present
|
|---|
| 68 | if (JobsQueue.isJobPresent()) {
|
|---|
| 69 | WorkerListener.initiateSocket();
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | ControllerListener.initiateSocket();
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /** Handle a new worker connection.
|
|---|
| 76 | *
|
|---|
| 77 | * We check whether jobs are in the JobsQueue. If present, job is sent.
|
|---|
| 78 | *
|
|---|
| 79 | * \sa handle_SendJobtoWorker()
|
|---|
| 80 | *
|
|---|
| 81 | * \param e error code if something went wrong
|
|---|
| 82 | * \param conn reference with the connection
|
|---|
| 83 | */
|
|---|
| 84 | void FragmentScheduler::WorkerListener_t::handle_Accept(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 85 | {
|
|---|
| 86 | Info info(__FUNCTION__);
|
|---|
| 87 | if (!e)
|
|---|
| 88 | {
|
|---|
| 89 | // Successfully accepted a new connection.
|
|---|
| 90 | // Check whether there are jobs in the queue
|
|---|
| 91 | if (JobsQueue.isJobPresent()) {
|
|---|
| 92 | // pop a job and send it to the client.
|
|---|
| 93 | FragmentJob::ptr job(JobsQueue.popJob());
|
|---|
| 94 | // The connection::async_write() function will automatically
|
|---|
| 95 | // serialize the data structure for us.
|
|---|
| 96 | LOG(1, "INFO: Sending job #" << job->getId() << ".");
|
|---|
| 97 | conn->async_write(job,
|
|---|
| 98 | boost::bind(&FragmentScheduler::WorkerListener_t::handle_SendJobtoWorker, this,
|
|---|
| 99 | boost::asio::placeholders::error, conn));
|
|---|
| 100 |
|
|---|
| 101 | } else {
|
|---|
| 102 | // send the static NoJob
|
|---|
| 103 | conn->async_write(NoJob,
|
|---|
| 104 | boost::bind(&FragmentScheduler::WorkerListener_t::handle_SendJobtoWorker, this,
|
|---|
| 105 | boost::asio::placeholders::error, conn));
|
|---|
| 106 |
|
|---|
| 107 | // then there must be no read necesary
|
|---|
| 108 |
|
|---|
| 109 | ELOG(2, "There is currently no job present in the queue.");
|
|---|
| 110 | }
|
|---|
| 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 | if (JobsQueue.isJobPresent()) {
|
|---|
| 122 | // Start an accept operation for a new Connection only when there
|
|---|
| 123 | // are still jobs present
|
|---|
| 124 | initiateSocket();
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /** Callback function when job has been sent.
|
|---|
| 129 | *
|
|---|
| 130 | * After job has been sent we start async_read() for the result.
|
|---|
| 131 | *
|
|---|
| 132 | * \sa handle_ReceiveResultFromWorker()
|
|---|
| 133 | *
|
|---|
| 134 | * \param e error code if something went wrong
|
|---|
| 135 | * \param conn reference with the connection
|
|---|
| 136 | */
|
|---|
| 137 | void FragmentScheduler::WorkerListener_t::handle_SendJobtoWorker(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 138 | {
|
|---|
| 139 | Info info(__FUNCTION__);
|
|---|
| 140 | LOG(1, "INFO: Job sent.");
|
|---|
| 141 | // obtain result
|
|---|
| 142 | LOG(1, "INFO: Receiving result for a job ...");
|
|---|
| 143 | conn->async_read(result,
|
|---|
| 144 | boost::bind(&FragmentScheduler::WorkerListener_t::handle_ReceiveResultFromWorker, this,
|
|---|
| 145 | boost::asio::placeholders::error, conn));
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /** Callback function when result has been received.
|
|---|
| 149 | *
|
|---|
| 150 | * \param e error code if something went wrong
|
|---|
| 151 | * \param conn reference with the connection
|
|---|
| 152 | */
|
|---|
| 153 | void FragmentScheduler::WorkerListener_t::handle_ReceiveResultFromWorker(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 154 | {
|
|---|
| 155 | Info info(__FUNCTION__);
|
|---|
| 156 | LOG(1, "INFO: Received result for job #" << result->getId() << " ...");
|
|---|
| 157 | // and push into queue
|
|---|
| 158 | ASSERT(result->getId() != (JobId_t)JobId::NoJob,
|
|---|
| 159 | "FragmentScheduler::handle_ReceiveResultFromWorker() - result received has NoJob id.");
|
|---|
| 160 | ASSERT(result->getId() != (JobId_t)JobId::IllegalJob,
|
|---|
| 161 | "FragmentScheduler::handle_ReceiveResultFromWorker() - result received has IllegalJob id.");
|
|---|
| 162 | // place id into expected
|
|---|
| 163 | if ((result->getId() != (JobId_t)JobId::NoJob) && (result->getId() != (JobId_t)JobId::IllegalJob))
|
|---|
| 164 | JobsQueue.pushResult(result);
|
|---|
| 165 | // erase result
|
|---|
| 166 | result.reset();
|
|---|
| 167 | LOG(1, "INFO: JobsQueue has " << JobsQueue.getDoneJobs() << " results.");
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | /** Handle a new controller connection.
|
|---|
| 171 | *
|
|---|
| 172 | * \sa handle_ReceiveJobs()
|
|---|
| 173 | * \sa handle_CheckResultState()
|
|---|
| 174 | * \sa handle_SendResults()
|
|---|
| 175 | *
|
|---|
| 176 | * \param e error code if something went wrong
|
|---|
| 177 | * \param conn reference with the connection
|
|---|
| 178 | */
|
|---|
| 179 | void FragmentScheduler::ControllerListener_t::handle_Accept(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 180 | {
|
|---|
| 181 | Info info(__FUNCTION__);
|
|---|
| 182 | if (!e)
|
|---|
| 183 | {
|
|---|
| 184 | conn->async_read(choice,
|
|---|
| 185 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_ReadChoice, this,
|
|---|
| 186 | boost::asio::placeholders::error, conn));
|
|---|
| 187 | }
|
|---|
| 188 | else
|
|---|
| 189 | {
|
|---|
| 190 | // An error occurred. Log it and return. Since we are not starting a new
|
|---|
| 191 | // accept operation the io_service will run out of work to do and the
|
|---|
| 192 | // server will exit.
|
|---|
| 193 | Exitflag = ErrorFlag;
|
|---|
| 194 | ELOG(0, e.message());
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /** Controller callback function to read the choice for next operation.
|
|---|
| 199 | *
|
|---|
| 200 | * \param e error code if something went wrong
|
|---|
| 201 | * \param conn reference with the connection
|
|---|
| 202 | */
|
|---|
| 203 | void FragmentScheduler::ControllerListener_t::handle_ReadChoice(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 204 | {
|
|---|
| 205 | Info info(__FUNCTION__);
|
|---|
| 206 | if (!e)
|
|---|
| 207 | {
|
|---|
| 208 | bool LaunchNewAcceptor = true;
|
|---|
| 209 | LOG(1, "INFO: Received request for operation " << choice << ".");
|
|---|
| 210 | // switch over the desired choice read previously
|
|---|
| 211 | switch(choice) {
|
|---|
| 212 | case NoOperation:
|
|---|
| 213 | {
|
|---|
| 214 | ELOG(1, "FragmentScheduler::handle_ReadChoice() - called with NoOperation.");
|
|---|
| 215 | break;
|
|---|
| 216 | }
|
|---|
| 217 | case GetNextJobId:
|
|---|
| 218 | {
|
|---|
| 219 | const JobId_t nextid = globalId.getNextId();
|
|---|
| 220 | LOG(1, "INFO: Sending next available job id " << nextid << " to controller ...");
|
|---|
| 221 | conn->async_write(nextid,
|
|---|
| 222 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_GetNextJobIdState, this,
|
|---|
| 223 | boost::asio::placeholders::error, conn));
|
|---|
| 224 | break;
|
|---|
| 225 | }
|
|---|
| 226 | case ReceiveJobs:
|
|---|
| 227 | {
|
|---|
| 228 | // The connection::async_write() function will automatically
|
|---|
| 229 | // serialize the data structure for us.
|
|---|
| 230 | LOG(1, "INFO: Receiving bunch of jobs from a controller ...");
|
|---|
| 231 | conn->async_read(jobs,
|
|---|
| 232 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_ReceiveJobs, this,
|
|---|
| 233 | boost::asio::placeholders::error, conn));
|
|---|
| 234 | break;
|
|---|
| 235 | }
|
|---|
| 236 | case CheckState:
|
|---|
| 237 | {
|
|---|
| 238 | // first update number
|
|---|
| 239 | jobInfo[0] = JobsQueue.getPresentJobs();
|
|---|
| 240 | jobInfo[1] = JobsQueue.getDoneJobs();
|
|---|
| 241 | // now we accept connections to check for state of calculations
|
|---|
| 242 | LOG(1, "INFO: Sending state that "+toString(jobInfo[0])+" jobs are present and "+toString(jobInfo[1])+" jobs are done to controller ...");
|
|---|
| 243 | conn->async_write(jobInfo,
|
|---|
| 244 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_CheckResultState, this,
|
|---|
| 245 | boost::asio::placeholders::error, conn));
|
|---|
| 246 | break;
|
|---|
| 247 | }
|
|---|
| 248 | case SendResults:
|
|---|
| 249 | {
|
|---|
| 250 | const std::vector<FragmentResult::ptr> results = JobsQueue.getAllResults();
|
|---|
| 251 | // ... or we give the results
|
|---|
| 252 | LOG(1, "INFO: Sending "+toString(results.size())+" results to controller ...");
|
|---|
| 253 | conn->async_write(results,
|
|---|
| 254 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_SendResults, this,
|
|---|
| 255 | boost::asio::placeholders::error, conn));
|
|---|
| 256 | break;
|
|---|
| 257 | }
|
|---|
| 258 | case Shutdown:
|
|---|
| 259 | {
|
|---|
| 260 | LaunchNewAcceptor = false;
|
|---|
| 261 | break;
|
|---|
| 262 | }
|
|---|
| 263 | default:
|
|---|
| 264 | Exitflag = ErrorFlag;
|
|---|
| 265 | ELOG(1, "FragmentScheduler::handle_ReadChoice() - called with no valid choice.");
|
|---|
| 266 | break;
|
|---|
| 267 | }
|
|---|
| 268 | // restore NoOperation choice such that choice is not read twice
|
|---|
| 269 | choice = NoOperation;
|
|---|
| 270 |
|
|---|
| 271 | if (LaunchNewAcceptor) {
|
|---|
| 272 | LOG(1, "Launching new acceptor on socket.");
|
|---|
| 273 | // Start an accept operation for a new Connection.
|
|---|
| 274 | initiateSocket();
|
|---|
| 275 | }
|
|---|
| 276 | }
|
|---|
| 277 | else
|
|---|
| 278 | {
|
|---|
| 279 | // An error occurred. Log it and return. Since we are not starting a new
|
|---|
| 280 | // accept operation the io_service will run out of work to do and the
|
|---|
| 281 | // server will exit.
|
|---|
| 282 | Exitflag = ErrorFlag;
|
|---|
| 283 | ELOG(0, e.message());
|
|---|
| 284 | }
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | /** Controller callback function when job has been sent.
|
|---|
| 288 | *
|
|---|
| 289 | * We check here whether the worker socket is accepting, if there
|
|---|
| 290 | * have been no jobs we re-activate it, as it is shut down after
|
|---|
| 291 | * last job.
|
|---|
| 292 | *
|
|---|
| 293 | * \param e error code if something went wrong
|
|---|
| 294 | * \param conn reference with the connection
|
|---|
| 295 | */
|
|---|
| 296 | void FragmentScheduler::ControllerListener_t::handle_ReceiveJobs(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 297 | {
|
|---|
| 298 | Info info(__FUNCTION__);
|
|---|
| 299 | bool need_initiateSocket = !JobsQueue.isJobPresent();
|
|---|
| 300 |
|
|---|
| 301 | // jobs are received, hence place in JobsQueue
|
|---|
| 302 | if (!jobs.empty()) {
|
|---|
| 303 | LOG(1, "INFO: Pushing " << jobs.size() << " jobs into queue.");
|
|---|
| 304 | JobsQueue.pushJobs(jobs);
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | jobs.clear();
|
|---|
| 308 |
|
|---|
| 309 | // initiate socket if we had no jobs before
|
|---|
| 310 | if (need_initiateSocket)
|
|---|
| 311 | initiateWorkerSocket();
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | /** Controller callback function when checking on state of results.
|
|---|
| 315 | *
|
|---|
| 316 | * \param e error code if something went wrong
|
|---|
| 317 | * \param conn reference with the connection
|
|---|
| 318 | */
|
|---|
| 319 | void FragmentScheduler::ControllerListener_t::handle_CheckResultState(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 320 | {
|
|---|
| 321 | Info info(__FUNCTION__);
|
|---|
| 322 | // do nothing
|
|---|
| 323 | LOG(1, "INFO: Sent that " << jobInfo << " jobs are (scheduled, done).");
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | /** Controller callback function when checking on state of results.
|
|---|
| 327 | *
|
|---|
| 328 | * \param e error code if something went wrong
|
|---|
| 329 | * \param conn reference with the connection
|
|---|
| 330 | */
|
|---|
| 331 | void FragmentScheduler::ControllerListener_t::handle_GetNextJobIdState(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 332 | {
|
|---|
| 333 | Info info(__FUNCTION__);
|
|---|
| 334 | // do nothing
|
|---|
| 335 | LOG(1, "INFO: Sent next available job id.");
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | /** Controller callback function when result has been received.
|
|---|
| 339 | *
|
|---|
| 340 | * \param e error code if something went wrong
|
|---|
| 341 | * \param conn reference with the connection
|
|---|
| 342 | */
|
|---|
| 343 | void FragmentScheduler::ControllerListener_t::handle_SendResults(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 344 | {
|
|---|
| 345 | Info info(__FUNCTION__);
|
|---|
| 346 | // do nothing
|
|---|
| 347 | LOG(1, "INFO: Results have been sent.");
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|