Skip to content

Commit 7721c69

Browse files
committed
[OVN] Rate limit the "Disallow caching" log from hash ring
This patch adds a condition prior to logging the "Disallow caching" log message from the hash ring. Prior to this patch, this message was logged when the number of nodes connected to the hash ring was different from the number of API workers at Neutron's startup. This is because the hash ring waits until all API workers are connected to build the hash ring cache. With this patch, we will only log the message once (per worker) until the number of connected nodes changes. When nodes connect and the cache is built the _wait_startup_before_caching() is no longer used until the service is restarted again. Change-Id: I4f62b723083215483a2277cfcb798506671e1a2d Closes-Bug: 1989480 Signed-off-by: Lucas Alvares Gomes <[email protected]> (cherry picked from commit 9655466)
1 parent 897b474 commit 7721c69

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

neutron/common/ovn/hash_ring_manager.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def __init__(self, group_name):
3535
self._last_time_loaded = None
3636
self._check_hashring_startup = True
3737
self._group = group_name
38+
# Flag to rate limit the caching log
39+
self._prev_num_nodes = -1
3840
self.admin_ctx = context.get_admin_context()
3941

4042
@property
@@ -56,11 +58,18 @@ def _wait_startup_before_caching(self):
5658
self.admin_ctx,
5759
constants.HASH_RING_CACHE_TIMEOUT, self._group, from_host=True)
5860

59-
if len(nodes) >= api_workers:
60-
LOG.debug("Allow caching, nodes %s>=%s", len(nodes), api_workers)
61+
num_nodes = len(nodes)
62+
if num_nodes >= api_workers:
63+
LOG.debug("Allow caching, nodes %s>=%s", num_nodes, api_workers)
6164
self._check_hashring_startup = False
6265
return False
63-
LOG.debug("Disallow caching, nodes %s<%s", len(nodes), api_workers)
66+
67+
# NOTE(lucasagomes): We only log when the number of connected
68+
# nodes are different to prevent this message from being spammed
69+
if self._prev_num_nodes != num_nodes:
70+
LOG.debug("Disallow caching, nodes %s<%s", num_nodes, api_workers)
71+
self._prev_num_nodes = num_nodes
72+
6473
return True
6574

6675
def _load_hash_ring(self, refresh=False):

neutron/tests/unit/common/ovn/test_hash_ring_manager.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,23 @@ def test_ring_rebalance(self):
110110
# The ring should re-balance and as it was before
111111
self._verify_hashes(hash_dict_before)
112112

113+
@mock.patch.object(hash_ring_manager.LOG, 'debug')
113114
@mock.patch.object(service, '_get_api_workers', return_value=2)
114-
def test__wait_startup_before_caching(self, api_workers):
115+
def test__wait_startup_before_caching(self, api_workers, mock_log):
115116
db_hash_ring.add_node(self.admin_ctx, HASH_RING_TEST_GROUP, 'node-1')
116117

117118
# Assert it will return True until until we equal api_workers
118119
self.assertTrue(self.hash_ring_manager._wait_startup_before_caching)
119120
self.assertTrue(self.hash_ring_manager._check_hashring_startup)
120121

122+
# Call it again with the same number of workers to test the
123+
# log rating
124+
self.assertTrue(self.hash_ring_manager._wait_startup_before_caching)
125+
self.assertTrue(self.hash_ring_manager._check_hashring_startup)
126+
127+
# Assert the cache message has been logged only once
128+
self.assertEqual(1, mock_log.call_count)
129+
121130
db_hash_ring.add_node(self.admin_ctx, HASH_RING_TEST_GROUP, 'node-2')
122131

123132
# Assert it's now False. Waiting is not needed anymore

0 commit comments

Comments
 (0)