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)
47 protected SortedMap<Long, LinkedList<SchedulerEvent>> eventMap;
49 // Scheduler has an internal Time "Abstraction"
50 // at every point in time it checks for Events in his "EventList"
51 // and handles them (via the individual scheduling algorithm)
52 protected long currTime;
54 // the timestamp at which the Scheduler is finished
55 // it is updated with every added "EndEvent"
56 protected long endTime;
59 public AbstractScheduler(int numPMs, int numCloudPartners,
60 File schedulerLog, SchedulerType schedulerType,
61 ScenarioType scenario) throws IOException {
62 this.manager = new MachineManager(numPMs);
63 this.schedulerLog = schedulerLog;
64 this.schedulerType = schedulerType;
65 this.scenario = scenario;
66 this.eventMap = new TreeMap<Long, LinkedList<SchedulerEvent>>();
67 this.logger = new CSVLogger(schedulerLog);
68 initCloudPartners(numCloudPartners);
71 // federation = new Federation(numCloudPartners);
74 private void initCloudPartners(int numCloudPartners) {
75 for (int i = 0; i < numCloudPartners; i++) {
76 // initialize a cloudpartner
77 // add to a member: List<CloudPartner>
78 // CloudPartner should have 2 functions:
79 // insource & outsource
80 // insource randomly gives an application to us
81 // outsource accepts randomly applications from us
85 // Initialize Scheduler with Data from CSV
86 // CSV will be parsed and sent as List<Application> to Scheduler
87 public ScenarioData initAndStart(LinkedList<Application> apps) {
88 for (Application app : apps) {
89 insertStartEvent(app.getTimestamp(), app);
90 insertStopEvent(app.getTimestamp() + app.getDuration(), app);
95 } catch (IOException e) {
96 log.error("couldn't close logger");
98 return doEndLogging();
102 * Insert a start event into the map, at timestamp when the application should start
103 * @param timestamp the timestamp when the application should start
104 * @param app the application to start
106 private void insertStartEvent(long timestamp, Application app) {
107 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startApplication, app);
108 if(!eventMap.containsKey(timestamp)) {
109 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
111 eventMap.put(timestamp, list);
113 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
119 * Insert a stop event into the map, at timestamp when the application should stop
120 * @param timestamp the timestamp when the application should stop
121 * @param app the application to stop
123 private void insertStopEvent(long timestamp, Application app) {
124 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endApplication, app);
125 if(!eventMap.containsKey(timestamp)) {
126 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
128 eventMap.put(timestamp, list);
130 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
133 if(endTime < timestamp) {
139 * Start the actual scheduling algorithm.
140 * Each scheduler will implement the method handleEvents differently
142 protected void startScheduling() {
144 LinkedList<SchedulerEvent> events = eventMap.get(currTime);
145 handleEvents(events);
147 //advance Time to next Event
150 if (currTime == endTime) {
151 // reached last Event, Scheduler will shut down
152 log.debug("Last event reached at time "+currTime);
159 * this method is where the Scheduling Algorithm resides
160 * it reads the Events (start & stop of applications)
161 * @param events the events to be read and used by the scheduler
163 protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
166 * Does the logging for each timestamp.
167 * It uses the CSVLogger to write directly to the specific
168 * scheduler/scenario csv file
170 protected void doStateLogging() {
171 SchedulerData data = new SchedulerData(
173 manager.getTotalRam(),
174 manager.getTotalCpu(),
175 manager.getTotalSize(),
176 manager.countCurrentlyRunningPMs(),
177 manager.countCurrentlyRunningVMs(),
178 manager.getCurrentConsumption(),
182 totalConsumption += manager.getCurrentConsumption();
184 logger.logSchedulerData(data);
185 } catch (IOException e) {
186 log.error("error logging data");
191 * this creates the total summary which should be written to a CSV at the end
192 * @return a ScenarioData Object that holds the values to be logged
194 protected ScenarioData doEndLogging() {
195 return new ScenarioData(schedulerType.toString(), scenario.toString(),
196 1, 1, 1, 1, numTotalInSourced, numTotalOutSourced);