]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
Getting executable
[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                 startScheduling();
93                 try {
94                         logger.close();
95                 } catch (IOException e) {
96                         log.error("couldn't close logger");
97                 }
98                 return doEndLogging();
99         }
100         
101         /**
102          * Insert a start event into the map, at timestamp when the application should start
103          * @param timestamp the timestamp when the application should start
104          * @param app the application to start
105          */
106         private void insertStartEvent(long timestamp, Application app) {
107                 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startApplication, app);
108                 if(!eventMap.containsKey(timestamp)) {
109                         LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
110                         list.add(evt);
111                         eventMap.put(timestamp, list);
112                 } else {
113                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
114                         list.add(evt);
115                 }
116         }
117         
118         /**
119          * Insert a stop event into the map, at timestamp when the application should stop
120          * @param timestamp the timestamp when the application should stop
121          * @param app the application to stop
122          */
123         private void insertStopEvent(long timestamp, Application app) {
124                 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endApplication, app);
125                 if(!eventMap.containsKey(timestamp)) {
126                         LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
127                         list.add(evt);
128                         eventMap.put(timestamp, list);
129                 } else {
130                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
131                         list.add(evt);
132                 }
133                 if(endTime < timestamp) {
134                         endTime = timestamp;
135                 }
136         }
137
138         /**
139          * Start the actual scheduling algorithm. 
140          * Each scheduler will implement the method handleEvents differently
141          */
142         protected void startScheduling() {
143                 while (true) {
144                         LinkedList<SchedulerEvent> events = eventMap.get(currTime);
145                         handleEvents(events);
146                         doStateLogging();
147                         //advance Time to next Event
148                         currTime++;
149
150                         if (currTime == endTime) {
151                                 // reached last Event, Scheduler will shut down
152                                 log.debug("Last event reached at time "+currTime);
153                                 break;
154                         }
155                 }
156         }
157
158         /**
159          * this method is where the Scheduling Algorithm resides
160          * it reads the Events (start & stop of applications)
161          * @param events the events to be read and used by the scheduler
162          */
163         protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
164
165         /**
166          * Does the logging for each timestamp.
167          * It uses the CSVLogger to write directly to the specific
168          * scheduler/scenario csv file
169          */
170         protected void doStateLogging() {
171                 SchedulerData data = new SchedulerData(
172                                                                 currTime, 
173                                                                 manager.getTotalRam(), 
174                                                                 manager.getTotalCpu(), 
175                                                                 manager.getTotalSize(),
176                                                                 manager.countCurrentlyRunningPMs(),
177                                                                 manager.countCurrentlyRunningVMs(),
178                                                                 manager.getCurrentConsumption(), 
179                                                                 numCurrInSourced,
180                                                                 numCurrOutSourced
181                                                         );
182                 totalConsumption += manager.getCurrentConsumption();
183                 try {
184                         logger.logSchedulerData(data);
185                 } catch (IOException e) {
186                         log.error("error logging data");
187                 }
188         }
189
190         /**
191          * this creates the total summary which should be written to a CSV at the end
192          * @return a ScenarioData Object that holds the values to be logged
193          */
194         protected ScenarioData doEndLogging() {
195                 return new ScenarioData(schedulerType.toString(), scenario.toString(),
196                                 1, 1, 1, 1, numTotalInSourced, numTotalOutSourced);
197         }
198 }