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