Skip to content

Commit adfe065

Browse files
committed
[OVN] Cleanup old Hash Ring node entries
This patch introduces a maintenance task that runs once a day and is responsible for cleaning up Hash Ring nodes that haven't been updated in 5 days or more. Change-Id: Ibed9e0d77500570c3d0f9f39bfe40cb9239d0d7a Closes-Bug: #2033281 Signed-off-by: Lucas Alvares Gomes <[email protected]> (cherry picked from commit 7f777c2)
1 parent 8ddf72d commit adfe065

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

neutron/db/ovn_hash_ring_db.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ def remove_node_by_uuid(context, node_uuid):
6060
LOG.info('Node "%s" removed from the Hash Ring', node_uuid)
6161

6262

63+
@db_api.retry_if_session_inactive()
64+
def cleanup_old_nodes(context, days):
65+
age = timeutils.utcnow() - datetime.timedelta(days=days)
66+
with db_api.CONTEXT_WRITER.using(context):
67+
context.session.query(ovn_models.OVNHashRing).filter(
68+
ovn_models.OVNHashRing.updated_at < age).delete()
69+
LOG.info('Cleaned up Hash Ring nodes older than %d days', days)
70+
71+
6372
@db_api.retry_if_session_inactive()
6473
def _touch(context, updated_at=None, **filter_args):
6574
if updated_at is None:

neutron/plugins/ml2/drivers/ovn/mech_driver/ovsdb/maintenance.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,20 @@ def remove_duplicated_chassis_registers(self):
11131113
for table in ('Chassis_Private', 'Chassis'):
11141114
txn.add(self._sb_idl.db_destroy(table, ch.name))
11151115

1116+
@periodics.periodic(spacing=86400, run_immediately=True)
1117+
def cleanup_old_hash_ring_nodes(self):
1118+
"""Daily task to cleanup old stable Hash Ring node entries.
1119+
1120+
Runs once a day and clean up Hash Ring entries that haven't
1121+
been updated in more than 5 days. See LP #2033281 for more
1122+
information.
1123+
1124+
"""
1125+
if not self.has_lock:
1126+
return
1127+
context = n_context.get_admin_context()
1128+
hash_ring_db.cleanup_old_nodes(context, days=5)
1129+
11161130

11171131
class HashRingHealthCheckPeriodics(object):
11181132

neutron/tests/unit/db/test_ovn_hash_ring_db.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,29 @@ def test_remove_node_by_uuid(self):
285285
self.admin_ctx, interval=60, group_name=HASH_RING_TEST_GROUP)
286286
self.assertEqual(2, len(active_nodes))
287287
self.assertNotIn(node_to_remove, [n.node_uuid for n in active_nodes])
288+
289+
def test_cleanup_old_nodes(self):
290+
# Add 2 new nodes
291+
self._add_nodes_and_assert_exists(count=2)
292+
293+
# Subtract 5 days from utcnow() and touch the nodes to make
294+
# them to appear stale
295+
fake_utcnow = timeutils.utcnow() - datetime.timedelta(days=5)
296+
with mock.patch.object(timeutils, 'utcnow') as mock_utcnow:
297+
mock_utcnow.return_value = fake_utcnow
298+
ovn_hash_ring_db.touch_nodes_from_host(self.admin_ctx,
299+
HASH_RING_TEST_GROUP)
300+
301+
# Add 3 new nodes
302+
self._add_nodes_and_assert_exists(count=3)
303+
304+
# Assert we have 5 nodes in the hash ring
305+
self.assertEqual(5, ovn_hash_ring_db.count_nodes_from_host(
306+
self.admin_ctx, HASH_RING_TEST_GROUP))
307+
308+
# Clean up the 2 stale nodes
309+
ovn_hash_ring_db.cleanup_old_nodes(self.admin_ctx, days=5)
310+
311+
# Assert we only have 3 node entries after the clean up
312+
self.assertEqual(3, ovn_hash_ring_db.count_nodes_from_host(
313+
self.admin_ctx, HASH_RING_TEST_GROUP))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
other:
3+
- |
4+
Adds a maintenance task that runs once a day and is responsible for
5+
cleaning up Hash Ring nodes that haven't been updated in 5 days or
6+
more. See LP #2033281 for more information.

0 commit comments

Comments
 (0)