]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Avoid dhcp agent race condition on subnet and network delete
authorarmando-migliaccio <amigliaccio@nicira.com>
Sat, 28 Sep 2013 01:38:16 +0000 (18:38 -0700)
committerGerrit Code Review <review@openstack.org>
Sat, 26 Oct 2013 17:36:05 +0000 (17:36 +0000)
This patch ensures that, when the dhcp agent queries the server
to retrieve and delete its DHCP port, it does so by selecting
for update. This has been done by introducing a new method
that combines the get and delete in one shot.

Closes-Bug:1197627

Change-Id: I1e8a87d7dc1a1cb9309aeefd41619e20f49f95a6

neutron/db/db_base_plugin_v2.py
neutron/db/dhcp_rpc_base.py
neutron/tests/unit/test_db_rpc_base.py

index 52fc45389aaa9938bf49aced9e7b910cb47d0a8e..f7b0442f061b4478290c1691400558ab4444c6d1 100644 (file)
@@ -1402,6 +1402,14 @@ class NeutronDbPluginV2(neutron_plugin_base_v2.NeutronPluginBaseV2,
         with context.session.begin(subtransactions=True):
             self._delete_port(context, id)
 
+    def delete_ports(self, context, filters):
+        with context.session.begin(subtransactions=True):
+            ports = self._get_ports_query(
+                context, filters=filters).with_lockmode('update')
+            if ports:
+                for port in ports:
+                    self.delete_port(context, port['id'])
+
     def _delete_port(self, context, id):
         port = self._get_port(context, id)
 
index a8a2b4bf2ade48651785aa8efadad4a6fbcd1d70..2eacbbff4590f13be6393a7ec1c6c41ac3c2f89d 100644 (file)
@@ -179,10 +179,7 @@ class DhcpRpcCallbackMixin(object):
                   {'network_id': network_id, 'host': host})
         plugin = manager.NeutronManager.get_plugin()
         filters = dict(network_id=[network_id], device_id=[device_id])
-        ports = plugin.get_ports(context, filters=filters)
-
-        if ports:
-            plugin.delete_port(context, ports[0]['id'])
+        plugin.delete_ports(context, filters=filters)
 
     def release_port_fixed_ip(self, context, **kwargs):
         """Release the fixed_ip associated the subnet on a port."""
index 9be906529e4f9487d81c54dd93a0ad494b5867fb..e878f0936fc9d87fa4be662a47eeab008d87cca7 100644 (file)
@@ -131,9 +131,9 @@ class TestDhcpRpcCallackMixin(base.BaseTestCase):
                                          device_id='devid')
 
         self.plugin.assert_has_calls([
-            mock.call.get_ports(mock.ANY, filters=dict(network_id=['netid'],
-                                                       device_id=['devid'])),
-            mock.call.delete_port(mock.ANY, 'port_id')])
+            mock.call.delete_ports(mock.ANY,
+                                   filters=dict(network_id=['netid'],
+                                                device_id=['devid']))])
 
     def test_release_port_fixed_ip(self):
         port_retval = dict(id='port_id', fixed_ips=[dict(subnet_id='a')])