From 1ad6ac448067306fcf7ea562840e63fd257f0556 Mon Sep 17 00:00:00 2001 From: Sudhakar Babu Gariganti Date: Fri, 11 Sep 2015 14:53:27 +0530 Subject: [PATCH] Prevent full sync in dhcp_agent when possible If an exception occurs in sync_state method, we try for a full sync even in the case where we have fewer networks to resync for. This turns out to be pretty costly in scaled environments. This patch addresses the above behavior by resyncing only for the eariler set of failed networks. Closes-Bug: #1495592 Change-Id: I069e992b3b7814370d409236b6a3c81a25829cc1 --- neutron/agent/dhcp/agent.py | 10 +++++++--- neutron/tests/unit/agent/dhcp/test_agent.py | 18 +++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/neutron/agent/dhcp/agent.py b/neutron/agent/dhcp/agent.py index 551c5be41..af0ccf5c7 100644 --- a/neutron/agent/dhcp/agent.py +++ b/neutron/agent/dhcp/agent.py @@ -137,11 +137,11 @@ class DhcpAgent(manager.Manager): LOG.exception(_LE('Unable to %(action)s dhcp for %(net_id)s.'), {'net_id': network.id, 'action': action}) - def schedule_resync(self, reason, network=None): + def schedule_resync(self, reason, network_id=None): """Schedule a resync for a given network and reason. If no network is specified, resync all networks. """ - self.needs_resync_reasons[network].append(reason) + self.needs_resync_reasons[network_id].append(reason) @utils.synchronized('dhcp-agent') def sync_state(self, networks=None): @@ -173,7 +173,11 @@ class DhcpAgent(manager.Manager): LOG.info(_LI('Synchronizing state complete')) except Exception as e: - self.schedule_resync(e) + if only_nets: + for network_id in only_nets: + self.schedule_resync(e, network_id) + else: + self.schedule_resync(e) LOG.exception(_LE('Unable to sync network state.')) @utils.exception_logger() diff --git a/neutron/tests/unit/agent/dhcp/test_agent.py b/neutron/tests/unit/agent/dhcp/test_agent.py index 3069c8772..ae5594a8e 100644 --- a/neutron/tests/unit/agent/dhcp/test_agent.py +++ b/neutron/tests/unit/agent/dhcp/test_agent.py @@ -384,7 +384,7 @@ class TestDhcpAgent(base.BaseTestCase): self._test_sync_state_helper(known_net_ids, active_net_ids) w.assert_called_once_with() - def test_sync_state_plugin_error(self): + def test_sync_state_for_all_networks_plugin_error(self): with mock.patch(DHCP_PLUGIN) as plug: mock_plugin = mock.Mock() mock_plugin.get_active_networks_info.side_effect = Exception @@ -399,6 +399,22 @@ class TestDhcpAgent(base.BaseTestCase): self.assertTrue(log.called) self.assertTrue(schedule_resync.called) + def test_sync_state_for_one_network_plugin_error(self): + with mock.patch(DHCP_PLUGIN) as plug: + mock_plugin = mock.Mock() + exc = Exception() + mock_plugin.get_active_networks_info.side_effect = exc + plug.return_value = mock_plugin + + with mock.patch.object(dhcp_agent.LOG, 'exception') as log: + dhcp = dhcp_agent.DhcpAgent(HOSTNAME) + with mock.patch.object(dhcp, + 'schedule_resync') as schedule_resync: + dhcp.sync_state(['foo_network']) + + self.assertTrue(log.called) + schedule_resync.assert_called_with(exc, 'foo_network') + def test_periodic_resync(self): dhcp = dhcp_agent.DhcpAgent(HOSTNAME) with mock.patch.object(dhcp_agent.eventlet, 'spawn') as spawn: -- 2.45.2