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.SchedulerType;
19 import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
20 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
21 import at.ac.tuwien.lsdc.util.CSVLogger;
22 import at.ac.tuwien.lsdc.util.NumberUtils;
24 public abstract class AbstractScheduler {
26 private static final Logger log = LoggerFactory
27 .getLogger(AbstractScheduler.class);
29 // the following types are only needed for correct
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 = 0;
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, ScenarioType scenario) throws IOException {
59 this.manager = new MachineManager(numPMs);
60 this.schedulerLog = schedulerLog;
61 this.scenario = scenario;
62 this.eventMap = new TreeMap<Long, LinkedList<SchedulerEvent>>();
63 this.logger = new CSVLogger(schedulerLog);
64 initCloudPartners(numCloudPartners);
67 // federation = new Federation(numCloudPartners);
70 // intialize cloudpartners for federation
71 private void initCloudPartners(int numCloudPartners) {
72 for (int i = 0; i < numCloudPartners; i++) {
73 // initialize a cloudpartner
74 // add to a member: List<CloudPartner>
75 // CloudPartner should have 2 functions:
76 // insource & outsource
77 // insource randomly gives an application to us
78 // outsource accepts randomly applications from us
82 // Initialize Scheduler with Data from CSV
83 // CSV will be parsed and sent as List<Application> to Scheduler
84 public ScenarioData initAndStart(LinkedList<Application> apps) {
85 for (Application app : apps) {
86 insertStartEvent(app.getTimestamp(), app);
89 endTime = apps.getLast().getTimestamp() + 1;
94 } catch (IOException e) {
95 log.error("couldn't close logger");
97 // TODO make end logging work
98 return doEndLogging();
102 * Insert a start event into the map, at timestamp when the application
106 * the timestamp when the application should start
108 * the application to start
110 protected void insertStartEvent(long timestamp, Application app) {
111 SchedulerEvent evt = new SchedulerEvent(timestamp,
112 EventType.startApplication, 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);
124 * Insert a stop event into the map, at timestamp when the application
128 * the timestamp when the application should stop
130 * the application to stop
132 protected void insertStopEvent(long timestamp, Application app) {
133 SchedulerEvent evt = new SchedulerEvent(timestamp,
134 EventType.endApplication, app);
135 if (!eventMap.containsKey(timestamp)) {
136 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
138 eventMap.put(timestamp, list);
140 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
143 if (endTime < timestamp) {
149 * Start the actual scheduling algorithm. Each scheduler will implement the
150 * method handleEvents differently
152 protected void startScheduling() {
154 if (eventMap.containsKey(currTime)) {
155 LinkedList<SchedulerEvent> events = eventMap.get(currTime);
156 // log.info(events.size() + " events at timestamp " + currTime);
157 handleEvents(events);
160 // advance Time to next step
163 if (currTime > endTime) {
164 // reached last Event, Scheduler will shut down
165 log.info("Last event reached at time " + currTime);
172 * this method is where the Scheduling Algorithm resides it reads the Events
173 * (start & stop of applications)
176 * the events to be read and used by the scheduler
178 protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
181 * Does the logging for each timestamp. It uses the CSVLogger to write
182 * directly to the specific scheduler/scenario csv file
184 protected void doStateLogging() {
185 SchedulerData data = new SchedulerData(
187 manager.getTotalRam(),
188 manager.getTotalCpu(),
189 manager.getTotalSize(),
190 manager.countCurrentlyRunningPMs(),
191 manager.countCurrentlyRunningVMs(),
192 manager.getCurrentConsumption(),
196 totalConsumption += manager.getCurrentConsumption();
198 logger.logSchedulerData(data);
199 } catch (IOException e) {
200 log.error("error logging data");
205 * this creates the total summary which should be written to a CSV at the
208 * @return a ScenarioData Object that holds the values to be logged
210 protected ScenarioData doEndLogging() {
211 return new ScenarioData(getSchedulerType(),
212 scenario.toString(), manager.getTotalPMs(), manager.getTotalVMs(), endTime, NumberUtils.roundDouble(totalConsumption), numTotalInSourced,
216 protected abstract String getSchedulerType();