]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Prevent full sync in dhcp_agent when possible
authorSudhakar Babu Gariganti <sudhakar-babu.gariganti@hp.com>
Fri, 11 Sep 2015 09:23:27 +0000 (14:53 +0530)
committerSudhakar Babu Gariganti <sudhakar-babu.gariganti@hp.com>
Tue, 15 Sep 2015 17:39:22 +0000 (23:09 +0530)
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
neutron/tests/unit/agent/dhcp/test_agent.py

index 551c5be41db00bdb64453511267562d76e3f6ec9..af0ccf5c7d2fe0914ae688670d52f63eecad76bb 100644 (file)
@@ -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()
index 3069c87728c5b53deecd82032c6fbe11ac0455ab..ae5594a8ef2653ae6b8e009e0e96db10fcdc2d63 100644 (file)
@@ -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: