]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
implemented end-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.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.SchedulerType;
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 public abstract class AbstractScheduler {
25
26         private static final Logger log = LoggerFactory
27                         .getLogger(AbstractScheduler.class);
28
29         // the following types are only needed for correct
30         // log output
31         protected ScenarioType scenario;
32         protected int numTotalInSourced;
33         protected int numTotalOutSourced;
34         protected int numCurrInSourced;
35         protected int numCurrOutSourced;
36         protected double totalConsumption;
37
38         protected MachineManager manager;
39         protected File schedulerLog;
40         protected CSVLogger logger;
41         protected VMType vmType;
42
43         // this map saves the following Type of Events:
44         // start of an Application, end of an Application
45         // (start outSourced, end outSourced, start inSourced, end inSourced)
46         protected SortedMap<Long, LinkedList<SchedulerEvent>> eventMap;
47
48         // Scheduler has an internal Time "Abstraction"
49         // at every point in time it checks for Events in his "EventList"
50         // and handles them (via the individual scheduling algorithm)
51         protected long currTime = 0;
52
53         // the timestamp at which the Scheduler is finished
54         // it is updated with every added "EndEvent"
55         protected long endTime;
56
57         public AbstractScheduler(int numPMs, int numCloudPartners,
58                         File schedulerLog, ScenarioType scenario) throws IOException {
59                 this.manager = new MachineManager(numPMs);
60                 this.schedulerLog = schedulerLog;
61                 this.scenario = scenario;
62                 this.eventMap = new TreeMap<Long, LinkedList<SchedulerEvent>>();
63                 this.logger = new CSVLogger(schedulerLog);
64                 initCloudPartners(numCloudPartners);
65
66                 // Using federation ?
67                 // federation = new Federation(numCloudPartners);
68         }
69
70         // intialize cloudpartners for federation
71         private void initCloudPartners(int numCloudPartners) {
72                 for (int i = 0; i < numCloudPartners; i++) {
73                         // initialize a cloudpartner
74                         // add to a member: List<CloudPartner>
75                         // CloudPartner should have 2 functions:
76                         // insource & outsource
77                         // insource randomly gives an application to us
78                         // outsource accepts randomly applications from us
79                 }
80         }
81
82         // Initialize Scheduler with Data from CSV
83         // CSV will be parsed and sent as List<Application> to Scheduler
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
103          * should 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,
112                                 EventType.startApplication, app);
113                 if (!eventMap.containsKey(timestamp)) {
114                         LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
115                         list.add(evt);
116                         eventMap.put(timestamp, list);
117                 } else {
118                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
119                         list.add(evt);
120                 }
121         }
122
123         /**
124          * Insert a stop event into the map, at timestamp when the application
125          * should stop
126          *
127          * @param timestamp
128          *            the timestamp when the application should stop
129          * @param app
130          *            the application to stop
131          */
132         protected void insertStopEvent(long timestamp, Application app) {
133                 SchedulerEvent evt = new SchedulerEvent(timestamp,
134                                 EventType.endApplication, app);
135                 if (!eventMap.containsKey(timestamp)) {
136                         LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
137                         list.add(evt);
138                         eventMap.put(timestamp, list);
139                 } else {
140                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
141                         list.add(evt);
142                 }
143                 if (endTime < timestamp) {
144                         endTime = timestamp;
145                 }
146         }
147
148         /**
149          * Start the actual scheduling algorithm. Each scheduler will implement the
150          * method handleEvents differently
151          */
152         protected void startScheduling() {
153                 while (true) {
154                         if (eventMap.containsKey(currTime)) {
155                                 LinkedList<SchedulerEvent> events = eventMap.get(currTime);
156                 //              log.info(events.size() + " events at timestamp " + currTime);
157                                 handleEvents(events);
158                         }
159                         doStateLogging();
160                         // advance Time to next step
161                         currTime++;
162
163                         if (currTime > endTime) {
164                                 // reached last Event, Scheduler will shut down
165                                 log.info("Last event reached at time " + currTime);
166                                 break;
167                         }
168                 }
169         }
170
171         /**
172          * this method is where the Scheduling Algorithm resides it reads the Events
173          * (start & stop of applications)
174          *
175          * @param events
176          *            the events to be read and used by the scheduler
177          */
178         protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
179
180         /**
181          * Does the logging for each timestamp. It uses the CSVLogger to write
182          * directly to the specific scheduler/scenario csv file
183          */
184         protected void doStateLogging() {
185                 SchedulerData data = new SchedulerData(
186                                                                         currTime, 
187                                                                         manager.getTotalRam(),
188                                                                         manager.getTotalCpu(), 
189                                                                         manager.getTotalSize(),
190                                                                         manager.countCurrentlyRunningPMs(),
191                                                                         manager.countCurrentlyRunningVMs(),
192                                                                         manager.getCurrentConsumption(), 
193                                                                         numCurrInSourced,
194                                                                         numCurrOutSourced
195                                                         );
196                 totalConsumption += manager.getCurrentConsumption();
197                 try {
198                         logger.logSchedulerData(data);
199                 } catch (IOException e) {
200                         log.error("error logging data");
201                 }
202         }
203
204         /**
205          * this creates the total summary which should be written to a CSV at the
206          * end
207          * 
208          * @return a ScenarioData Object that holds the values to be logged
209          */
210         protected ScenarioData doEndLogging() {
211                 return new ScenarioData(getSchedulerType(),
212                                 scenario.toString(), manager.getTotalPMs(), manager.getTotalVMs(), endTime, NumberUtils.roundDouble(totalConsumption), numTotalInSourced,
213                                 numTotalOutSourced);
214         }
215         
216         protected abstract String getSchedulerType();
217         
218 }