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.management.MachineManager;
13 import at.ac.tuwien.lsdc.types.Application;
14 import at.ac.tuwien.lsdc.types.ScenarioData;
15 import at.ac.tuwien.lsdc.types.ScenarioType;
16 import at.ac.tuwien.lsdc.types.SchedulerData;
17 import at.ac.tuwien.lsdc.types.SchedulerEvent;
18 import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
19 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
20 import at.ac.tuwien.lsdc.util.CSVLogger;
22 public abstract class AbstractScheduler {
24 private static final Logger log = LoggerFactory
25 .getLogger(AbstractScheduler.class);
27 // the following types are only needed for correct
29 protected ScenarioType scenario;
30 protected int numTotalInSourced;
31 protected int numTotalOutSourced;
32 protected int numCurrInSourced;
33 protected int numCurrOutSourced;
34 protected double totalConsumption;
36 protected MachineManager manager;
37 protected File schedulerLog;
38 protected CSVLogger logger;
39 protected VMType vmType;
41 // this map saves the following Type of Events:
42 // start of an Application, end of an Application
43 // (start outSourced, end outSourced, start inSourced, end inSourced)
44 protected SortedMap<Long, LinkedList<SchedulerEvent>> eventMap;
46 // Scheduler has an internal Time "Abstraction"
47 // at every point in time it checks for Events in his "EventList"
48 // and handles them (via the individual scheduling algorithm)
49 protected long currTime;
51 // the timestamp at which the Scheduler is finished
52 // it is updated with every added "EndEvent"
53 protected long endTime;
55 public AbstractScheduler(int numPMs, int numCloudPartners,
56 File schedulerLog, ScenarioType scenario) throws IOException {
57 this.manager = new MachineManager(numPMs);
58 this.schedulerLog = schedulerLog;
59 this.scenario = scenario;
60 this.eventMap = new TreeMap<Long, LinkedList<SchedulerEvent>>();
61 this.logger = new CSVLogger(schedulerLog);
62 initCloudPartners(numCloudPartners);
65 // federation = new Federation(numCloudPartners);
68 // intialize cloudpartners for federation
69 private void initCloudPartners(int numCloudPartners) {
70 for (int i = 0; i < numCloudPartners; i++) {
71 // initialize a cloudpartner
72 // add to a member: List<CloudPartner>
73 // CloudPartner should have 2 functions:
74 // insource & outsource
75 // insource randomly gives an application to us
76 // outsource accepts randomly applications from us
80 // Initialize Scheduler with Data from CSV
81 // CSV will be parsed and sent as List<Application> to Scheduler
82 public ScenarioData initAndStart(LinkedList<Application> apps) {
83 for (Application app : apps) {
84 insertStartEvent(app.getTimestamp(), app);
87 endTime = apps.getLast().getTimestamp() + 1;
92 } catch (IOException e) {
93 log.error("couldn't close logger");
95 // TODO make end logging work
97 // return doEndLogging();
101 * Insert a start event into the map, at timestamp when the application
105 * the timestamp when the application should start
107 * the application to start
109 protected void insertStartEvent(long timestamp, Application app) {
110 SchedulerEvent evt = new SchedulerEvent(timestamp,
111 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 stop event into the map, at timestamp when the application
127 * the timestamp when the application should stop
129 * the application to stop
131 protected void insertStopEvent(long timestamp, Application app) {
132 SchedulerEvent evt = new SchedulerEvent(timestamp,
133 EventType.endApplication, app);
134 if (!eventMap.containsKey(timestamp)) {
135 LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
137 eventMap.put(timestamp, list);
139 LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
142 if (endTime < timestamp) {
148 * Start the actual scheduling algorithm. Each scheduler will implement the
149 * method handleEvents differently
151 protected void startScheduling() {
153 if (eventMap.containsKey(currTime)) {
154 LinkedList<SchedulerEvent> events = eventMap.get(currTime);
155 // log.info(events.size() + " events at timestamp " + currTime);
156 handleEvents(events);
159 // advance Time to next step
162 if (currTime > endTime) {
163 // reached last Event, Scheduler will shut down
164 log.info("Last event reached at time " + currTime);
171 * this method is where the Scheduling Algorithm resides it reads the Events
172 * (start & stop of applications)
175 * the events to be read and used by the scheduler
177 protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
180 * Does the logging for each timestamp. It uses the CSVLogger to write
181 * directly to the specific scheduler/scenario csv file
183 protected void doStateLogging() {
184 SchedulerData data = new SchedulerData(
186 manager.getTotalRam(),
187 manager.getTotalCpu(),
188 manager.getTotalSize(),
189 manager.countCurrentlyRunningPMs(),
190 manager.countCurrentlyRunningVMs(),
191 manager.getCurrentConsumption(),
195 totalConsumption += manager.getCurrentConsumption();
197 logger.logSchedulerData(data);
198 } catch (IOException e) {
199 log.error("error logging data");
204 * this creates the total summary which should be written to a CSV at the
207 * @return a ScenarioData Object that holds the values to be logged
209 protected abstract ScenarioData doEndLogging();