]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Use save_and_reraise_exception when reraise exception
authorAkihiro Motoki <motoki@da.jp.nec.com>
Wed, 12 Feb 2014 14:18:36 +0000 (23:18 +0900)
committerThomas Goirand <thomas@goirand.fr>
Thu, 13 Mar 2014 07:20:21 +0000 (15:20 +0800)
This commit fixes neutron core and common codes.

Partial-Bug: #1279813
Change-Id: Id109924c7ff9b4f0d74c23665016a54bfd7dff77

13 files changed:
neutron/agent/l3_agent.py
neutron/agent/linux/utils.py
neutron/agent/metadata/agent.py
neutron/common/exceptions.py
neutron/db/agents_db.py
neutron/db/dhcp_rpc_base.py
neutron/db/loadbalancer/loadbalancer_db.py
neutron/db/vpn/vpn_db.py
neutron/policy.py
neutron/service.py
neutron/services/loadbalancer/drivers/haproxy/namespace_driver.py
neutron/services/service_base.py
neutron/wsgi.py

index 69414d771d42902cf505542e52a3297201e9512b..84e96bdecea86f400213d3e6b63a25361d91c660 100644 (file)
@@ -35,6 +35,7 @@ from neutron.common import topics
 from neutron.common import utils as common_utils
 from neutron import context
 from neutron import manager
+from neutron.openstack.common import excutils
 from neutron.openstack.common import importutils
 from neutron.openstack.common import lockutils
 from neutron.openstack.common import log as logging
@@ -321,14 +322,13 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager):
         try:
             return self.plugin_rpc.get_external_network_id(self.context)
         except rpc_common.RemoteError as e:
-            if e.exc_type == 'TooManyExternalNetworks':
-                msg = _(
-                    "The 'gateway_external_network_id' option must be "
-                    "configured for this agent as Neutron has more than "
-                    "one external network.")
-                raise Exception(msg)
-            else:
-                raise
+            with excutils.save_and_reraise_exception():
+                if e.exc_type == 'TooManyExternalNetworks':
+                    msg = _(
+                        "The 'gateway_external_network_id' option must be "
+                        "configured for this agent as Neutron has more than "
+                        "one external network.")
+                    raise Exception(msg)
 
     def _router_added(self, router_id, router):
         ri = RouterInfo(router_id, self.root_helper,
index fdb8d0d4d9a71b838a794fa59b20a9d2411a4208..72b17b42520eae6fdd8045d52bf640c3fe7c32c9 100644 (file)
@@ -28,6 +28,7 @@ from eventlet.green import subprocess
 from eventlet import greenthread
 
 from neutron.common import utils
+from neutron.openstack.common import excutils
 from neutron.openstack.common import log as logging
 
 
@@ -116,10 +117,11 @@ def find_child_pids(pid):
     try:
         raw_pids = execute(['ps', '--ppid', pid, '-o', 'pid='])
     except RuntimeError as e:
-        # Exception has already been logged by execute
-        no_children_found = 'Exit code: 1' in str(e)
-        if no_children_found:
-            return []
         # Unexpected errors are the responsibility of the caller
-        raise
+        with excutils.save_and_reraise_exception() as ctxt:
+            # Exception has already been logged by execute
+            no_children_found = 'Exit code: 1' in str(e)
+            if no_children_found:
+                ctxt.reraise = False
+                return []
     return [x.strip() for x in raw_pids.split('\n') if x.strip()]
index 6209ebee82e7279824d46cfba581d9e570dd820c..86a3d6075101bd9dc0cef667b9b6047af4606ae3 100644 (file)
@@ -35,6 +35,7 @@ from neutron.common import constants as n_const
 from neutron.common import topics
 from neutron.common import utils
 from neutron import context
+from neutron.openstack.common import excutils
 from neutron.openstack.common import log as logging
 from neutron.openstack.common import loopingcall
 from neutron.openstack.common import service
@@ -255,8 +256,9 @@ class UnixDomainMetadataProxy(object):
             try:
                 os.unlink(cfg.CONF.metadata_proxy_socket)
             except OSError:
-                if os.path.exists(cfg.CONF.metadata_proxy_socket):
-                    raise
+                with excutils.save_and_reraise_exception() as ctxt:
+                    if not os.path.exists(cfg.CONF.metadata_proxy_socket):
+                        ctxt.reraise = False
         else:
             os.makedirs(dirname, 0o755)
 
index 9e77832b4d623b23cd4a8ef55d76d517d6dc288b..1b23a5274306fa6caf265aaeb511a4fe85053560 100644 (file)
@@ -19,6 +19,8 @@
 Neutron base exception handling.
 """
 
+from neutron.openstack.common import excutils
+
 
 class NeutronException(Exception):
     """Base Neutron Exception.
@@ -34,11 +36,11 @@ class NeutronException(Exception):
             super(NeutronException, self).__init__(self.message % kwargs)
             self.msg = self.message % kwargs
         except Exception:
-            if self.use_fatal_exceptions():
-                raise
-            else:
-                # at least get the core message out if something happened
-                super(NeutronException, self).__init__(self.message)
+            with excutils.save_and_reraise_exception() as ctxt:
+                if not self.use_fatal_exceptions():
+                    ctxt.reraise = False
+                    # at least get the core message out if something happened
+                    super(NeutronException, self).__init__(self.message)
 
     def __unicode__(self):
         return unicode(self.msg)
index 960085f97e7ec72e11eadc22ad45ec7c57a98965..938c30fa2c818e9aa7633f88fba844f9567379c0 100644 (file)
@@ -26,6 +26,7 @@ from neutron.db import models_v2
 from neutron.extensions import agent as ext_agent
 from neutron import manager
 from neutron.openstack.common.db import exception as db_exc
+from neutron.openstack.common import excutils
 from neutron.openstack.common import jsonutils
 from neutron.openstack.common import log as logging
 from neutron.openstack.common import timeutils
@@ -178,21 +179,22 @@ class AgentDbMixin(ext_agent.AgentPluginBase):
         try:
             return self._create_or_update_agent(context, agent)
         except db_exc.DBDuplicateEntry as e:
-            if e.columns == ['agent_type', 'host']:
-                # It might happen that two or more concurrent transactions are
-                # trying to insert new rows having the same value of
-                # (agent_type, host) pair at the same time (if there has been
-                # no such entry in the table and multiple agent status updates
-                # are being processed at the moment). In this case having a
-                # unique constraint on (agent_type, host) columns guarantees
-                # that only one transaction will succeed and insert a new agent
-                # entry, others will fail and be rolled back. That means we
-                # must retry them one more time: no INSERTs will be issued,
-                # because _get_agent_by_type_and_host() will return the
-                # existing agent entry, which will be updated multiple times
-                return self._create_or_update_agent(context, agent)
-
-            raise
+            with excutils.save_and_reraise_exception() as ctxt:
+                if e.columns == ['agent_type', 'host']:
+                    # It might happen that two or more concurrent transactions
+                    # are trying to insert new rows having the same value of
+                    # (agent_type, host) pair at the same time (if there has
+                    # been no such entry in the table and multiple agent status
+                    # updates are being processed at the moment). In this case
+                    # having a unique constraint on (agent_type, host) columns
+                    # guarantees that only one transaction will succeed and
+                    # insert a new agent entry, others will fail and be rolled
+                    # back. That means we must retry them one more time: no
+                    # INSERTs will be issued, because
+                    # _get_agent_by_type_and_host() will return the existing
+                    # agent entry, which will be updated multiple times
+                    ctxt.reraise = False
+                    return self._create_or_update_agent(context, agent)
 
 
 class AgentExtRpcCallback(object):
index 056141270d2f79803db30430295d5465f1c541b0..b945fec27df9e8c1b1db60ab50a94df8426d8c69 100644 (file)
@@ -22,6 +22,7 @@ from neutron.common import utils
 from neutron.extensions import portbindings
 from neutron import manager
 from neutron.openstack.common.db import exception as db_exc
+from neutron.openstack.common import excutils
 from neutron.openstack.common import log as logging
 
 
@@ -58,20 +59,22 @@ class DhcpRpcCallbackMixin(object):
                 raise n_exc.Invalid(message=msg)
         except (db_exc.DBError, n_exc.NetworkNotFound,
                 n_exc.SubnetNotFound, n_exc.IpAddressGenerationFailure) as e:
-            if isinstance(e, n_exc.IpAddressGenerationFailure):
-                # Check if the subnet still exists and if it does not, this is
-                # the reason why the ip address generation failed. In any other
-                # unlikely event re-raise
-                try:
-                    subnet_id = port['port']['fixed_ips'][0]['subnet_id']
-                    plugin.get_subnet(context, subnet_id)
-                except n_exc.SubnetNotFound:
-                    pass
-                else:
-                    raise
-            network_id = port['port']['network_id']
-            LOG.warn(_("Port for network %(net_id)s could not be created: "
-                       "%(reason)s") % {"net_id": network_id, 'reason': e})
+            with excutils.save_and_reraise_exception() as ctxt:
+                ctxt.reraise = False
+                if isinstance(e, n_exc.IpAddressGenerationFailure):
+                    # Check if the subnet still exists and if it does not,
+                    # this is the reason why the ip address generation failed.
+                    # In any other unlikely event re-raise
+                    try:
+                        subnet_id = port['port']['fixed_ips'][0]['subnet_id']
+                        plugin.get_subnet(context, subnet_id)
+                    except n_exc.SubnetNotFound:
+                        pass
+                    else:
+                        ctxt.reraise = True
+                network_id = port['port']['network_id']
+                LOG.warn(_("Port for network %(net_id)s could not be created: "
+                           "%(reason)s") % {"net_id": network_id, 'reason': e})
 
     def get_active_networks(self, context, **kwargs):
         """Retrieve and return a list of the active network ids."""
index 5968c9a7c7a553090c3b2bf700d87e25f261a1ac..7c711b6b61332af1c035aa3ecd4e01dfbf06de5b 100644 (file)
@@ -30,6 +30,7 @@ from neutron.extensions import loadbalancer
 from neutron.extensions.loadbalancer import LoadBalancerPluginBase
 from neutron import manager
 from neutron.openstack.common.db import exception
+from neutron.openstack.common import excutils
 from neutron.openstack.common import log as logging
 from neutron.openstack.common import uuidutils
 from neutron.plugins.common import constants
@@ -204,16 +205,15 @@ class LoadBalancerPluginDb(LoadBalancerPluginBase,
         try:
             r = self._get_by_id(context, model, id)
         except exc.NoResultFound:
-            if issubclass(model, Vip):
-                raise loadbalancer.VipNotFound(vip_id=id)
-            elif issubclass(model, Pool):
-                raise loadbalancer.PoolNotFound(pool_id=id)
-            elif issubclass(model, Member):
-                raise loadbalancer.MemberNotFound(member_id=id)
-            elif issubclass(model, HealthMonitor):
-                raise loadbalancer.HealthMonitorNotFound(monitor_id=id)
-            else:
-                raise
+            with excutils.save_and_reraise_exception():
+                if issubclass(model, Vip):
+                    raise loadbalancer.VipNotFound(vip_id=id)
+                elif issubclass(model, Pool):
+                    raise loadbalancer.PoolNotFound(pool_id=id)
+                elif issubclass(model, Member):
+                    raise loadbalancer.MemberNotFound(member_id=id)
+                elif issubclass(model, HealthMonitor):
+                    raise loadbalancer.HealthMonitorNotFound(monitor_id=id)
         return r
 
     def assert_modification_allowed(self, obj):
index 46406da72226a0a0631349f7470bb3e4145de895..cbcf6efe92e03d585b1c744f8a48beeeeaf9e44d 100644 (file)
@@ -32,6 +32,7 @@ from neutron.db import models_v2
 from neutron.extensions import vpnaas
 from neutron.extensions.vpnaas import VPNPluginBase
 from neutron import manager
+from neutron.openstack.common import excutils
 from neutron.openstack.common import log as logging
 from neutron.openstack.common import uuidutils
 from neutron.plugins.common import constants
@@ -178,18 +179,17 @@ class VPNPluginDb(VPNPluginBase, base_db.CommonDbMixin):
         try:
             r = self._get_by_id(context, model, v_id)
         except exc.NoResultFound:
-            if issubclass(model, IPsecSiteConnection):
-                raise vpnaas.IPsecSiteConnectionNotFound(
-                    ipsec_site_conn_id=v_id
-                )
-            elif issubclass(model, IKEPolicy):
-                raise vpnaas.IKEPolicyNotFound(ikepolicy_id=v_id)
-            elif issubclass(model, IPsecPolicy):
-                raise vpnaas.IPsecPolicyNotFound(ipsecpolicy_id=v_id)
-            elif issubclass(model, VPNService):
-                raise vpnaas.VPNServiceNotFound(vpnservice_id=v_id)
-            else:
-                raise
+            with excutils.save_and_reraise_exception():
+                if issubclass(model, IPsecSiteConnection):
+                    raise vpnaas.IPsecSiteConnectionNotFound(
+                        ipsec_site_conn_id=v_id
+                    )
+                elif issubclass(model, IKEPolicy):
+                    raise vpnaas.IKEPolicyNotFound(ikepolicy_id=v_id)
+                elif issubclass(model, IPsecPolicy):
+                    raise vpnaas.IPsecPolicyNotFound(ipsecpolicy_id=v_id)
+                elif issubclass(model, VPNService):
+                    raise vpnaas.VPNServiceNotFound(vpnservice_id=v_id)
         return r
 
     def assert_update_allowed(self, obj):
index c4c241a589f6448fb760da4d7f40d6d754c045bf..6906373c985f694b761e7db22608907c54b75e9f 100644 (file)
@@ -27,6 +27,7 @@ from neutron.api.v2 import attributes
 from neutron.common import exceptions
 import neutron.common.utils as utils
 from neutron import manager
+from neutron.openstack.common import excutils
 from neutron.openstack.common import importutils
 from neutron.openstack.common import log as logging
 from neutron.openstack.common import policy
@@ -274,8 +275,8 @@ class OwnerCheck(policy.Check):
                          fields=[parent_field])
                 target[self.target_field] = data[parent_field]
             except Exception:
-                LOG.exception(_('Policy check error while calling %s!'), f)
-                raise
+                with excutils.save_and_reraise_exception():
+                    LOG.exception(_('Policy check error while calling %s!'), f)
         match = self.match % target
         if self.kind in creds:
             return match == unicode(creds[self.kind])
index 32362343c8e27d93bc6fec51ab4e2dfdf18bcd29..d0c2d7eaa2c9a453a8caa683083a3508eabc9b84 100644 (file)
@@ -25,6 +25,7 @@ from oslo.config import cfg
 from neutron.common import config
 from neutron.common import legacy
 from neutron import context
+from neutron.openstack.common import excutils
 from neutron.openstack.common import importutils
 from neutron.openstack.common import log as logging
 from neutron.openstack.common import loopingcall
@@ -102,8 +103,9 @@ def serve_wsgi(cls):
             service = cls.create('quantum')
             service.start()
     except Exception:
-        LOG.exception(_('Unrecoverable error: please check log for details.'))
-        raise
+        with excutils.save_and_reraise_exception():
+            LOG.exception(_('Unrecoverable error: please check log '
+                            'for details.'))
 
     return service
 
index f815ea2261912d083448eda143f019be58d93df2..91fadb5151ed890e8d5afe9adf575b7c766f470d 100644 (file)
@@ -27,6 +27,7 @@ from neutron.agent.linux import ip_lib
 from neutron.agent.linux import utils
 from neutron.common import exceptions
 from neutron.common import utils as n_utils
+from neutron.openstack.common import excutils
 from neutron.openstack.common import importutils
 from neutron.openstack.common import log as logging
 from neutron.plugins.common import constants
@@ -65,10 +66,10 @@ class HaproxyNSDriver(agent_device_driver.AgentDeviceDriver):
         try:
             vif_driver = importutils.import_object(conf.interface_driver, conf)
         except ImportError:
-            msg = (_('Error importing interface driver: %s')
-                   % conf.haproxy.interface_driver)
-            LOG.error(msg)
-            raise
+            with excutils.save_and_reraise_exception():
+                msg = (_('Error importing interface driver: %s')
+                       % conf.haproxy.interface_driver)
+                LOG.error(msg)
 
         self.vif_driver = vif_driver
         self.plugin_rpc = plugin_rpc
index 2730dee6de96b614890bbc0d6b1c3b5606ca96a4..100979b9a4ba32823b3334ea0881be975d9bcb6a 100644 (file)
@@ -21,6 +21,7 @@ import six
 
 from neutron.api import extensions
 from neutron.db import servicetype_db as sdb
+from neutron.openstack.common import excutils
 from neutron.openstack.common import importutils
 from neutron.openstack.common import log as logging
 from neutron.services import provider_configuration as pconf
@@ -84,11 +85,11 @@ def load_drivers(service_type, plugin):
                       {'provider': provider['driver'],
                        'service_type': service_type})
         except ImportError:
-            LOG.exception(_("Error loading provider '%(provider)s' for "
-                            "service %(service_type)s"),
-                          {'provider': provider['driver'],
-                           'service_type': service_type})
-            raise
+            with excutils.save_and_reraise_exception():
+                LOG.exception(_("Error loading provider '%(provider)s' for "
+                                "service %(service_type)s"),
+                              {'provider': provider['driver'],
+                               'service_type': service_type})
 
     default_provider = None
     try:
index 8898e7af6125224008206956a95584aec5b547e8..9d9ff7b90ef823b29403c3fdbdef848602c27182 100644 (file)
@@ -40,6 +40,7 @@ from neutron.common import constants
 from neutron.common import exceptions as exception
 from neutron import context
 from neutron.openstack.common.db.sqlalchemy import session
+from neutron.openstack.common import excutils
 from neutron.openstack.common import gettextutils
 from neutron.openstack.common import jsonutils
 from neutron.openstack.common import log as logging
@@ -176,9 +177,10 @@ class Server(object):
                     sock = wrap_ssl(sock)
 
             except socket.error as err:
-                if err.errno != errno.EADDRINUSE:
-                    raise
-                eventlet.sleep(0.1)
+                with excutils.save_and_reraise_exception() as ctxt:
+                    if err.errno == errno.EADDRINUSE:
+                        ctxt.reraise = False
+                        eventlet.sleep(0.1)
         if not sock:
             raise RuntimeError(_("Could not bind to %(host)s:%(port)s "
                                "after trying for %(time)d seconds") %
@@ -698,19 +700,18 @@ class XMLDeserializer(TextDeserializer):
                 return result
             return dict({root_tag: result}, **links)
         except Exception as e:
-            parseError = False
-            # Python2.7
-            if (hasattr(etree, 'ParseError') and
-                isinstance(e, getattr(etree, 'ParseError'))):
-                parseError = True
-            # Python2.6
-            elif isinstance(e, expat.ExpatError):
-                parseError = True
-            if parseError:
-                msg = _("Cannot understand XML")
-                raise exception.MalformedRequestBody(reason=msg)
-            else:
-                raise
+            with excutils.save_and_reraise_exception():
+                parseError = False
+                # Python2.7
+                if (hasattr(etree, 'ParseError') and
+                    isinstance(e, getattr(etree, 'ParseError'))):
+                    parseError = True
+                # Python2.6
+                elif isinstance(e, expat.ExpatError):
+                    parseError = True
+                if parseError:
+                    msg = _("Cannot understand XML")
+                    raise exception.MalformedRequestBody(reason=msg)
 
     def _from_xml_node(self, node, listnames):
         """Convert a minidom node to a simple Python type.
@@ -832,8 +833,9 @@ class RequestDeserializer(object):
         try:
             deserializer = self.get_body_deserializer(content_type)
         except exception.InvalidContentType:
-            LOG.debug(_("Unable to deserialize body as provided Content-Type"))
-            raise
+            with excutils.save_and_reraise_exception():
+                LOG.debug(_("Unable to deserialize body as provided "
+                            "Content-Type"))
 
         return deserializer.deserialize(request.body, action)