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