]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
change CSV logging to own logging
[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.LinkedList;
6 import java.util.SortedMap;
7 import java.util.TreeMap;
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
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;
23
24 /**
25  * Class doing all the generic scheduling work.
26  */
27 public abstract class AbstractScheduler {
28
29         private static final Logger log = LoggerFactory.getLogger(AbstractScheduler.class);
30
31         // the following types are only needed for correct
32         // log output
33         protected ScenarioType scenario;
34         protected int numTotalInSourced;
35         protected int numTotalOutSourced;
36         protected int numCurrInSourced;
37         protected int numCurrOutSourced;
38         protected double totalConsumption;
39
40         protected MachineManager manager;
41         protected File schedulerLog;
42         protected CSVLogger logger;
43         protected VMType vmType;
44
45         /**
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
48          * inSourced)
49          */
50         protected SortedMap<Long, LinkedList<SchedulerEvent>> eventMap;
51
52         /**
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)
56          */
57         protected long currTime = 0;
58
59         /**
60          * the timestamp at which the Scheduler is finished it is updated with every
61          * added "EndEvent"
62          */
63         protected long endTime;
64
65         /**
66          * All our cloud partners.
67          */
68         protected Federation federation;
69
70         public AbstractScheduler(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
71                         throws IOException {
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);
78         }
79
80         /**
81          * Initialize Scheduler with Data from CSV CSV will be parsed and sent as
82          * List<Application> to Scheduler
83          */
84         public ScenarioData initAndStart(LinkedList<Application> apps) {
85                 for (Application app : apps) {
86                         insertStartEvent(app.getTimestamp(), app);
87                 }
88
89                 endTime = apps.getLast().getTimestamp() + 1;
90
91                 startScheduling();
92                 /*try {
93                         logger.close();
94                 } catch (IOException e) {
95                         log.error("couldn't close logger");
96                 }*/
97                 // TODO make end logging work
98                 return doEndLogging();
99         }
100
101         /**
102          * Insert a start event into the map, at timestamp when the application should
103          * start
104          * 
105          * @param timestamp
106          *          the timestamp when the application should start
107          * @param app
108          *          the application to start
109          */
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>();
114                         list.add(evt);
115                         eventMap.put(timestamp, list);
116                 } else {
117                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
118                         list.add(evt);
119                 }
120         }
121
122         /**
123          * Insert a start outsourced event into the map, at timestamp when the
124          * application should start
125          * 
126          * @param timestamp
127          *          the timestamp when the application should start
128          * @param app
129          *          the application to start
130          */
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>();
135                         list.add(evt);
136                         eventMap.put(timestamp, list);
137                 } else {
138                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
139                         list.add(evt);
140                 }
141         }
142
143         /**
144          * Insert a stop event into the map, at timestamp when the application should
145          * stop.
146          * 
147          * @param timestamp
148          *          the timestamp when the application should stop
149          * @param app
150          *          the application to stop
151          */
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>();
156                         list.add(evt);
157                         eventMap.put(timestamp, list);
158                 } else {
159                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
160                         list.add(evt);
161                 }
162                 if (endTime < timestamp) {
163                         endTime = timestamp;
164                 }
165         }
166
167         /**
168          * Insert a stop event into the map, at timestamp when the application should
169          * stop.
170          * 
171          * @param timestamp
172          *          the timestamp when the application should stop
173          * @param app
174          *          the application to stop
175          */
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>();
180                         list.add(evt);
181                         eventMap.put(timestamp, list);
182                 } else {
183                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
184                         list.add(evt);
185                 }
186                 if (endTime < timestamp) {
187                         endTime = timestamp;
188                 }
189         }
190
191         /**
192          * Start the actual scheduling algorithm. Each scheduler will implement the
193          * method handleEvents differently
194          */
195         protected void startScheduling() {
196                 while (true) {
197                         if (eventMap.containsKey(currTime)) {
198                                 LinkedList<SchedulerEvent> events = eventMap.get(currTime);
199                                 // log.info(events.size() + " events at timestamp " + currTime);
200                                 handleEvents(events);
201                         }
202                         doStateLogging();
203                         // advance Time to next step
204                         currTime++;
205
206                         if (currTime > endTime) {
207                                 // reached last Event, Scheduler will shut down
208                                 log.info("Last event reached at time " + currTime);
209                                 break;
210                         }
211                 }
212         }
213
214         /**
215          * this method is where the Scheduling Algorithm resides it reads the Events
216          * (start & stop of applications)
217          * 
218          * @param events
219          *          the events to be read and used by the scheduler
220          */
221         protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
222
223         /**
224          * Does the logging for each timestamp. It uses the CSVLogger to write
225          * directly to the specific scheduler/scenario csv file
226          */
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();
232                 try {
233                         logger.logSchedulerData(data);
234                 } catch (IOException e) {
235                         log.error("error logging data");
236                 }
237         }
238
239         /**
240          * this creates the total summary which should be written to a CSV at the end
241          * 
242          * @return a ScenarioData Object that holds the values to be logged
243          */
244         protected ScenarioData doEndLogging() {
245                 return new ScenarioData(getSchedulerType(), scenario.toString(), manager.getTotalPMs(), manager.getTotalVMs(),
246                                 endTime, NumberUtils.roundDouble(totalConsumption), numTotalInSourced, numTotalOutSourced);
247         }
248
249         protected abstract String getSchedulerType();
250
251 }