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