1 package at.ac.tuwien.lsdc.sched;
4 import java.io.IOException;
5 import java.util.Collections;
6 import java.util.LinkedList;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
12 import at.ac.tuwien.lsdc.exception.OutOfPMsException;
13 import at.ac.tuwien.lsdc.exception.VMResizeException;
14 import at.ac.tuwien.lsdc.types.Application;
15 import at.ac.tuwien.lsdc.types.PhysicalMachine;
16 import at.ac.tuwien.lsdc.types.ScenarioType;
17 import at.ac.tuwien.lsdc.types.SchedulerEvent;
18 import at.ac.tuwien.lsdc.types.SchedulerType;
19 import at.ac.tuwien.lsdc.types.VirtualMachine;
20 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
25 * Initial State: All PMs switched off. If an application arrives, try to modify size, CPU and RAM
26 * of an existing VM to run the application. If no VM is running, create a new one (start a new PM
27 * if necessary). If the application has finished decrease the size, CPU and RAM of the VM. If no
28 * applications are running on a VM, shut down the VM. If no VM is running on a PM, shut down the
29 * PM. Try to get a maximum of utilization on every PM. Migration: Try to move applications from VMs
30 * to other VMs to get a better utilization and to use less PMs.
35 public class SchedulerB extends AbstractSchedulerWithMigration {
39 private static final Logger LOG = LoggerFactory.getLogger(SchedulerB.class);
41 public SchedulerB(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
43 super(numPMs, numCloudPartners, schedulerLog, scenario);
44 vmType = VMType.Resizable;
48 protected boolean deployApp(VirtualMachine vm, Application app) {
49 if (!vm.enoughResources(app)) {
51 vm.resizeVM(vm.getSize() + app.getSize(), vm.getRAM() + app.getRam(), vm.getCPU()
53 } catch (VMResizeException ex) {
57 vm.startApplication(app);
63 * Cleanup completed apps. Downsize VMs and if an VM becomes empty, shut down VM + PM.
65 * @param events list of all events that happened in this timeslot.
68 protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
69 LOG.debug("stopApps():" + events);
70 for (SchedulerEvent evt : events) {
71 VirtualMachine vm = evt.getApp().getRunningOn();
72 evt.getApp().setRunningOn(null);
73 vm.stopApplication(evt.getApp());
75 vm.resizeVM(vm.getSize() - evt.getApp().getSize(), vm.getRAM()
76 - evt.getApp().getRam(), vm.getCPU() - evt.getApp().getCpu());
77 } catch (VMResizeException e) {
78 // LOG.error("failed to resize VM: " + e.getVm(), e);
80 if (vm.getApplications().size() == 0) {
81 PhysicalMachine pm = vm.getRunningOn();
82 pm.stopVirtualMachine(vm);
83 manager.stopPhysicalMachine(pm.getId());
89 * Try to start all Apps. Upsize the VM, if not possible start another PM, if not possible,
92 * @param events list of all events that happened in this timeslot.
95 protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
96 LOG.debug("startApps():" + events);
97 boolean deployed = false;
98 for (SchedulerEvent evt : events) {
99 VirtualMachine vm = null;
100 List<PhysicalMachine> sortedPMs = manager.getSortedPMs();
101 Collections.reverse(sortedPMs);
102 for (PhysicalMachine pm : sortedPMs) {
103 if (pm.isRunning() && (pm.getTotalVMs() > 0)) {
104 vm = pm.getVirtualMachines().values().iterator().next();
105 deployed = deployApp(vm, evt.getApp());
112 vm = manager.startPhysicalMachine().startVirtualMachine(evt.getApp().getSize(),
113 evt.getApp().getRam(), evt.getApp().getCpu(), vmType);
114 deployed = deployApp(vm, evt.getApp());
115 } catch (OutOfPMsException e) {
116 if (federation.askToOutsource(evt.getApp())) {
117 insertOutsourcedStartEvent(currTime + 1, evt.getApp());
119 LOG.info("delaying the start of:" + evt.getApp());
120 delayedApps.add(evt.getApp());
126 insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
131 protected String getSchedulerType() {
132 return SchedulerType.B.toString();