-
Notifications
You must be signed in to change notification settings - Fork 10
Worker rotation via the longest-idle criteria #181
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
AlanCoding
wants to merge
4
commits into
ansible:main
Choose a base branch
from
AlanCoding:longest_idle
base: main
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
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
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,104 @@ | ||
| import asyncio | ||
| from typing import AsyncIterator | ||
|
|
||
| import pytest | ||
| import pytest_asyncio | ||
|
|
||
| from dispatcherd.protocols import DispatcherMain | ||
| from dispatcherd.testing.asyncio import adispatcher_service | ||
|
|
||
|
|
||
| @pytest.fixture(scope='session') | ||
| def order_config(): | ||
| return { | ||
| "version": 2, | ||
| "service": { | ||
| "pool_kwargs": {"min_workers": 2, "max_workers": 2}, | ||
| "main_kwargs": {"node_id": "order-test"}, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| @pytest_asyncio.fixture | ||
| async def aorder_dispatcher(order_config) -> AsyncIterator[DispatcherMain]: | ||
| async with adispatcher_service(order_config) as dispatcher: | ||
| yield dispatcher | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_workers_reorder_and_dispatch_longest_idle(aorder_dispatcher): | ||
| pool = aorder_dispatcher.pool | ||
| assert list(pool.workers.workers.keys()) == [0, 1] | ||
|
|
||
| pool.events.work_cleared.clear() | ||
| await aorder_dispatcher.process_message({ | ||
| "task": "tests.data.methods.sleep_function", | ||
| "kwargs": {"seconds": 0.1}, | ||
| "uuid": "t1", | ||
| }) | ||
| await aorder_dispatcher.process_message({ | ||
| "task": "tests.data.methods.sleep_function", | ||
| "kwargs": {"seconds": 0.05}, | ||
| "uuid": "t2", | ||
| }) | ||
| await asyncio.wait_for(pool.events.work_cleared.wait(), timeout=1) | ||
|
|
||
| assert list(pool.workers.workers.keys()) == [1, 0] | ||
|
|
||
| pool.events.work_cleared.clear() | ||
| await aorder_dispatcher.process_message({ | ||
| "task": "tests.data.methods.sleep_function", | ||
| "kwargs": {"seconds": 0.01}, | ||
| "uuid": "t3", | ||
| }) | ||
| await asyncio.sleep(0.01) | ||
| assert pool.workers.get_by_id(1).current_task["uuid"] == "t3" | ||
| assert pool.workers.get_by_id(0).current_task is None | ||
| await asyncio.wait_for(pool.events.work_cleared.wait(), timeout=1) | ||
|
|
||
| pool.events.work_cleared.clear() | ||
| await aorder_dispatcher.process_message({ | ||
| "task": "tests.data.methods.sleep_function", | ||
| "kwargs": {"seconds": 0.01}, | ||
| "uuid": "t4", | ||
| }) | ||
| await asyncio.sleep(0.01) | ||
| assert pool.workers.get_by_id(0).current_task["uuid"] == "t4" | ||
| await asyncio.wait_for(pool.events.work_cleared.wait(), timeout=1) | ||
|
|
||
| assert list(pool.workers.workers.keys()) == [1, 0] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_process_finished_with_removed_worker(): | ||
| """Test that process_finished handles KeyError gracefully when worker has been removed | ||
|
|
||
| This simulates the race condition where a worker finishes a task, but has been | ||
| removed from self.workers before process_finished is called. | ||
| """ | ||
| from unittest.mock import MagicMock, patch | ||
| from dispatcherd.service.pool import WorkerData, PoolWorker | ||
|
|
||
| # Create a minimal WorkerData instance | ||
| worker_data = WorkerData() | ||
|
|
||
| # Create a mock worker | ||
| mock_process = MagicMock() | ||
| worker = PoolWorker(worker_id=0, process=mock_process) | ||
| worker.current_task = {"uuid": "test-uuid", "task": "test.task"} | ||
| worker.finished_count = 0 | ||
|
|
||
| # Add worker then remove it (simulating the race condition) | ||
| worker_data.add_worker(worker) | ||
| worker_data.remove_by_id(0) | ||
| assert 0 not in worker_data | ||
|
|
||
| # Mock the logger to verify the warning is logged | ||
| with patch('dispatcherd.service.pool.logger') as mock_logger: | ||
| # Call move_to_end on the removed worker - should not raise KeyError | ||
| worker_data.move_to_end(0) | ||
|
|
||
| # Verify the warning was logged | ||
| mock_logger.warning.assert_called_once() | ||
| warning_call = mock_logger.warning.call_args[0][0] | ||
| assert "Attempted to move worker_id=0 to end, but worker was already removed" in warning_call |
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.
Bug: Race Condition in Worker Management
A
KeyErrorcan occur at line 566 whenmark_worker_done()callsself.workers.move_to_end(). This is a race condition wheremanage_old_workers()removes a worker fromself.workersafter it's been retrieved for processing, but beforemove_to_end()is called, even though the call is within themanagement_lock.