]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
start MachineManager from Scheduler, add more logging info
[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.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 SchedulerType schedulerType;
30         protected ScenarioType scenario;
31         protected int numTotalInSourced;
32         protected int numTotalOutSourced;
33         protected int numCurrInSourced;
34         protected int numCurrOutSourced;
35         protected double totalConsumption;
36
37         protected MachineManager manager;
38
39         File schedulerLog;
40         CSVLogger logger;
41
42         // this map saves the following Type of Events:
43         // start of an Application, end of an Application
44         // (start outSourced, end outSourced, start inSourced, end inSourced)
45         protected SortedMap<Long, LinkedList<SchedulerEvent>> eventMap;
46
47         // Scheduler has an internal Time "Abstraction"
48         // at every point in time it checks for Events in his "EventList"
49         // and handles them (via the individual scheduling algorithm)
50         protected long currTime;
51
52         // the timestamp at which the Scheduler is finished
53         // it is updated with every added "EndEvent"
54         protected long endTime;
55
56         public AbstractScheduler(int numPMs, int numCloudPartners,
57                         File schedulerLog, SchedulerType schedulerType,
58                         ScenarioType scenario) throws IOException {
59                 this.manager = new MachineManager(numPMs);
60                 this.schedulerLog = schedulerLog;
61                 this.schedulerType = schedulerType;
62                 this.scenario = scenario;
63                 this.eventMap = new TreeMap<Long, LinkedList<SchedulerEvent>>();
64                 this.logger = new CSVLogger(schedulerLog);
65                 initCloudPartners(numCloudPartners);
66         }
67
68         private void initCloudPartners(int numCloudPartners) {
69                 for (int i = 0; i < numCloudPartners; i++) {
70                         // initialize a cloudpartner
71                         // add to a member: List<CloudPartner>
72                         // CloudPartner should have 2 functions:
73                         // insource & outsource
74                         // insource randomly gives an application to us
75                         // outsource accepts randomly applications from us
76                 }
77         }
78
79         // Initialize Scheduler with Data from CSV
80         // CSV will be parsed and sent as List<Application> to Scheduler
81         public ScenarioData initAndStart(LinkedList<Application> apps) {
82                 for (Application a : apps) {
83                         SchedulerEvent event = new SchedulerEvent(a.getTimestamp(),
84                                         EventType.startApplication, a);
85                         LinkedList<SchedulerEvent> list = eventMap.get(a.getTimestamp());
86                         if (list != null) {
87                                 list.add(event);
88                         } else {
89                                 list = new LinkedList<SchedulerEvent>();
90                                 list.add(event);
91                                 eventMap.put(a.getTimestamp(), list);
92                         }
93                 }
94                 startScheduling();
95                 try {
96                         logger.close();
97                 } catch (IOException e) {
98                         log.error("couldn't close logger");
99                 }
100                 return doEndLogging();
101         }
102
103         protected void startScheduling() {
104                 while (true) {
105                         LinkedList<SchedulerEvent> events = eventMap.get(currTime);
106                         handleEvents(events);
107                         doStateLogging();
108                         // advance Time to next Event
109                         if (currTime == endTime) {
110                                 // reached last Event, Scheduler will shut down
111                                 break;
112                         }
113                 }
114         }
115
116         // this method is where the Scheduling Algorithm resides
117         // it reads the Events (start & stop of applications)
118         protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
119
120         protected void doStateLogging() {
121                 SchedulerData data = new SchedulerData(currTime, 1, 1, 1,
122                                 manager.countCurrentlyRunningPMs(),
123                                 manager.countCurrentlyRunningVMs(),
124                                 manager.getCurrentConsumption(), numCurrInSourced,
125                                 numCurrOutSourced);
126                 totalConsumption += manager.getCurrentConsumption();
127                 try {
128                         logger.logSchedulerData(data);
129                 } catch (IOException e) {
130                         log.error("error logging data");
131                 }
132         }
133
134         // this creates the total summary which should be written to a CSV at the
135         // end
136         protected ScenarioData doEndLogging() {
137                 return new ScenarioData(schedulerType.toString(), scenario.toString(),
138                                 1, 1, 1, 1, numTotalInSourced, numTotalOutSourced);
139         }
140 }