1 package at.ac.tuwien.lsdc.sched;
4 import java.io.IOException;
6 import java.util.HashMap;
7 import java.util.LinkedList;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
13 import at.ac.tuwien.lsdc.federation.Federation;
14 import at.ac.tuwien.lsdc.management.MachineManager;
15 import at.ac.tuwien.lsdc.types.Application;
16 import at.ac.tuwien.lsdc.types.ScenarioData;
17 import at.ac.tuwien.lsdc.types.ScenarioType;
18 import at.ac.tuwien.lsdc.types.SchedulerData;
19 import at.ac.tuwien.lsdc.types.SchedulerEvent;
20 import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
21 import at.ac.tuwien.lsdc.types.SchedulerType;
22 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
23 import at.ac.tuwien.lsdc.util.CSVLogger;
25 public abstract class AbstractScheduler {
27 private static final Logger log = LoggerFactory.getLogger(AbstractScheduler.class);
29 // the following types are only needed for correct
31 protected SchedulerType schedulerType;
32 protected ScenarioType scenario;
33 protected int numInSourced;
34 protected int numOutSourced;
35 protected int numCurrInSourced;
36 protected int numCurrOutSourced;
38 protected MachineManager machineManager;
39 protected Federation federation;
40 protected File schedulerLog;
41 protected CSVLogger logger;
42 protected VMType VMType;
44 // this map saves the following Type of Events:
45 // start of an Application, end of an Application
46 // (start outSourced, end outSourced, start inSourced, end inSourced)
47 protected HashMap<Long, LinkedList<SchedulerEvent>> eventMap;
49 // // this map saves the following Type of Events:
50 // // start of an Application, end of an Application
51 // // (start outSourced, end outSourced, start inSourced, end inSourced)
52 // protected HashMap<Long, LinkedList<SchedulerEvent>> endEventMap;
54 // Scheduler has an internal Time "Abstraction"
55 // at every point in time it checks for Events in his "EventList"
56 // and handles them (via the individual scheduling algorithm)
57 protected long currTime;
59 // the timestamp at which the Scheduler is finished
60 // it is updated with every added "EndEvent"
61 protected long endTime;
63 public AbstractScheduler(File schedulerLog, SchedulerType schedulerType, ScenarioType scenario, int numPMs, int numCloudPartners) throws IOException {
64 this.schedulerLog = schedulerLog;
65 this.schedulerType = schedulerType;
66 this.scenario = scenario;
67 this.machineManager = new MachineManager(numPMs);
68 this.federation = new Federation(numCloudPartners);
69 this.eventMap = new HashMap<Long,LinkedList<SchedulerEvent>>();
70 this.logger = new CSVLogger(schedulerLog);
73 // Initialize Scheduler with Data from CSV
74 // CSV will be parsed and sent as List<Application> to Scheduler
75 public ScenarioData initAndStart(LinkedList<Application> apps) {
76 for (Application app : apps) {
77 insertStartEvent(app.getTimestamp(), app);
78 insertStopEvent(app.getTimestamp() + app.getDuration(), app);
83 } catch (IOException e) {
84 log.error("couldn't close logger");
86 return doEndLogging();
90 * Insert a start event into the map, at timestamp when the application should start
91 * @param timestamp the timestamp when the application should start
92 * @param app the application to start
94 private void insertStartEvent(long timestamp, Application app) {
95 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startApplication, app);
96 if(!eventMap.containsKey(timestamp)) {
97 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
99 eventMap.put(timestamp, list);
101 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
107 * Insert a stop event into the map, at timestamp when the application should stop
108 * @param timestamp the timestamp when the application should stop
109 * @param app the application to stop
111 private void insertStopEvent(long timestamp, Application app) {
112 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endApplication, app);
113 if(!eventMap.containsKey(timestamp)) {
114 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
116 eventMap.put(timestamp, list);
118 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
121 if(endTime < timestamp) {
126 protected void startScheduling() {
128 LinkedList<SchedulerEvent> events = eventMap.get(currTime);
129 handleEvents(events);
131 //advance Time to next Event
133 if (currTime == endTime) {
134 // reached last Event, Scheduler will shut down
140 // this method is where the Scheduling Algorithm resides
141 // it reads the Events (start & stop of applications)
142 protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
144 protected void doStateLogging() {
145 SchedulerData data = new SchedulerData(currTime,1,1,1,1,1,1,numCurrInSourced,numCurrOutSourced);
147 logger.logSchedulerData(data);
148 } catch (IOException e) {
149 log.error("error logging data");
153 // this creates the total summary which should be written to a CSV at the
155 protected ScenarioData doEndLogging() {
156 return new ScenarioData(schedulerType.toString(),scenario.toString(),1,1,1,1,1,1);