From 882b381ccca18cea37d574ba561928d9536722da Mon Sep 17 00:00:00 2001 From: Slawek Kaplonski Date: Thu, 28 Apr 2022 16:51:25 +0200 Subject: [PATCH] Handle properly ObjectNotFound while deleting network from DHCP agent In case when 2 neutron servers are trying to remove network from the same DHCP agent it may happend that one of them will not be able to find binding object anymore thus deletion of that binding object will raise ObjectNotFound exception. This patch adds proper handling of such case to not raise ugly stacktrace in neutron logs. Closes-Bug: #1970759 Change-Id: I67d516c4583aa0c20416114b92a6d69ece5b970c (cherry picked from commit 8df2f69b6f93fd964cc96efb42d085063b90a8e6) --- neutron/db/agentschedulers_db.py | 5 ++--- neutron/tests/unit/db/test_agentschedulers_db.py | 5 +++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/neutron/db/agentschedulers_db.py b/neutron/db/agentschedulers_db.py index 6c8ada03d2b..c5c0bc70b0a 100644 --- a/neutron/db/agentschedulers_db.py +++ b/neutron/db/agentschedulers_db.py @@ -407,9 +407,9 @@ class DhcpAgentSchedulerDbMixin(dhcpagentscheduler def remove_network_from_dhcp_agent(self, context, id, network_id, notify=True): agent = self._get_agent(context, id) - binding_obj = network.NetworkDhcpAgentBinding.get_object( + deleted_bindings = network.NetworkDhcpAgentBinding.delete_objects( context, network_id=network_id, dhcp_agent_id=id) - if not binding_obj: + if not deleted_bindings: raise das_exc.NetworkNotHostedByDhcpAgent( network_id=network_id, agent_id=id) @@ -428,7 +428,6 @@ class DhcpAgentSchedulerDbMixin(dhcpagentscheduler except n_exc.PortNotFound: LOG.debug("DHCP port %s has been deleted concurrently", port['id']) - binding_obj.delete() if not notify: return diff --git a/neutron/tests/unit/db/test_agentschedulers_db.py b/neutron/tests/unit/db/test_agentschedulers_db.py index c81e2f59139..f0cdbcf133c 100644 --- a/neutron/tests/unit/db/test_agentschedulers_db.py +++ b/neutron/tests/unit/db/test_agentschedulers_db.py @@ -1472,6 +1472,11 @@ class OvsDhcpAgentNotifierTestCase(test_agent.AgentDBTestMixIn, self._remove_network_from_dhcp_agent(hosta_id, network_id) + # Call it second time, it should be already deleted so should 409 be + # returned this time + self._remove_network_from_dhcp_agent( + hosta_id, network_id, + expected_code=exc.HTTPConflict.code) self.dhcp_notifier_cast.assert_called_with( mock.ANY, 'network_delete_end', {'network_id': network_id,