Catch a specific exception on scheduling

If the scheduler fails to find a host for the container/capsule,
a NoValidHost exception is expected. Besides NoValidHost, other
exceptions are unexpected. This patch separated the handling of
expected and unexpected exception.

Change-Id: I3b39793a7911d36895e8ecc7fc9f2973355a3d81
This commit is contained in:
Hongbin Lu 2018-03-31 19:30:30 +00:00
parent 24a6602f0b
commit 6b7f1640ce
2 changed files with 23 additions and 5 deletions

View File

@ -13,8 +13,11 @@
"""Handles all requests relating to compute resources (e.g. containers, """Handles all requests relating to compute resources (e.g. containers,
networking and storage of containers, and compute hosts on which they run).""" networking and storage of containers, and compute hosts on which they run)."""
from oslo_log import log as logging
from zun.common import consts from zun.common import consts
from zun.common import exception from zun.common import exception
from zun.common.i18n import _
from zun.common import profiler from zun.common import profiler
from zun.compute import container_actions from zun.compute import container_actions
from zun.compute import rpcapi from zun.compute import rpcapi
@ -22,7 +25,9 @@ import zun.conf
from zun import objects from zun import objects
from zun.scheduler import client as scheduler_client from zun.scheduler import client as scheduler_client
CONF = zun.conf.CONF CONF = zun.conf.CONF
LOG = logging.getLogger(__name__)
@profiler.trace_cls("rpc") @profiler.trace_cls("rpc")
@ -44,11 +49,17 @@ class API(object):
try: try:
host_state = self._schedule_container(context, new_container, host_state = self._schedule_container(context, new_container,
extra_spec) extra_spec)
except Exception as exc: except exception.NoValidHost:
new_container.status = consts.ERROR new_container.status = consts.ERROR
new_container.status_reason = str(exc) new_container.status_reason = _(
"There are not enough hosts available.")
new_container.save(context) new_container.save(context)
return return
except Exception:
new_container.status = consts.ERROR
new_container.status_reason = _("Unexpected exception occurred.")
new_container.save(context)
raise
# NOTE(mkrai): Intent here is to check the existence of image # NOTE(mkrai): Intent here is to check the existence of image
# before proceeding to create container. If image is not found, # before proceeding to create container. If image is not found,
@ -181,11 +192,17 @@ class API(object):
try: try:
host_state = self._schedule_container(context, new_capsule, host_state = self._schedule_container(context, new_capsule,
extra_spec) extra_spec)
except Exception as exc: except exception.NoValidHost:
new_capsule.status = consts.FAILED new_capsule.status = consts.FAILED
new_capsule.status_reason = str(exc) new_capsule.status_reason = _(
"There are not enough hosts available.")
new_capsule.save(context) new_capsule.save(context)
return return
except Exception:
new_capsule.status = consts.FAILED
new_capsule.status_reason = _("Unexpected exception occurred.")
new_capsule.save(context)
raise
for container in new_capsule.containers: for container in new_capsule.containers:
self._record_action_start(context, container, self._record_action_start(context, container,
container_actions.CREATE) container_actions.CREATE)

View File

@ -59,7 +59,8 @@ class TestAPI(base.TestCase):
mock_schedule_container): mock_schedule_container):
container = self.container container = self.container
container.status = consts.CREATING container.status = consts.CREATING
mock_schedule_container.side_effect = Exception mock_schedule_container.side_effect = exception.NoValidHost(
reason='not enough host')
self.compute_api.container_create(self.context, container, self.compute_api.container_create(self.context, container,
None, None, None, False) None, None, None, False)
self.assertTrue(mock_schedule_container.called) self.assertTrue(mock_schedule_container.called)