From b9b7ee049a6999277fd0e00d67bc29fcfdb1dc64 Mon Sep 17 00:00:00 2001 From: Someone Date: Mon, 3 Jun 2013 15:26:21 +0200 Subject: [PATCH] Split the one big ugly handle functions into smaller parts. --- src/at/ac/tuwien/lsdc/sched/SchedulerB.java | 62 ++++++++++++++++++--- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java index 4d5f5e6..a82297c 100644 --- a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java +++ b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java @@ -2,6 +2,7 @@ package at.ac.tuwien.lsdc.sched; import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.LinkedList; import org.slf4j.Logger; @@ -9,6 +10,7 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.lsdc.exception.OutOfPMsException; import at.ac.tuwien.lsdc.exception.VMResizeException; +import at.ac.tuwien.lsdc.types.Application; import at.ac.tuwien.lsdc.types.PhysicalMachine; import at.ac.tuwien.lsdc.types.ScenarioType; import at.ac.tuwien.lsdc.types.SchedulerEvent; @@ -33,18 +35,52 @@ import at.ac.tuwien.lsdc.types.VirtualMachine.VMType; * */ public class SchedulerB extends AbstractScheduler { + /** + * Logger. + */ + private static final Logger LOG = LoggerFactory.getLogger(SchedulerB.class); - private static final Logger log = LoggerFactory.getLogger(SchedulerB.class); + private final ArrayList delayedApps; public SchedulerB(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario) throws IOException { super(numPMs, numCloudPartners, schedulerLog, scenario); vmType = VMType.Resizable; + delayedApps = new ArrayList<>(); } @Override protected void handleEvents(LinkedList events) { - log.debug("handleEvents():" + events); + LOG.debug("handleEvents():" + events); + handleEndEvents(events); + checkMigration(); + checkDelayedApps(); + handleStartEvents(events); + } + + /** + * Check if we have any delayed apps. Try to launch them. + */ + private void checkDelayedApps() { + // TODO Auto-generated method stub + } + + /** + * Check if we can free up a VM to shut it down. + */ + private void checkMigration() { + // TODO Auto-generated method stub + } + + /** + * Cleanup completed apps. Downsize VMs and if an VM becomes empty, shut down + * VM + PM. + * + * @param events + * list of all events that happened in this timeslot. + */ + protected void handleEndEvents(LinkedList events) { + LOG.debug("stopApps():" + events); for (SchedulerEvent evt : events) { if (evt.getType() == EventType.endApplication) { VirtualMachine vm = evt.getApp().getRunningOn(); @@ -54,7 +90,7 @@ public class SchedulerB extends AbstractScheduler { vm.resizeVM(vm.getSize() - evt.getApp().getSize(), vm.getRAM() - evt.getApp().getRam(), vm.getCPU() - evt.getApp().getCpu()); } catch (VMResizeException e) { - log.error("lol: " + e.getVm(), e); + LOG.error("failed to resize VM: " + e.getVm(), e); } if (vm.getApplications().size() == 0) { PhysicalMachine pm = vm.getRunningOn(); @@ -63,15 +99,22 @@ public class SchedulerB extends AbstractScheduler { } } } + } - // TODO: migrate apps to get an empty VM and shut it down. - // TODO: run all delayed. - + /** + * Try to start all Apps. Upsize the VM, if not possible start another PM, if + * not possible, delay start. + * + * @param events + * list of all events that happened in this timeslot. + */ + protected void handleStartEvents(LinkedList events) { + LOG.debug("startApps():" + events); for (SchedulerEvent evt : events) { if (evt.getType() == EventType.startApplication) { VirtualMachine vm = null; for (PhysicalMachine pm : manager.getPMs()) { - log.debug("PMs: " + pm); + LOG.debug("PMs: " + pm); vm = pm.getVirtualMachines().get((pm.getVirtualMachines().keySet().toArray(new Integer[0]))[0]); try { vm.resizeVM(vm.getSize() + evt.getApp().getSize(), vm.getRAM() + evt.getApp().getRam(), vm.getCPU() @@ -86,8 +129,9 @@ public class SchedulerB extends AbstractScheduler { vm = manager.startPhysicalMachine().startVirtualMachine(evt.getApp().getSize(), evt.getApp().getRam(), evt.getApp().getCpu(), vmType); } catch (OutOfPMsException e) { - log.error("OOP!", e); - // TODO: delay APP start. + LOG.error("failed to start PM.", e); + // TODO: try outsourcing here. + delayedApps.add(evt.getApp()); return; } } -- 2.43.0