[72eaf7f] | 1 | /*
|
---|
[926a49] | 2 | * FragmentScheduler.hpp
|
---|
[72eaf7f] | 3 | *
|
---|
[cd4a6e] | 4 | * Created on: Oct 19, 2011
|
---|
[72eaf7f] | 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[cd4a6e] | 8 | #ifndef FRAGMENTSCHEDULER_HPP_
|
---|
| 9 | #define FRAGMENTSCHEDULER_HPP_
|
---|
[72eaf7f] | 10 |
|
---|
[f93842] | 11 | // include config.h
|
---|
| 12 | #ifdef HAVE_CONFIG_H
|
---|
| 13 | #include <config.h>
|
---|
| 14 | #endif
|
---|
| 15 |
|
---|
[72eaf7f] | 16 | #include <vector>
|
---|
[cd4a6e] | 17 | #include <boost/asio.hpp>
|
---|
[8036b7] | 18 | #include <boost/function.hpp>
|
---|
[cd4a6e] | 19 |
|
---|
[2344a3] | 20 | #include "CodePatterns/Observer/Observer.hpp"
|
---|
[cd4a6e] | 21 | #include "Connection.hpp"
|
---|
[778abb] | 22 | #include "ControllerChoices.hpp"
|
---|
[50d095] | 23 | #include "Operations/Servers/SendJobToWorkerOperation.hpp"
|
---|
| 24 | #include "Operations/Servers/ShutdownWorkerOperation.hpp"
|
---|
[2344a3] | 25 | #include "ControllerChoices.hpp"
|
---|
[7670865] | 26 | #include "FragmentQueue.hpp"
|
---|
[d1dbfc] | 27 | #include "GlobalJobId.hpp"
|
---|
[7670865] | 28 | #include "Jobs/FragmentJob.hpp"
|
---|
[8036b7] | 29 | #include "Listener.hpp"
|
---|
[7670865] | 30 | #include "Results/FragmentResult.hpp"
|
---|
[778abb] | 31 | #include "types.hpp"
|
---|
[41c1b7] | 32 | #include "Pool/WorkerPool.hpp"
|
---|
| 33 | #include "WorkerAddress.hpp"
|
---|
[9a3f84] | 34 | #include "WorkerChoices.hpp"
|
---|
[72eaf7f] | 35 |
|
---|
[8036b7] | 36 | /** FragmentScheduler serves FragmentJobs to Workers and accepts commands from
|
---|
| 37 | * a Controller.
|
---|
[cd4a6e] | 38 | *
|
---|
| 39 | */
|
---|
[2344a3] | 40 | class FragmentScheduler : public Observer
|
---|
[72eaf7f] | 41 | {
|
---|
| 42 | public:
|
---|
| 43 | /// Constructor opens the acceptor and starts waiting for the first incoming
|
---|
[cd4a6e] | 44 | /// Connection.
|
---|
[db03d9] | 45 | FragmentScheduler(boost::asio::io_service& io_service, unsigned short workerport, unsigned short controllerport);
|
---|
[2344a3] | 46 | ~FragmentScheduler();
|
---|
[72eaf7f] | 47 |
|
---|
[8036b7] | 48 | private:
|
---|
[41c1b7] | 49 | void sendJobToWorker(const WorkerAddress &address, FragmentJob::ptr &job);
|
---|
[2344a3] | 50 | void sendAvailableJobToNextIdleWorker();
|
---|
| 51 | void shutdown();
|
---|
| 52 | void shutdownWorker(const WorkerAddress &address);
|
---|
| 53 | void removeAllWorkers();
|
---|
| 54 |
|
---|
| 55 | void update(Observable *publisher);
|
---|
| 56 | void recieveNotification(Observable *publisher, Notification_ptr notification);
|
---|
| 57 | void subjectKilled(Observable *publisher);
|
---|
[41c1b7] | 58 |
|
---|
[8036b7] | 59 | class WorkerListener_t : public Listener
|
---|
| 60 | {
|
---|
| 61 | public:
|
---|
| 62 | WorkerListener_t(
|
---|
| 63 | boost::asio::io_service& io_service,
|
---|
| 64 | unsigned short port,
|
---|
[41c1b7] | 65 | FragmentQueue &_JobsQueue,
|
---|
| 66 | WorkerPool &_pool,
|
---|
| 67 | boost::function<void (const WorkerAddress&, FragmentJob::ptr&)> _callback) :
|
---|
[8036b7] | 68 | Listener(io_service, port),
|
---|
[41c1b7] | 69 | address("127.0.0.1", "0"),
|
---|
[8036b7] | 70 | JobsQueue(_JobsQueue),
|
---|
[41c1b7] | 71 | pool(_pool),
|
---|
| 72 | result( new FragmentResult(JobId::NoJob) ),
|
---|
[9a3f84] | 73 | callback_sendJobToWorker(_callback),
|
---|
| 74 | choice(NoWorkerOperation)
|
---|
[8036b7] | 75 | {}
|
---|
| 76 | virtual ~WorkerListener_t() {}
|
---|
| 77 |
|
---|
| 78 | protected:
|
---|
| 79 | /// Handle completion of a accept worker operation.
|
---|
| 80 | void handle_Accept(const boost::system::error_code& e, connection_ptr conn);
|
---|
| 81 |
|
---|
[9a3f84] | 82 | /// Handle completion of Worker operation to read choice
|
---|
| 83 | void handle_ReadChoice(const boost::system::error_code& e, connection_ptr conn);
|
---|
| 84 |
|
---|
[8036b7] | 85 | /// Worker callback function when job has been sent.
|
---|
[9a3f84] | 86 | void handle_ReadAddress(const boost::system::error_code& e, connection_ptr conn);
|
---|
[41c1b7] | 87 |
|
---|
| 88 | /// Worker callback function when new worker has enrolled.
|
---|
| 89 | void handle_enrolled(const boost::system::error_code& e, connection_ptr conn);
|
---|
[8036b7] | 90 |
|
---|
| 91 | /// Worker callback function when result has been received.
|
---|
| 92 | void handle_ReceiveResultFromWorker(const boost::system::error_code& e, connection_ptr conn);
|
---|
[9a3f84] | 93 |
|
---|
| 94 | /// Worker callback function when invalid result has been received.
|
---|
| 95 | void handle_RejectResultFromWorker(const boost::system::error_code& e, connection_ptr conn);
|
---|
[8036b7] | 96 | private:
|
---|
[41c1b7] | 97 | //!> address of new Worker
|
---|
| 98 | WorkerAddress address;
|
---|
| 99 |
|
---|
| 100 | //!> reference to Queue
|
---|
| 101 | FragmentQueue &JobsQueue;
|
---|
| 102 |
|
---|
| 103 | //!> callback reference to container class
|
---|
| 104 | WorkerPool &pool;
|
---|
[8036b7] | 105 |
|
---|
[9a3f84] | 106 | //!> result that is received from the client.
|
---|
[8036b7] | 107 | FragmentResult::ptr result;
|
---|
| 108 |
|
---|
[41c1b7] | 109 | //!> callback function to access send job function
|
---|
| 110 | boost::function<void (const WorkerAddress&, FragmentJob::ptr&)> callback_sendJobToWorker;
|
---|
| 111 |
|
---|
[9a3f84] | 112 | //!> choice
|
---|
| 113 | enum WorkerChoices choice;
|
---|
[db03d9] | 114 | };
|
---|
[72eaf7f] | 115 |
|
---|
[8036b7] | 116 | class ControllerListener_t : public Listener
|
---|
[db03d9] | 117 | {
|
---|
[8036b7] | 118 | public:
|
---|
| 119 | ControllerListener_t(
|
---|
| 120 | boost::asio::io_service& io_service,
|
---|
| 121 | unsigned short port,
|
---|
| 122 | FragmentQueue &_JobsQueue,
|
---|
[2344a3] | 123 | boost::function<void ()> _shutdownAllSockets) :
|
---|
[8036b7] | 124 | Listener(io_service, port),
|
---|
| 125 | JobsQueue(_JobsQueue),
|
---|
| 126 | jobInfo((size_t)2, 0),
|
---|
[38032a] | 127 | choice(NoControllerOperation),
|
---|
[2344a3] | 128 | shutdownAllSockets(_shutdownAllSockets),
|
---|
| 129 | globalId(0)
|
---|
[8036b7] | 130 | {}
|
---|
| 131 | virtual ~ControllerListener_t() {}
|
---|
| 132 |
|
---|
| 133 | protected:
|
---|
| 134 | /// Handle completion of a accept controller operation.
|
---|
| 135 | void handle_Accept(const boost::system::error_code& e, connection_ptr conn);
|
---|
| 136 |
|
---|
| 137 | /// Handle completion of controller operation to read choice
|
---|
| 138 | void handle_ReadChoice(const boost::system::error_code& e, connection_ptr conn);
|
---|
| 139 |
|
---|
| 140 | /// Controller callback function when job has been sent.
|
---|
| 141 | void handle_ReceiveJobs(const boost::system::error_code& e, connection_ptr conn);
|
---|
| 142 |
|
---|
| 143 | /// Controller callback function when checking on state of results.
|
---|
| 144 | void handle_CheckResultState(const boost::system::error_code& e, connection_ptr conn);
|
---|
| 145 |
|
---|
| 146 | /// Controller callback function when checking on state of results.
|
---|
| 147 | void handle_GetNextJobIdState(const boost::system::error_code& e, connection_ptr conn);
|
---|
| 148 |
|
---|
| 149 | /// Controller callback function when result has been received.
|
---|
| 150 | void handle_SendResults(const boost::system::error_code& e, connection_ptr conn);
|
---|
| 151 |
|
---|
| 152 | private:
|
---|
| 153 | //!> reference to external FragmentQueue containing jobs to work on
|
---|
| 154 | FragmentQueue & JobsQueue;
|
---|
| 155 |
|
---|
[9a3f84] | 156 | //!> bunch of jobs received from controller before placed in JobsQueue
|
---|
[8036b7] | 157 | std::vector<FragmentJob::ptr> jobs;
|
---|
| 158 |
|
---|
[9a3f84] | 159 | //!> number of jobs that are waiting to be and are calculated, required for returning status
|
---|
[8036b7] | 160 | std::vector<size_t> jobInfo;
|
---|
| 161 |
|
---|
[9a3f84] | 162 | //!> choice
|
---|
[8036b7] | 163 | enum ControllerChoices choice;
|
---|
| 164 |
|
---|
[2344a3] | 165 | //!> bound function to shutdown all sockets
|
---|
| 166 | boost::function<void ()> shutdownAllSockets;
|
---|
| 167 |
|
---|
[8036b7] | 168 | // TODO: replace this instance by a IdPool.
|
---|
| 169 | //!> global id to give next available job id
|
---|
| 170 | GlobalJobId globalId;
|
---|
| 171 | };
|
---|
[402bde] | 172 |
|
---|
| 173 | private:
|
---|
[2344a3] | 174 | //!> static entity to indicate to clients that the queue is empty.
|
---|
| 175 | static FragmentJob::ptr NoJob;
|
---|
| 176 |
|
---|
| 177 | //!> reference to the io_service which we use for connections
|
---|
| 178 | boost::asio::io_service& io_service;
|
---|
| 179 |
|
---|
[9a3f84] | 180 | //!> Queue with data to be sent to each client.
|
---|
[41c1b7] | 181 | FragmentQueue JobsQueue;
|
---|
| 182 |
|
---|
| 183 | //!> Pool of Workers
|
---|
| 184 | WorkerPool pool;
|
---|
| 185 |
|
---|
[8036b7] | 186 | //!> Listener instance that waits for a worker
|
---|
| 187 | WorkerListener_t WorkerListener;
|
---|
[72eaf7f] | 188 |
|
---|
[8036b7] | 189 | //!> Listener instance that waits for a controller
|
---|
| 190 | ControllerListener_t ControllerListener;
|
---|
[778abb] | 191 |
|
---|
[8036b7] | 192 | public:
|
---|
| 193 | /** Getter for Exitflag.
|
---|
| 194 | *
|
---|
| 195 | * @return Exitflag of operations
|
---|
| 196 | */
|
---|
| 197 | size_t getExitflag() const
|
---|
| 198 | {
|
---|
| 199 | if (WorkerListener.getExitflag() != 0)
|
---|
| 200 | return WorkerListener.getExitflag();
|
---|
| 201 | if (ControllerListener.getExitflag() != 0)
|
---|
| 202 | return ControllerListener.getExitflag();
|
---|
| 203 | return 0;
|
---|
| 204 | }
|
---|
[d1dbfc] | 205 |
|
---|
[41c1b7] | 206 | private:
|
---|
| 207 | //!> Connection for sending jobs to workers
|
---|
| 208 | Connection connection;
|
---|
| 209 |
|
---|
| 210 | //!> internal operation to send jobs to workers
|
---|
| 211 | mutable SendJobToWorkerOperation sendJobOp;
|
---|
[6ea7f4] | 212 |
|
---|
| 213 | //!> internal operation to shutdown a worker
|
---|
| 214 | ShutdownWorkerOperation shutdownWorkerOp;
|
---|
[72eaf7f] | 215 | };
|
---|
| 216 |
|
---|
[cd4a6e] | 217 | #endif /* FRAGMENTSCHEDULER_HPP_ */
|
---|