1 package at.ac.tuwien.lsdc.sched;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.LinkedList;
8 import java.util.SortedMap;
9 import java.util.TreeMap;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
14 import at.ac.tuwien.lsdc.federation.Federation;
15 import at.ac.tuwien.lsdc.management.MachineManager;
16 import at.ac.tuwien.lsdc.types.Application;
17 import at.ac.tuwien.lsdc.types.ScenarioData;
18 import at.ac.tuwien.lsdc.types.ScenarioType;
19 import at.ac.tuwien.lsdc.types.SchedulerData;
20 import at.ac.tuwien.lsdc.types.SchedulerEvent;
21 import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
22 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
23 import at.ac.tuwien.lsdc.util.CSVLogger;
24 import at.ac.tuwien.lsdc.util.NumberUtils;
27 * Class doing all the generic scheduling work.
29 public abstract class AbstractScheduler {
31 private static final Logger log = LoggerFactory.getLogger(AbstractScheduler.class);
33 // the following types are only needed for correct
35 protected ScenarioType scenario;
36 protected int numTotalInSourced;
37 protected int numTotalOutSourced;
38 protected int numCurrInSourced;
39 protected int numCurrOutSourced;
40 protected double totalConsumption;
42 protected MachineManager manager;
43 protected File schedulerLog;
44 protected CSVLogger logger;
45 protected VMType vmType;
47 // List of Apps that were delayed due to not enough free resources.
48 protected ArrayList<Application> delayedApps;
51 * this map saves the following Type of Events: start of an Application, end of an
52 * Application(start outSourced, end outSourced, start inSourced, end inSourced)
54 protected SortedMap<Long, HashMap<EventType, LinkedList<SchedulerEvent>>> eventMap;
57 * Scheduler has an internal Time "Abstraction" at every point in time it checks for Events in
58 * his "EventList" and handles them (via the individual scheduling algorithm)
60 protected static long currTime = 0;
63 * the timestamp at which the Scheduler is finished it is updated with every added "EndEvent"
65 protected long endTime;
68 * All our cloud partners.
70 protected Federation federation;
72 public AbstractScheduler(int numPMs, int numCloudPartners, File schedulerLog,
73 ScenarioType scenario) throws IOException {
74 manager = new MachineManager(numPMs);
75 this.schedulerLog = schedulerLog;
76 this.scenario = scenario;
77 eventMap = new TreeMap<Long, HashMap<EventType, LinkedList<SchedulerEvent>>>();
78 logger = new CSVLogger(schedulerLog);
79 federation = new Federation(numCloudPartners);
80 delayedApps = new ArrayList<Application>();
84 * Initialize Scheduler with Data from CSV CSV will be parsed and sent as List<Application> to
87 public ScenarioData initAndStart(LinkedList<Application> apps) {
88 for (Application app : apps) {
89 insertStartEvent(app.getTimestamp(), app);
92 endTime = apps.getLast().getTimestamp() + 1;
95 return doEndLogging();
99 * Insert a start event into the map, at timestamp when the application should start
101 * @param timestamp the timestamp when the application should start
102 * @param app the application to start
104 protected void insertStartEvent(long timestamp, Application app) {
105 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startApplication, app);
110 * Insert a start outsourced event into the map, at timestamp when the application should start
112 * @param timestamp the timestamp when the application should start
113 * @param app the application to start
115 protected void insertOutsourcedStartEvent(long timestamp, Application app) {
116 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startOutsourcedApplication,
122 * Insert a start insourced event into the map, at timestamp when the application should start
124 * @param timestamp the timestamp when the application should start
125 * @param app the application to start
127 protected void insertInsourcedStartEvent(long timestamp, Application app) {
128 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startInsourcedApplication,
134 * Insert a stop event into the map, at timestamp when the application should stop.
136 * @param timestamp the timestamp when the application should stop
137 * @param app the application to stop
139 protected void insertStopEvent(long timestamp, Application app) {
140 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endApplication, app);
142 if (endTime < timestamp) {
148 * Insert a stop event into the map, at timestamp when the application should stop.
150 * @param timestamp the timestamp when the application should stop
151 * @param app the application to stop
153 protected void insertOutsourcedStopEvent(long timestamp, Application app) {
154 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endOutsourcedApplication, app);
156 if (endTime < timestamp) {
162 * Insert a stop event into the map, at timestamp when the application should stop.
164 * @param timestamp the timestamp when the application should stop
165 * @param app the application to stop
167 protected void insertInsourcedStopEvent(long timestamp, Application app) {
168 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endInsourcedApplication, app);
170 if (endTime < timestamp) {
175 private void insertEvent(SchedulerEvent evt) {
176 LinkedList<SchedulerEvent> list;
177 if (!eventMap.containsKey(evt.getTimestamp())) {
178 HashMap<EventType, LinkedList<SchedulerEvent>> map = new HashMap<EventType, LinkedList<SchedulerEvent>>();
179 eventMap.put(evt.getTimestamp(), map);
181 if (!eventMap.get(evt.getTimestamp()).containsKey(evt.getType())) {
182 list = new LinkedList<SchedulerEvent>();
183 eventMap.get(evt.getTimestamp()).put(evt.getType(), list);
185 list = eventMap.get(evt.getTimestamp()).get(evt.getType());
191 * Start the actual scheduling algorithm. Each scheduler will implement the method handleEvents
194 protected void startScheduling() {
196 if (eventMap.containsKey(currTime)) {
197 // log.info(events.size() + " events at timestamp " + currTime);
198 handleEvents(eventMap.get(currTime));
201 // advance Time to next step
204 if (currTime > endTime) {
205 // reached last Event, Scheduler will shut down
206 log.info("Last event reached at time " + currTime);
213 * this method is where the Scheduling Algorithm resides it reads the Events (start & stop of
216 * @param events the events to be read and used by the scheduler
218 protected void handleEvents(HashMap<EventType, LinkedList<SchedulerEvent>> events) {
219 if (events.containsKey(EventType.endApplication))
220 handleEndEvents(events.get(EventType.endApplication));
221 if (events.containsKey(EventType.endOutsourcedApplication))
222 handleOutsourcedEndEvents(events.get(EventType.endOutsourcedApplication));
223 if (events.containsKey(EventType.endInsourcedApplication))
224 handleInsourcedEndEvents(events.get(EventType.endInsourcedApplication));
226 if (events.containsKey(EventType.startOutsourcedApplication))
227 handleOutsourcedStartEvents(events.get(EventType.startOutsourcedApplication));
228 if (events.containsKey(EventType.startApplication))
229 handleStartEvents(events.get(EventType.startApplication));
230 if (events.containsKey(EventType.startInsourcedApplication))
231 handleInsourcedStartEvents(events.get(EventType.startInsourcedApplication));
234 protected void handleDelayedApps() {
235 LinkedList<SchedulerEvent> delayedStartEvents = new LinkedList<SchedulerEvent>();
236 for (Application app : delayedApps) {
237 SchedulerEvent evt = new SchedulerEvent(currTime, EventType.startApplication, app);
238 delayedStartEvents.add(evt);
241 handleStartEvents(delayedStartEvents);
244 protected abstract void handleEndEvents(LinkedList<SchedulerEvent> events);
246 protected abstract void handleStartEvents(LinkedList<SchedulerEvent> events);
249 * handle running of outsourced apps.
251 * @param events list of all events that happened in this timeslot.
253 protected void handleOutsourcedStartEvents(LinkedList<SchedulerEvent> events) {
254 for (SchedulerEvent evt : events) {
255 if (evt.getType() == EventType.startOutsourcedApplication) {
256 insertOutsourcedStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
258 numTotalOutSourced++;
264 * handle running of outsourced apps.
266 * @param events list of all events that happened in this timeslot.
268 protected void handleInsourcedStartEvents(LinkedList<SchedulerEvent> events) {
269 for (SchedulerEvent evt : events) {
270 if (evt.getType() == EventType.startInsourcedApplication) {
271 insertInsourcedStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
279 * handle stopping of outsourced apps.
281 * @param events list of all events that happened in this timeslot.
283 protected void handleOutsourcedEndEvents(LinkedList<SchedulerEvent> events) {
284 for (SchedulerEvent evt : events) {
285 if (evt.getType() == EventType.endOutsourcedApplication) {
292 * handle stopping of outsourced apps.
294 * @param events list of all events that happened in this timeslot.
296 protected void handleInsourcedEndEvents(LinkedList<SchedulerEvent> events) {
297 for (SchedulerEvent evt : events) {
298 if (evt.getType() == EventType.endInsourcedApplication) {
305 * Does the logging for each timestamp. It uses the CSVLogger to write directly to the specific
306 * scheduler/scenario csv file
308 protected void doStateLogging() {
309 SchedulerData data = new SchedulerData(currTime, manager.getTotalRam(),
310 manager.getTotalCpu(), manager.getTotalSize(), manager.countCurrentlyRunningPMs(),
311 manager.countCurrentlyRunningVMs(), manager.getCurrentConsumption(),
312 numCurrInSourced, numCurrOutSourced);
313 totalConsumption += manager.getCurrentConsumption();
315 logger.logSchedulerData(data);
316 } catch (IOException e) {
317 log.error("error logging data");
322 * this creates the total summary which should be written to a CSV at the end
324 * @return a ScenarioData Object that holds the values to be logged
326 protected ScenarioData doEndLogging() {
327 return new ScenarioData(getSchedulerType(), scenario.toString(), manager.getTotalPMs(),
328 manager.getTotalVMs(), endTime, NumberUtils.roundDouble(totalConsumption),
329 numTotalInSourced, numTotalOutSourced, manager.getTotalResizeCalls());
332 protected abstract String getSchedulerType();
334 public static long getCurrentTime() {