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