]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/SchedulerA.java
Big changes: change EventMap, move the stop/stopOutsourced/delayed/start/startOutsour...
[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 handleDelayedApps() {
59                 // TODO Auto-generated method stub
60
61         }
62
63         @Override
64         protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
65                 // sorting applications by amount of resources (descending)
66                 List<Application> sortedApps = sortApps(events);
67
68                 for (Application app : sortedApps) {
69                         boolean appDeployed = false;
70
71                         if (manager.getPMs().size() == 0) {
72                                 PhysicalMachine pm = manager.startPhysicalMachine();
73                                 boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
74
75                                 if (enoughResources) {
76                                         VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(),
77                                                         app.getCpu(), vmType);
78                                         vm.startApplication(app);
79                                         insertStopEvent(currTime + app.getDuration(), app);
80                                         appDeployed = true;
81                                         log.info("Application " + app.toString() + " started on new pm " + pm.getId());
82                                 } else {
83                                         log.warn("Application " + app.toString() + " cannot be run on empty pm "
84                                                         + pm.getId());
85                                 }
86                         } else {
87                                 // sorting physical machines by resource utilization
88                                 // (descending)
89                                 List<PhysicalMachine> sortedPMs = sortPMs();
90
91                                 for (PhysicalMachine pm : sortedPMs) {
92
93                                         boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
94
95                                         if (enoughResources) {
96                                                 VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(),
97                                                                 app.getCpu(), vmType);
98                                                 vm.startApplication(app);
99                                                 insertStopEvent(currTime + app.getDuration(), app);
100                                                 appDeployed = true;
101                                                 log.info("Application " + app.toString() + " started new vm "
102                                                                 + vm.getPositionOnPM() + " on pm " + pm.getId());
103                                                 break;
104                                         }
105                                 }
106                                 if (!appDeployed && (manager.getPMs().size() < manager.getMaxPMs())) {
107
108                                         PhysicalMachine pm = manager.startPhysicalMachine();
109                                         boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
110
111                                         if (enoughResources) {
112                                                 VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(),
113                                                                 app.getCpu(), vmType);
114                                                 vm.startApplication(app);
115                                                 insertStopEvent(currTime + app.getDuration(), app);
116                                                 appDeployed = true;
117                                                 log.info("Application " + app.toString() + " started on new pm "
118                                                                 + pm.getId());
119                                         } else {
120                                                 log.warn("Application " + app.toString() + " cannot be run on empty pm "
121                                                                 + pm.getId());
122                                         }
123                                 }
124                         }
125                         if (!appDeployed) {
126                                 if (federation.askToOutsource(app)) {
127                                         insertOutsourcedStartEvent(currTime + 1, app);
128                                         appDeployed = true;
129                                 } else
130                                         delayedApps.add(app);
131                         }
132                         if (!appDeployed)
133                                 log.warn("Application " + app.toString() + " could not be deployed on any pm");
134                 }
135
136         }
137
138         @Override
139         protected void runMigration() {
140                 // Scheduler A doesn't support Migration, nothing to do here
141         }
142
143         // sorting applications by amount of resources (descending)
144         private List<Application> sortApps(LinkedList<SchedulerEvent> events) {
145                 List<Application> sortedApps = new LinkedList<Application>();
146                 for (SchedulerEvent evt : events) {
147                         sortedApps.add(evt.getApp());
148                 }
149                 Collections.sort(sortedApps, new ApplicationResourceComparator());
150                 Collections.reverse(sortedApps);
151                 return sortedApps;
152         }
153
154         // sorting physical machines by resource utilization (descending)
155         private List<PhysicalMachine> sortPMs() {
156                 List<PhysicalMachine> sortedPMs = new LinkedList<PhysicalMachine>();
157                 for (PhysicalMachine pm : manager.getPMs()) {
158                         sortedPMs.add(pm);
159                         // log.info("pm util = "+pm.getAverageUtilization());
160                 }
161
162                 Collections.sort(sortedPMs);
163                 Collections.reverse(sortedPMs);
164                 return sortedPMs;
165         }
166
167         @Override
168         protected String getSchedulerType() {
169                 return SchedulerType.A.toString();
170         }
171
172 }