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