]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
write end Events when deploying app, fix consumption function
[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         // 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;
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, SchedulerType schedulerType,
59                         ScenarioType scenario) throws IOException {
60                 this.manager = new MachineManager(numPMs);
61                 this.schedulerLog = schedulerLog;
62                 this.schedulerType = schedulerType;
63                 this.scenario = scenario;
64                 this.eventMap = new TreeMap<Long, LinkedList<SchedulerEvent>>();
65                 this.logger = new CSVLogger(schedulerLog);
66                 initCloudPartners(numCloudPartners);
67
68                 // Using federation ?
69                 // federation = new Federation(numCloudPartners);
70         }
71
72         // intialize cloudpartners for federation
73         private void initCloudPartners(int numCloudPartners) {
74                 for (int i = 0; i < numCloudPartners; i++) {
75                         // initialize a cloudpartner
76                         // add to a member: List<CloudPartner>
77                         // CloudPartner should have 2 functions:
78                         // insource & outsource
79                         // insource randomly gives an application to us
80                         // outsource accepts randomly applications from us
81                 }
82         }
83
84         // Initialize Scheduler with Data from CSV
85         // CSV will be parsed and sent as List<Application> to Scheduler
86         public ScenarioData initAndStart(LinkedList<Application> apps) {
87                 for (Application app : apps) {
88                         insertStartEvent(app.getTimestamp(), app);
89                 }
90
91                 log.info("event map = " + eventMap.size());
92                 startScheduling();
93                 try {
94                         logger.close();
95                 } catch (IOException e) {
96                         log.error("couldn't close logger");
97                 }
98                 // TODO make end logging work
99                 return null;
100                 // return doEndLogging();
101         }
102
103         /**
104          * Insert a start event into the map, at timestamp when the application
105          * should start
106          *
107          * @param timestamp
108          *            the timestamp when the application should start
109          * @param app
110          *            the application to start
111          */
112         protected void insertStartEvent(long timestamp, Application app) {
113                 SchedulerEvent evt = new SchedulerEvent(timestamp,
114                                 EventType.startApplication, app);
115                 if (!eventMap.containsKey(timestamp)) {
116                         LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
117                         list.add(evt);
118                         eventMap.put(timestamp, list);
119                 } else {
120                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
121                         list.add(evt);
122                 }
123         }
124
125         /**
126          * Insert a stop event into the map, at timestamp when the application
127          * should stop
128          *
129          * @param timestamp
130          *            the timestamp when the application should stop
131          * @param app
132          *            the application to stop
133          */
134         protected void insertStopEvent(long timestamp, Application app) {
135                 SchedulerEvent evt = new SchedulerEvent(timestamp,
136                                 EventType.endApplication, app);
137                 if (!eventMap.containsKey(timestamp)) {
138                         LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
139                         list.add(evt);
140                         eventMap.put(timestamp, list);
141                 } else {
142                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
143                         list.add(evt);
144                 }
145                 if (endTime < timestamp) {
146                         endTime = timestamp;
147                 }
148         }
149
150         /**
151          * Start the actual scheduling algorithm. Each scheduler will implement the
152          * method handleEvents differently
153          */
154         protected void startScheduling() {
155                 while (true) {
156                         if (eventMap.containsKey(currTime)) {
157                                 LinkedList<SchedulerEvent> events = eventMap.get(currTime);
158                                 log.info(events.size() + " events at timestamp " + currTime);
159                                 handleEvents(events);
160                                 doStateLogging();
161                         }
162                         // advance Time to next Event
163                         currTime++;
164
165                         if (currTime == endTime) {
166                                 // reached last Event, Scheduler will shut down
167                                 log.info("Last event reached at time " + currTime);
168                                 break;
169                         }
170                 }
171         }
172
173         /**
174          * this method is where the Scheduling Algorithm resides it reads the Events
175          * (start & stop of applications)
176          *
177          * @param events
178          *            the events to be read and used by the scheduler
179          */
180         protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
181
182         /**
183          * Does the logging for each timestamp. It uses the CSVLogger to write
184          * directly to the specific scheduler/scenario csv file
185          */
186         protected void doStateLogging() {
187                 SchedulerData data = new SchedulerData(currTime, manager.getTotalRam(),
188                                 manager.getTotalCpu(), manager.getTotalSize(),
189                                 manager.countCurrentlyRunningPMs(),
190                                 manager.countCurrentlyRunningVMs(),
191                                 manager.getCurrentConsumption(), numCurrInSourced,
192                                 numCurrOutSourced);
193                 totalConsumption += manager.getCurrentConsumption();
194                 try {
195                         logger.logSchedulerData(data);
196                 } catch (IOException e) {
197                         log.error("error logging data");
198                 }
199         }
200
201         /**
202          * this creates the total summary which should be written to a CSV at the
203          * end
204          *
205          * @return a ScenarioData Object that holds the values to be logged
206          */
207         protected ScenarioData doEndLogging() {
208                 return new ScenarioData(schedulerType.toString(), scenario.toString(),
209                                 1, 1, 1, 1, numTotalInSourced, numTotalOutSourced);
210         }
211 }