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 | conn->async_write(false,
|
---|
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 | conn->async_write(true,
|
---|
178 | boost::bind(&FragmentScheduler::WorkerListener_t::handle_enrolled, this,
|
---|
179 | boost::asio::placeholders::error, conn));
|
---|
180 | break;
|
---|
181 | }
|
---|
182 | case SendResult:
|
---|
183 | {
|
---|
184 | if (pool.presentInPool(address)) {
|
---|
185 | // check whether its priority is busy_priority
|
---|
186 | if (pool.isWorkerBusy(address)) {
|
---|
187 | conn->async_read(result,
|
---|
188 | boost::bind(&FragmentScheduler::WorkerListener_t::handle_ReceiveResultFromWorker, this,
|
---|
189 | boost::asio::placeholders::error, conn));
|
---|
190 | } else {
|
---|
191 | ELOG(1, "Worker " << address << " trying to send result who is not marked as busy.");
|
---|
192 | conn->async_read(result,
|
---|
193 | boost::bind(&FragmentScheduler::WorkerListener_t::handle_RejectResultFromWorker, this,
|
---|
194 | boost::asio::placeholders::error, conn));
|
---|
195 | }
|
---|
196 | } else {
|
---|
197 | ELOG(1, "Worker " << address << " trying to send result who is not in pool.");
|
---|
198 | conn->async_read(result,
|
---|
199 | boost::bind(&FragmentScheduler::WorkerListener_t::handle_RejectResultFromWorker, this,
|
---|
200 | boost::asio::placeholders::error, conn));
|
---|
201 | }
|
---|
202 | break;
|
---|
203 | }
|
---|
204 | case RemoveFromPool:
|
---|
205 | {
|
---|
206 | if (pool.presentInPool(address)) {
|
---|
207 | // removing present worker
|
---|
208 | pool.removeWorker(address);
|
---|
209 | } else {
|
---|
210 | ELOG(1, "Shutting down Worker " << address << " not contained in pool.");
|
---|
211 | }
|
---|
212 | break;
|
---|
213 | }
|
---|
214 | default:
|
---|
215 | Exitflag = ErrorFlag;
|
---|
216 | ELOG(1, "WorkerListener_t::handle_ReadChoice() - called with no valid choice.");
|
---|
217 | break;
|
---|
218 | }
|
---|
219 | }
|
---|
220 | // restore NoOperation choice such that choice is not read twice
|
---|
221 | choice = NoWorkerOperation;
|
---|
222 |
|
---|
223 | initiateSocket();
|
---|
224 | }
|
---|
225 | else
|
---|
226 | {
|
---|
227 | // An error occurred. Log it and return. Since we are not starting a new
|
---|
228 | // accept operation the io_service will run out of work to do and the
|
---|
229 | // server will exit.
|
---|
230 | Exitflag = ErrorFlag;
|
---|
231 | ELOG(0, e.message());
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | /** Callback function when new worker has enrolled.
|
---|
237 | *
|
---|
238 | * \param e error code if something went wrong
|
---|
239 | * \param conn reference with the connection
|
---|
240 | */
|
---|
241 | void FragmentScheduler::WorkerListener_t::handle_enrolled(const boost::system::error_code& e, connection_ptr conn)
|
---|
242 | {
|
---|
243 | Info info(__FUNCTION__);
|
---|
244 | if (!e) {
|
---|
245 | LOG(2, "DEBUG: Successfully enrolled.");
|
---|
246 | LOG(1, "INFO: There are " << pool.getNoTotalWorkers() << " workers in the queue, "
|
---|
247 | << pool.getNoIdleWorkers() << " of which are idle.");
|
---|
248 | } else {
|
---|
249 | // An error occurred. Log it and return. Since we are not starting a new
|
---|
250 | // accept operation the io_service will run out of work to do and the
|
---|
251 | // server will exit.
|
---|
252 | Exitflag = ErrorFlag;
|
---|
253 | ELOG(0, e.message());
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | /** Callback function when result has been received.
|
---|
258 | *
|
---|
259 | * \param e error code if something went wrong
|
---|
260 | * \param conn reference with the connection
|
---|
261 | */
|
---|
262 | void FragmentScheduler::WorkerListener_t::handle_ReceiveResultFromWorker(const boost::system::error_code& e, connection_ptr conn)
|
---|
263 | {
|
---|
264 | Info info(__FUNCTION__);
|
---|
265 | LOG(1, "INFO: Received result for job #" << result->getId() << " ...");
|
---|
266 |
|
---|
267 | // and push into queue
|
---|
268 | ASSERT(result->getId() != (JobId_t)JobId::NoJob,
|
---|
269 | "WorkerListener_t::handle_ReceiveResultFromWorker() - result received has NoJob id.");
|
---|
270 | ASSERT(result->getId() != (JobId_t)JobId::IllegalJob,
|
---|
271 | "WorkerListener_t::handle_ReceiveResultFromWorker() - result received has IllegalJob id.");
|
---|
272 | // place id into expected
|
---|
273 | if ((result->getId() != (JobId_t)JobId::NoJob) && (result->getId() != (JobId_t)JobId::IllegalJob))
|
---|
274 | JobsQueue.pushResult(result);
|
---|
275 |
|
---|
276 | // mark as idle
|
---|
277 | pool.unmarkWorkerBusy(address);
|
---|
278 | LOG(1, "INFO: There are " << pool.getNoTotalWorkers() << " workers in the queue, "
|
---|
279 | << pool.getNoIdleWorkers() << " of which are idle.");
|
---|
280 |
|
---|
281 | // erase result
|
---|
282 | result.reset();
|
---|
283 | LOG(1, "INFO: JobsQueue has " << JobsQueue.getDoneJobs() << " results.");
|
---|
284 | }
|
---|
285 |
|
---|
286 | /** Callback function when result has been received.
|
---|
287 | *
|
---|
288 | * \param e error code if something went wrong
|
---|
289 | * \param conn reference with the connection
|
---|
290 | */
|
---|
291 | void FragmentScheduler::WorkerListener_t::handle_RejectResultFromWorker(const boost::system::error_code& e, connection_ptr conn)
|
---|
292 | {
|
---|
293 | Info info(__FUNCTION__);
|
---|
294 | // nothing to do
|
---|
295 | LOG(1, "INFO: Rejecting result for job #" << result->getId() << ", placing back into queue.");
|
---|
296 |
|
---|
297 | JobsQueue.resubmitJob(result->getId());
|
---|
298 |
|
---|
299 | LOG(1, "INFO: JobsQueue has " << JobsQueue.getDoneJobs() << " results.");
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | /** Handle a new controller connection.
|
---|
304 | *
|
---|
305 | * \sa handle_ReceiveJobs()
|
---|
306 | * \sa handle_CheckResultState()
|
---|
307 | * \sa handle_SendResults()
|
---|
308 | *
|
---|
309 | * \param e error code if something went wrong
|
---|
310 | * \param conn reference with the connection
|
---|
311 | */
|
---|
312 | void FragmentScheduler::ControllerListener_t::handle_Accept(const boost::system::error_code& e, connection_ptr conn)
|
---|
313 | {
|
---|
314 | Info info(__FUNCTION__);
|
---|
315 | if (!e)
|
---|
316 | {
|
---|
317 | conn->async_read(choice,
|
---|
318 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_ReadChoice, this,
|
---|
319 | boost::asio::placeholders::error, conn));
|
---|
320 | }
|
---|
321 | else
|
---|
322 | {
|
---|
323 | // An error occurred. Log it and return. Since we are not starting a new
|
---|
324 | // accept operation the io_service will run out of work to do and the
|
---|
325 | // server will exit.
|
---|
326 | Exitflag = ErrorFlag;
|
---|
327 | ELOG(0, e.message());
|
---|
328 | }
|
---|
329 | }
|
---|
330 |
|
---|
331 | /** Controller callback function to read the choice for next operation.
|
---|
332 | *
|
---|
333 | * \param e error code if something went wrong
|
---|
334 | * \param conn reference with the connection
|
---|
335 | */
|
---|
336 | void FragmentScheduler::ControllerListener_t::handle_ReadChoice(const boost::system::error_code& e, connection_ptr conn)
|
---|
337 | {
|
---|
338 | Info info(__FUNCTION__);
|
---|
339 | if (!e)
|
---|
340 | {
|
---|
341 | bool LaunchNewAcceptor = true;
|
---|
342 | LOG(1, "INFO: Received request for operation " << choice << ".");
|
---|
343 | // switch over the desired choice read previously
|
---|
344 | switch(choice) {
|
---|
345 | case NoControllerOperation:
|
---|
346 | {
|
---|
347 | ELOG(1, "ControllerListener_t::handle_ReadChoice() - called with NoOperation.");
|
---|
348 | break;
|
---|
349 | }
|
---|
350 | case GetNextJobId:
|
---|
351 | {
|
---|
352 | LOG(1, "INFO: Receiving number of desired job ids from controller ...");
|
---|
353 | conn->async_read(NumberIds,
|
---|
354 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_GetNextJobIdState, this,
|
---|
355 | boost::asio::placeholders::error, conn));
|
---|
356 | break;
|
---|
357 | }
|
---|
358 | case SendJobs:
|
---|
359 | {
|
---|
360 | // The connection::async_write() function will automatically
|
---|
361 | // serialize the data structure for us.
|
---|
362 | LOG(1, "INFO: Receiving bunch of jobs from a controller ...");
|
---|
363 | conn->async_read(jobs,
|
---|
364 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_ReceiveJobs, this,
|
---|
365 | boost::asio::placeholders::error, conn));
|
---|
366 | break;
|
---|
367 | }
|
---|
368 | case CheckState:
|
---|
369 | {
|
---|
370 | // first update number
|
---|
371 | jobInfo[0] = JobsQueue.getPresentJobs();
|
---|
372 | jobInfo[1] = JobsQueue.getDoneJobs();
|
---|
373 | // now we accept connections to check for state of calculations
|
---|
374 | LOG(1, "INFO: Sending state that "+toString(jobInfo[0])+" jobs are present and "+toString(jobInfo[1])+" jobs are done to controller ...");
|
---|
375 | conn->async_write(jobInfo,
|
---|
376 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_CheckResultState, this,
|
---|
377 | boost::asio::placeholders::error, conn));
|
---|
378 | break;
|
---|
379 | }
|
---|
380 | case RemoveAll:
|
---|
381 | {
|
---|
382 | removeallWorkers();
|
---|
383 | break;
|
---|
384 | }
|
---|
385 | case ReceiveResults:
|
---|
386 | {
|
---|
387 | const std::vector<FragmentResult::ptr> results = JobsQueue.getAllResults();
|
---|
388 | // ... or we give the results
|
---|
389 | LOG(1, "INFO: Sending "+toString(results.size())+" results to controller ...");
|
---|
390 | conn->async_write(results,
|
---|
391 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_SendResults, this,
|
---|
392 | boost::asio::placeholders::error, conn));
|
---|
393 | break;
|
---|
394 | }
|
---|
395 | case ShutdownControllerSocket:
|
---|
396 | {
|
---|
397 | LOG(1, "INFO: Received shutdown from controller ...");
|
---|
398 | // only allow for shutdown when there are no more jobs in the queue
|
---|
399 | if (!JobsQueue.isJobPresent()) {
|
---|
400 | // we shutdown? Hence, also shutdown controller
|
---|
401 | LaunchNewAcceptor = !shutdownAllSockets();
|
---|
402 | } else {
|
---|
403 | ELOG(2, "There are still jobs waiting in the queue.");
|
---|
404 | }
|
---|
405 | break;
|
---|
406 | }
|
---|
407 | default:
|
---|
408 | Exitflag = ErrorFlag;
|
---|
409 | ELOG(1, "ControllerListener_t::handle_ReadChoice() - called with no valid choice.");
|
---|
410 | break;
|
---|
411 | }
|
---|
412 | // restore NoControllerOperation choice such that choice is not read twice
|
---|
413 | choice = NoControllerOperation;
|
---|
414 |
|
---|
415 | if (LaunchNewAcceptor) {
|
---|
416 | LOG(1, "Launching new acceptor on socket.");
|
---|
417 | // Start an accept operation for a new Connection.
|
---|
418 | initiateSocket();
|
---|
419 | }
|
---|
420 | }
|
---|
421 | else
|
---|
422 | {
|
---|
423 | // An error occurred. Log it and return. Since we are not starting a new
|
---|
424 | // accept operation the io_service will run out of work to do and the
|
---|
425 | // server will exit.
|
---|
426 | Exitflag = ErrorFlag;
|
---|
427 | ELOG(0, e.message());
|
---|
428 | }
|
---|
429 | }
|
---|
430 |
|
---|
431 | /** Controller callback function when job has been sent.
|
---|
432 | *
|
---|
433 | * We check here whether the worker socket is accepting, if there
|
---|
434 | * have been no jobs we re-activate it, as it is shut down after
|
---|
435 | * last job.
|
---|
436 | *
|
---|
437 | * \param e error code if something went wrong
|
---|
438 | * \param conn reference with the connection
|
---|
439 | */
|
---|
440 | void FragmentScheduler::ControllerListener_t::handle_ReceiveJobs(const boost::system::error_code& e, connection_ptr conn)
|
---|
441 | {
|
---|
442 | Info info(__FUNCTION__);
|
---|
443 | // jobs are received, hence place in JobsQueue
|
---|
444 | if (!jobs.empty()) {
|
---|
445 | LOG(1, "INFO: Pushing " << jobs.size() << " jobs into queue.");
|
---|
446 | JobsQueue.pushJobs(jobs);
|
---|
447 | }
|
---|
448 | jobs.clear();
|
---|
449 | }
|
---|
450 |
|
---|
451 | /** Controller callback function when checking on state of results.
|
---|
452 | *
|
---|
453 | * \param e error code if something went wrong
|
---|
454 | * \param conn reference with the connection
|
---|
455 | */
|
---|
456 | void FragmentScheduler::ControllerListener_t::handle_CheckResultState(const boost::system::error_code& e, connection_ptr conn)
|
---|
457 | {
|
---|
458 | Info info(__FUNCTION__);
|
---|
459 | // do nothing
|
---|
460 | LOG(1, "INFO: Sent that " << jobInfo << " jobs are (scheduled, done).");
|
---|
461 | }
|
---|
462 |
|
---|
463 | /** Controller callback function when checking on state of results.
|
---|
464 | *
|
---|
465 | * \param e error code if something went wrong
|
---|
466 | * \param conn reference with the connection
|
---|
467 | */
|
---|
468 | void FragmentScheduler::ControllerListener_t::handle_GetNextJobIdState(const boost::system::error_code& e, connection_ptr conn)
|
---|
469 | {
|
---|
470 | Info info(__FUNCTION__);
|
---|
471 |
|
---|
472 | std::vector<JobId_t> nextids( NumberIds, JobId::IllegalJob);
|
---|
473 | std::generate(nextids.begin(), nextids.end(),
|
---|
474 | boost::bind(&GlobalJobId::getNextId, boost::ref(globalId)));
|
---|
475 | LOG(1, "INFO: Sending next available job ids " << nextids << " to controller ...");
|
---|
476 | conn->async_write(nextids,
|
---|
477 | boost::bind(&FragmentScheduler::ControllerListener_t::handle_SendIds, this,
|
---|
478 | boost::asio::placeholders::error, conn));
|
---|
479 | }
|
---|
480 |
|
---|
481 | /** Controller callback function when free job ids have been sent.
|
---|
482 | *
|
---|
483 | * \param e error code if something went wrong
|
---|
484 | * \param conn reference with the connection
|
---|
485 | */
|
---|
486 | void FragmentScheduler::ControllerListener_t::handle_SendIds(const boost::system::error_code& e, connection_ptr conn)
|
---|
487 | {
|
---|
488 | Info info(__FUNCTION__);
|
---|
489 | // do nothing
|
---|
490 | LOG(1, "INFO: Ids have been sent.");
|
---|
491 | }
|
---|
492 |
|
---|
493 | /** Controller callback function when result has been received.
|
---|
494 | *
|
---|
495 | * \param e error code if something went wrong
|
---|
496 | * \param conn reference with the connection
|
---|
497 | */
|
---|
498 | void FragmentScheduler::ControllerListener_t::handle_SendResults(const boost::system::error_code& e, connection_ptr conn)
|
---|
499 | {
|
---|
500 | Info info(__FUNCTION__);
|
---|
501 | // do nothing
|
---|
502 | LOG(1, "INFO: Results have been sent.");
|
---|
503 | }
|
---|
504 |
|
---|
505 |
|
---|
506 | /** Helper function to send a job to worker.
|
---|
507 | *
|
---|
508 | * Note that we do not set the worker as busy. We simply send it the job.
|
---|
509 | *
|
---|
510 | * @param address address of worker
|
---|
511 | * @param job job to send
|
---|
512 | */
|
---|
513 | void FragmentScheduler::sendJobToWorker(const WorkerAddress &address, FragmentJob::ptr &job)
|
---|
514 | {
|
---|
515 | ASSERT( pool.isWorkerBusy(address),
|
---|
516 | "FragmentScheduler::sendJobToWorker() - Worker "+toString(address)+" is not marked as busy.");
|
---|
517 | LOG(1, "INFO: Sending job " << job->getId() << " to worker " << address << ".");
|
---|
518 |
|
---|
519 | // create op, sign on, and hand over to queue
|
---|
520 | AsyncOperation *sendJobOp = new SendJobToWorkerOperation(connection,job);
|
---|
521 | OpQueue.push_back(sendJobOp, address);
|
---|
522 | }
|
---|
523 |
|
---|
524 | /** Helper function to shutdown a single worker.
|
---|
525 | *
|
---|
526 | * We send NoJob to indicate shutdown
|
---|
527 | *
|
---|
528 | * @param address of worker to shutdown
|
---|
529 | */
|
---|
530 | void FragmentScheduler::shutdownWorker(const WorkerAddress &address)
|
---|
531 | {
|
---|
532 | ASSERT( !pool.isWorkerBusy(address),
|
---|
533 | "FragmentScheduler::sendJobToWorker() - Worker "+toString(address)+" is already busy.");
|
---|
534 | LOG(2, "INFO: Shutting down worker " << address << "...");
|
---|
535 | AsyncOperation *shutdownWorkerOp = new ShutdownWorkerOperation(connection);
|
---|
536 | OpQueue.push_back(shutdownWorkerOp, address);
|
---|
537 | }
|
---|
538 |
|
---|
539 | /** Sends shutdown to all current workers in the pool.
|
---|
540 | *
|
---|
541 | */
|
---|
542 | void FragmentScheduler::removeAllWorkers()
|
---|
543 | {
|
---|
544 | // first, sign off such that no new jobs are given to workers
|
---|
545 | pool.signOff(this, WorkerPool::WorkerIdle);
|
---|
546 |
|
---|
547 | LOG(2, "DEBUG: Waiting for busy workers to finish ...");
|
---|
548 | while (pool.hasBusyWorkers())
|
---|
549 | ;
|
---|
550 |
|
---|
551 | LOG(2, "INFO: Shutting down workers ...");
|
---|
552 | // iterate until there are no more idle workers
|
---|
553 | // get list of all idle workers
|
---|
554 | typedef std::vector<std::pair<std::string, std::string> > WorkerList_t;
|
---|
555 | WorkerList_t WorkerList = pool.getListOfIdleWorkers();
|
---|
556 |
|
---|
557 | // give all workers shutdown signal
|
---|
558 | for (WorkerList_t::const_iterator iter = WorkerList.begin(); iter != WorkerList.end(); ++iter)
|
---|
559 | shutdownWorker(WorkerAddress(iter->first, iter->second));
|
---|
560 | }
|
---|
561 |
|
---|
562 | /** Function to shutdown server properly, e.g. for use as signal handler.
|
---|
563 | *
|
---|
564 | * @param sig signal number
|
---|
565 | */
|
---|
566 | void FragmentScheduler::shutdown(int sig)
|
---|
567 | {
|
---|
568 | LOG(0, "STATUS: Shutting down due to signal " << sig << ".");
|
---|
569 |
|
---|
570 | if (!pool.presentIdleWorkers() && !pool.hasBusyWorkers()) {
|
---|
571 | shutdown();
|
---|
572 | } else {
|
---|
573 | removeAllWorkers();
|
---|
574 | }
|
---|
575 | }
|
---|
576 |
|
---|
577 | /** Helper function to shutdown the server properly.
|
---|
578 | *
|
---|
579 | * \todo one should idle here until all workers have returned from
|
---|
580 | * calculating stuff (or workers need to still listen while they are
|
---|
581 | * calculating which is probably better).
|
---|
582 | *
|
---|
583 | * \note We only shutdown when there are no workers left
|
---|
584 | *
|
---|
585 | * @return true - doing shutdown, false - precondition not met, not shutting down
|
---|
586 | */
|
---|
587 | bool FragmentScheduler::shutdown()
|
---|
588 | {
|
---|
589 | if (!pool.presentIdleWorkers() && !pool.hasBusyWorkers()) {
|
---|
590 | LOG(1, "INFO: Shutting all down ...");
|
---|
591 |
|
---|
592 | /// close the worker listener's socket
|
---|
593 | WorkerListener.closeSocket();
|
---|
594 |
|
---|
595 | /// close the controller listener's socket
|
---|
596 | ControllerListener.closeSocket();
|
---|
597 |
|
---|
598 | /// finally, stop the io_service
|
---|
599 | io_service.stop();
|
---|
600 | return true;
|
---|
601 | } else {
|
---|
602 | ELOG(2, "There are still idle or busy workers present.");
|
---|
603 | return false;
|
---|
604 | }
|
---|
605 | }
|
---|
606 |
|
---|
607 | /** Internal helper to send the next available job to the next idle worker.
|
---|
608 | *
|
---|
609 | */
|
---|
610 | void FragmentScheduler::sendAvailableJobToNextIdleWorker()
|
---|
611 | {
|
---|
612 | const WorkerAddress address = pool.getNextIdleWorker();
|
---|
613 | FragmentJob::ptr job = JobsQueue.popJob();
|
---|
614 | sendJobToWorker(address, job);
|
---|
615 | }
|
---|
616 |
|
---|
617 | void FragmentScheduler::update(Observable *publisher)
|
---|
618 | {
|
---|
619 | ASSERT(0, "FragmentScheduler::update() - we are not signed on for global updates.");
|
---|
620 | }
|
---|
621 |
|
---|
622 | void FragmentScheduler::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
623 | {
|
---|
624 | if ((publisher == &pool) && (notification->getChannelNo() == WorkerPool::WorkerIdle)) {
|
---|
625 | // we have an idle worker
|
---|
626 | LOG(1, "INFO: We are notified of an idle worker.");
|
---|
627 | // are jobs available?
|
---|
628 | if (JobsQueue.isJobPresent()) {
|
---|
629 | sendAvailableJobToNextIdleWorker();
|
---|
630 | }
|
---|
631 | } else if ((publisher == &JobsQueue) && (notification->getChannelNo() == FragmentQueue::JobAdded)) {
|
---|
632 | // we have new jobs
|
---|
633 | LOG(1, "INFO: We are notified of a new job.");
|
---|
634 | // check for idle workers
|
---|
635 | if (pool.presentIdleWorkers()) {
|
---|
636 | sendAvailableJobToNextIdleWorker();
|
---|
637 | }
|
---|
638 | } else {
|
---|
639 | ASSERT(0, "FragmentScheduler::recieveNotification() - we are not signed on for updates in channel "
|
---|
640 | +toString(notification->getChannelNo())+".");
|
---|
641 | }
|
---|
642 | }
|
---|
643 |
|
---|
644 | void FragmentScheduler::subjectKilled(Observable *publisher)
|
---|
645 | {}
|
---|