Added proper exception handling, if there are not enough free resources to allocate...
authorpeusterm <manuel.peuster@uni-paderborn.de>
Wed, 27 Apr 2016 09:01:24 +0000 (11:01 +0200)
committerpeusterm <manuel.peuster@uni-paderborn.de>
Wed, 27 Apr 2016 09:01:24 +0000 (11:01 +0200)
src/emuvim/dcemulator/node.py
src/emuvim/dcemulator/resourcemodel/__init__.py
src/emuvim/dcemulator/resourcemodel/upb/simple.py
src/emuvim/test/test_resourcemodel.py

index decff2b..3a4fbc2 100755 (executable)
@@ -4,6 +4,7 @@ Distributed Cloud Emulator (dcemulator)
 """
 from mininet.node import Docker
 from mininet.link import Link
+from emuvim.dcemulator.resourcemodel import NotEnoughResourcesAvailable
 import logging
 import time
 import json
@@ -152,8 +153,15 @@ class Datacenter(object):
 
         # apply resource limits to container if a resource model is defined
         if self._resource_model is not None:
-            self._resource_model.allocate(d)
-            self._resource_model.write_allocation_log(d, self.resource_log_path)
+            try:
+                self._resource_model.allocate(d)
+                self._resource_model.write_allocation_log(d, self.resource_log_path)
+            except NotEnoughResourcesAvailable as ex:
+                LOG.warning("Allocation of container %r was blocked by resource model." % name)
+                LOG.info(ex.message)
+                # ensure that we remove the container
+                self.net.removeDocker(name)
+                return None
 
         # connect all given networks
         # if no --net option is given, network = [{}], so 1 empty dict in the list
index 21d0868..b79cd7a 100644 (file)
@@ -157,4 +157,8 @@ class BaseResourceModel(object):
         :param action: allocate or free
         :return:
         """
-        pass
\ No newline at end of file
+        pass
+
+
+class NotEnoughResourcesAvailable(BaseException):
+    pass
index 736b97f..d7a0897 100644 (file)
@@ -4,7 +4,7 @@ Playground for resource models created by University of Paderborn.
 import time
 import json
 import logging
-from emuvim.dcemulator.resourcemodel import BaseResourceModel
+from emuvim.dcemulator.resourcemodel import BaseResourceModel, NotEnoughResourcesAvailable
 
 LOG = logging.getLogger("rm.upb.simple")
 LOG.setLevel(logging.DEBUG)
@@ -66,7 +66,7 @@ class UpbSimpleCloudDcRM(BaseResourceModel):
         fl_cu = self._get_flavor(d).get("compute")
         # check for over provisioning
         if self.dc_alloc_cu + fl_cu > self.dc_max_cu and self.raise_no_cpu_resources_left:
-            raise Exception("Not enough compute resources left.")
+            raise NotEnoughResourcesAvailable("Not enough compute resources left.")
         self.dc_alloc_cu += fl_cu
 
     def _allocate_mem(self, d):
@@ -78,7 +78,7 @@ class UpbSimpleCloudDcRM(BaseResourceModel):
         fl_mu = self._get_flavor(d).get("memory")
         # check for over provisioning
         if self.dc_alloc_mu + fl_mu > self.dc_max_mu and self.raise_no_mem_resources_left:
-            raise Exception("Not enough memory resources left.")
+            raise NotEnoughResourcesAvailable("Not enough memory resources left.")
         self.dc_alloc_mu += fl_mu
 
     def free(self, d):
index 01cc792..3976536 100644 (file)
@@ -1,9 +1,9 @@
 import time
 import os
 from emuvim.test.base import SimpleTestTopology
-from emuvim.dcemulator.resourcemodel import BaseResourceModel, ResourceFlavor
+from emuvim.dcemulator.resourcemodel import BaseResourceModel, ResourceFlavor, NotEnoughResourcesAvailable, ResourceModelRegistrar
 from emuvim.dcemulator.resourcemodel.upb.simple import UpbSimpleCloudDcRM, UpbOverprovisioningCloudDcRM
-from emuvim.dcemulator.resourcemodel import ResourceModelRegistrar
+
 
 
 class testResourceModel(SimpleTestTopology):
@@ -159,7 +159,7 @@ class testUpbSimpleCloudDcRM(SimpleTestTopology):
             rm.allocate(c7)  # calculate allocation
             rm.allocate(c8)  # calculate allocation
             rm.allocate(c9)  # calculate allocation
-        except Exception as e:
+        except NotEnoughResourcesAvailable as e:
             self.assertIn("Not enough compute", e.message)
             exception = True
         self.assertTrue(exception)
@@ -188,7 +188,7 @@ class testUpbSimpleCloudDcRM(SimpleTestTopology):
             rm.allocate(c6)  # calculate allocation
             rm.allocate(c7)  # calculate allocation
             rm.allocate(c8)  # calculate allocation
-        except Exception as e:
+        except NotEnoughResourcesAvailable as e:
             self.assertIn("Not enough memory", e.message)
             exception = True
         self.assertTrue(exception)