From 8397ba2a7d3036e677480747f446f9a672079455 Mon Sep 17 00:00:00 2001
From: Stefan Derkits <stefan@derkits.at>
Date: Tue, 11 Jun 2013 15:28:25 +0200
Subject: [PATCH] put code for migration into new class
 AbstractSchedulerWithMigration

---
 .../sched/AbstractSchedulerWithMigration.java | 56 +++++++++++++++++++
 src/at/ac/tuwien/lsdc/sched/SchedulerB.java   | 38 +------------
 src/at/ac/tuwien/lsdc/sched/SchedulerC.java   | 10 +++-
 3 files changed, 67 insertions(+), 37 deletions(-)
 create mode 100644 src/at/ac/tuwien/lsdc/sched/AbstractSchedulerWithMigration.java

diff --git a/src/at/ac/tuwien/lsdc/sched/AbstractSchedulerWithMigration.java b/src/at/ac/tuwien/lsdc/sched/AbstractSchedulerWithMigration.java
new file mode 100644
index 0000000..96a93ba
--- /dev/null
+++ b/src/at/ac/tuwien/lsdc/sched/AbstractSchedulerWithMigration.java
@@ -0,0 +1,56 @@
+package at.ac.tuwien.lsdc.sched;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+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.VirtualMachine;
+
+public abstract class AbstractSchedulerWithMigration extends AbstractScheduler {
+
+	public AbstractSchedulerWithMigration(int numPMs, int numCloudPartners, File schedulerLog,
+			ScenarioType scenario) throws IOException {
+		super(numPMs, numCloudPartners, schedulerLog, scenario);
+	}
+
+	/**
+	 * Check if we can free up a VM to shut it down.
+	 */
+	@Override
+	protected void runMigration() {
+		List<PhysicalMachine> 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());
+					}
+				}
+			}
+		}
+	}
+
+	protected abstract boolean deployApp(VirtualMachine vm, Application app);
+
+}
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
index 2f06ac7..5be16e5 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
@@ -32,7 +32,7 @@ import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
  * @author jan
  *
  */
-public class SchedulerB extends AbstractScheduler {
+public class SchedulerB extends AbstractSchedulerWithMigration {
 	/**
 	 * Logger.
 	 */
@@ -44,42 +44,8 @@ public class SchedulerB extends AbstractScheduler {
 		vmType = VMType.Resizable;
 	}
 
-	/**
-	 * Check if we can free up a VM to shut it down.
-	 */
 	@Override
-	protected void runMigration() {
-		List<PhysicalMachine> 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) {
+	protected boolean deployApp(VirtualMachine vm, Application app) {
 		if (!vm.enoughResources(app)) {
 			try {
 				vm.resizeVM(vm.getSize() + app.getSize(), vm.getRAM() + app.getRam(), vm.getCPU()
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerC.java b/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
index 199a0da..afe1837 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
@@ -7,11 +7,13 @@ import java.util.LinkedList;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import at.ac.tuwien.lsdc.types.Application;
 import at.ac.tuwien.lsdc.types.ScenarioType;
 import at.ac.tuwien.lsdc.types.SchedulerEvent;
 import at.ac.tuwien.lsdc.types.SchedulerType;
+import at.ac.tuwien.lsdc.types.VirtualMachine;
 
-public class SchedulerC extends AbstractScheduler {
+public class SchedulerC extends AbstractSchedulerWithMigration {
 
 	private static final Logger log = LoggerFactory.getLogger(SchedulerC.class);
 
@@ -49,4 +51,10 @@ public class SchedulerC extends AbstractScheduler {
 
 	}
 
+	@Override
+	protected boolean deployApp(VirtualMachine vm, Application app) {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
 }
-- 
2.43.0