From a41f78690e310457bf88553a1acdbfb8385a36ab Mon Sep 17 00:00:00 2001 From: peusterm Date: Wed, 27 Apr 2016 11:01:24 +0200 Subject: [PATCH] Added proper exception handling, if there are not enough free resources to allocate a container. --- src/emuvim/dcemulator/node.py | 12 ++++++++++-- src/emuvim/dcemulator/resourcemodel/__init__.py | 6 +++++- src/emuvim/dcemulator/resourcemodel/upb/simple.py | 6 +++--- src/emuvim/test/test_resourcemodel.py | 8 ++++---- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/emuvim/dcemulator/node.py b/src/emuvim/dcemulator/node.py index decff2b..3a4fbc2 100755 --- a/src/emuvim/dcemulator/node.py +++ b/src/emuvim/dcemulator/node.py @@ -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 diff --git a/src/emuvim/dcemulator/resourcemodel/__init__.py b/src/emuvim/dcemulator/resourcemodel/__init__.py index 21d0868..b79cd7a 100644 --- a/src/emuvim/dcemulator/resourcemodel/__init__.py +++ b/src/emuvim/dcemulator/resourcemodel/__init__.py @@ -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 diff --git a/src/emuvim/dcemulator/resourcemodel/upb/simple.py b/src/emuvim/dcemulator/resourcemodel/upb/simple.py index 736b97f..d7a0897 100644 --- a/src/emuvim/dcemulator/resourcemodel/upb/simple.py +++ b/src/emuvim/dcemulator/resourcemodel/upb/simple.py @@ -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): diff --git a/src/emuvim/test/test_resourcemodel.py b/src/emuvim/test/test_resourcemodel.py index 01cc792..3976536 100644 --- a/src/emuvim/test/test_resourcemodel.py +++ b/src/emuvim/test/test_resourcemodel.py @@ -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) -- 2.25.1