-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add backpressure queue for Celery event ingestion #1491
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
Closed
Closed
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
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
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,156 @@ | ||
| import queue | ||
| import time | ||
| import unittest | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from celery.events import Event | ||
| from tornado.ioloop import IOLoop | ||
|
|
||
| from flower.events import Events, EventsState, get_prometheus_metrics | ||
|
|
||
| import celery | ||
|
|
||
|
|
||
| class TestEventsState(unittest.TestCase): | ||
| def test_counter_tracks_events_by_worker(self): | ||
| state = EventsState() | ||
| state.get_or_create_worker('w1') | ||
| e = Event('worker-online', hostname='w1') | ||
| e['clock'] = 0 | ||
| e['local_received'] = time.time() | ||
| state.event(e) | ||
|
|
||
| self.assertIn('w1', state.counter) | ||
| self.assertEqual(state.counter['w1']['worker-online'], 1) | ||
|
|
||
| def test_counter_increments(self): | ||
| state = EventsState() | ||
| state.get_or_create_worker('w1') | ||
| for i in range(5): | ||
| e = Event('worker-heartbeat', hostname='w1', active=0) | ||
| e['clock'] = i | ||
| e['local_received'] = time.time() | ||
| state.event(e) | ||
|
|
||
| self.assertEqual(state.counter['w1']['worker-heartbeat'], 5) | ||
|
|
||
|
|
||
| class TestEventsBackpressure(unittest.TestCase): | ||
| def test_on_event_drops_when_queue_full(self): | ||
| capp = celery.Celery() | ||
| io_loop = MagicMock() | ||
| events = Events(capp, io_loop) | ||
| # Fill the queue | ||
| for i in range(events._BACKPRESSURE_MAXSIZE): | ||
| events.on_event({'hostname': 'w1', 'type': 'worker-heartbeat'}) | ||
|
|
||
| # Next event should be dropped without raising | ||
| events.on_event({'hostname': 'w1', 'type': 'worker-heartbeat'}) | ||
| self.assertEqual(events._event_queue.qsize(), events._BACKPRESSURE_MAXSIZE) | ||
|
|
||
| def test_drop_logging_is_rate_limited(self): | ||
| capp = celery.Celery() | ||
| io_loop = MagicMock() | ||
| events = Events(capp, io_loop) | ||
| # Fill the queue | ||
| for i in range(events._BACKPRESSURE_MAXSIZE): | ||
| events.on_event({'hostname': 'w1', 'type': 'worker-heartbeat'}) | ||
|
|
||
| # Reset drop state so we control it entirely within the patch. | ||
| # Set _last_drop_log_time far enough in the past to guarantee the | ||
| # 5-second cooldown has elapsed (time.monotonic() can be small on | ||
| # short-lived processes). | ||
| events._drop_count = 0 | ||
| events._last_drop_log_time = time.monotonic() - 10.0 | ||
|
|
||
| with patch('flower.events.logger') as mock_logger: | ||
| # First drop should trigger a log (cooldown elapsed) | ||
| events.on_event({'hostname': 'w1', 'type': 'worker-heartbeat'}) | ||
| self.assertEqual(mock_logger.warning.call_count, 1) | ||
|
|
||
| # Subsequent drops within 5s should NOT trigger more logs | ||
| for _ in range(99): | ||
| events.on_event({'hostname': 'w1', 'type': 'worker-heartbeat'}) | ||
| self.assertEqual(mock_logger.warning.call_count, 1) | ||
|
|
||
| def test_drain_events_processes_batch(self): | ||
| capp = celery.Celery() | ||
| io_loop = MagicMock() | ||
| events = Events(capp, io_loop) | ||
| events.state = MagicMock() | ||
|
|
||
| for i in range(10): | ||
| events._event_queue.put({'hostname': 'w1', 'type': 'worker-heartbeat', | ||
| 'clock': i, 'local_received': time.time()}) | ||
|
|
||
| events._drain_events() | ||
|
|
||
| self.assertEqual(events.state.event.call_count, 10) | ||
| self.assertTrue(events._event_queue.empty()) | ||
|
|
||
| def test_drain_events_handles_errors_gracefully(self): | ||
| capp = celery.Celery() | ||
| io_loop = MagicMock() | ||
| events = Events(capp, io_loop) | ||
| events.state = MagicMock() | ||
| events.state.event.side_effect = [RuntimeError("test"), None] | ||
|
|
||
| events._event_queue.put({'hostname': 'w1', 'type': 'a'}) | ||
| events._event_queue.put({'hostname': 'w1', 'type': 'b'}) | ||
|
|
||
| events._drain_events() | ||
|
|
||
| # Both events should be consumed despite the error on the first one | ||
| self.assertEqual(events.state.event.call_count, 2) | ||
| self.assertTrue(events._event_queue.empty()) | ||
|
|
||
| def test_drain_respects_batch_size(self): | ||
| capp = celery.Celery() | ||
| io_loop = MagicMock() | ||
| events = Events(capp, io_loop) | ||
| events.state = MagicMock() | ||
|
|
||
| count = events._DRAIN_BATCH_SIZE + 100 | ||
| for i in range(count): | ||
| events._event_queue.put({'hostname': 'w1', 'type': 'hb'}) | ||
|
|
||
| events._drain_events() | ||
|
|
||
| # Should process exactly _DRAIN_BATCH_SIZE, leaving 100 | ||
| self.assertEqual(events.state.event.call_count, events._DRAIN_BATCH_SIZE) | ||
| self.assertEqual(events._event_queue.qsize(), 100) | ||
|
|
||
|
|
||
| class TestEventsRetryBackoff(unittest.TestCase): | ||
| def test_retry_interval_caps_at_max(self): | ||
| from flower.events import MAX_RETRY_INTERVAL | ||
| try_interval = 1 | ||
| for _ in range(100): | ||
| try_interval *= 2 | ||
| if try_interval > MAX_RETRY_INTERVAL: | ||
| try_interval = MAX_RETRY_INTERVAL | ||
|
|
||
| self.assertEqual(try_interval, MAX_RETRY_INTERVAL) | ||
| self.assertEqual(MAX_RETRY_INTERVAL, 60) | ||
|
|
||
|
|
||
| class TestEventsStopSafety(unittest.TestCase): | ||
| def test_stop_calls_save_state_even_if_timer_fails(self): | ||
| capp = celery.Celery() | ||
| io_loop = MagicMock() | ||
| events = Events(capp, io_loop, persistent=True, db='test_db') | ||
|
|
||
| events.timer = MagicMock() | ||
| events.timer.stop.side_effect = RuntimeError("timer error") | ||
| events.state_save_timer = MagicMock() | ||
| events.state_save_timer.stop.side_effect = RuntimeError("save timer error") | ||
| events._drain_timer = MagicMock() | ||
| events._drain_timer.stop.side_effect = RuntimeError("drain timer error") | ||
|
|
||
| with patch.object(events, 'save_state') as mock_save: | ||
| events.stop() | ||
| mock_save.assert_called_once() | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
Oops, something went wrong.
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 test uses a hard-coded
db='test_db'withpersistent=True, which will causeshelve.open()to touch the real filesystem (creating/locking files in the repo working directory) and can make unit tests flaky or fail in read-only/parallel environments. Use a temporary path (e.g.,tempfile.TemporaryDirectory()/NamedTemporaryFile) or patchshelve.openso the test doesn't create persistent artifacts.