From d6f2a7322b07764ad53d5fe8b30bf1050e7e7e8b Mon Sep 17 00:00:00 2001 From: Stefan Derkits Date: Tue, 11 Jun 2013 15:20:10 +0200 Subject: [PATCH] added Migration to SchedB --- src/at/ac/tuwien/lsdc/sched/SchedulerB.java | 71 +++++++++++++++---- .../ac/tuwien/lsdc/types/VirtualMachine.java | 11 ++- 2 files changed, 62 insertions(+), 20 deletions(-) diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java index 6044677..2f06ac7 100644 --- a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java +++ b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java @@ -2,13 +2,16 @@ package at.ac.tuwien.lsdc.sched; import java.io.File; import java.io.IOException; +import java.util.Collections; import java.util.LinkedList; +import java.util.List; import org.slf4j.Logger; 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; @@ -46,7 +49,48 @@ public class SchedulerB extends AbstractScheduler { */ @Override protected void runMigration() { - // TODO Auto-generated method stub + List pms = manager.getSortedPMs(); + // iterate through all the PMs (except the one with the highest utilization) + for (int i = 0; i < (pms.size() - 1); i++) { + PhysicalMachine currentPM = pms.get(i); + if (currentPM.isRunning() && (currentPM.getTotalVMs() > 0)) { + VirtualMachine currentVM = currentPM.getVirtualMachines().values().iterator() + .next(); + for (Application app : currentVM.getApplications()) { + for (int j = i; i < pms.size(); i++) { + PhysicalMachine nextPM = pms.get(j); + if (nextPM.isRunning() && (nextPM.getTotalVMs() > 0)) { + VirtualMachine nextVM = nextPM.getVirtualMachines().values().iterator() + .next(); + if (deployApp(nextVM, app)) { + currentVM.stopApplication(app); + break; + } + } + } + } + if (currentVM.getApplications().size() == 0) { + currentPM.stopVirtualMachine(currentVM); + if (currentPM.getTotalVMs() == 0) { + manager.stopPhysicalMachine(currentPM.getId()); + } + } + } + } + } + + private boolean deployApp(VirtualMachine vm, Application app) { + if (!vm.enoughResources(app)) { + try { + vm.resizeVM(vm.getSize() + app.getSize(), vm.getRAM() + app.getRam(), vm.getCPU() + + app.getCpu()); + } catch (VMResizeException ex) { + return false; + } + } + vm.startApplication(app); + app.setRunningOn(vm); + return true; } /** @@ -84,24 +128,24 @@ public class SchedulerB extends AbstractScheduler { @Override protected void handleStartEvents(LinkedList events) { LOG.debug("startApps():" + events); + boolean deployed = false; for (SchedulerEvent evt : events) { VirtualMachine vm = null; - for (PhysicalMachine pm : manager.getPMs()) { - // TODO: choose VM with good Utilization to get even better utilization - 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() + evt.getApp().getCpu()); - break; - } catch (VMResizeException ex) { - vm = null; + List sortedPMs = manager.getSortedPMs(); + Collections.reverse(sortedPMs); + for (PhysicalMachine pm : sortedPMs) { + if (pm.isRunning() && (pm.getTotalVMs() > 0)) { + vm = pm.getVirtualMachines().values().iterator().next(); + deployed = deployApp(vm, evt.getApp()); + if (deployed) + break; } } if (vm == null) { try { vm = manager.startPhysicalMachine().startVirtualMachine(evt.getApp().getSize(), evt.getApp().getRam(), evt.getApp().getCpu(), vmType); + deployed = deployApp(vm, evt.getApp()); } catch (OutOfPMsException e) { if (federation.askToOutsource(evt.getApp())) { insertOutsourcedStartEvent(currTime + 1, evt.getApp()); @@ -111,9 +155,8 @@ public class SchedulerB extends AbstractScheduler { return; } } - vm.startApplication(evt.getApp()); - evt.getApp().setRunningOn(vm); - insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp()); + if (deployed) + insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp()); } } diff --git a/src/at/ac/tuwien/lsdc/types/VirtualMachine.java b/src/at/ac/tuwien/lsdc/types/VirtualMachine.java index abf4637..2fb7673 100644 --- a/src/at/ac/tuwien/lsdc/types/VirtualMachine.java +++ b/src/at/ac/tuwien/lsdc/types/VirtualMachine.java @@ -52,7 +52,7 @@ public class VirtualMachine { /** * Set the VM size to the given new values. - * + * * @param newSize * the new size of the VM, excluding the initial consumption. * @param newRAM @@ -76,25 +76,24 @@ public class VirtualMachine { public boolean stopApplication(Application app) { if (applications.containsKey(app.getID())) { applications.remove(app.getID()); - app.setRunningOn(null); return true; } else return false; } - private boolean enoughResources(Application app) { + public boolean enoughResources(Application app) { return (app.getSize() <= availableSize()) && (app.getRam() <= availableRAM()) && (app.getCpu() <= availableCPU()); } - private int availableSize() { + public int availableSize() { return reservedSize - getSize(); } - private int availableRAM() { + public int availableRAM() { return reservedRAM - getRAM(); } - private int availableCPU() { + public int availableCPU() { return reservedCPU - getCPU(); } -- 2.43.0