1 package at.ac.tuwien.lsdc.sched;
4 import java.io.IOException;
5 import java.util.LinkedList;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
11 import at.ac.tuwien.lsdc.exception.OutOfPMsException;
12 import at.ac.tuwien.lsdc.exception.VMResizeException;
13 import at.ac.tuwien.lsdc.types.Application;
14 import at.ac.tuwien.lsdc.types.PhysicalMachine;
15 import at.ac.tuwien.lsdc.types.ScenarioType;
16 import at.ac.tuwien.lsdc.types.SchedulerEvent;
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 AbstractSchedulerWithMigration {
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 boolean deployApp(VirtualMachine vm, Application app) {
48 VirtualMachine current = app.getRunningOn();
49 boolean deployed = false;
50 if (!vm.enoughResources(app)) {
53 } catch (VMResizeException ex) {
57 deployed = vm.startApplication(app);
60 insertStopEvent(currTime + app.getDuration(), app);
61 if (current != null) {
62 current.stopApplication(app);
63 current.resizeDown(app);
70 * Cleanup completed apps. Downsize VMs and if an VM becomes empty, shut down VM + PM.
72 * @param events list of all events that happened in this timeslot.
75 protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
76 // LOG.debug("stopApps():" + events);
77 for (SchedulerEvent evt : events) {
78 VirtualMachine vm = evt.getApp().getRunningOn();
79 vm.stopApplication(evt.getApp());
80 vm.resizeDown(evt.getApp());
84 if (vm.getApplications().size() == 0) {
85 PhysicalMachine pm = vm.getRunningOn();
86 pm.stopVirtualMachine(vm);
87 manager.stopPhysicalMachine(pm.getId());
93 * Try to start all Apps. Upsize the VM, if not possible start another PM, if not possible,
96 * @param events list of all events that happened in this timeslot.
99 protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
100 // LOG.debug("startApps():" + events);
101 boolean deployed = false;
102 for (SchedulerEvent evt : events) {
104 VirtualMachine vm = null;
105 List<PhysicalMachine> sortedPMs = manager.getRevSortedPMs();
107 for (PhysicalMachine pm : sortedPMs) {
108 if (pm.isRunning() && (pm.countCurrentlyRunningVMs() > 0)) {
109 vm = pm.getVirtualMachines().values().iterator().next();
110 deployed = deployApp(vm, evt.getApp());
117 vm = manager.startPhysicalMachine().startVirtualMachine(evt.getApp().getSize(),
118 evt.getApp().getRam(), evt.getApp().getCpu(), vmType);
119 deployed = deployApp(vm, evt.getApp());
120 } catch (OutOfPMsException e) {
121 if (federation.askToOutsource(evt.getApp())) {
122 insertOutsourcedStartEvent(currTime + 1, evt.getApp());
124 // LOG.info("delaying the start of:" + evt.getApp());
125 delayedApps.add(evt.getApp());
132 if(manager.countCurrentlyRunningPMs() < manager.getMaxPMs()) {
133 Application app = federation.askToInsource();
135 insertInsourcedStartEvent(currTime + 1, evt.getApp());
143 protected String getSchedulerType() {
144 return SchedulerType.B.toString();