From 4ca98bdd1a4ce4f289016ff37a83e078c2503cd4 Mon Sep 17 00:00:00 2001
From: Andreas Egger <egger.andreas.1@gmail.com>
Date: Thu, 13 Jun 2013 17:55:05 +0200
Subject: [PATCH] Sched B resized

---
 .../lsdc/exception/VMResizeException.java     |  2 +-
 src/at/ac/tuwien/lsdc/sched/SchedulerB.java   | 17 ++++-----
 .../ac/tuwien/lsdc/types/PhysicalMachine.java | 26 ++++++++++++++
 .../ac/tuwien/lsdc/types/VirtualMachine.java  | 36 ++++++++++++++++++-
 4 files changed, 71 insertions(+), 10 deletions(-)

diff --git a/src/at/ac/tuwien/lsdc/exception/VMResizeException.java b/src/at/ac/tuwien/lsdc/exception/VMResizeException.java
index 4010145..4f82926 100644
--- a/src/at/ac/tuwien/lsdc/exception/VMResizeException.java
+++ b/src/at/ac/tuwien/lsdc/exception/VMResizeException.java
@@ -8,7 +8,7 @@ import at.ac.tuwien.lsdc.types.VirtualMachine;
  * @author jan
  * 
  */
-public class VMResizeException extends RuntimeException {
+public class VMResizeException extends Exception {
 	private final VirtualMachine vm;
 
 	public VMResizeException(String message, VirtualMachine vm) {
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
index b859824..9d1da79 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
@@ -48,8 +48,9 @@ public class SchedulerB extends AbstractSchedulerWithMigration {
 	protected 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());
+//				vm.resizeVM(vm.getSize() + app.getSize(), vm.getRAM() + app.getRam(), vm.getCPU()
+//						+ app.getCpu());
+				vm.resizeUp(app);
 			} catch (VMResizeException ex) {
 				return false;
 			}
@@ -71,12 +72,12 @@ public class SchedulerB extends AbstractSchedulerWithMigration {
 			VirtualMachine vm = evt.getApp().getRunningOn();
 			evt.getApp().setRunningOn(null);
 			vm.stopApplication(evt.getApp());
-			try {
-				vm.resizeVM(vm.getSize() - evt.getApp().getSize(), vm.getRAM()
-						- evt.getApp().getRam(), vm.getCPU() - evt.getApp().getCpu());
-			} catch (VMResizeException e) {
-				// LOG.error("failed to resize VM: " + e.getVm(), e);
-			}
+			
+//				vm.resizeVM(vm.getSize() - evt.getApp().getSize(), vm.getRAM()
+//						- evt.getApp().getRam(), vm.getCPU() - evt.getApp().getCpu());
+			
+			vm.resizeDown(evt.getApp());
+	
 			if (vm.getApplications().size() == 0) {
 				PhysicalMachine pm = vm.getRunningOn();
 				pm.stopVirtualMachine(vm);
diff --git a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
index 4a482b0..38f8f3a 100644
--- a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
+++ b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
@@ -4,6 +4,7 @@ import java.util.HashMap;
 import java.util.Iterator;
 
 import at.ac.tuwien.lsdc.exception.ActiveApplicationsException;
+import at.ac.tuwien.lsdc.exception.VMResizeException;
 import at.ac.tuwien.lsdc.exception.VMsRunningException;
 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
 
@@ -100,11 +101,36 @@ public class PhysicalMachine implements Comparable<PhysicalMachine> {
 		}
 	}
 
+	/**
+	 * Checks if there is enough space for the VM initial resources plus its netto resources 
+	 * @param size the _netto_ size reserved on the VM
+	 * @param RAM the _netto_ RAM reserved on the VM
+	 * @param CPU the _netto_ CPU reserved on the VM
+	 * @return true if there is enough space to run the VM
+	 */
 	public boolean checkVM(int size, int RAM, int CPU) {
 		return ((VirtualMachine.initialSize + size) <= availableSize())
 				&& ((VirtualMachine.initialRAM + RAM) <= availableRAM())
 				&& ((VirtualMachine.initialCPU + CPU) <= availableCPU());
 	}
+	
+	public boolean resizeUp(int diffSize, int diffRAM, int diffCPU) throws VMResizeException {		
+		reservedSize = reservedSize + diffSize;
+		reservedRAM = reservedRAM + diffRAM;
+		reservedCPU = reservedCPU + diffCPU;
+		return true;
+	}
+	
+	public boolean resizeDown(Application app) {
+		reservedSize = reservedSize - app.getSize();
+		reservedRAM = reservedRAM - app.getRam();
+		reservedCPU = reservedCPU - app.getCpu();
+		return true;
+	}
+	
+	public void resizeVM(VirtualMachine vm, Application app) {
+		
+	}
 
 	private int availableSize() {
 		return maxSize - reservedSize;
diff --git a/src/at/ac/tuwien/lsdc/types/VirtualMachine.java b/src/at/ac/tuwien/lsdc/types/VirtualMachine.java
index 2fb7673..c522114 100644
--- a/src/at/ac/tuwien/lsdc/types/VirtualMachine.java
+++ b/src/at/ac/tuwien/lsdc/types/VirtualMachine.java
@@ -69,8 +69,38 @@ public class VirtualMachine {
 			reservedRAM = initialRAM + newRAM;
 			reservedCPU = initialCPU + newCPU;
 			return true;
-		} else
+		} else 
+			return false;
+//			throw new VMResizeException("Could not resize VM!", this);
+	}
+	
+	public boolean resizeUp(Application app) throws VMResizeException {
+		
+		int newSize = reservedSize + (app.getSize() - availableSize()) - initialSize;
+		int newRAM = reservedRAM + (app.getRam() - availableRAM()) - initialRAM;
+		int newCPU = reservedCPU + (app.getCpu() - availableCPU()) - initialCPU;
+		
+		int diffSize = newSize + initialSize - reservedSize;
+		int diffRAM = newRAM + initialRAM - reservedRAM;
+		int diffCPU = newCPU + initialCPU - reservedCPU;
+		
+		if(!(type == VMType.Resizable) || !runningOn.checkVM(newSize, newRAM, newCPU)) {			
 			throw new VMResizeException("Could not resize VM!", this);
+		}
+		
+		runningOn.resizeUp(diffSize, diffRAM, diffCPU);
+		
+		reservedSize = newSize + initialSize;
+		reservedRAM = newRAM + initialRAM;
+		reservedCPU = newCPU + initialCPU;
+		return true;
+	}
+	
+	public boolean resizeDown(Application app) {
+		reservedSize = reservedSize - app.getSize();
+		reservedRAM = reservedRAM - app.getRam();
+		reservedCPU = reservedCPU - app.getCpu();
+		return true;
 	}
 
 	public boolean stopApplication(Application app) {
@@ -145,6 +175,10 @@ public class VirtualMachine {
 	public int getReservedCPU() {
 		return reservedCPU;
 	}
+	
+	public int getDiffSize() {
+		
+	}
 
 	@Override
 	public String toString() {
-- 
2.43.0