[OVN] Optimize `HAChassisGroupRouterEvent` code

This patch is addressing some comments made in the previous patch [1]
and optimizing the code of ``HAChassisGroupRouterEvent`` class.

Related-Bug: #2052821

[1]https://review.opendev.org/c/openstack/neutron/+/909437

Change-Id: Ibc4afce16e95ea7d48737c254cd11431e9994704
This commit is contained in:
Rodolfo Alonso Hernandez
2024-04-08 22:52:42 +00:00
parent 019294c71d
commit c2f34e136d
2 changed files with 16 additions and 21 deletions

View File

@@ -677,14 +677,12 @@ class HAChassisGroupRouterEvent(row_event.RowEvent):
self.event_name = 'HAChassisGroupRouterEvent'
def match_fn(self, event, row, old):
if ovn_const.OVN_ROUTER_ID_EXT_ID_KEY not in row.external_ids:
# "HA_Chassis_Group" not assigned to a router.
return False
elif getattr(old, 'ha_chassis', None) is None:
# No changes in the "ha_chassis" list has been done.
return False
return True
if (ovn_const.OVN_ROUTER_ID_EXT_ID_KEY in row.external_ids or
hasattr(old, 'ha_chassis')):
# "HA_Chassis_Group" has been assigned to a router or there are
# changes in the "ha_chassis" list.
return True
return False
def run(self, event, row, old):
router_id = row.external_ids[ovn_const.OVN_ROUTER_ID_EXT_ID_KEY]
@@ -698,15 +696,16 @@ class HAChassisGroupRouterEvent(row_event.RowEvent):
router_id)
return
highest_prio_hc = None
for hc in row.ha_chassis:
if not highest_prio_hc or hc.priority > highest_prio_hc.priority:
highest_prio_hc = hc
try:
highest_prio_hc = max(row.ha_chassis, key=lambda hc: hc.priority)
except ValueError:
highest_prio_hc = None
options = {'chassis': highest_prio_hc.chassis_name}
self.driver.nb_ovn.db_set(
'Logical_Router', router_name, ('options', options)).execute(
check_error=True)
if highest_prio_hc:
options = {'chassis': highest_prio_hc.chassis_name}
self.driver.nb_ovn.db_set(
'Logical_Router', router_name, ('options', options)).execute(
check_error=True)
class OvnDbNotifyHandler(row_event.RowEventHandler):

View File

@@ -550,11 +550,7 @@ class TestNBDbMonitor(base.TestOVNFunctionalBase):
hp_chassis_lr = lr.options['chassis']
hcg = self.nb_api.lookup('HA_Chassis_Group', ovn_r_name)
self.assertEqual(num_chassis, len(hcg.ha_chassis))
hp_chassis_hcg = None
for hc in hcg.ha_chassis:
if not hp_chassis_hcg or hc.priority > hp_chassis_hcg.priority:
hp_chassis_hcg = hc
hp_chassis_hcg = max(hcg.ha_chassis, key=lambda hc: hc.priority)
self.assertEqual(hp_chassis_lr, hp_chassis_hcg.chassis_name)
chassis_list = []