1 package at.ac.tuwien.lsdc.sched;
4 import java.io.IOException;
5 import java.util.LinkedList;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
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;
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.
33 public class SchedulerB extends AbstractScheduler {
37 private static final Logger LOG = LoggerFactory.getLogger(SchedulerB.class);
39 public SchedulerB(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
41 super(numPMs, numCloudPartners, schedulerLog, scenario);
42 vmType = VMType.Resizable;
46 * Check if we have any delayed apps. Try to launch them.
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]);
59 vm.resizeVM(vm.getSize() + app.getSize(), vm.getRAM() + app.getRam(),
60 vm.getCPU() + app.getCpu());
62 } catch (VMResizeException ex) {
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);
75 LOG.info("delaying the start of:" + app);
79 vm.startApplication(app);
81 insertStopEvent(currTime + app.getDuration(), app);
82 delayedApps.remove(app);
87 * Check if we can free up a VM to shut it down.
90 protected void runMigration() {
91 // TODO Auto-generated method stub
95 * Cleanup completed apps. Downsize VMs and if an VM becomes empty, shut down VM + PM.
97 * @param events list of all events that happened in this timeslot.
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());
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);
112 if (vm.getApplications().size() == 0) {
113 PhysicalMachine pm = vm.getRunningOn();
114 pm.stopVirtualMachine(vm);
115 manager.stopPhysicalMachine(pm.getId());
121 * Try to start all Apps. Upsize the VM, if not possible start another PM, if not possible,
124 * @param events list of all events that happened in this timeslot.
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]);
136 vm.resizeVM(vm.getSize() + evt.getApp().getSize(), vm.getRAM()
137 + evt.getApp().getRam(), vm.getCPU() + evt.getApp().getCpu());
139 } catch (VMResizeException ex) {
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());
151 LOG.info("delaying the start of:" + evt.getApp());
152 delayedApps.add(evt.getApp());
156 vm.startApplication(evt.getApp());
157 evt.getApp().setRunningOn(vm);
158 insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
163 protected String getSchedulerType() {
164 return SchedulerType.B.toString();