1 package at.ac.tuwien.lsdc.sched;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.LinkedList;
7 import java.util.SortedMap;
8 import java.util.TreeMap;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
13 import at.ac.tuwien.lsdc.federation.Federation;
14 import at.ac.tuwien.lsdc.management.MachineManager;
15 import at.ac.tuwien.lsdc.types.Application;
16 import at.ac.tuwien.lsdc.types.ScenarioData;
17 import at.ac.tuwien.lsdc.types.ScenarioType;
18 import at.ac.tuwien.lsdc.types.SchedulerData;
19 import at.ac.tuwien.lsdc.types.SchedulerEvent;
20 import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
21 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
22 import at.ac.tuwien.lsdc.util.CSVLogger;
23 import at.ac.tuwien.lsdc.util.NumberUtils;
26 * Class doing all the generic scheduling work.
28 public abstract class AbstractScheduler {
30 private static final Logger log = LoggerFactory.getLogger(AbstractScheduler.class);
32 // the following types are only needed for correct
34 protected ScenarioType scenario;
35 protected int numTotalInSourced;
36 protected int numTotalOutSourced;
37 protected int numCurrInSourced;
38 protected int numCurrOutSourced;
39 protected double totalConsumption;
41 protected MachineManager manager;
42 protected File schedulerLog;
43 protected CSVLogger logger;
44 protected VMType vmType;
46 // List of Apps that were delayed due to not enough free resources.
47 protected ArrayList<Application> delayedApps;
50 * this map saves the following Type of Events: start of an Application, end of an
51 * Application(start outSourced, end outSourced, start inSourced, end inSourced)
53 protected SortedMap<Long, LinkedList<SchedulerEvent>> eventMap;
56 * Scheduler has an internal Time "Abstraction" at every point in time it checks for Events in
57 * his "EventList" and handles them (via the individual scheduling algorithm)
59 protected long currTime = 0;
62 * the timestamp at which the Scheduler is finished it is updated with every added "EndEvent"
64 protected long endTime;
67 * All our cloud partners.
69 protected Federation federation;
71 public AbstractScheduler(int numPMs, int numCloudPartners, File schedulerLog,
72 ScenarioType scenario) throws IOException {
73 manager = new MachineManager(numPMs);
74 this.schedulerLog = schedulerLog;
75 this.scenario = scenario;
76 eventMap = new TreeMap<Long, LinkedList<SchedulerEvent>>();
77 logger = new CSVLogger(schedulerLog);
78 federation = new Federation(numCloudPartners);
79 delayedApps = new ArrayList<Application>();
83 * Initialize Scheduler with Data from CSV CSV will be parsed and sent as List<Application> to
86 public ScenarioData initAndStart(LinkedList<Application> apps) {
87 for (Application app : apps) {
88 insertStartEvent(app.getTimestamp(), app);
91 endTime = apps.getLast().getTimestamp() + 1;
94 return doEndLogging();
98 * Insert a start event into the map, at timestamp when the application should start
100 * @param timestamp the timestamp when the application should start
101 * @param app the application to start
103 protected void insertStartEvent(long timestamp, Application app) {
104 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startApplication, app);
105 if (!eventMap.containsKey(timestamp)) {
106 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
108 eventMap.put(timestamp, list);
110 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
116 * Insert a start outsourced event into the map, at timestamp when the application should start
118 * @param timestamp the timestamp when the application should start
119 * @param app the application to start
121 protected void insertOutsourcedStartEvent(long timestamp, Application app) {
122 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startOutsourcedApplication,
124 if (!eventMap.containsKey(timestamp)) {
125 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
127 eventMap.put(timestamp, list);
129 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
135 * Insert a stop event into the map, at timestamp when the application should stop.
137 * @param timestamp the timestamp when the application should stop
138 * @param app the application to stop
140 protected void insertStopEvent(long timestamp, Application app) {
141 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endApplication, app);
142 if (!eventMap.containsKey(timestamp)) {
143 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
145 eventMap.put(timestamp, list);
147 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
150 if (endTime < timestamp) {
156 * Insert a stop event into the map, at timestamp when the application should stop.
158 * @param timestamp the timestamp when the application should stop
159 * @param app the application to stop
161 protected void insertOutsourcedStopEvent(long timestamp, Application app) {
162 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endOutsourcedApplication, app);
163 if (!eventMap.containsKey(timestamp)) {
164 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
166 eventMap.put(timestamp, list);
168 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
171 if (endTime < timestamp) {
177 * Start the actual scheduling algorithm. Each scheduler will implement the method handleEvents
180 protected void startScheduling() {
182 if (eventMap.containsKey(currTime)) {
183 LinkedList<SchedulerEvent> events = eventMap.get(currTime);
184 // log.info(events.size() + " events at timestamp " + currTime);
185 handleEvents(events);
188 // advance Time to next step
191 if (currTime > endTime) {
192 // reached last Event, Scheduler will shut down
193 log.info("Last event reached at time " + currTime);
200 * this method is where the Scheduling Algorithm resides it reads the Events (start & stop of
203 * @param events the events to be read and used by the scheduler
205 protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
208 * Does the logging for each timestamp. It uses the CSVLogger to write directly to the specific
209 * scheduler/scenario csv file
211 protected void doStateLogging() {
212 SchedulerData data = new SchedulerData(currTime, manager.getTotalRam(),
213 manager.getTotalCpu(), manager.getTotalSize(), manager.countCurrentlyRunningPMs(),
214 manager.countCurrentlyRunningVMs(), manager.getCurrentConsumption(),
215 numCurrInSourced, numCurrOutSourced);
216 totalConsumption += manager.getCurrentConsumption();
218 logger.logSchedulerData(data);
219 } catch (IOException e) {
220 log.error("error logging data");
225 * this creates the total summary which should be written to a CSV at the end
227 * @return a ScenarioData Object that holds the values to be logged
229 protected ScenarioData doEndLogging() {
230 return new ScenarioData(getSchedulerType(), scenario.toString(), manager.getTotalPMs(),
231 manager.getTotalVMs(), endTime, NumberUtils.roundDouble(totalConsumption),
232 numTotalInSourced, numTotalOutSourced);
235 protected abstract String getSchedulerType();