]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/SchedulerB.java
Big changes: change EventMap, move the stop/stopOutsourced/delayed/start/startOutsour...
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / sched / SchedulerB.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
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9
10 import at.ac.tuwien.lsdc.exception.OutOfPMsException;
11 import at.ac.tuwien.lsdc.exception.VMResizeException;
12 import at.ac.tuwien.lsdc.types.Application;
13 import at.ac.tuwien.lsdc.types.PhysicalMachine;
14 import at.ac.tuwien.lsdc.types.ScenarioType;
15 import at.ac.tuwien.lsdc.types.SchedulerEvent;
16 import at.ac.tuwien.lsdc.types.SchedulerType;
17 import at.ac.tuwien.lsdc.types.VirtualMachine;
18 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
19
20 /**
21  * Scheduler B.
22  *
23  * Initial State: All PMs switched off. If an application arrives, try to modify size, CPU and RAM
24  * of an existing VM to run the application. If no VM is running, create a new one (start a new PM
25  * if necessary). If the application has finished decrease the size, CPU and RAM of the VM. If no
26  * applications are running on a VM, shut down the VM. If no VM is running on a PM, shut down the
27  * PM. Try to get a maximum of utilization on every PM. Migration: Try to move applications from VMs
28  * to other VMs to get a better utilization and to use less PMs.
29  *
30  * @author jan
31  *
32  */
33 public class SchedulerB extends AbstractScheduler {
34         /**
35          * Logger.
36          */
37         private static final Logger LOG = LoggerFactory.getLogger(SchedulerB.class);
38
39         public SchedulerB(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
40                         throws IOException {
41                 super(numPMs, numCloudPartners, schedulerLog, scenario);
42                 vmType = VMType.Resizable;
43         }
44
45         /**
46          * Check if we have any delayed apps. Try to launch them.
47          */
48         @Override
49         protected void handleDelayedApps() {
50                 // TODO: probably mostly the same code as handleStartEvents
51                 // TOOD: (namely: use case: "start an application",do merge it
52                 for (Application app : delayedApps) {
53                         VirtualMachine vm = null;
54                         for (PhysicalMachine pm : manager.getPMs()) {
55                                 // TODO: choose PM better to get better Utilization
56                                 vm = pm.getVirtualMachines().get(
57                                                 (pm.getVirtualMachines().keySet().toArray(new Integer[0]))[0]);
58                                 try {
59                                         vm.resizeVM(vm.getSize() + app.getSize(), vm.getRAM() + app.getRam(),
60                                                         vm.getCPU() + app.getCpu());
61                                         break;
62                                 } catch (VMResizeException ex) {
63                                         vm = null;
64                                 }
65                         }
66                         if (vm == null) {
67                                 try {
68                                         vm = manager.startPhysicalMachine().startVirtualMachine(app.getSize(),
69                                                         app.getRam(), app.getCpu(), vmType);
70                                 } catch (OutOfPMsException e) {
71                                         // LOG.error("failed to start PM.", e);
72                                         if (federation.askToOutsource(app)) {
73                                                 insertOutsourcedStartEvent(currTime + 1, app);
74                                         } else
75                                                 LOG.info("delaying the start of:" + app);
76                                         return;
77                                 }
78                         }
79                         vm.startApplication(app);
80                         app.setRunningOn(vm);
81                         insertStopEvent(currTime + app.getDuration(), app);
82                         delayedApps.remove(app);
83                 }
84         }
85
86         /**
87          * Check if we can free up a VM to shut it down.
88          */
89         @Override
90         protected void runMigration() {
91                 // TODO Auto-generated method stub
92         }
93
94         /**
95          * Cleanup completed apps. Downsize VMs and if an VM becomes empty, shut down VM + PM.
96          *
97          * @param events list of all events that happened in this timeslot.
98          */
99         @Override
100         protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
101                 LOG.debug("stopApps():" + events);
102                 for (SchedulerEvent evt : events) {
103                         VirtualMachine vm = evt.getApp().getRunningOn();
104                         evt.getApp().setRunningOn(null);
105                         vm.stopApplication(evt.getApp());
106                         try {
107                                 vm.resizeVM(vm.getSize() - evt.getApp().getSize(), vm.getRAM()
108                                                 - evt.getApp().getRam(), vm.getCPU() - evt.getApp().getCpu());
109                         } catch (VMResizeException e) {
110                                 // LOG.error("failed to resize VM: " + e.getVm(), e);
111                         }
112                         if (vm.getApplications().size() == 0) {
113                                 PhysicalMachine pm = vm.getRunningOn();
114                                 pm.stopVirtualMachine(vm);
115                                 manager.stopPhysicalMachine(pm.getId());
116                         }
117                 }
118         }
119
120         /**
121          * Try to start all Apps. Upsize the VM, if not possible start another PM, if not possible,
122          * delay start.
123          *
124          * @param events list of all events that happened in this timeslot.
125          */
126         @Override
127         protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
128                 LOG.debug("startApps():" + events);
129                 for (SchedulerEvent evt : events) {
130                         VirtualMachine vm = null;
131                         for (PhysicalMachine pm : manager.getPMs()) {
132                                 // TODO: choose VM with good Utilization to get even better utilization
133                                 vm = pm.getVirtualMachines().get(
134                                                 (pm.getVirtualMachines().keySet().toArray(new Integer[0]))[0]);
135                                 try {
136                                         vm.resizeVM(vm.getSize() + evt.getApp().getSize(), vm.getRAM()
137                                                         + evt.getApp().getRam(), vm.getCPU() + evt.getApp().getCpu());
138                                         break;
139                                 } catch (VMResizeException ex) {
140                                         vm = null;
141                                 }
142                         }
143                         if (vm == null) {
144                                 try {
145                                         vm = manager.startPhysicalMachine().startVirtualMachine(evt.getApp().getSize(),
146                                                         evt.getApp().getRam(), evt.getApp().getCpu(), vmType);
147                                 } catch (OutOfPMsException e) {
148                                         if (federation.askToOutsource(evt.getApp())) {
149                                                 insertOutsourcedStartEvent(currTime + 1, evt.getApp());
150                                         } else
151                                                 LOG.info("delaying the start of:" + evt.getApp());
152                                         delayedApps.add(evt.getApp());
153                                         return;
154                                 }
155                         }
156                         vm.startApplication(evt.getApp());
157                         evt.getApp().setRunningOn(vm);
158                         insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
159                 }
160         }
161
162         @Override
163         protected String getSchedulerType() {
164                 return SchedulerType.B.toString();
165         }
166
167 }