1 package at.ac.tuwien.lsdc.sched;
4 import java.io.IOException;
5 import java.util.LinkedList;
6 import java.util.SortedMap;
7 import java.util.TreeMap;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
12 import at.ac.tuwien.lsdc.management.MachineManager;
13 import at.ac.tuwien.lsdc.types.Application;
14 import at.ac.tuwien.lsdc.types.ScenarioData;
15 import at.ac.tuwien.lsdc.types.ScenarioType;
16 import at.ac.tuwien.lsdc.types.SchedulerData;
17 import at.ac.tuwien.lsdc.types.SchedulerEvent;
18 import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
19 import at.ac.tuwien.lsdc.types.SchedulerType;
20 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
21 import at.ac.tuwien.lsdc.util.CSVLogger;
23 public abstract class AbstractScheduler {
25 private static final Logger log = LoggerFactory
26 .getLogger(AbstractScheduler.class);
28 // the following types are only needed for correct
30 protected SchedulerType schedulerType;
31 protected ScenarioType scenario;
32 protected int numTotalInSourced;
33 protected int numTotalOutSourced;
34 protected int numCurrInSourced;
35 protected int numCurrOutSourced;
36 protected double totalConsumption;
38 protected MachineManager manager;
39 protected File schedulerLog;
40 protected CSVLogger logger;
41 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)
48 // protected HashMap<Long, LinkedList<SchedulerEvent>> eventMap;
50 protected SortedMap<Long, LinkedList<SchedulerEvent>> eventMap;
52 // Scheduler has an internal Time "Abstraction"
53 // at every point in time it checks for Events in his "EventList"
54 // and handles them (via the individual scheduling algorithm)
55 protected long currTime;
57 // the timestamp at which the Scheduler is finished
58 // it is updated with every added "EndEvent"
59 protected long endTime;
62 public AbstractScheduler(int numPMs, int numCloudPartners,
63 File schedulerLog, SchedulerType schedulerType,
64 ScenarioType scenario) throws IOException {
65 this.manager = new MachineManager(numPMs);
66 this.schedulerLog = schedulerLog;
67 this.schedulerType = schedulerType;
68 this.scenario = scenario;
69 this.eventMap = new TreeMap<Long, LinkedList<SchedulerEvent>>();
70 this.logger = new CSVLogger(schedulerLog);
71 initCloudPartners(numCloudPartners);
74 // federation = new Federation(numCloudPartners);
77 private void initCloudPartners(int numCloudPartners) {
78 for (int i = 0; i < numCloudPartners; i++) {
79 // initialize a cloudpartner
80 // add to a member: List<CloudPartner>
81 // CloudPartner should have 2 functions:
82 // insource & outsource
83 // insource randomly gives an application to us
84 // outsource accepts randomly applications from us
88 // Initialize Scheduler with Data from CSV
89 // CSV will be parsed and sent as List<Application> to Scheduler
90 public ScenarioData initAndStart(LinkedList<Application> apps) {
91 for (Application app : apps) {
92 insertStartEvent(app.getTimestamp(), app);
93 insertStopEvent(app.getTimestamp() + app.getDuration(), app);
98 } catch (IOException e) {
99 log.error("couldn't close logger");
101 return doEndLogging();
105 * Insert a start event into the map, at timestamp when the application should start
106 * @param timestamp the timestamp when the application should start
107 * @param app the application to start
109 private void insertStartEvent(long timestamp, Application app) {
110 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startApplication, app);
111 if(!eventMap.containsKey(timestamp)) {
112 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
114 eventMap.put(timestamp, list);
116 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
122 * Insert a stop event into the map, at timestamp when the application should stop
123 * @param timestamp the timestamp when the application should stop
124 * @param app the application to stop
126 private void insertStopEvent(long timestamp, Application app) {
127 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endApplication, app);
128 if(!eventMap.containsKey(timestamp)) {
129 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
131 eventMap.put(timestamp, list);
133 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
136 if(endTime < timestamp) {
141 protected void startScheduling() {
143 LinkedList<SchedulerEvent> events = eventMap.get(currTime);
144 handleEvents(events);
146 //advance Time to next Event
149 if (currTime == endTime) {
150 // reached last Event, Scheduler will shut down
156 // this method is where the Scheduling Algorithm resides
157 // it reads the Events (start & stop of applications)
158 protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
160 protected void doStateLogging() {
161 SchedulerData data = new SchedulerData(currTime, 1, 1, 1,
162 manager.countCurrentlyRunningPMs(),
163 manager.countCurrentlyRunningVMs(),
164 manager.getCurrentConsumption(), numCurrInSourced,
166 totalConsumption += manager.getCurrentConsumption();
168 logger.logSchedulerData(data);
169 } catch (IOException e) {
170 log.error("error logging data");
174 // this creates the total summary which should be written to a CSV at the
176 protected ScenarioData doEndLogging() {
177 return new ScenarioData(schedulerType.toString(), scenario.toString(),
178 1, 1, 1, 1, numTotalInSourced, numTotalOutSourced);