]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fix race condition in network scheduling to dhcp agent
authorEugene Nikanorov <enikanorov@mirantis.com>
Wed, 5 Feb 2014 21:52:35 +0000 (01:52 +0400)
committerThomas Goirand <thomas@goirand.fr>
Thu, 13 Mar 2014 07:20:17 +0000 (15:20 +0800)
Rarely dhcp agent rpc call get_active_networks_info() can interleave
with network scheduling initiated by create.port.end notification.
In this case scheduling raises and port creation returns 500.
Need to synchronize on DhcpNetworkBindings table.

Closes-Bug: #1276552
Change-Id: I52d94a40772a99c7032dba15b200bf0f21362f93

neutron/scheduler/dhcp_agent_scheduler.py

index 5134bca64c54c6717ccac079fe55b83a5627d7ca..20632c58726478a07e0ce5d2181bfd8fcb47d5d1 100644 (file)
@@ -22,6 +22,7 @@ from oslo.config import cfg
 from neutron.common import constants
 from neutron.db import agents_db
 from neutron.db import agentschedulers_db
+from neutron.openstack.common.db import exception as db_exc
 from neutron.openstack.common import log as logging
 
 
@@ -35,10 +36,17 @@ class ChanceScheduler(object):
     """
 
     def _schedule_bind_network(self, context, agent, network_id):
-        binding = agentschedulers_db.NetworkDhcpAgentBinding()
-        binding.dhcp_agent = agent
-        binding.network_id = network_id
-        context.session.add(binding)
+        try:
+            binding = agentschedulers_db.NetworkDhcpAgentBinding()
+            binding.dhcp_agent = agent
+            binding.network_id = network_id
+            context.session.add(binding)
+            # try to actually write the changes and catch integrity
+            # DBDuplicateEntry
+            context.session.flush()
+        except db_exc.DBDuplicateEntry:
+            # it's totally ok, someone just did our job!
+            pass
         LOG.debug(_('Network %(network_id)s is scheduled to be hosted by '
                     'DHCP agent %(agent_id)s'),
                   {'network_id': network_id,