]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
removed some commented code
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / sched / AbstractScheduler.java
1 package at.ac.tuwien.lsdc.sched;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.LinkedList;
6 import java.util.SortedMap;
7 import java.util.TreeMap;
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import at.ac.tuwien.lsdc.federation.Federation;
13 import at.ac.tuwien.lsdc.management.MachineManager;
14 import at.ac.tuwien.lsdc.types.Application;
15 import at.ac.tuwien.lsdc.types.ScenarioData;
16 import at.ac.tuwien.lsdc.types.ScenarioType;
17 import at.ac.tuwien.lsdc.types.SchedulerData;
18 import at.ac.tuwien.lsdc.types.SchedulerEvent;
19 import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
20 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
21 import at.ac.tuwien.lsdc.util.CSVLogger;
22 import at.ac.tuwien.lsdc.util.NumberUtils;
23
24 /**
25  * Class doing all the generic scheduling work.
26  */
27 public abstract class AbstractScheduler {
28
29         private static final Logger log = LoggerFactory.getLogger(AbstractScheduler.class);
30
31         // the following types are only needed for correct
32         // log output
33         protected ScenarioType scenario;
34         protected int numTotalInSourced;
35         protected int numTotalOutSourced;
36         protected int numCurrInSourced;
37         protected int numCurrOutSourced;
38         protected double totalConsumption;
39
40         protected MachineManager manager;
41         protected File schedulerLog;
42         protected CSVLogger logger;
43         protected VMType vmType;
44
45         /**
46          * this map saves the following Type of Events: start of an Application, end
47          * of an Application(start outSourced, end outSourced, start inSourced, end
48          * inSourced)
49          */
50         protected SortedMap<Long, LinkedList<SchedulerEvent>> eventMap;
51
52         /**
53          * Scheduler has an internal Time "Abstraction" at every point in time it
54          * checks for Events in his "EventList" and handles them (via the individual
55          * scheduling algorithm)
56          */
57         protected long currTime = 0;
58
59         /**
60          * the timestamp at which the Scheduler is finished it is updated with every
61          * added "EndEvent"
62          */
63         protected long endTime;
64
65         /**
66          * All our cloud partners.
67          */
68         protected Federation federation;
69
70         public AbstractScheduler(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
71                         throws IOException {
72                 manager = new MachineManager(numPMs);
73                 this.schedulerLog = schedulerLog;
74                 this.scenario = scenario;
75                 eventMap = new TreeMap<Long, LinkedList<SchedulerEvent>>();
76                 logger = new CSVLogger(schedulerLog);
77                 federation = new Federation(numCloudPartners);
78         }
79
80         /**
81          * Initialize Scheduler with Data from CSV CSV will be parsed and sent as
82          * List<Application> to Scheduler
83          */
84         public ScenarioData initAndStart(LinkedList<Application> apps) {
85                 for (Application app : apps) {
86                         insertStartEvent(app.getTimestamp(), app);
87                 }
88
89                 endTime = apps.getLast().getTimestamp() + 1;
90
91                 startScheduling();
92                 return doEndLogging();
93         }
94
95         /**
96          * Insert a start event into the map, at timestamp when the application should
97          * start
98          * 
99          * @param timestamp
100          *          the timestamp when the application should start
101          * @param app
102          *          the application to start
103          */
104         protected void insertStartEvent(long timestamp, Application app) {
105                 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startApplication, app);
106                 if (!eventMap.containsKey(timestamp)) {
107                         LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
108                         list.add(evt);
109                         eventMap.put(timestamp, list);
110                 } else {
111                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
112                         list.add(evt);
113                 }
114         }
115
116         /**
117          * Insert a start outsourced event into the map, at timestamp when the
118          * application should start
119          * 
120          * @param timestamp
121          *          the timestamp when the application should start
122          * @param app
123          *          the application to start
124          */
125         protected void insertOutsourcedStartEvent(long timestamp, Application app) {
126                 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startOutsourcedApplication, app);
127                 if (!eventMap.containsKey(timestamp)) {
128                         LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
129                         list.add(evt);
130                         eventMap.put(timestamp, list);
131                 } else {
132                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
133                         list.add(evt);
134                 }
135         }
136
137         /**
138          * Insert a stop event into the map, at timestamp when the application should
139          * stop.
140          * 
141          * @param timestamp
142          *          the timestamp when the application should stop
143          * @param app
144          *          the application to stop
145          */
146         protected void insertStopEvent(long timestamp, Application app) {
147                 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endApplication, app);
148                 if (!eventMap.containsKey(timestamp)) {
149                         LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
150                         list.add(evt);
151                         eventMap.put(timestamp, list);
152                 } else {
153                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
154                         list.add(evt);
155                 }
156                 if (endTime < timestamp) {
157                         endTime = timestamp;
158                 }
159         }
160
161         /**
162          * Insert a stop event into the map, at timestamp when the application should
163          * stop.
164          * 
165          * @param timestamp
166          *          the timestamp when the application should stop
167          * @param app
168          *          the application to stop
169          */
170         protected void insertOutsourcedStopEvent(long timestamp, Application app) {
171                 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endOutsourcedApplication, app);
172                 if (!eventMap.containsKey(timestamp)) {
173                         LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
174                         list.add(evt);
175                         eventMap.put(timestamp, list);
176                 } else {
177                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
178                         list.add(evt);
179                 }
180                 if (endTime < timestamp) {
181                         endTime = timestamp;
182                 }
183         }
184
185         /**
186          * Start the actual scheduling algorithm. Each scheduler will implement the
187          * method handleEvents differently
188          */
189         protected void startScheduling() {
190                 while (true) {
191                         if (eventMap.containsKey(currTime)) {
192                                 LinkedList<SchedulerEvent> events = eventMap.get(currTime);
193                                 // log.info(events.size() + " events at timestamp " + currTime);
194                                 handleEvents(events);
195                         }
196                         doStateLogging();
197                         // advance Time to next step
198                         currTime++;
199
200                         if (currTime > endTime) {
201                                 // reached last Event, Scheduler will shut down
202                                 log.info("Last event reached at time " + currTime);
203                                 break;
204                         }
205                 }
206         }
207
208         /**
209          * this method is where the Scheduling Algorithm resides it reads the Events
210          * (start & stop of applications)
211          * 
212          * @param events
213          *          the events to be read and used by the scheduler
214          */
215         protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
216
217         /**
218          * Does the logging for each timestamp. It uses the CSVLogger to write
219          * directly to the specific scheduler/scenario csv file
220          */
221         protected void doStateLogging() {
222                 SchedulerData data = new SchedulerData(currTime, manager.getTotalRam(), manager.getTotalCpu(),
223                                 manager.getTotalSize(), manager.countCurrentlyRunningPMs(), manager.countCurrentlyRunningVMs(),
224                                 manager.getCurrentConsumption(), numCurrInSourced, numCurrOutSourced);
225                 totalConsumption += manager.getCurrentConsumption();
226                 try {
227                         logger.logSchedulerData(data);
228                 } catch (IOException e) {
229                         log.error("error logging data");
230                 }
231         }
232
233         /**
234          * this creates the total summary which should be written to a CSV at the end
235          * 
236          * @return a ScenarioData Object that holds the values to be logged
237          */
238         protected ScenarioData doEndLogging() {
239                 return new ScenarioData(getSchedulerType(), scenario.toString(), manager.getTotalPMs(), manager.getTotalVMs(),
240                                 endTime, NumberUtils.roundDouble(totalConsumption), numTotalInSourced, numTotalOutSourced);
241         }
242
243         protected abstract String getSchedulerType();
244
245 }