1 package at.ac.tuwien.lsdc.sched;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 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.SchedulerEvent.EventType;
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
26 * size, CPU and RAM of an existing VM to run the application. If no VM is
27 * running, create a new one (start a new PM if necessary). If the application
28 * has finished decrease the size, CPU and RAM of the VM. If no applications are
29 * running on a VM, shut down the VM. If no VM is running on a PM, shut down the
30 * PM. Try to get a maximum of utilization on every PM. Migration: Try to move
31 * applications from VMs to other VMs to get a better utilization and to use
37 public class SchedulerB extends AbstractScheduler {
41 private static final Logger LOG = LoggerFactory.getLogger(SchedulerB.class);
43 private final ArrayList<Application> delayedApps;
45 public SchedulerB(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario) throws IOException {
46 super(numPMs, numCloudPartners, schedulerLog, scenario);
48 vmType = VMType.Resizable;
49 delayedApps = new ArrayList<>();
53 protected void handleEvents(LinkedList<SchedulerEvent> events) {
54 LOG.debug("handleEvents():" + events);
55 handleEndEvents(events);
58 handleStartEvents(events);
62 * Check if we have any delayed apps. Try to launch them.
64 private void checkDelayedApps() {
65 // TODO Auto-generated method stub
69 * Check if we can free up a VM to shut it down.
71 private void checkMigration() {
72 // TODO Auto-generated method stub
76 * Cleanup completed apps. Downsize VMs and if an VM becomes empty, shut down
80 * list of all events that happened in this timeslot.
82 protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
83 LOG.debug("stopApps():" + events);
84 for (SchedulerEvent evt : events) {
85 if (evt.getType() == EventType.endApplication) {
86 VirtualMachine vm = evt.getApp().getRunningOn();
87 evt.getApp().setRunningOn(null);
88 vm.stopApplication(evt.getApp());
90 vm.resizeVM(vm.getSize() - evt.getApp().getSize(), vm.getRAM() - evt.getApp().getRam(), vm.getCPU()
91 - evt.getApp().getCpu());
92 } catch (VMResizeException e) {
93 LOG.error("failed to resize VM: " + e.getVm(), e);
95 if (vm.getApplications().size() == 0) {
96 PhysicalMachine pm = vm.getRunningOn();
97 pm.stopVirtualMachine(vm);
98 manager.stopPhysicalMachine(pm.getId());
105 * Try to start all Apps. Upsize the VM, if not possible start another PM, if
106 * not possible, delay start.
109 * list of all events that happened in this timeslot.
111 protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
112 LOG.debug("startApps():" + events);
113 for (SchedulerEvent evt : events) {
114 if (evt.getType() == EventType.startApplication) {
115 VirtualMachine vm = null;
116 for (PhysicalMachine pm : manager.getPMs()) {
117 LOG.debug("PMs: " + pm);
118 vm = pm.getVirtualMachines().get((pm.getVirtualMachines().keySet().toArray(new Integer[0]))[0]);
120 vm.resizeVM(vm.getSize() + evt.getApp().getSize(), vm.getRAM() + evt.getApp().getRam(), vm.getCPU()
121 + evt.getApp().getCpu());
123 } catch (VMResizeException ex) {
129 vm = manager.startPhysicalMachine().startVirtualMachine(evt.getApp().getSize(), evt.getApp().getRam(),
130 evt.getApp().getCpu(), vmType);
131 } catch (OutOfPMsException e) {
132 LOG.error("failed to start PM.", e);
133 // TODO: try outsourcing here.
134 delayedApps.add(evt.getApp());
138 vm.startApplication(evt.getApp());
139 evt.getApp().setRunningOn(vm);
140 insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
146 protected String getSchedulerType() {
147 return SchedulerType.B.toString();