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;
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 should
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, EventType.startApplication, app);
112 if (!eventMap.containsKey(timestamp)) {
113 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
115 eventMap.put(timestamp, list);
117 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
123 * Insert a start outsourced event into the map, at timestamp when the
124 * application should start
127 * the timestamp when the application should start
129 * the application to start
131 protected void insertOutsourcedStartEvent(long timestamp, Application app) {
132 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startOutsourcedApplication, app);
133 if (!eventMap.containsKey(timestamp)) {
134 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
136 eventMap.put(timestamp, list);
138 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
144 * Insert a stop event into the map, at timestamp when the application should
148 * the timestamp when the application should stop
150 * the application to stop
152 protected void insertStopEvent(long timestamp, Application app) {
153 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endApplication, app);
154 if (!eventMap.containsKey(timestamp)) {
155 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
157 eventMap.put(timestamp, list);
159 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
162 if (endTime < timestamp) {
168 * Insert a stop event into the map, at timestamp when the application should
172 * the timestamp when the application should stop
174 * the application to stop
176 protected void insertOutsourcedStopEvent(long timestamp, Application app) {
177 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endOutsourcedApplication, app);
178 if (!eventMap.containsKey(timestamp)) {
179 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
181 eventMap.put(timestamp, list);
183 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
186 if (endTime < timestamp) {
192 * Start the actual scheduling algorithm. Each scheduler will implement the
193 * method handleEvents differently
195 protected void startScheduling() {
197 if (eventMap.containsKey(currTime)) {
198 LinkedList<SchedulerEvent> events = eventMap.get(currTime);
199 // log.info(events.size() + " events at timestamp " + currTime);
200 handleEvents(events);
203 // advance Time to next step
206 if (currTime > endTime) {
207 // reached last Event, Scheduler will shut down
208 log.info("Last event reached at time " + currTime);
215 * this method is where the Scheduling Algorithm resides it reads the Events
216 * (start & stop of applications)
219 * the events to be read and used by the scheduler
221 protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
224 * Does the logging for each timestamp. It uses the CSVLogger to write
225 * directly to the specific scheduler/scenario csv file
227 protected void doStateLogging() {
228 SchedulerData data = new SchedulerData(currTime, manager.getTotalRam(), manager.getTotalCpu(),
229 manager.getTotalSize(), manager.countCurrentlyRunningPMs(), manager.countCurrentlyRunningVMs(),
230 manager.getCurrentConsumption(), numCurrInSourced, numCurrOutSourced);
231 totalConsumption += manager.getCurrentConsumption();
233 logger.logSchedulerData(data);
234 } catch (IOException e) {
235 log.error("error logging data");
240 * this creates the total summary which should be written to a CSV at the end
242 * @return a ScenarioData Object that holds the values to be logged
244 protected ScenarioData doEndLogging() {
245 return new ScenarioData(getSchedulerType(), scenario.toString(), manager.getTotalPMs(), manager.getTotalVMs(),
246 endTime, NumberUtils.roundDouble(totalConsumption), numTotalInSourced, numTotalOutSourced);
249 protected abstract String getSchedulerType();