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.PhysicalMachine;
13 import at.ac.tuwien.lsdc.types.ScenarioType;
14 import at.ac.tuwien.lsdc.types.SchedulerEvent;
15 import at.ac.tuwien.lsdc.types.SchedulerType;
16 import at.ac.tuwien.lsdc.types.VirtualMachine;
17 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
22 * Initial State: All PMs switched off. If an application arrives, try to modify size, CPU and RAM
23 * of an existing VM to run the application. If no VM is running, create a new one (start a new PM
24 * if necessary). If the application has finished decrease the size, CPU and RAM of the VM. If no
25 * applications are running on a VM, shut down the VM. If no VM is running on a PM, shut down the
26 * PM. Try to get a maximum of utilization on every PM. Migration: Try to move applications from VMs
27 * to other VMs to get a better utilization and to use less PMs.
32 public class SchedulerB extends AbstractScheduler {
36 private static final Logger LOG = LoggerFactory.getLogger(SchedulerB.class);
38 public SchedulerB(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
40 super(numPMs, numCloudPartners, schedulerLog, scenario);
41 vmType = VMType.Resizable;
45 * Check if we can free up a VM to shut it down.
48 protected void runMigration() {
49 // TODO Auto-generated method stub
53 * Cleanup completed apps. Downsize VMs and if an VM becomes empty, shut down VM + PM.
55 * @param events list of all events that happened in this timeslot.
58 protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
59 LOG.debug("stopApps():" + events);
60 for (SchedulerEvent evt : events) {
61 VirtualMachine vm = evt.getApp().getRunningOn();
62 evt.getApp().setRunningOn(null);
63 vm.stopApplication(evt.getApp());
65 vm.resizeVM(vm.getSize() - evt.getApp().getSize(), vm.getRAM()
66 - evt.getApp().getRam(), vm.getCPU() - evt.getApp().getCpu());
67 } catch (VMResizeException e) {
68 // LOG.error("failed to resize VM: " + e.getVm(), e);
70 if (vm.getApplications().size() == 0) {
71 PhysicalMachine pm = vm.getRunningOn();
72 pm.stopVirtualMachine(vm);
73 manager.stopPhysicalMachine(pm.getId());
79 * Try to start all Apps. Upsize the VM, if not possible start another PM, if not possible,
82 * @param events list of all events that happened in this timeslot.
85 protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
86 LOG.debug("startApps():" + events);
87 for (SchedulerEvent evt : events) {
88 VirtualMachine vm = null;
89 for (PhysicalMachine pm : manager.getPMs()) {
90 // TODO: choose VM with good Utilization to get even better utilization
91 vm = pm.getVirtualMachines().get(
92 (pm.getVirtualMachines().keySet().toArray(new Integer[0]))[0]);
94 vm.resizeVM(vm.getSize() + evt.getApp().getSize(), vm.getRAM()
95 + evt.getApp().getRam(), vm.getCPU() + evt.getApp().getCpu());
97 } catch (VMResizeException ex) {
103 vm = manager.startPhysicalMachine().startVirtualMachine(evt.getApp().getSize(),
104 evt.getApp().getRam(), evt.getApp().getCpu(), vmType);
105 } catch (OutOfPMsException e) {
106 if (federation.askToOutsource(evt.getApp())) {
107 insertOutsourcedStartEvent(currTime + 1, evt.getApp());
109 LOG.info("delaying the start of:" + evt.getApp());
110 delayedApps.add(evt.getApp());
114 vm.startApplication(evt.getApp());
115 evt.getApp().setRunningOn(vm);
116 insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
121 protected String getSchedulerType() {
122 return SchedulerType.B.toString();