]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
Merge branch 'master' of ssh://dev.somenet.org:666/tu/lsdc
[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
48 //      protected HashMap<Long, LinkedList<SchedulerEvent>> eventMap;
49         
50         protected SortedMap<Long, LinkedList<SchedulerEvent>> eventMap;
51
52         // Scheduler has an internal Time "Abstraction"
53         // at every point in time it checks for Events in his "EventList"
54         // and handles them (via the individual scheduling algorithm)
55         protected long currTime;
56
57         // the timestamp at which the Scheduler is finished
58         // it is updated with every added "EndEvent"
59         protected long endTime;
60
61         
62         public AbstractScheduler(int numPMs, int numCloudPartners,
63                         File schedulerLog, SchedulerType schedulerType,
64                         ScenarioType scenario) throws IOException {
65                 this.manager = new MachineManager(numPMs);
66                 this.schedulerLog = schedulerLog;
67                 this.schedulerType = schedulerType;
68                 this.scenario = scenario;
69                 this.eventMap = new TreeMap<Long, LinkedList<SchedulerEvent>>();
70                 this.logger = new CSVLogger(schedulerLog);
71                 initCloudPartners(numCloudPartners);
72                 
73         //      Using federation ?      
74         //      federation = new Federation(numCloudPartners);
75         }
76
77         private void initCloudPartners(int numCloudPartners) {
78                 for (int i = 0; i < numCloudPartners; i++) {
79                         // initialize a cloudpartner
80                         // add to a member: List<CloudPartner>
81                         // CloudPartner should have 2 functions:
82                         // insource & outsource
83                         // insource randomly gives an application to us
84                         // outsource accepts randomly applications from us
85                 }
86         }
87
88         // Initialize Scheduler with Data from CSV
89         // CSV will be parsed and sent as List<Application> to Scheduler
90         public ScenarioData initAndStart(LinkedList<Application> apps) {
91                 for (Application app : apps) {
92                         insertStartEvent(app.getTimestamp(), app);
93                         insertStopEvent(app.getTimestamp() + app.getDuration(), app);
94                 }
95                 startScheduling();
96                 try {
97                         logger.close();
98                 } catch (IOException e) {
99                         log.error("couldn't close logger");
100                 }
101                 return doEndLogging();
102         }
103         
104         /**
105          * Insert a start event into the map, at timestamp when the application should start
106          * @param timestamp the timestamp when the application should start
107          * @param app the application to start
108          */
109         private void insertStartEvent(long timestamp, Application app) {
110                 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startApplication, app);
111                 if(!eventMap.containsKey(timestamp)) {
112                         LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
113                         list.add(evt);
114                         eventMap.put(timestamp, list);
115                 } else {
116                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
117                         list.add(evt);
118                 }
119         }
120         
121         /**
122          * Insert a stop event into the map, at timestamp when the application should stop
123          * @param timestamp the timestamp when the application should stop
124          * @param app the application to stop
125          */
126         private void insertStopEvent(long timestamp, Application app) {
127                 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endApplication, app);
128                 if(!eventMap.containsKey(timestamp)) {
129                         LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
130                         list.add(evt);
131                         eventMap.put(timestamp, list);
132                 } else {
133                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
134                         list.add(evt);
135                 }
136                 if(endTime < timestamp) {
137                         endTime = timestamp;
138                 }
139         }
140
141         protected void startScheduling() {
142                 while (true) {
143                         LinkedList<SchedulerEvent> events = eventMap.get(currTime);
144                         handleEvents(events);
145                         doStateLogging();
146                         //advance Time to next Event
147                         currTime++;
148
149                         if (currTime == endTime) {
150                                 // reached last Event, Scheduler will shut down
151                                 break;
152                         }
153                 }
154         }
155
156         // this method is where the Scheduling Algorithm resides
157         // it reads the Events (start & stop of applications)
158         protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
159
160         protected void doStateLogging() {
161                 SchedulerData data = new SchedulerData(currTime, 1, 1, 1,
162                                 manager.countCurrentlyRunningPMs(),
163                                 manager.countCurrentlyRunningVMs(),
164                                 manager.getCurrentConsumption(), numCurrInSourced,
165                                 numCurrOutSourced);
166                 totalConsumption += manager.getCurrentConsumption();
167                 try {
168                         logger.logSchedulerData(data);
169                 } catch (IOException e) {
170                         log.error("error logging data");
171                 }
172         }
173
174         // this creates the total summary which should be written to a CSV at the
175         // end
176         protected ScenarioData doEndLogging() {
177                 return new ScenarioData(schedulerType.toString(), scenario.toString(),
178                                 1, 1, 1, 1, numTotalInSourced, numTotalOutSourced);
179         }
180 }