]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
Implemented Scheduler A and utility classes
[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.io.Writer;
6 import java.util.HashMap;
7 import java.util.LinkedList;
8 import java.util.Map;
9
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 import at.ac.tuwien.lsdc.federation.Federation;
14 import at.ac.tuwien.lsdc.management.MachineManager;
15 import at.ac.tuwien.lsdc.types.Application;
16 import at.ac.tuwien.lsdc.types.ScenarioData;
17 import at.ac.tuwien.lsdc.types.ScenarioType;
18 import at.ac.tuwien.lsdc.types.SchedulerData;
19 import at.ac.tuwien.lsdc.types.SchedulerEvent;
20 import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
21 import at.ac.tuwien.lsdc.types.SchedulerType;
22 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
23 import at.ac.tuwien.lsdc.util.CSVLogger;
24
25 public abstract class AbstractScheduler {
26         
27         private static final Logger log = LoggerFactory.getLogger(AbstractScheduler.class);
28         
29         // the following types are only needed for correct
30         // log output
31         protected SchedulerType schedulerType;
32         protected ScenarioType scenario;
33         protected int numInSourced;
34         protected int numOutSourced;
35         protected int numCurrInSourced;
36         protected int numCurrOutSourced;
37         
38         protected MachineManager machineManager;
39         protected Federation federation;
40         protected File schedulerLog;
41         protected CSVLogger logger;
42         protected VMType VMType;
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 HashMap<Long, LinkedList<SchedulerEvent>> eventMap;
48         
49 //      // this map saves the following Type of Events:
50 //      // start of an Application, end of an Application
51 //      // (start outSourced, end outSourced, start inSourced, end inSourced)
52 //      protected HashMap<Long, LinkedList<SchedulerEvent>> endEventMap;
53
54         // Scheduler has an internal Time "Abstraction"
55         // at every point in time it checks for Events in his "EventList"
56         // and handles them (via the individual scheduling algorithm)
57         protected long currTime;
58
59         // the timestamp at which the Scheduler is finished
60         // it is updated with every added "EndEvent"
61         protected long endTime;
62
63         public AbstractScheduler(File schedulerLog, SchedulerType schedulerType, ScenarioType scenario, int numPMs, int numCloudPartners) throws IOException {
64                 this.schedulerLog = schedulerLog;
65                 this.schedulerType = schedulerType;
66                 this.scenario = scenario;
67                 this.machineManager = new MachineManager(numPMs);
68                 this.federation = new Federation(numCloudPartners);
69                 this.eventMap = new HashMap<Long,LinkedList<SchedulerEvent>>();
70                 this.logger = new CSVLogger(schedulerLog);
71         }
72         
73         // Initialize Scheduler with Data from CSV
74         // CSV will be parsed and sent as List<Application> to Scheduler
75         public ScenarioData initAndStart(LinkedList<Application> apps) {
76                 for (Application app : apps) {
77                         insertStartEvent(app.getTimestamp(), app);
78                         insertStopEvent(app.getTimestamp() + app.getDuration(), app);
79                 }
80                 startScheduling();
81                 try {
82                         logger.close();
83                 } catch (IOException e) {
84                         log.error("couldn't close logger");
85                 }
86                 return doEndLogging();
87         }
88         
89         /**
90          * Insert a start event into the map, at timestamp when the application should start
91          * @param timestamp the timestamp when the application should start
92          * @param app the application to start
93          */
94         private void insertStartEvent(long timestamp, Application app) {
95                 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.startApplication, app);
96                 if(!eventMap.containsKey(timestamp)) {
97                         LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
98                         list.add(evt);
99                         eventMap.put(timestamp, list);
100                 } else {
101                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
102                         list.add(evt);
103                 }
104         }
105         
106         /**
107          * Insert a stop event into the map, at timestamp when the application should stop
108          * @param timestamp the timestamp when the application should stop
109          * @param app the application to stop
110          */
111         private void insertStopEvent(long timestamp, Application app) {
112                 SchedulerEvent evt = new SchedulerEvent(timestamp, EventType.endApplication, app);
113                 if(!eventMap.containsKey(timestamp)) {
114                         LinkedList<SchedulerEvent> list = new LinkedList<SchedulerEvent>();
115                         list.add(evt);
116                         eventMap.put(timestamp, list);
117                 } else {
118                         LinkedList<SchedulerEvent> list = eventMap.get(timestamp);
119                         list.add(evt);
120                 }
121                 if(endTime < timestamp) {
122                         endTime = timestamp;
123                 }
124         }
125
126         protected void startScheduling() {
127                 while (true) {
128                         LinkedList<SchedulerEvent> events = eventMap.get(currTime);
129                         handleEvents(events);
130                         doStateLogging();
131                         //advance Time to next Event
132                         currTime++;
133                         if (currTime == endTime) {
134                                 // reached last Event, Scheduler will shut down
135                                 break;
136                         }
137                 }
138         }
139
140         // this method is where the Scheduling Algorithm resides
141         // it reads the Events (start & stop of applications)
142         protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
143
144         protected void doStateLogging() {
145                 SchedulerData data = new SchedulerData(currTime,1,1,1,1,1,1,numCurrInSourced,numCurrOutSourced);
146                 try {
147                         logger.logSchedulerData(data);
148                 } catch (IOException e) {
149                         log.error("error logging data");
150                 }
151         }
152
153         // this creates the total summary which should be written to a CSV at the
154         // end
155         protected ScenarioData doEndLogging() {
156                 return new ScenarioData(schedulerType.toString(),scenario.toString(),1,1,1,1,1,1);
157         }
158 }