-
Notifications
You must be signed in to change notification settings - Fork 27
perf: allow concurrent dataset pushes by narrowing the push-path locks #1079
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
vdusek
wants to merge
4
commits into
master
Choose a base branch
from
worktree-fix-p1
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
4 commits
Select commit
Hold shift + click to select a range
d14cdd7
fix: skip the charge lock in Actor.push_data for non-pay-per-event runs
vdusek ef63509
perf: allow concurrent dataset pushes by narrowing the push-path locks
vdusek fc8b73b
Merge branch 'master' into worktree-fix-p1
vdusek d08d9ab
test: cover per-push item order under concurrent multi-chunk pushes
vdusek 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
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
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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| import asyncio | ||
| import json | ||
| from typing import Any | ||
| from unittest.mock import AsyncMock, Mock | ||
|
|
||
| import pytest | ||
|
|
@@ -104,3 +105,48 @@ async def test_push_data_rejects_a_non_serializable_item() -> None: | |
|
|
||
| with pytest.raises(ValueError, match='at index 0 is not serializable'): | ||
| await client.push_data(circular) | ||
|
|
||
|
|
||
| async def test_concurrent_push_data_overlaps() -> None: | ||
| """Concurrent pushes reach the API at the same time instead of queueing behind each other.""" | ||
| concurrency = 3 | ||
| barrier = asyncio.Barrier(concurrency) | ||
| api_client = AsyncMock() | ||
|
|
||
| async def push_items(**_kwargs: Any) -> None: | ||
| # Every concurrent push must reach the API call before any of them is allowed to return. | ||
| await barrier.wait() | ||
|
|
||
| api_client.push_items = push_items | ||
| client, _ = _make_dataset_client(api_client) | ||
|
|
||
| async with asyncio.timeout(5): | ||
| await asyncio.gather(*(client.push_data({'id': i}) for i in range(concurrency))) | ||
|
|
||
|
|
||
| async def test_concurrent_multi_chunk_pushes_preserve_per_push_order(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """A push's own chunks stay in order even while they interleave on the wire with other pushes' chunks.""" | ||
| monkeypatch.setattr(ApifyDatasetClient, '_EFFECTIVE_LIMIT_SIZE', ByteSize(60)) | ||
| api_client = AsyncMock() | ||
| received: list[tuple[int, int]] = [] | ||
| chunk_count = 0 | ||
|
|
||
| async def push_items(**kwargs: Any) -> None: | ||
| nonlocal chunk_count | ||
| chunk_count += 1 | ||
| await asyncio.sleep(0) # Yield so chunks from other concurrent pushes can land in between. | ||
| received.extend((item['push'], item['i']) for item in json.loads(kwargs['items'])) | ||
|
|
||
| api_client.push_items = push_items | ||
|
Comment on lines
+132
to
+140
Collaborator
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. Let's try doing this using |
||
| client, _ = _make_dataset_client(api_client) | ||
|
|
||
| concurrency, items_per_push = 4, 6 | ||
| async with asyncio.timeout(5): | ||
| await asyncio.gather( | ||
| *(client.push_data([{'push': p, 'i': i} for i in range(items_per_push)]) for p in range(concurrency)) | ||
| ) | ||
|
|
||
| assert chunk_count > concurrency, 'each push must split into more than one chunk' | ||
| for push in range(concurrency): | ||
| indices = [i for p, i in received if p == push] | ||
| assert indices == list(range(items_per_push)) | ||
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.
Does drop really need a lock now? If I'm not mistaken, it was there to keep the dataset from being dropped while a push was in progress.