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.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;
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.
34 public class SchedulerB extends AbstractScheduler {
38 private static final Logger LOG = LoggerFactory.getLogger(SchedulerB.class);
40 public SchedulerB(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
42 super(numPMs, numCloudPartners, schedulerLog, scenario);
43 vmType = VMType.Resizable;
47 protected void handleEvents(LinkedList<SchedulerEvent> events) {
48 LOG.debug("handleEvents():" + events);
49 handleEndEvents(events);
50 handleOutsourcedEndEvents(events);
53 handleOutsourcedStartEvents(events);
54 handleStartEvents(events);
58 * Check if we have any delayed apps. Try to launch them.
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]);
70 vm.resizeVM(vm.getSize() + app.getSize(), vm.getRAM() + app.getRam(),
71 vm.getCPU() + app.getCpu());
73 } catch (VMResizeException ex) {
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);
86 LOG.info("delaying the start of:" + app);
90 vm.startApplication(app);
92 insertStopEvent(currTime + app.getDuration(), app);
93 delayedApps.remove(app);
98 * Check if we can free up a VM to shut it down.
100 private void runMigration() {
101 // TODO Auto-generated method stub
105 * Cleanup completed apps. Downsize VMs and if an VM becomes empty, shut down VM + PM.
107 * @param events list of all events that happened in this timeslot.
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());
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);
122 if (vm.getApplications().size() == 0) {
123 PhysicalMachine pm = vm.getRunningOn();
124 pm.stopVirtualMachine(vm);
125 manager.stopPhysicalMachine(pm.getId());
132 * Try to start all Apps. Upsize the VM, if not possible start another PM, if not possible,
135 * @param events list of all events that happened in this timeslot.
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]);
147 vm.resizeVM(vm.getSize() + evt.getApp().getSize(), vm.getRAM()
148 + evt.getApp().getRam(), vm.getCPU() + evt.getApp().getCpu());
150 } catch (VMResizeException ex) {
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());
163 LOG.info("delaying the start of:" + evt.getApp());
164 delayedApps.add(evt.getApp());
168 vm.startApplication(evt.getApp());
169 evt.getApp().setRunningOn(vm);
170 insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
176 protected String getSchedulerType() {
177 return SchedulerType.B.toString();