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 * handle running of outsourced apps.
60 * @param events list of all events that happened in this timeslot.
62 private void handleOutsourcedStartEvents(LinkedList<SchedulerEvent> events) {
63 for (SchedulerEvent evt : events) {
64 if (evt.getType() == EventType.startOutsourcedApplication) {
65 insertOutsourcedStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
73 * handle stopping of outsourced apps.
75 * @param events list of all events that happened in this timeslot.
77 private void handleOutsourcedEndEvents(LinkedList<SchedulerEvent> events) {
78 for (SchedulerEvent evt : events) {
79 if (evt.getType() == EventType.endOutsourcedApplication) {
86 * Check if we have any delayed apps. Try to launch them.
88 private void runDelayedApps() {
89 // TODO: probably mostly the same code as handleStartEvents
90 // TOOD: (namely: use case: "start an application",do merge it
91 for (Application app : delayedApps) {
92 VirtualMachine vm = null;
93 for (PhysicalMachine pm : manager.getPMs()) {
94 // TODO: choose PM better to get better Utilization
95 vm = pm.getVirtualMachines().get(
96 (pm.getVirtualMachines().keySet().toArray(new Integer[0]))[0]);
98 vm.resizeVM(vm.getSize() + app.getSize(), vm.getRAM() + app.getRam(),
99 vm.getCPU() + app.getCpu());
101 } catch (VMResizeException ex) {
107 vm = manager.startPhysicalMachine().startVirtualMachine(app.getSize(),
108 app.getRam(), app.getCpu(), vmType);
109 } catch (OutOfPMsException e) {
110 // LOG.error("failed to start PM.", e);
111 if (federation.askToOutsource(app)) {
112 insertOutsourcedStartEvent(currTime + 1, app);
114 LOG.info("delaying the start of:" + app);
118 vm.startApplication(app);
119 app.setRunningOn(vm);
120 insertStopEvent(currTime + app.getDuration(), app);
121 delayedApps.remove(app);
126 * Check if we can free up a VM to shut it down.
128 private void runMigration() {
129 // TODO Auto-generated method stub
133 * Cleanup completed apps. Downsize VMs and if an VM becomes empty, shut down VM + PM.
135 * @param events list of all events that happened in this timeslot.
137 protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
138 LOG.debug("stopApps():" + events);
139 for (SchedulerEvent evt : events) {
140 if (evt.getType() == EventType.endApplication) {
141 VirtualMachine vm = evt.getApp().getRunningOn();
142 evt.getApp().setRunningOn(null);
143 vm.stopApplication(evt.getApp());
145 vm.resizeVM(vm.getSize() - evt.getApp().getSize(), vm.getRAM()
146 - evt.getApp().getRam(), vm.getCPU() - evt.getApp().getCpu());
147 } catch (VMResizeException e) {
148 // LOG.error("failed to resize VM: " + e.getVm(), e);
150 if (vm.getApplications().size() == 0) {
151 PhysicalMachine pm = vm.getRunningOn();
152 pm.stopVirtualMachine(vm);
153 manager.stopPhysicalMachine(pm.getId());
160 * Try to start all Apps. Upsize the VM, if not possible start another PM, if not possible,
163 * @param events list of all events that happened in this timeslot.
165 protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
166 LOG.debug("startApps():" + events);
167 for (SchedulerEvent evt : events) {
168 if (evt.getType() == EventType.startApplication) {
169 VirtualMachine vm = null;
170 for (PhysicalMachine pm : manager.getPMs()) {
171 // TODO: choose VM with good Utilization to get even better utilization
172 vm = pm.getVirtualMachines().get(
173 (pm.getVirtualMachines().keySet().toArray(new Integer[0]))[0]);
175 vm.resizeVM(vm.getSize() + evt.getApp().getSize(), vm.getRAM()
176 + evt.getApp().getRam(), vm.getCPU() + evt.getApp().getCpu());
178 } catch (VMResizeException ex) {
184 vm = manager.startPhysicalMachine().startVirtualMachine(
185 evt.getApp().getSize(), evt.getApp().getRam(),
186 evt.getApp().getCpu(), vmType);
187 } catch (OutOfPMsException e) {
188 if (federation.askToOutsource(evt.getApp())) {
189 insertOutsourcedStartEvent(currTime + 1, evt.getApp());
191 LOG.info("delaying the start of:" + evt.getApp());
192 delayedApps.add(evt.getApp());
196 vm.startApplication(evt.getApp());
197 evt.getApp().setRunningOn(vm);
198 insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
204 protected String getSchedulerType() {
205 return SchedulerType.B.toString();