-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add configurable max time between successful resyncs #11577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LeoBJenkins
wants to merge
6
commits into
projectcalico:master
Choose a base branch
from
bloomberg:resync-monitor-thread
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5c7b3dc
Add max time between successful resyncs config
164e60f
Remove compaction patch from monitor thread tests
8cea22a
Style fixes
eab2caf
Rename test helper function
666b235
Fix unit test CFG object
0936288
Rewrite sleep timing logic to be more efficient
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
networking-calico/networking_calico/plugins/ml2/drivers/calico/test/test_monitor_thread.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright (c) 2026 Tigera, Inc. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """ | ||
| networking_calico.plugins.ml2.drivers.calico.test.test_monitor_thread | ||
|
|
||
| Unit tests for the thread that monitors the periodic resync thread. | ||
| """ | ||
| from datetime import datetime, timedelta | ||
| import mock | ||
| import unittest | ||
|
|
||
| import networking_calico.plugins.ml2.drivers.calico.test.lib as lib | ||
| from networking_calico.plugins.ml2.drivers.calico import mech_calico | ||
|
|
||
|
|
||
| INITIAL_EPOCH = 0 | ||
| TEST_MAX_INTERVAL = 30 | ||
|
|
||
|
|
||
| class TestResyncMonitorThread(lib.Lib, unittest.TestCase): | ||
| """Tests for the driver's resync monitor thread logic.""" | ||
|
|
||
| def setUp(self): | ||
| super(TestResyncMonitorThread, self).setUp() | ||
|
|
||
| # thread logic mocks | ||
| self.driver.elector = mock.Mock() | ||
| self.sleep_patcher = mock.patch("eventlet.sleep") | ||
| self.mock_sleep = self.sleep_patcher.start() | ||
|
|
||
| # log mocks | ||
| self.log_error = mock.patch.object(mech_calico.LOG, "error").start() | ||
| self.log_info = mock.patch.object(mech_calico.LOG, "info").start() | ||
| self.log_debug = mock.patch.object(mech_calico.LOG, "debug").start() | ||
|
|
||
| # resync mocks | ||
| self.driver.subnet_syncer = mock.Mock() | ||
| self.driver.policy_syncer = mock.Mock() | ||
| self.driver.endpoint_syncer = mock.Mock() | ||
| self.driver.provide_felix_config = mock.Mock() | ||
|
|
||
| def tearDown(self): | ||
| self.sleep_patcher.stop() | ||
| super(TestResyncMonitorThread, self).tearDown() | ||
|
|
||
| def simulate_epoch_progression(self, expected_sleep_time=None): | ||
| def increment_epoch(actual_sleep_time): | ||
| if expected_sleep_time is not None: | ||
| assert expected_sleep_time == actual_sleep_time | ||
| self.driver._epoch += 1 | ||
|
|
||
| return increment_epoch | ||
|
|
||
| def test_monitor_does_nothing_when_not_master(self): | ||
| """Test that a driver that is not master does not monitor.""" | ||
| self.driver.elector.master.return_value = False | ||
| self.mock_sleep.side_effect = self.simulate_epoch_progression() | ||
|
|
||
| self.driver.resync_monitor_thread(INITIAL_EPOCH) | ||
|
|
||
| self.log_debug.assert_called_once_with("I am not master") | ||
| self.log_error.assert_not_called() | ||
|
|
||
| def test_monitor_logs_error_when_over_max(self): | ||
| """Test that an error is logged when interval surpasses maximum.""" | ||
| lib.m_oslo_config.cfg.CONF.calico.resync_max_interval_secs = TEST_MAX_INTERVAL | ||
| self.driver.elector.master.return_value = True | ||
| fake_resync_time = datetime.now() - timedelta(seconds=TEST_MAX_INTERVAL + 1) | ||
| self.driver.last_resync_time = fake_resync_time | ||
| self.mock_sleep.side_effect = self.simulate_epoch_progression() | ||
|
|
||
| self.driver.resync_monitor_thread(INITIAL_EPOCH) | ||
|
|
||
| self.log_error.assert_called_once() | ||
| self.assertIn( | ||
| "The time since the last resync completion has surpassed", | ||
| self.log_error.call_args[0][0], | ||
| ) | ||
|
|
||
| def test_monitor_no_error_if_interval_under_max(self): | ||
| """If interval is below max, no error should be logged.""" | ||
| lib.m_oslo_config.cfg.CONF.calico.resync_max_interval_secs = TEST_MAX_INTERVAL | ||
| self.driver.elector.master.return_value = True | ||
| self.mock_sleep.side_effect = self.simulate_epoch_progression() | ||
|
|
||
| self.driver.resync_monitor_thread(INITIAL_EPOCH) | ||
|
|
||
| self.log_error.assert_not_called() | ||
|
|
||
| def test_monitor_exception_stops_elector(self): | ||
| """On unexpected exception, elector.stop() must be called.""" | ||
| self.driver.elector.master.return_value = True | ||
|
|
||
| with mock.patch.object(self.driver, "elector") as mock_elector: | ||
| mock_elector.master.side_effect = Exception("Test exception") | ||
|
|
||
| with self.assertRaises(Exception): | ||
| self.driver.resync_monitor_thread(INITIAL_EPOCH) | ||
|
|
||
| mock_elector.stop.assert_called_once() | ||
|
|
||
| def test_resync_resets_time(self): | ||
| """Test that resync resets current interval duration to below max.""" | ||
| lib.m_oslo_config.cfg.CONF.calico.resync_max_interval_secs = TEST_MAX_INTERVAL | ||
| self.driver.elector.master.return_value = True | ||
| fake_resync_time_time = datetime.now() - timedelta( | ||
| seconds=TEST_MAX_INTERVAL + 1 | ||
| ) | ||
| self.driver.last_resync_time = fake_resync_time_time | ||
|
|
||
| self.mock_sleep.side_effect = self.simulate_epoch_progression() | ||
| self.driver.resync_monitor_thread(INITIAL_EPOCH) | ||
|
|
||
| self.log_error.assert_called_once() | ||
| self.assertIn( | ||
| "The time since the last resync completion has surpassed", | ||
| self.log_error.call_args[0][0], | ||
| ) | ||
|
|
||
| # Resync | ||
| self.mock_sleep.side_effect = self.simulate_epoch_progression() | ||
| self.driver.periodic_resync_thread(INITIAL_EPOCH + 1) | ||
|
|
||
| self.mock_sleep.side_effect = self.simulate_epoch_progression() | ||
| self.driver.resync_monitor_thread(INITIAL_EPOCH + 2) | ||
|
|
||
| self.log_error.assert_called_once() | ||
|
|
||
| def test_errors_continue_to_log(self): | ||
| """Test that errors continue logging if resync does not occur.""" | ||
| lib.m_oslo_config.cfg.CONF.calico.resync_max_interval_secs = TEST_MAX_INTERVAL | ||
| self.driver.elector.master.return_value = True | ||
| fake_resync_time_time = datetime.now() - timedelta( | ||
| seconds=TEST_MAX_INTERVAL + 1 | ||
| ) | ||
| self.driver.last_resync_time = fake_resync_time_time | ||
|
|
||
| self.mock_sleep.side_effect = self.simulate_epoch_progression() | ||
| self.driver.resync_monitor_thread(INITIAL_EPOCH) | ||
|
|
||
| self.log_error.assert_called_once() | ||
| self.assertIn( | ||
| "The time since the last resync completion has surpassed", | ||
| self.log_error.call_args[0][0], | ||
| ) | ||
|
|
||
| self.mock_sleep.side_effect = self.simulate_epoch_progression() | ||
| self.driver.resync_monitor_thread(INITIAL_EPOCH + 1) | ||
|
|
||
| self.assertEqual(self.log_error.call_count, 2) | ||
| self.assertIn( | ||
| "The time since the last resync completion has surpassed", | ||
| self.log_error.call_args[0][0], | ||
| ) | ||
|
|
||
| @mock.patch("networking_calico.plugins.ml2.drivers.calico.mech_calico.datetime") | ||
| def test_sleep_time_logic_before_deadline(self, mock_datetime): | ||
| """Test that we sleep until deadline if there is time left.""" | ||
| lib.m_oslo_config.cfg.CONF.calico.resync_max_interval_secs = TEST_MAX_INTERVAL | ||
| self.driver.elector.master.return_value = True | ||
|
|
||
| curr_time = datetime.now() | ||
| self.driver.last_resync_time = curr_time | ||
| expected_sleep_time = TEST_MAX_INTERVAL | ||
| mock_datetime.now.return_value = curr_time | ||
|
|
||
| self.mock_sleep.side_effect = self.simulate_epoch_progression( | ||
| expected_sleep_time | ||
| ) | ||
| self.driver.resync_monitor_thread(INITIAL_EPOCH) | ||
|
|
||
| def test_sleep_time_logic_after_deadline(self): | ||
| """Test that we poll if the deadline has passed.""" | ||
| lib.m_oslo_config.cfg.CONF.calico.resync_max_interval_secs = TEST_MAX_INTERVAL | ||
| self.driver.elector.master.return_value = True | ||
|
|
||
| fake_resync_time = datetime.now() - timedelta(seconds=TEST_MAX_INTERVAL + 1) | ||
| self.driver.last_resync_time = fake_resync_time | ||
| expected_sleep_time = TEST_MAX_INTERVAL / 5 | ||
|
|
||
| self.mock_sleep.side_effect = self.simulate_epoch_progression( | ||
| expected_sleep_time | ||
| ) | ||
| self.driver.resync_monitor_thread(INITIAL_EPOCH) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one feels like it would belong better in test_election.py - WDYT? (It doesn't have any resync-related detail.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it belongs where it is because it calls and tests the monitor thread. Plus, it does not test any of the actual election logic (the actual elector is entirely mocked out), it just checks that the function is called.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, you're right. I must have misread before.