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.federation.Federation;
13 import at.ac.tuwien.lsdc.management.MachineManager;
14 import at.ac.tuwien.lsdc.types.Application;
15 import at.ac.tuwien.lsdc.types.ScenarioData;
16 import at.ac.tuwien.lsdc.types.ScenarioType;
17 import at.ac.tuwien.lsdc.types.SchedulerData;
18 import at.ac.tuwien.lsdc.types.SchedulerEvent;
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;
25 * Class doing all the generic scheduling work.
27 public abstract class AbstractScheduler {
29 private static final Logger log = LoggerFactory.getLogger(AbstractScheduler.class);
31 // the following types are only needed for correct
33 protected ScenarioType scenario;
34 protected int numTotalInSourced;
35 protected int numTotalOutSourced;
36 protected int numCurrInSourced;
37 protected int numCurrOutSourced;
38 protected double totalConsumption;
40 protected MachineManager manager;
41 protected File schedulerLog;
42 protected CSVLogger logger;
43 protected VMType vmType;
46 * this map saves the following Type of Events: start of an Application, end
47 * of an Application(start outSourced, end outSourced, start inSourced, end
50 protected SortedMap<Long, LinkedList<SchedulerEvent>> eventMap;
53 * Scheduler has an internal Time "Abstraction" at every point in time it
54 * checks for Events in his "EventList" and handles them (via the individual
55 * scheduling algorithm)
57 protected long currTime = 0;
60 * the timestamp at which the Scheduler is finished it is updated with every
63 protected long endTime;
66 * All our cloud partners.
68 protected Federation federation;
70 public AbstractScheduler(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
72 manager = new MachineManager(numPMs);
73 this.schedulerLog = schedulerLog;
74 this.scenario = scenario;
75 eventMap = new TreeMap<Long, LinkedList<SchedulerEvent>>();
76 logger = new CSVLogger(schedulerLog);
77 federation = new Federation(numCloudPartners);
81 * Initialize Scheduler with Data from CSV CSV will be parsed and sent as
82 * 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;
92 return doEndLogging();
96 * Insert a start event into the map, at timestamp when the application should
100 * the timestamp when the application should start
102 * the application to start
104 protected void insertStartEvent(long timestamp, Application app) {
105 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startApplication, app);
106 if (!eventMap.containsKey(timestamp)) {
107 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
109 eventMap.put(timestamp, list);
111 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
117 * Insert a start outsourced event into the map, at timestamp when the
118 * application should start
121 * the timestamp when the application should start
123 * the application to start
125 protected void insertOutsourcedStartEvent(long timestamp, Application app) {
126 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startOutsourcedApplication, app);
127 if (!eventMap.containsKey(timestamp)) {
128 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
130 eventMap.put(timestamp, list);
132 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
138 * Insert a stop event into the map, at timestamp when the application should
142 * the timestamp when the application should stop
144 * the application to stop
146 protected void insertStopEvent(long timestamp, Application app) {
147 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endApplication, app);
148 if (!eventMap.containsKey(timestamp)) {
149 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
151 eventMap.put(timestamp, list);
153 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
156 if (endTime < timestamp) {
162 * Insert a stop event into the map, at timestamp when the application should
166 * the timestamp when the application should stop
168 * the application to stop
170 protected void insertOutsourcedStopEvent(long timestamp, Application app) {
171 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endOutsourcedApplication, app);
172 if (!eventMap.containsKey(timestamp)) {
173 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
175 eventMap.put(timestamp, list);
177 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
180 if (endTime < timestamp) {
186 * Start the actual scheduling algorithm. Each scheduler will implement the
187 * method handleEvents differently
189 protected void startScheduling() {
191 if (eventMap.containsKey(currTime)) {
192 LinkedList<SchedulerEvent> events = eventMap.get(currTime);
193 // log.info(events.size() + " events at timestamp " + currTime);
194 handleEvents(events);
197 // advance Time to next step
200 if (currTime > endTime) {
201 // reached last Event, Scheduler will shut down
202 log.info("Last event reached at time " + currTime);
209 * this method is where the Scheduling Algorithm resides it reads the Events
210 * (start & stop of applications)
213 * the events to be read and used by the scheduler
215 protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
218 * Does the logging for each timestamp. It uses the CSVLogger to write
219 * directly to the specific scheduler/scenario csv file
221 protected void doStateLogging() {
222 SchedulerData data = new SchedulerData(currTime, manager.getTotalRam(), manager.getTotalCpu(),
223 manager.getTotalSize(), manager.countCurrentlyRunningPMs(), manager.countCurrentlyRunningVMs(),
224 manager.getCurrentConsumption(), numCurrInSourced, numCurrOutSourced);
225 totalConsumption += manager.getCurrentConsumption();
227 logger.logSchedulerData(data);
228 } catch (IOException e) {
229 log.error("error logging data");
234 * this creates the total summary which should be written to a CSV at the end
236 * @return a ScenarioData Object that holds the values to be logged
238 protected ScenarioData doEndLogging() {
239 return new ScenarioData(getSchedulerType(), scenario.toString(), manager.getTotalPMs(), manager.getTotalVMs(),
240 endTime, NumberUtils.roundDouble(totalConsumption), numTotalInSourced, numTotalOutSourced);
243 protected abstract String getSchedulerType();