From 2d6325bd2d4d4649d87a4eb09debf8258f04686f Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Wed, 15 Jan 2025 10:28:30 -0500 Subject: [PATCH] refactor: Remove OvnIdlDistributedLock._last_touch attribute It's overwritten on every notify call. Signed-off-by: Ihar Hrachyshka Change-Id: I4524034c51940532e6156dfdf2320813e5e91b5d --- .../ovn/mech_driver/ovsdb/ovsdb_monitor.py | 7 ++----- .../ovn/mech_driver/ovsdb/test_ovsdb_monitor.py | 15 +++++---------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/neutron/plugins/ml2/drivers/ovn/mech_driver/ovsdb/ovsdb_monitor.py b/neutron/plugins/ml2/drivers/ovn/mech_driver/ovsdb/ovsdb_monitor.py index 13eac78cba1..cae022ac145 100644 --- a/neutron/plugins/ml2/drivers/ovn/mech_driver/ovsdb/ovsdb_monitor.py +++ b/neutron/plugins/ml2/drivers/ovn/mech_driver/ovsdb/ovsdb_monitor.py @@ -818,7 +818,6 @@ class OvnIdlDistributedLock(BaseOvnIdl): self._node_uuid = self.driver.node_uuid self._hash_ring = hash_ring_manager.HashRingManager( self.driver.hash_ring_group) - self._last_touch = None def notify(self, event, row, updates=None): try: @@ -834,11 +833,9 @@ class OvnIdlDistributedLock(BaseOvnIdl): # If the worker hasn't been health checked by the maintenance # thread (see bug #1834498), indicate that it's alive here - self._last_touch = node_last_touch - time_now = timeutils.utcnow() - touch_timeout = time_now - datetime.timedelta( + touch_timeout = timeutils.utcnow() - datetime.timedelta( seconds=ovn_const.HASH_RING_TOUCH_INTERVAL) - if not self._last_touch or touch_timeout >= self._last_touch: + if not node_last_touch or touch_timeout >= node_last_touch: # NOTE(lucasagomes): Guard the db operation with an exception # handler. If heartbeating fails for whatever reason, log # the error and continue with processing the event diff --git a/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_ovsdb_monitor.py b/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_ovsdb_monitor.py index 818a4a11fca..e76ca99867d 100644 --- a/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_ovsdb_monitor.py +++ b/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/ovsdb/test_ovsdb_monitor.py @@ -251,8 +251,6 @@ class TestOvnIdlDistributedLock(base.BaseTestCase): @mock.patch.object(ovn_hash_ring_db, 'touch_node') def test_notify_skip_touch_node(self, mock_touch_node): - # Set a time for last touch - self.idl._last_touch = timeutils.utcnow() self.idl.notify(self.fake_event, self.fake_row) # Assert that touch_node() wasn't called @@ -261,15 +259,12 @@ class TestOvnIdlDistributedLock(base.BaseTestCase): @mock.patch.object(ovn_hash_ring_db, 'touch_node') def test_notify_last_touch_expired(self, mock_touch_node): - # Set a time for last touch - self.idl._last_touch = timeutils.utcnow() + # make the node old enough to require a touch + updated_at = timeutils.utcnow() - datetime.timedelta( + seconds=ovn_const.HASH_RING_TOUCH_INTERVAL + 1) + self.mock_get_node.return_value = (self.node_uuid, updated_at) - # Let's expire the touch node interval for the next utcnow() - with mock.patch.object(timeutils, 'utcnow') as mock_utcnow: - mock_utcnow.return_value = ( - self.idl._last_touch + datetime.timedelta( - seconds=ovn_const.HASH_RING_TOUCH_INTERVAL + 1)) - self.idl.notify(self.fake_event, self.fake_row) + self.idl.notify(self.fake_event, self.fake_row) # Assert that touch_node() was invoked mock_touch_node.assert_called_once_with(mock.ANY, self.node_uuid)