From 7dbc61346a89af1f4f4a002171f7f36a74034049 Mon Sep 17 00:00:00 2001 From: zhouhenglc Date: Thu, 9 Jun 2022 09:16:06 +0800 Subject: [PATCH] remove unused updated_at parameter for AgentCache.update we cannot find this parameter passed in anywhere except for some unit tests. currently we have used nb_cfg_timestamp[1] as agent updated time. there are no other scenarios for this parameter. this patch remove it and update some unit test. [1] https://review.opendev.org/c/openstack/neutron/+/802834 Closes-bug: #1978035 Change-Id: Ic964b7ddc70988bb1a822b07be6a1be4d197287e --- .../ml2/drivers/ovn/agent/neutron_agent.py | 54 +++++++++---------- .../ovn/mech_driver/test_mech_driver.py | 54 +++++++++---------- 2 files changed, 51 insertions(+), 57 deletions(-) diff --git a/neutron/plugins/ml2/drivers/ovn/agent/neutron_agent.py b/neutron/plugins/ml2/drivers/ovn/agent/neutron_agent.py index 6f0d69eec76..ac87a71b474 100644 --- a/neutron/plugins/ml2/drivers/ovn/agent/neutron_agent.py +++ b/neutron/plugins/ml2/drivers/ovn/agent/neutron_agent.py @@ -38,27 +38,26 @@ class NeutronAgent(abc.ABC): # Register the subclasses to be looked up by their type NeutronAgent.types[cls.agent_type] = cls - def __init__(self, chassis_private, driver, updated_at=None): + def __init__(self, chassis_private, driver): self.driver = driver self.set_down = False - self.update(chassis_private, updated_at) + self.update(chassis_private) - def update(self, chassis_private, updated_at=None, clear_down=False): + def update(self, chassis_private, clear_down=False): self.chassis_private = chassis_private - if not updated_at: - # When use the Chassis_Private table for agents health check, - # chassis_private has attribute nb_cfg_timestamp. - # nb_cfg_timestamp: the timestamp when ovn-controller finishes - # processing the change corresponding to nb_cfg( - # https://www.ovn.org/support/dist-docs/ovn-sb.5.html). - # it can better reflect the status of chassis. - # nb_cfg_timestamp is milliseconds, need to convert to datetime. - if hasattr(chassis_private, 'nb_cfg_timestamp'): - updated_at = datetime.datetime.fromtimestamp( - chassis_private.nb_cfg_timestamp / 1000, - datetime.timezone.utc) - else: - updated_at = timeutils.utcnow(with_timezone=True) + # When use the Chassis_Private table for agents health check, + # chassis_private has attribute nb_cfg_timestamp. + # nb_cfg_timestamp: the timestamp when ovn-controller finishes + # processing the change corresponding to nb_cfg( + # https://www.ovn.org/support/dist-docs/ovn-sb.5.html). + # it can better reflect the status of chassis. + # nb_cfg_timestamp is milliseconds, need to convert to datetime. + if hasattr(chassis_private, 'nb_cfg_timestamp'): + updated_at = datetime.datetime.fromtimestamp( + chassis_private.nb_cfg_timestamp / 1000, + datetime.timezone.utc) + else: + updated_at = timeutils.utcnow(with_timezone=True) self.updated_at = updated_at if clear_down: self.set_down = False @@ -112,8 +111,8 @@ class NeutronAgent(abc.ABC): return False @classmethod - def from_type(cls, _type, chassis_private, driver, updated_at=None): - return cls.types[_type](chassis_private, driver, updated_at) + def from_type(cls, _type, chassis_private, driver): + return cls.types[_type](chassis_private, driver) @property @abc.abstractmethod @@ -141,7 +140,7 @@ class ControllerAgent(NeutronAgent): binary = 'ovn-controller' @staticmethod # it is by default, but this makes pep8 happy - def __new__(cls, chassis_private, driver, updated_at=None): + def __new__(cls, chassis_private, driver): external_ids = cls.chassis_from_private(chassis_private).external_ids if ('enable-chassis-as-gw' in external_ids.get('ovn-cms-options', [])): @@ -165,8 +164,8 @@ class ControllerAgent(NeutronAgent): return self.chassis_private.external_ids.get( ovn_const.OVN_AGENT_DESC_KEY, '') - def update(self, chassis_private, updated_at=None, clear_down=False): - super().update(chassis_private, updated_at, clear_down) + def update(self, chassis_private, clear_down=False): + super().update(chassis_private, clear_down) external_ids = self.chassis_from_private(chassis_private).external_ids if 'enable-chassis-as-gw' in external_ids.get('ovn-cms-options', []): self.__class__ = ControllerGatewayAgent @@ -175,8 +174,8 @@ class ControllerAgent(NeutronAgent): class ControllerGatewayAgent(ControllerAgent): agent_type = ovn_const.OVN_CONTROLLER_GW_AGENT - def update(self, chassis_private, updated_at=None, clear_down=False): - super().update(chassis_private, updated_at, clear_down) + def update(self, chassis_private, clear_down=False): + super().update(chassis_private, clear_down) external_ids = self.chassis_from_private(chassis_private).external_ids if ('enable-chassis-as-gw' not in external_ids.get('ovn-cms-options', [])): @@ -238,14 +237,13 @@ class AgentCache: def __getitem__(self, key): return self.agents[key] - def update(self, agent_type, row, updated_at=None, clear_down=False): + def update(self, agent_type, row, clear_down=False): cls = NeutronAgent.types[agent_type] try: agent = self.agents[cls.id_from_chassis_private(row)] - agent.update(row, updated_at=updated_at, clear_down=clear_down) + agent.update(row, clear_down=clear_down) except KeyError: - agent = NeutronAgent.from_type(agent_type, row, self.driver, - updated_at=updated_at) + agent = NeutronAgent.from_type(agent_type, row, self.driver) self.agents[agent.agent_id] = agent return agent diff --git a/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/test_mech_driver.py b/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/test_mech_driver.py index 92442c65929..1e27dee29d7 100644 --- a/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/test_mech_driver.py +++ b/neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/test_mech_driver.py @@ -101,37 +101,25 @@ class MechDriverSetupBase(abc.ABC): chassis_private.name = name if name else str(uuid.uuid4()) return chassis_private - def _add_chassis_agent(self, nb_cfg, agent_type, chassis_private=None, - updated_at=None): + def _add_chassis_agent(self, nb_cfg, agent_type, chassis_private=None): chassis_private = chassis_private or self._add_chassis(nb_cfg) if hasattr(chassis_private, 'nb_cfg_timestamp') and isinstance( chassis_private.nb_cfg_timestamp, mock.Mock): del chassis_private.nb_cfg_timestamp chassis_private.external_ids = {} - if updated_at: - chassis_private.external_ids = { - ovn_const.OVN_LIVENESS_CHECK_EXT_ID_KEY: - datetime.datetime.isoformat(updated_at)} if agent_type == ovn_const.OVN_METADATA_AGENT: chassis_private.external_ids.update({ ovn_const.OVN_AGENT_METADATA_SB_CFG_KEY: nb_cfg, ovn_const.OVN_AGENT_METADATA_ID_KEY: str(uuid.uuid4())}) chassis_private.chassis = [chassis_private] - return neutron_agent.AgentCache().update(agent_type, chassis_private, - updated_at) + return neutron_agent.AgentCache().update(agent_type, chassis_private) - def _add_agent(self, name, alive=True): + def _add_agent(self, name, nb_cfg_offset=0): nb_cfg = 5 - now = timeutils.utcnow(with_timezone=True) - if not alive: - updated_at = now - datetime.timedelta(cfg.CONF.agent_down_time + 1) - self.mech_driver.nb_ovn.nb_global.nb_cfg = nb_cfg - else: - updated_at = now - self.mech_driver.nb_ovn.nb_global.nb_cfg = nb_cfg + 2 + self.mech_driver.nb_ovn.nb_global.nb_cfg = nb_cfg + nb_cfg_offset chassis = self._add_chassis(nb_cfg, name=name) return self._add_chassis_agent( - nb_cfg, ovn_const.OVN_CONTROLLER_AGENT, chassis, updated_at) + nb_cfg, ovn_const.OVN_CONTROLLER_AGENT, chassis) class TestOVNMechanismDriverBase(MechDriverSetupBase, @@ -1266,9 +1254,13 @@ class TestOVNMechanismDriver(TestOVNMechanismDriverBase): self._test_bind_port_failed(fake_segments) def test_bind_port_host_not_alive(self): - agent = self._add_agent('agent_no_alive', False) - neutron_agent.AgentCache().get_agents.return_value = [agent] - self._test_bind_port_failed([]) + agent = self._add_agent('agent_no_alive', 2) + now = timeutils.utcnow(with_timezone=True) + fake_now = now + datetime.timedelta(cfg.CONF.agent_down_time + 1) + with mock.patch.object(timeutils, 'utcnow') as get_now: + get_now.return_value = fake_now + neutron_agent.AgentCache().get_agents.return_value = [agent] + self._test_bind_port_failed([]) def _test_bind_port(self, fake_segments): fake_port = fakes.FakePort.create_one_port().info() @@ -2141,12 +2133,14 @@ class TestOVNMechanismDriver(TestOVNMechanismDriverBase): for agent_type in (ovn_const.OVN_CONTROLLER_AGENT, ovn_const.OVN_METADATA_AGENT): self.mech_driver.nb_ovn.nb_global.nb_cfg = nb_cfg + 1 - now = timeutils.utcnow() - updated_at = now - datetime.timedelta(cfg.CONF.agent_down_time + 1) agent = self._add_chassis_agent(nb_cfg, agent_type, - chassis_private, updated_at) - self.assertTrue(agent.alive, "Agent of type %s alive=%s" % - (agent.agent_type, agent.alive)) + chassis_private) + now = timeutils.utcnow() + fake_now = now + datetime.timedelta(cfg.CONF.agent_down_time + 1) + with mock.patch.object(timeutils, 'utcnow') as get_now: + get_now.return_value = fake_now + self.assertTrue(agent.alive, "Agent of type %s alive=%s" % + (agent.agent_type, agent.alive)) def test_agent_alive_not_timed_out(self): nb_cfg = 3 @@ -2166,11 +2160,13 @@ class TestOVNMechanismDriver(TestOVNMechanismDriverBase): ovn_const.OVN_METADATA_AGENT): self.mech_driver.nb_ovn.nb_global.nb_cfg = nb_cfg + 2 now = timeutils.utcnow(with_timezone=True) - updated_at = now - datetime.timedelta(cfg.CONF.agent_down_time + 1) agent = self._add_chassis_agent(nb_cfg, agent_type, - chassis_private, updated_at) - self.assertFalse(agent.alive, "Agent of type %s alive=%s" % - (agent.agent_type, agent.alive)) + chassis_private) + fake_now = now + datetime.timedelta(cfg.CONF.agent_down_time + 1) + with mock.patch.object(timeutils, 'utcnow') as get_now: + get_now.return_value = fake_now + self.assertFalse(agent.alive, "Agent of type %s alive=%s" % + (agent.agent_type, agent.alive)) def test_agent_with_nb_cfg_timestamp_timeout(self): nb_cfg = 3