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 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 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 protected void insertStopEvent(long timestamp, Application app) {
128 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endApplication, app);
130 if (endTime < timestamp) {
136 * Insert a stop event into the map, at timestamp when the application should stop.
138 * @param timestamp the timestamp when the application should stop
139 * @param app the application to stop
141 protected void insertOutsourcedStopEvent(long timestamp, Application app) {
142 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endOutsourcedApplication, app);
144 if (endTime < timestamp) {
149 private void insertEvent(SchedulerEvent evt) {
150 LinkedList<SchedulerEvent> list;
151 if (!eventMap.containsKey(evt.getTimestamp())) {
152 HashMap<EventType, LinkedList<SchedulerEvent>> map = new HashMap<EventType, LinkedList<SchedulerEvent>>();
153 eventMap.put(evt.getTimestamp(), map);
155 if (!eventMap.get(evt.getTimestamp()).containsKey(evt.getType())) {
156 list = new LinkedList<SchedulerEvent>();
157 eventMap.get(evt.getTimestamp()).put(evt.getType(), list);
159 list = eventMap.get(evt.getTimestamp()).get(evt.getType());
165 * Start the actual scheduling algorithm. Each scheduler will implement the method handleEvents
168 protected void startScheduling() {
170 if (eventMap.containsKey(currTime)) {
171 // log.info(events.size() + " events at timestamp " + currTime);
172 handleEvents(eventMap.get(currTime));
175 // advance Time to next step
178 if (currTime > endTime) {
179 // reached last Event, Scheduler will shut down
180 log.info("Last event reached at time " + currTime);
187 * this method is where the Scheduling Algorithm resides it reads the Events (start & stop of
190 * @param events the events to be read and used by the scheduler
192 private void handleEvents(HashMap<EventType, LinkedList<SchedulerEvent>> events) {
193 if (events.containsKey(EventType.endApplication))
194 handleEndEvents(events.get(EventType.endApplication));
195 if (events.containsKey(EventType.endOutsourcedApplication))
196 handleOutsourcedEndEvents(events.get(EventType.endOutsourcedApplication));
198 if (events.containsKey(EventType.startOutsourcedApplication))
199 handleOutsourcedStartEvents(events.get(EventType.startOutsourcedApplication));
200 if (events.containsKey(EventType.startApplication))
201 handleStartEvents(events.get(EventType.startApplication));
205 protected void handleDelayedApps() {
206 LinkedList<SchedulerEvent> delayedStartEvents = new LinkedList<SchedulerEvent>();
207 for (Application app : delayedApps) {
208 SchedulerEvent evt = new SchedulerEvent(currTime, EventType.startApplication, app);
209 delayedStartEvents.add(evt);
210 delayedApps.remove(app);
212 handleStartEvents(delayedStartEvents);
215 protected abstract void handleEndEvents(LinkedList<SchedulerEvent> events);
217 protected abstract void handleStartEvents(LinkedList<SchedulerEvent> events);
219 protected abstract void runMigration();
222 * handle running of outsourced apps.
224 * @param events list of all events that happened in this timeslot.
226 protected void handleOutsourcedStartEvents(LinkedList<SchedulerEvent> events) {
227 for (SchedulerEvent evt : events) {
228 if (evt.getType() == EventType.startOutsourcedApplication) {
229 insertOutsourcedStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
231 numTotalOutSourced++;
237 * handle stopping of outsourced apps.
239 * @param events list of all events that happened in this timeslot.
241 protected void handleOutsourcedEndEvents(LinkedList<SchedulerEvent> events) {
242 for (SchedulerEvent evt : events) {
243 if (evt.getType() == EventType.endOutsourcedApplication) {
250 * Does the logging for each timestamp. It uses the CSVLogger to write directly to the specific
251 * scheduler/scenario csv file
253 protected void doStateLogging() {
254 SchedulerData data = new SchedulerData(currTime, manager.getTotalRam(),
255 manager.getTotalCpu(), manager.getTotalSize(), manager.countCurrentlyRunningPMs(),
256 manager.countCurrentlyRunningVMs(), manager.getCurrentConsumption(),
257 numCurrInSourced, numCurrOutSourced);
258 totalConsumption += manager.getCurrentConsumption();
260 logger.logSchedulerData(data);
261 } catch (IOException e) {
262 log.error("error logging data");
267 * this creates the total summary which should be written to a CSV at the end
269 * @return a ScenarioData Object that holds the values to be logged
271 protected ScenarioData doEndLogging() {
272 return new ScenarioData(getSchedulerType(), scenario.toString(), manager.getTotalPMs(),
273 manager.getTotalVMs(), endTime, NumberUtils.roundDouble(totalConsumption),
274 numTotalInSourced, numTotalOutSourced);
277 protected abstract String getSchedulerType();