]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
moved DelayedApps List to Abstract scheduler, remove started apps from delayed apps...
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / sched / AbstractScheduler.java
1 package at.ac.tuwien.lsdc.sched;
2
3 import java.io.File;
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;
9
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
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;
24
25 /**
26  * Class doing all the generic scheduling work.
27  */
28 public abstract class AbstractScheduler {
29
30         private static final Logger log = LoggerFactory.getLogger(AbstractScheduler.class);
31
32         // the following types are only needed for correct
33         // log output
34         protected ScenarioType scenario;
35         protected int numTotalInSourced;
36         protected int numTotalOutSourced;
37         protected int numCurrInSourced;
38         protected int numCurrOutSourced;
39         protected double totalConsumption;
40
41         protected MachineManager manager;
42         protected File schedulerLog;
43         protected CSVLogger logger;
44         protected VMType vmType;
45
46         // List of Apps that were delayed due to not enough free resources.
47         protected ArrayList<Application> delayedApps;
48
49         /**
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)
52          */
53         protected SortedMap<Long, LinkedList<SchedulerEvent>> eventMap;
54
55         /**
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)
58          */
59         protected long currTime = 0;
60
61         /**
62          * the timestamp at which the Scheduler is finished it is updated with every added "EndEvent"
63          */
64         protected long endTime;
65
66         /**
67          * All our cloud partners.
68          */
69         protected Federation federation;
70
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>();
80         }
81
82         /**
83          * Initialize Scheduler with Data from CSV CSV will be parsed and sent as List<Application> to
84          * Scheduler
85          */
86         public ScenarioData initAndStart(LinkedList<Application> apps) {
87                 for (Application app : apps) {
88                         insertStartEvent(app.getTimestamp(), app);
89                 }
90
91                 endTime = apps.getLast().getTimestamp() + 1;
92
93                 startScheduling();
94                 return doEndLogging();
95         }
96
97         /**
98          * Insert a start event into the map, at timestamp when the application should start
99          * 
100          * @param timestamp the timestamp when the application should start
101          * @param app the application to start
102          */
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>();
107                         list.add(evt);
108                         eventMap.put(timestamp, list);
109                 } else {
110                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
111                         list.add(evt);
112                 }
113         }
114
115         /**
116          * Insert a start outsourced event into the map, at timestamp when the application should start
117          * 
118          * @param timestamp the timestamp when the application should start
119          * @param app the application to start
120          */
121         protected void insertOutsourcedStartEvent(long timestamp, Application app) {
122                 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startOutsourcedApplication,
123                                 app);
124                 if (!eventMap.containsKey(timestamp)) {
125                         LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
126                         list.add(evt);
127                         eventMap.put(timestamp, list);
128                 } else {
129                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
130                         list.add(evt);
131                 }
132         }
133
134         /**
135          * Insert a stop event into the map, at timestamp when the application should stop.
136          * 
137          * @param timestamp the timestamp when the application should stop
138          * @param app the application to stop
139          */
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>();
144                         list.add(evt);
145                         eventMap.put(timestamp, list);
146                 } else {
147                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
148                         list.add(evt);
149                 }
150                 if (endTime < timestamp) {
151                         endTime = timestamp;
152                 }
153         }
154
155         /**
156          * Insert a stop event into the map, at timestamp when the application should stop.
157          * 
158          * @param timestamp the timestamp when the application should stop
159          * @param app the application to stop
160          */
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>();
165                         list.add(evt);
166                         eventMap.put(timestamp, list);
167                 } else {
168                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
169                         list.add(evt);
170                 }
171                 if (endTime < timestamp) {
172                         endTime = timestamp;
173                 }
174         }
175
176         /**
177          * Start the actual scheduling algorithm. Each scheduler will implement the method handleEvents
178          * differently
179          */
180         protected void startScheduling() {
181                 while (true) {
182                         if (eventMap.containsKey(currTime)) {
183                                 LinkedList<SchedulerEvent> events = eventMap.get(currTime);
184                                 // log.info(events.size() + " events at timestamp " + currTime);
185                                 handleEvents(events);
186                         }
187                         doStateLogging();
188                         // advance Time to next step
189                         currTime++;
190
191                         if (currTime > endTime) {
192                                 // reached last Event, Scheduler will shut down
193                                 log.info("Last event reached at time " + currTime);
194                                 break;
195                         }
196                 }
197         }
198
199         /**
200          * this method is where the Scheduling Algorithm resides it reads the Events (start & stop of
201          * applications)
202          * 
203          * @param events the events to be read and used by the scheduler
204          */
205         protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
206
207         /**
208          * Does the logging for each timestamp. It uses the CSVLogger to write directly to the specific
209          * scheduler/scenario csv file
210          */
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();
217                 try {
218                         logger.logSchedulerData(data);
219                 } catch (IOException e) {
220                         log.error("error logging data");
221                 }
222         }
223
224         /**
225          * this creates the total summary which should be written to a CSV at the end
226          * 
227          * @return a ScenarioData Object that holds the values to be logged
228          */
229         protected ScenarioData doEndLogging() {
230                 return new ScenarioData(getSchedulerType(), scenario.toString(), manager.getTotalPMs(),
231                                 manager.getTotalVMs(), endTime, NumberUtils.roundDouble(totalConsumption),
232                                 numTotalInSourced, numTotalOutSourced);
233         }
234
235         protected abstract String getSchedulerType();
236
237 }