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