Skip to content

Commit ef3729e

Browse files
zhouhenglcralonsoh
authored andcommitted
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 (cherry picked from commit 7dbc613)
1 parent 4303039 commit ef3729e

File tree

2 files changed

+51
-57
lines changed

2 files changed

+51
-57
lines changed

neutron/plugins/ml2/drivers/ovn/agent/neutron_agent.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,26 @@ def __init_subclass__(cls):
3838
# Register the subclasses to be looked up by their type
3939
NeutronAgent.types[cls.agent_type] = cls
4040

41-
def __init__(self, chassis_private, driver, updated_at=None):
41+
def __init__(self, chassis_private, driver):
4242
self.driver = driver
4343
self.set_down = False
44-
self.update(chassis_private, updated_at)
44+
self.update(chassis_private)
4545

46-
def update(self, chassis_private, updated_at=None, clear_down=False):
46+
def update(self, chassis_private, clear_down=False):
4747
self.chassis_private = chassis_private
48-
if not updated_at:
49-
# When use the Chassis_Private table for agents health check,
50-
# chassis_private has attribute nb_cfg_timestamp.
51-
# nb_cfg_timestamp: the timestamp when ovn-controller finishes
52-
# processing the change corresponding to nb_cfg(
53-
# https://www.ovn.org/support/dist-docs/ovn-sb.5.html).
54-
# it can better reflect the status of chassis.
55-
# nb_cfg_timestamp is milliseconds, need to convert to datetime.
56-
if hasattr(chassis_private, 'nb_cfg_timestamp'):
57-
updated_at = datetime.datetime.fromtimestamp(
58-
chassis_private.nb_cfg_timestamp / 1000,
59-
datetime.timezone.utc)
60-
else:
61-
updated_at = timeutils.utcnow(with_timezone=True)
48+
# When use the Chassis_Private table for agents health check,
49+
# chassis_private has attribute nb_cfg_timestamp.
50+
# nb_cfg_timestamp: the timestamp when ovn-controller finishes
51+
# processing the change corresponding to nb_cfg(
52+
# https://www.ovn.org/support/dist-docs/ovn-sb.5.html).
53+
# it can better reflect the status of chassis.
54+
# nb_cfg_timestamp is milliseconds, need to convert to datetime.
55+
if hasattr(chassis_private, 'nb_cfg_timestamp'):
56+
updated_at = datetime.datetime.fromtimestamp(
57+
chassis_private.nb_cfg_timestamp / 1000,
58+
datetime.timezone.utc)
59+
else:
60+
updated_at = timeutils.utcnow(with_timezone=True)
6261
self.updated_at = updated_at
6362
if clear_down:
6463
self.set_down = False
@@ -112,8 +111,8 @@ def alive(self):
112111
return False
113112

114113
@classmethod
115-
def from_type(cls, _type, chassis_private, driver, updated_at=None):
116-
return cls.types[_type](chassis_private, driver, updated_at)
114+
def from_type(cls, _type, chassis_private, driver):
115+
return cls.types[_type](chassis_private, driver)
117116

118117
@property
119118
@abc.abstractmethod
@@ -141,7 +140,7 @@ class ControllerAgent(NeutronAgent):
141140
binary = 'ovn-controller'
142141

143142
@staticmethod # it is by default, but this makes pep8 happy
144-
def __new__(cls, chassis_private, driver, updated_at=None):
143+
def __new__(cls, chassis_private, driver):
145144
external_ids = cls.chassis_from_private(chassis_private).external_ids
146145
if ('enable-chassis-as-gw' in
147146
external_ids.get('ovn-cms-options', [])):
@@ -165,8 +164,8 @@ def description(self):
165164
return self.chassis_private.external_ids.get(
166165
ovn_const.OVN_AGENT_DESC_KEY, '')
167166

168-
def update(self, chassis_private, updated_at=None, clear_down=False):
169-
super().update(chassis_private, updated_at, clear_down)
167+
def update(self, chassis_private, clear_down=False):
168+
super().update(chassis_private, clear_down)
170169
external_ids = self.chassis_from_private(chassis_private).external_ids
171170
if 'enable-chassis-as-gw' in external_ids.get('ovn-cms-options', []):
172171
self.__class__ = ControllerGatewayAgent
@@ -175,8 +174,8 @@ def update(self, chassis_private, updated_at=None, clear_down=False):
175174
class ControllerGatewayAgent(ControllerAgent):
176175
agent_type = ovn_const.OVN_CONTROLLER_GW_AGENT
177176

178-
def update(self, chassis_private, updated_at=None, clear_down=False):
179-
super().update(chassis_private, updated_at, clear_down)
177+
def update(self, chassis_private, clear_down=False):
178+
super().update(chassis_private, clear_down)
180179
external_ids = self.chassis_from_private(chassis_private).external_ids
181180
if ('enable-chassis-as-gw' not in
182181
external_ids.get('ovn-cms-options', [])):
@@ -238,14 +237,13 @@ def __iter__(self):
238237
def __getitem__(self, key):
239238
return self.agents[key]
240239

241-
def update(self, agent_type, row, updated_at=None, clear_down=False):
240+
def update(self, agent_type, row, clear_down=False):
242241
cls = NeutronAgent.types[agent_type]
243242
try:
244243
agent = self.agents[cls.id_from_chassis_private(row)]
245-
agent.update(row, updated_at=updated_at, clear_down=clear_down)
244+
agent.update(row, clear_down=clear_down)
246245
except KeyError:
247-
agent = NeutronAgent.from_type(agent_type, row, self.driver,
248-
updated_at=updated_at)
246+
agent = NeutronAgent.from_type(agent_type, row, self.driver)
249247
self.agents[agent.agent_id] = agent
250248
return agent
251249

neutron/tests/unit/plugins/ml2/drivers/ovn/mech_driver/test_mech_driver.py

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -100,37 +100,25 @@ def _add_chassis(self, nb_cfg, name=None):
100100
chassis_private.name = name if name else str(uuid.uuid4())
101101
return chassis_private
102102

103-
def _add_chassis_agent(self, nb_cfg, agent_type, chassis_private=None,
104-
updated_at=None):
103+
def _add_chassis_agent(self, nb_cfg, agent_type, chassis_private=None):
105104
chassis_private = chassis_private or self._add_chassis(nb_cfg)
106105
if hasattr(chassis_private, 'nb_cfg_timestamp') and isinstance(
107106
chassis_private.nb_cfg_timestamp, mock.Mock):
108107
del chassis_private.nb_cfg_timestamp
109108
chassis_private.external_ids = {}
110-
if updated_at:
111-
chassis_private.external_ids = {
112-
ovn_const.OVN_LIVENESS_CHECK_EXT_ID_KEY:
113-
datetime.datetime.isoformat(updated_at)}
114109
if agent_type == ovn_const.OVN_METADATA_AGENT:
115110
chassis_private.external_ids.update({
116111
ovn_const.OVN_AGENT_METADATA_SB_CFG_KEY: nb_cfg,
117112
ovn_const.OVN_AGENT_METADATA_ID_KEY: str(uuid.uuid4())})
118113
chassis_private.chassis = [chassis_private]
119-
return neutron_agent.AgentCache().update(agent_type, chassis_private,
120-
updated_at)
114+
return neutron_agent.AgentCache().update(agent_type, chassis_private)
121115

122-
def _add_agent(self, name, alive=True):
116+
def _add_agent(self, name, nb_cfg_offset=0):
123117
nb_cfg = 5
124-
now = timeutils.utcnow(with_timezone=True)
125-
if not alive:
126-
updated_at = now - datetime.timedelta(cfg.CONF.agent_down_time + 1)
127-
self.mech_driver.nb_ovn.nb_global.nb_cfg = nb_cfg
128-
else:
129-
updated_at = now
130-
self.mech_driver.nb_ovn.nb_global.nb_cfg = nb_cfg + 2
118+
self.mech_driver.nb_ovn.nb_global.nb_cfg = nb_cfg + nb_cfg_offset
131119
chassis = self._add_chassis(nb_cfg, name=name)
132120
return self._add_chassis_agent(
133-
nb_cfg, ovn_const.OVN_CONTROLLER_AGENT, chassis, updated_at)
121+
nb_cfg, ovn_const.OVN_CONTROLLER_AGENT, chassis)
134122

135123

136124
class TestOVNMechanismDriverBase(MechDriverSetupBase,
@@ -1252,9 +1240,13 @@ def test_bind_port_physnet_not_found(self):
12521240
self._test_bind_port_failed(fake_segments)
12531241

12541242
def test_bind_port_host_not_alive(self):
1255-
agent = self._add_agent('agent_no_alive', False)
1256-
neutron_agent.AgentCache().get_agents.return_value = [agent]
1257-
self._test_bind_port_failed([])
1243+
agent = self._add_agent('agent_no_alive', 2)
1244+
now = timeutils.utcnow(with_timezone=True)
1245+
fake_now = now + datetime.timedelta(cfg.CONF.agent_down_time + 1)
1246+
with mock.patch.object(timeutils, 'utcnow') as get_now:
1247+
get_now.return_value = fake_now
1248+
neutron_agent.AgentCache().get_agents.return_value = [agent]
1249+
self._test_bind_port_failed([])
12581250

12591251
def _test_bind_port(self, fake_segments):
12601252
fake_port = fakes.FakePort.create_one_port().info()
@@ -2239,12 +2231,14 @@ def test_agent_alive_true_one_diff(self):
22392231
for agent_type in (ovn_const.OVN_CONTROLLER_AGENT,
22402232
ovn_const.OVN_METADATA_AGENT):
22412233
self.mech_driver.nb_ovn.nb_global.nb_cfg = nb_cfg + 1
2242-
now = timeutils.utcnow()
2243-
updated_at = now - datetime.timedelta(cfg.CONF.agent_down_time + 1)
22442234
agent = self._add_chassis_agent(nb_cfg, agent_type,
2245-
chassis_private, updated_at)
2246-
self.assertTrue(agent.alive, "Agent of type %s alive=%s" %
2247-
(agent.agent_type, agent.alive))
2235+
chassis_private)
2236+
now = timeutils.utcnow()
2237+
fake_now = now + datetime.timedelta(cfg.CONF.agent_down_time + 1)
2238+
with mock.patch.object(timeutils, 'utcnow') as get_now:
2239+
get_now.return_value = fake_now
2240+
self.assertTrue(agent.alive, "Agent of type %s alive=%s" %
2241+
(agent.agent_type, agent.alive))
22482242

22492243
def test_agent_alive_not_timed_out(self):
22502244
nb_cfg = 3
@@ -2264,11 +2258,13 @@ def test_agent_alive_timed_out(self):
22642258
ovn_const.OVN_METADATA_AGENT):
22652259
self.mech_driver.nb_ovn.nb_global.nb_cfg = nb_cfg + 2
22662260
now = timeutils.utcnow(with_timezone=True)
2267-
updated_at = now - datetime.timedelta(cfg.CONF.agent_down_time + 1)
22682261
agent = self._add_chassis_agent(nb_cfg, agent_type,
2269-
chassis_private, updated_at)
2270-
self.assertFalse(agent.alive, "Agent of type %s alive=%s" %
2271-
(agent.agent_type, agent.alive))
2262+
chassis_private)
2263+
fake_now = now + datetime.timedelta(cfg.CONF.agent_down_time + 1)
2264+
with mock.patch.object(timeutils, 'utcnow') as get_now:
2265+
get_now.return_value = fake_now
2266+
self.assertFalse(agent.alive, "Agent of type %s alive=%s" %
2267+
(agent.agent_type, agent.alive))
22722268

22732269
def test_agent_with_nb_cfg_timestamp_timeout(self):
22742270
nb_cfg = 3

0 commit comments

Comments
 (0)