]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Skip DBDuplicateEntry exception in security group creation
authorAnn Kamyshnikova <akamyshnikova@mirantis.com>
Fri, 13 Feb 2015 15:41:21 +0000 (18:41 +0300)
committerAnn Kamyshnikova <akamyshnikova@mirantis.com>
Tue, 17 Feb 2015 15:48:02 +0000 (18:48 +0300)
An DBDuplicateEntry expection appeared if it is attempted to
create secutity groups in more then one thread. In this situation
at one moment it is trying to be created 2 default security groups
which is not allowed. In this case exception should skipped with
a message about failed attempt.

Closes-bug: #1419723

Change-Id: Id6fe997ad3b72f875160e1e31a3dc17c3cc53b75

neutron/db/securitygroups_db.py
neutron/tests/unit/test_extension_security_group.py

index 58b802a15083212189b1370056aff3a4e1492d29..ec3f548e4c501ef4557b121bf3be756c428dc79a 100644 (file)
@@ -13,6 +13,7 @@
 #    under the License.
 
 import netaddr
+from oslo_db import exception
 import sqlalchemy as sa
 from sqlalchemy import orm
 from sqlalchemy.orm import exc
@@ -24,8 +25,10 @@ from neutron.db import db_base_plugin_v2
 from neutron.db import model_base
 from neutron.db import models_v2
 from neutron.extensions import securitygroup as ext_sg
+from neutron.openstack.common import log as logging
 from neutron.openstack.common import uuidutils
 
+LOG = logging.getLogger(__name__)
 
 IP_PROTOCOL_MAP = {constants.PROTO_NAME_TCP: constants.PROTO_NUM_TCP,
                    constants.PROTO_NAME_UDP: constants.PROTO_NUM_UDP,
@@ -124,7 +127,11 @@ class SecurityGroupDbMixin(ext_sg.SecurityGroupPluginBase):
         tenant_id = self._get_tenant_id_for_create(context, s)
 
         if not default_sg:
-            self._ensure_default_security_group(context, tenant_id)
+            try:
+                self._ensure_default_security_group(context, tenant_id)
+            except exception.DBDuplicateEntry as ex:
+                LOG.debug("Duplicate default security group %s was not"
+                          " created", ex.value)
 
         with context.session.begin(subtransactions=True):
             security_group_db = SecurityGroup(id=s.get('id') or (
index 1743605e6589f3c2475f6552dfb6713865c1bc06..1489037fa73eedca68275ad2c7462b4f48f7c220 100644 (file)
@@ -16,6 +16,7 @@
 import contextlib
 
 import mock
+import oslo_db.exception as exc
 import webob.exc
 
 from neutron.api.v2 import attributes as attr
@@ -270,6 +271,12 @@ class TestSecurityGroups(SecurityGroupDBTestCase):
                     'port_range_min': None}
         self._assert_sg_rule_has_kvs(v6_rule, expected)
 
+    def test_skip_duplicate_default_sg_error(self):
+        with mock.patch.object(SecurityGroupTestPlugin,
+                               '_ensure_default_security_group',
+                               side_effect=exc.DBDuplicateEntry()):
+            self._make_security_group(self.fmt, 'test_sg', 'test_desc')
+
     def test_update_security_group(self):
         with self.security_group() as sg:
             data = {'security_group': {'name': 'new_name',