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);
93 log.info("event map = "+ eventMap.size());
97 } catch (IOException e) {
98 log.error("couldn't close logger");
100 // TODO make end logging work
102 // return doEndLogging();
106 * Insert a start event into the map, at timestamp when the application should start
107 * @param timestamp the timestamp when the application should start
108 * @param app the application to start
110 private void insertStartEvent(long timestamp, Application app) {
111 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startApplication, app);
112 if(!eventMap.containsKey(timestamp)) {
113 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
115 eventMap.put(timestamp, list);
117 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
123 * Insert a stop event into the map, at timestamp when the application should stop
124 * @param timestamp the timestamp when the application should stop
125 * @param app the application to stop
127 private void insertStopEvent(long timestamp, Application app) {
128 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endApplication, app);
129 if(!eventMap.containsKey(timestamp)) {
130 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
132 eventMap.put(timestamp, list);
134 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
137 if(endTime < timestamp) {
143 * Start the actual scheduling algorithm.
144 * Each scheduler will implement the method handleEvents differently
146 protected void startScheduling() {
148 if(eventMap.containsKey(currTime)) {
149 LinkedList<SchedulerEvent> events = eventMap.get(currTime);
150 log.info(events.size()+" events at timestamp "+currTime);
151 handleEvents(events);
154 //advance Time to next Event
157 if (currTime == endTime) {
158 // reached last Event, Scheduler will shut down
159 log.info("Last event reached at time "+currTime);
166 * this method is where the Scheduling Algorithm resides
167 * it reads the Events (start & stop of applications)
168 * @param events the events to be read and used by the scheduler
170 protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
173 * Does the logging for each timestamp.
174 * It uses the CSVLogger to write directly to the specific
175 * scheduler/scenario csv file
177 protected void doStateLogging() {
178 SchedulerData data = new SchedulerData(
180 manager.getTotalRam(),
181 manager.getTotalCpu(),
182 manager.getTotalSize(),
183 manager.countCurrentlyRunningPMs(),
184 manager.countCurrentlyRunningVMs(),
185 manager.getCurrentConsumption(),
189 totalConsumption += manager.getCurrentConsumption();
191 logger.logSchedulerData(data);
192 } catch (IOException e) {
193 log.error("error logging data");
198 * this creates the total summary which should be written to a CSV at the end
199 * @return a ScenarioData Object that holds the values to be logged
201 protected ScenarioData doEndLogging() {
202 return new ScenarioData(schedulerType.toString(), scenario.toString(),
203 1, 1, 1, 1, numTotalInSourced, numTotalOutSourced);