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;
43 // this map saves the following Type of Events:
44 // start of an Application, end of an Application
45 // (start outSourced, end outSourced, start inSourced, end inSourced)
46 protected SortedMap<Long, LinkedList<SchedulerEvent>> eventMap;
48 // Scheduler has an internal Time "Abstraction"
49 // at every point in time it checks for Events in his "EventList"
50 // and handles them (via the individual scheduling algorithm)
51 protected long currTime;
53 // the timestamp at which the Scheduler is finished
54 // it is updated with every added "EndEvent"
55 protected long endTime;
57 public AbstractScheduler(int numPMs, int numCloudPartners,
58 File schedulerLog, SchedulerType schedulerType,
59 ScenarioType scenario) throws IOException {
60 this.manager = new MachineManager(numPMs);
61 this.schedulerLog = schedulerLog;
62 this.schedulerType = schedulerType;
63 this.scenario = scenario;
64 this.eventMap = new TreeMap<Long, LinkedList<SchedulerEvent>>();
65 this.logger = new CSVLogger(schedulerLog);
66 initCloudPartners(numCloudPartners);
69 // federation = new Federation(numCloudPartners);
72 // intialize cloudpartners for federation
73 private void initCloudPartners(int numCloudPartners) {
74 for (int i = 0; i < numCloudPartners; i++) {
75 // initialize a cloudpartner
76 // add to a member: List<CloudPartner>
77 // CloudPartner should have 2 functions:
78 // insource & outsource
79 // insource randomly gives an application to us
80 // outsource accepts randomly applications from us
84 // Initialize Scheduler with Data from CSV
85 // CSV will be parsed and sent as List<Application> to Scheduler
86 public ScenarioData initAndStart(LinkedList<Application> apps) {
87 for (Application app : apps) {
88 insertStartEvent(app.getTimestamp(), app);
91 log.info("event map = " + eventMap.size());
95 } catch (IOException e) {
96 log.error("couldn't close logger");
98 // TODO make end logging work
100 // return doEndLogging();
104 * Insert a start event into the map, at timestamp when the application
108 * the timestamp when the application should start
110 * the application to start
112 protected void insertStartEvent(long timestamp, Application app) {
113 SchedulerEvent evt = new SchedulerEvent(timestamp,
114 EventType.startApplication, app);
115 if (!eventMap.containsKey(timestamp)) {
116 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
118 eventMap.put(timestamp, list);
120 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
126 * Insert a stop event into the map, at timestamp when the application
130 * the timestamp when the application should stop
132 * the application to stop
134 protected void insertStopEvent(long timestamp, Application app) {
135 SchedulerEvent evt = new SchedulerEvent(timestamp,
136 EventType.endApplication, app);
137 if (!eventMap.containsKey(timestamp)) {
138 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
140 eventMap.put(timestamp, list);
142 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
145 if (endTime < timestamp) {
151 * Start the actual scheduling algorithm. Each scheduler will implement the
152 * method handleEvents differently
154 protected void startScheduling() {
156 if (eventMap.containsKey(currTime)) {
157 LinkedList<SchedulerEvent> events = eventMap.get(currTime);
158 log.info(events.size() + " events at timestamp " + currTime);
159 handleEvents(events);
162 // advance Time to next Event
165 if (currTime == endTime) {
166 // reached last Event, Scheduler will shut down
167 log.info("Last event reached at time " + currTime);
174 * this method is where the Scheduling Algorithm resides it reads the Events
175 * (start & stop of applications)
178 * the events to be read and used by the scheduler
180 protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
183 * Does the logging for each timestamp. It uses the CSVLogger to write
184 * directly to the specific scheduler/scenario csv file
186 protected void doStateLogging() {
187 SchedulerData data = new SchedulerData(currTime, manager.getTotalRam(),
188 manager.getTotalCpu(), manager.getTotalSize(),
189 manager.countCurrentlyRunningPMs(),
190 manager.countCurrentlyRunningVMs(),
191 manager.getCurrentConsumption(), numCurrInSourced,
193 totalConsumption += manager.getCurrentConsumption();
195 logger.logSchedulerData(data);
196 } catch (IOException e) {
197 log.error("error logging data");
202 * this creates the total summary which should be written to a CSV at the
205 * @return a ScenarioData Object that holds the values to be logged
207 protected ScenarioData doEndLogging() {
208 return new ScenarioData(schedulerType.toString(), scenario.toString(),
209 1, 1, 1, 1, numTotalInSourced, numTotalOutSourced);