]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
some fixes
[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.SchedulerEvent.EventType;
19 import at.ac.tuwien.lsdc.types.SchedulerType;
20 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
21 import at.ac.tuwien.lsdc.util.CSVLogger;
22
23 public abstract class AbstractScheduler {
24
25         private static final Logger log = LoggerFactory
26                         .getLogger(AbstractScheduler.class);
27
28         // the following types are only needed for correct
29         // log output
30         protected SchedulerType schedulerType;
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
44         // this map saves the following Type of Events:
45         // start of an Application, end of an Application
46         // (start outSourced, end outSourced, start inSourced, end inSourced)   
47         protected SortedMap<Long, LinkedList<SchedulerEvent>> eventMap;
48
49         // Scheduler has an internal Time "Abstraction"
50         // at every point in time it checks for Events in his "EventList"
51         // and handles them (via the individual scheduling algorithm)
52         protected long currTime;
53
54         // the timestamp at which the Scheduler is finished
55         // it is updated with every added "EndEvent"
56         protected long endTime;
57
58         
59         public AbstractScheduler(int numPMs, int numCloudPartners,
60                         File schedulerLog, SchedulerType schedulerType,
61                         ScenarioType scenario) throws IOException {
62                 this.manager = new MachineManager(numPMs);
63                 this.schedulerLog = schedulerLog;
64                 this.schedulerType = schedulerType;
65                 this.scenario = scenario;
66                 this.eventMap = new TreeMap<Long, LinkedList<SchedulerEvent>>();
67                 this.logger = new CSVLogger(schedulerLog);
68                 initCloudPartners(numCloudPartners);
69                 
70         //      Using federation ?      
71         //      federation = new Federation(numCloudPartners);
72         }
73
74         private void initCloudPartners(int numCloudPartners) {
75                 for (int i = 0; i < numCloudPartners; i++) {
76                         // initialize a cloudpartner
77                         // add to a member: List<CloudPartner>
78                         // CloudPartner should have 2 functions:
79                         // insource & outsource
80                         // insource randomly gives an application to us
81                         // outsource accepts randomly applications from us
82                 }
83         }
84
85         // Initialize Scheduler with Data from CSV
86         // CSV will be parsed and sent as List<Application> to Scheduler
87         public ScenarioData initAndStart(LinkedList<Application> apps) {
88                 for (Application app : apps) {
89                         insertStartEvent(app.getTimestamp(), app);
90                         insertStopEvent(app.getTimestamp() + app.getDuration(), app);
91                 }
92                 
93                 log.info("event map = "+ eventMap.size());
94                 startScheduling();
95                 try {
96                         logger.close();
97                 } catch (IOException e) {
98                         log.error("couldn't close logger");
99                 }
100                 // TODO make end logging work
101                 return null;
102 //              return doEndLogging();
103         }
104         
105         /**
106          * Insert a start event into the map, at timestamp when the application should start
107          * @param timestamp the timestamp when the application should start
108          * @param app the application to start
109          */
110         private 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 stop event into the map, at timestamp when the application should stop
124          * @param timestamp the timestamp when the application should stop
125          * @param app the application to stop
126          */
127         private void insertStopEvent(long timestamp, Application app) {
128                 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endApplication, app);
129                 if(!eventMap.containsKey(timestamp)) {
130                         LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
131                         list.add(evt);
132                         eventMap.put(timestamp, list);
133                 } else {
134                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
135                         list.add(evt);
136                 }
137                 if(endTime < timestamp) {
138                         endTime = timestamp;
139                 }
140         }
141
142         /**
143          * Start the actual scheduling algorithm. 
144          * Each scheduler will implement the method handleEvents differently
145          */
146         protected void startScheduling() {
147                 while (true) {
148                         if(eventMap.containsKey(currTime)) {
149                                 LinkedList<SchedulerEvent> events = eventMap.get(currTime);
150                                 log.info(events.size()+" events at timestamp "+currTime);
151                                 handleEvents(events);
152                                 doStateLogging();
153                         }
154                         //advance Time to next Event
155                         currTime++;
156
157                         if (currTime == endTime) {
158                                 // reached last Event, Scheduler will shut down
159                                 log.info("Last event reached at time "+currTime);
160                                 break;
161                         }
162                 }
163         }
164
165         /**
166          * this method is where the Scheduling Algorithm resides
167          * it reads the Events (start & stop of applications)
168          * @param events the events to be read and used by the scheduler
169          */
170         protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
171
172         /**
173          * Does the logging for each timestamp.
174          * It uses the CSVLogger to write directly to the specific
175          * scheduler/scenario csv file
176          */
177         protected void doStateLogging() {
178                 SchedulerData data = new SchedulerData(
179                                                                 currTime, 
180                                                                 manager.getTotalRam(), 
181                                                                 manager.getTotalCpu(), 
182                                                                 manager.getTotalSize(),
183                                                                 manager.countCurrentlyRunningPMs(),
184                                                                 manager.countCurrentlyRunningVMs(),
185                                                                 manager.getCurrentConsumption(), 
186                                                                 numCurrInSourced,
187                                                                 numCurrOutSourced
188                                                         );
189                 totalConsumption += manager.getCurrentConsumption();
190                 try {
191                         logger.logSchedulerData(data);
192                 } catch (IOException e) {
193                         log.error("error logging data");
194                 }
195         }
196
197         /**
198          * this creates the total summary which should be written to a CSV at the end
199          * @return a ScenarioData Object that holds the values to be logged
200          */
201         protected ScenarioData doEndLogging() {
202                 return new ScenarioData(schedulerType.toString(), scenario.toString(),
203                                 1, 1, 1, 1, numTotalInSourced, numTotalOutSourced);
204         }
205 }