From d10ee544b2619c7b23b5fa8f2be35a82a41a3459 Mon Sep 17 00:00:00 2001 From: Nurmatov Mamatisa Date: Fri, 4 Jun 2021 12:04:27 +0300 Subject: [PATCH] [DHCP] Fix cleanup_deleted_ports method Assume that only one port is deleted within 24 hours, in method cleanup_deleted_ports the port removes from deleted_ports but not removes from deleted_ports_ts In this fix ports older than 1 day will be dropped from deleted_ports and deleted_ports_ts properly Closes-Bug: #1930838 Change-Id: I1af32e72abb9f101f9729aa6d1354c33a95c98ee (cherry picked from commit 383f209b502493ca6b059394e2644def754b2de1) --- neutron/agent/dhcp/agent.py | 5 +++-- neutron/tests/unit/agent/dhcp/test_agent.py | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/neutron/agent/dhcp/agent.py b/neutron/agent/dhcp/agent.py index 4ed96537d6a..6b5a41ad735 100644 --- a/neutron/agent/dhcp/agent.py +++ b/neutron/agent/dhcp/agent.py @@ -1003,11 +1003,12 @@ class NetworkCache(object): "self._deleted_ports" and "self._deleted_ports_ts". """ timestamp_min = timeutils.utcnow_ts() - DELETED_PORT_MAX_AGE - idx = None - for idx, (ts, port_id) in enumerate(self._deleted_ports_ts): + idx = 0 + for (ts, port_id) in self._deleted_ports_ts: if ts > timestamp_min: break self._deleted_ports.remove(port_id) + idx += 1 if idx: self._deleted_ports_ts = self._deleted_ports_ts[idx:] diff --git a/neutron/tests/unit/agent/dhcp/test_agent.py b/neutron/tests/unit/agent/dhcp/test_agent.py index 494c20702fb..bf4b96624ba 100644 --- a/neutron/tests/unit/agent/dhcp/test_agent.py +++ b/neutron/tests/unit/agent/dhcp/test_agent.py @@ -1655,6 +1655,12 @@ class TestNetworkCache(base.BaseTestCase): nc = dhcp_agent.NetworkCache() nc.add_to_deleted_ports(fake_port1.id) utils.wait_until_true(lambda: nc._deleted_ports == set(), timeout=7) + self.assertEqual([], self.nc._deleted_ports_ts) + + # check the second iteration is ok too + nc.add_to_deleted_ports(fake_port2.id) + utils.wait_until_true(lambda: nc._deleted_ports == set(), timeout=7) + self.assertEqual([], self.nc._deleted_ports_ts) class FakePort1(object):