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