-
Notifications
You must be signed in to change notification settings - Fork 59
Fix q global concurrency #211
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
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
1cedb94
add client name to transact sysdb connections
maxdml a4bfe3b
fix global concurrency
maxdml f48c117
remove debug entry
maxdml 82d2823
fix
maxdml d1417ef
black
maxdml c44aacf
ignore errors.LockNotAvailable
maxdml ecba3ce
cherry pick conflict
maxdml 9a604c5
update test
maxdml c4a2b44
nit
maxdml 3d83428
?
maxdml 5ce9fe3
unused import
maxdml f6e0b12
and
maxdml 5589e0d
align implementation with TS
maxdml 9ba7f51
mypy
maxdml 5044acb
no need to manually destroy because we have the _dbos_exit_hook atexit
maxdml b61f232
debug gha
maxdml 7fd23c3
sett spawn method manually
maxdml 6c7de64
fix (again) limits check
maxdml 86e824d
devise a more thorough test that goes over multiple qlen situations
maxdml 6f884ad
comments + sanity check
maxdml dacccef
coments
maxdml 82af02a
remove redundant check
maxdml f514588
1 more check
maxdml 27e2363
comment
maxdml 9f6c13d
adjust comments
maxdml 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 |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| import logging | ||
| import multiprocessing | ||
| import multiprocessing.synchronize | ||
| import os | ||
| import subprocess | ||
| import threading | ||
| import time | ||
| import uuid | ||
| from multiprocessing import Process | ||
|
|
||
| import pytest | ||
| import sqlalchemy as sa | ||
|
|
@@ -17,7 +18,6 @@ | |
| SetWorkflowID, | ||
| WorkflowHandle, | ||
| ) | ||
| from dbos._error import DBOSDeadLetterQueueError | ||
| from dbos._schemas.system_database import SystemSchema | ||
| from dbos._sys_db import WorkflowStatusString | ||
| from tests.conftest import default_config | ||
|
|
@@ -379,11 +379,6 @@ def test_queue_workflow_in_recovered_workflow(dbos: DBOS) -> None: | |
| return | ||
|
|
||
|
|
||
| ########################### | ||
| # TEST WORKER CONCURRENCY # | ||
| ########################### | ||
|
|
||
|
|
||
| def test_one_at_a_time_with_worker_concurrency(dbos: DBOS) -> None: | ||
| wf_counter = 0 | ||
| flag = False | ||
|
|
@@ -423,12 +418,25 @@ def workflow_two() -> None: | |
|
|
||
|
|
||
| # Declare a workflow globally (we need it to be registered across process under a known name) | ||
| start_event = threading.Event() | ||
| end_event = threading.Event() | ||
|
|
||
|
|
||
| @DBOS.workflow() | ||
| def worker_concurrency_test_workflow() -> None: | ||
| pass | ||
| start_event.set() | ||
| end_event.wait() | ||
|
|
||
|
|
||
| def run_dbos_test_in_process(i: int) -> None: | ||
| local_concurrency_limit: int = 5 | ||
| global_concurrency_limit: int = local_concurrency_limit * 2 | ||
|
|
||
|
|
||
| def run_dbos_test_in_process( | ||
| i: int, | ||
| start_signal: multiprocessing.synchronize.Event, | ||
| end_signal: multiprocessing.synchronize.Event, | ||
| ) -> None: | ||
| dbos_config: ConfigFile = { | ||
| "name": "test-app", | ||
| "language": "python", | ||
|
|
@@ -445,39 +453,144 @@ def run_dbos_test_in_process(i: int) -> None: | |
| }, | ||
| "telemetry": {}, | ||
| "env": {}, | ||
| "application": {}, | ||
| } | ||
| dbos = DBOS(config=dbos_config) | ||
| DBOS.launch() | ||
|
|
||
| Queue("test_queue", worker_concurrency=1) | ||
| time.sleep( | ||
| 2 | ||
| ) # Give some time for the parent worker to enqueue and for this worker to dequeue | ||
|
|
||
| Queue( | ||
| "test_queue", | ||
| worker_concurrency=local_concurrency_limit, | ||
| concurrency=global_concurrency_limit, | ||
| ) | ||
| # Wait to dequeue as many tasks as we can locally | ||
| for _ in range(0, local_concurrency_limit): | ||
| start_event.wait() | ||
| start_event.clear() | ||
| # Signal the parent process we've dequeued | ||
| start_signal.set() | ||
| # Wait for the parent process to signal we can move on | ||
| end_signal.wait() | ||
| # Complete the task. 1 set should unblock them all | ||
| end_event.set() | ||
|
|
||
| # Now whatever is in the queue should be cleared up fast (start/end events are already set) | ||
| queue_entries_are_cleaned_up(dbos) | ||
|
|
||
| DBOS.destroy() | ||
|
|
||
|
|
||
| # Test global concurrency and worker utilization by carefully filling the queue up to 1) the local limit 2) the global limit | ||
| # For the global limit, we fill the queue in 2 steps, ensuring that the 2nd worker is able to cap its local utilization even | ||
| # after having dequeued some tasks already | ||
| def test_worker_concurrency_with_n_dbos_instances(dbos: DBOS) -> None: | ||
| # Ensure children processes do not share global variables (including DBOS instance) with the parent | ||
| multiprocessing.set_start_method("spawn") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found out that GHA uses "fork", which ofc shares the parent's global variable with the child, including the DBOS global instance, which would result in a |
||
|
|
||
| queue = Queue( | ||
| "test_queue", limiter={"limit": 0, "period": 1} | ||
| ) # This process cannot dequeue tasks | ||
|
|
||
| # Start N proccesses to dequeue | ||
| # First, start local concurrency limit tasks | ||
| handles = [] | ||
| for _ in range(0, local_concurrency_limit): | ||
| handles.append(queue.enqueue(worker_concurrency_test_workflow)) | ||
|
|
||
| # Start 2 workers | ||
| processes = [] | ||
| for i in range(0, 10): | ||
| start_signals = [] | ||
| end_signals = [] | ||
| manager = multiprocessing.Manager() | ||
| for i in range(0, 2): | ||
| os.environ["DBOS__VMID"] = f"test-executor-{i}" | ||
| process = Process(target=run_dbos_test_in_process, args=(i,)) | ||
| start_signal = manager.Event() | ||
| start_signals.append(start_signal) | ||
| end_signal = manager.Event() | ||
| end_signals.append(end_signal) | ||
| process = multiprocessing.Process( | ||
| target=run_dbos_test_in_process, args=(i, start_signal, end_signal) | ||
| ) | ||
| process.start() | ||
| processes.append(process) | ||
| del os.environ["DBOS__VMID"] | ||
|
|
||
| # Check that a single worker was able to acquire all the tasks | ||
| loop = True | ||
| while loop: | ||
| for signal in start_signals: | ||
| signal.wait(timeout=1) | ||
| if signal.is_set(): | ||
| loop = False | ||
| executors = [] | ||
| for handle in handles: | ||
| status = handle.get_status() | ||
| assert status.status == WorkflowStatusString.PENDING.value | ||
| executors.append(status.executor_id) | ||
| assert len(set(executors)) == 1 | ||
|
|
||
| # Now enqueue less than the local concurrency limit. Check that the 2nd worker acquired them. We won't have a signal set from the worker so we need to sleep a little. | ||
| handles = [] | ||
| for _ in range(0, local_concurrency_limit - 1): | ||
| handles.append(queue.enqueue(worker_concurrency_test_workflow)) | ||
| time.sleep(2) | ||
| executors = [] | ||
| for handle in handles: | ||
| status = handle.get_status() | ||
| assert status.status == WorkflowStatusString.PENDING.value | ||
| executors.append(status.executor_id) | ||
| assert len(set(executors)) == 1 | ||
|
|
||
| # Now, enqueue two more tasks. This means qlen > local concurrency limit * 2 and qlen > global concurrency limit | ||
| # We should have 1 tasks PENDING and 1 ENQUEUED, thus meeting both local and global concurrency limits | ||
| handles = [] | ||
| for _ in range(0, 2): | ||
| handles.append(queue.enqueue(worker_concurrency_test_workflow)) | ||
| # we can check the signal because the 2nd executor will set it | ||
| num_dequeued = 0 | ||
| while num_dequeued < 2: | ||
| for signal in start_signals: | ||
| signal.wait(timeout=1) | ||
| if signal.is_set(): | ||
| num_dequeued += 1 | ||
| executors = [] | ||
| statuses = [] | ||
| for handle in handles: | ||
| status = handle.get_status() | ||
| statuses.append(status.status) | ||
| executors.append(status.executor_id) | ||
| assert set(statuses) == { | ||
| WorkflowStatusString.PENDING.value, | ||
| WorkflowStatusString.ENQUEUED.value, | ||
| } | ||
| assert len(set(executors)) == 2 | ||
| assert "local" in executors | ||
|
|
||
| # Now check in the DB that global concurrency is met | ||
| with dbos._sys_db.engine.begin() as conn: | ||
| query = ( | ||
| sa.select(sa.func.count()) | ||
| .select_from(SystemSchema.workflow_status) | ||
| .where( | ||
| SystemSchema.workflow_status.c.status | ||
| == WorkflowStatusString.PENDING.value | ||
| ) | ||
| ) | ||
| row = conn.execute(query).fetchone() | ||
|
|
||
| # Enqueue N tasks but ensure this worker cannot dequeue | ||
| assert row is not None, "Query returned no results" | ||
| count = row[0] | ||
| assert ( | ||
| count == global_concurrency_limit | ||
| ), f"Expected {global_concurrency_limit} workflows, found {count}" | ||
|
|
||
| queue = Queue("test_queue", limiter={"limit": 0, "period": 1}) | ||
| for i in range(0, 10): | ||
| queue.enqueue(worker_concurrency_test_workflow) | ||
| # Signal the workers they can move on | ||
| for signal in end_signals: | ||
| signal.set() | ||
|
|
||
| for process in processes: | ||
| process.join() | ||
|
|
||
| # Verify all queue entries eventually get cleaned up. | ||
| assert queue_entries_are_cleaned_up(dbos) | ||
|
|
||
|
|
||
| # Test error cases where we have duplicated workflows starting with the same workflow ID. | ||
| def test_duplicate_workflow_id(dbos: DBOS, caplog: pytest.LogCaptureFixture) -> None: | ||
|
|
@@ -661,7 +774,9 @@ def blocked_workflow(i: int) -> None: | |
| def noop() -> None: | ||
| pass | ||
|
|
||
| queue = Queue("test_queue", concurrency=2) | ||
| queue = Queue( | ||
| "test_queue", worker_concurrency=2 | ||
| ) # covers global concurrency limit because we have a single process | ||
| handle1 = queue.enqueue(blocked_workflow, 0) | ||
| handle2 = queue.enqueue(blocked_workflow, 1) | ||
| handle3 = queue.enqueue(noop) | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.