]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/SchedulerA.java
Added UML diagram and Presentation-Skeleton.
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / sched / SchedulerA.java
1 package at.ac.tuwien.lsdc.sched;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import java.util.Iterator;
7 import java.util.LinkedList;
8 import java.util.List;
9 import java.util.Collections;
10
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 import at.ac.tuwien.lsdc.exception.ActiveApplicationsException;
15 import at.ac.tuwien.lsdc.types.Application;
16 import at.ac.tuwien.lsdc.types.PhysicalMachine;
17 import at.ac.tuwien.lsdc.types.ScenarioData;
18 import at.ac.tuwien.lsdc.types.ScenarioType;
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.SortedApplication;
23 import at.ac.tuwien.lsdc.types.SortedPhysicalMachine;
24 import at.ac.tuwien.lsdc.types.VirtualMachine;
25 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
26
27 public class SchedulerA extends AbstractScheduler {
28
29         private static final Logger log = LoggerFactory.getLogger(SchedulerA.class);
30
31         public SchedulerA(int numPMs, int numCloudPartners, File schedulerLog,
32                                                 ScenarioType scenario) throws IOException {
33                 super(numPMs, numCloudPartners, schedulerLog, scenario);
34
35                 this.vmType = VMType.NonResizable;
36         }
37
38         @Override
39         protected void handleEvents(LinkedList<SchedulerEvent> events) {
40                 for (SchedulerEvent evt : events) {
41                         if (evt.getType() == EventType.endApplication) {
42                                 VirtualMachine vm = evt.getApp().getRunningOn();
43                                 vm.stopApplication(evt.getApp());
44                                 PhysicalMachine pm = vm.getRunningOn();
45                                 try {
46                                         pm.stopVirtualMachine(vm);
47                                         log.info("application stopped at timestamp "+currTime+", " +
48                                                         "vm "+vm.getPositionOnPM()+", pm "+pm.getId());
49                                 } catch (ActiveApplicationsException e) {
50                                         log.warn("VM " + vm.getId() + "could not be stopped, "
51                                                         + e.getMessage());
52                                 }
53                         }
54                 }
55
56                 // sorting applications by amount of resources (descending)
57                 List<SortedApplication> sortedApps = sortApps(events);
58
59                 for (Iterator<SortedApplication> iter = sortedApps.iterator(); iter
60                                 .hasNext();) {
61                         boolean appDeployed = false;
62                         Application app = iter.next().getApp();
63
64                         if (manager.getPMs().size() == 0) {
65                                 PhysicalMachine pm = manager.startPhysicalMachine();
66                                 boolean enoughResources = pm.checkVM(app.getSize(),
67                                                 app.getRam(), app.getCpu());
68
69                                 if (enoughResources) {
70                                         VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(),
71                                                         app.getCpu(), vmType);
72                                         vm.startApplication(app);
73                                         insertStopEvent(currTime + app.getDuration(), app);
74                                         appDeployed = true;
75                                         log.info("Application " + app.toString()
76                                                         + " started on new pm " + pm.getId());
77                                 } else {
78                                         log.warn("Application " + app.toString()
79                                                         + " cannot be run on empty pm " + pm.getId());
80                                 }
81                         } else {
82                                 // sorting physical machines by resource utilization
83                                 // (descending)
84                                 List<SortedPhysicalMachine> sortedPMs = sortPMs();
85                                 
86                                 for (Iterator<SortedPhysicalMachine> it = sortedPMs.iterator(); it
87                                                 .hasNext();) {
88                                         
89                                         PhysicalMachine pm = it.next().getPm();
90                                         boolean enoughResources = pm.checkVM(app.getSize(),
91                                                         app.getRam(), app.getCpu());
92                                         
93                                         if (enoughResources) {
94                                                 VirtualMachine vm = pm.startVirtualMachine(
95                                                                 app.getSize(), app.getRam(), app.getCpu(),
96                                                                 vmType);
97                                                 vm.startApplication(app);
98                                                 insertStopEvent(currTime + app.getDuration(), app);
99                                                 appDeployed = true;
100                                                 log.info("Application " + app.toString()
101                                                                 + " started new vm " +vm.getPositionOnPM()+" on pm "+ pm.getId());
102                                                 break;
103                                         }
104                                 }
105                                 if (!appDeployed
106                                                 && (manager.getPMs().size() < manager.getMaxPMs())) {
107
108                                         PhysicalMachine pm = manager.startPhysicalMachine();
109                                         boolean enoughResources = pm.checkVM(app.getSize(),
110                                                         app.getRam(), app.getCpu());
111
112                                         if (enoughResources) {
113                                                 VirtualMachine vm = pm.startVirtualMachine(
114                                                                 app.getSize(), app.getRam(), app.getCpu(),
115                                                                 vmType);
116                                                 vm.startApplication(app);
117                                                 insertStopEvent(currTime + app.getDuration(), app);
118                                                 appDeployed = true;
119                                                 log.info("Application " + app.toString()
120                                                                 + " started on new pm " + pm.getId());
121                                         } else {
122                                                 log.warn("Application " + app.toString()
123                                                                 + " cannot be run on empty pm " + pm.getId());
124                                         }
125                                 }
126                         }
127                         if (!appDeployed)
128                                 log.warn("Application " + app.toString()
129                                                 + " could not be deployed on any pm");
130                         //TODO: save app in a List and try to deploy with next handled events
131                 }
132         }
133
134         // sorting applications by amount of resources (descending)
135         private List<SortedApplication> sortApps(LinkedList<SchedulerEvent> events) {
136                 List<SortedApplication> sortedApps = new LinkedList<SortedApplication>();
137                 for (SchedulerEvent evt : events) {
138                         if (evt.getType() == EventType.startApplication)
139                                 sortedApps.add(new SortedApplication(evt.getApp()));
140                 }
141                 Collections.sort(sortedApps);
142                 Collections.reverse(sortedApps);
143                 return sortedApps;
144         }
145
146         // sorting physical machines by resource utilization (descending)
147         private List<SortedPhysicalMachine> sortPMs() {
148                 List<SortedPhysicalMachine> sortedPMs = new LinkedList<SortedPhysicalMachine>();
149                 for (PhysicalMachine pm : manager.getPMs()) {
150                         sortedPMs.add(new SortedPhysicalMachine(pm));
151         //              log.info("pm util = "+pm.getAverageUtilization());
152                 }
153
154                 Collections.sort(sortedPMs);
155                 Collections.reverse(sortedPMs);
156                 return sortedPMs;
157         }
158         
159         
160         /**
161          * this creates the total summary which should be written to a CSV at the
162          * end
163          *
164          * @return a ScenarioData Object that holds the values to be logged
165          */
166         protected ScenarioData doEndLogging() {
167                 return new ScenarioData(SchedulerType.A.toString(), scenario.toString(),
168                                 1, 1, 1, 1, numTotalInSourced, numTotalOutSourced);
169         }
170
171 }