perf: allow concurrent dataset pushes by narrowing the push-path locks - #1079
Open
vdusek wants to merge 4 commits into
Open
perf: allow concurrent dataset pushes by narrowing the push-path locks#1079vdusek wants to merge 4 commits into
vdusek wants to merge 4 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1079 +/- ##
==========================================
+ Coverage 91.98% 92.14% +0.15%
==========================================
Files 51 51
Lines 3232 3234 +2
==========================================
+ Hits 2973 2980 +7
+ Misses 259 254 -5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
vdusek
marked this pull request as ready for review
August 5, 2026 11:49
Mantisus
approved these changes
Aug 5, 2026
Mantisus
left a comment
Collaborator
There was a problem hiding this comment.
LGTM. Just a couple of nits
Comment on lines
57
to
+58
| self._lock = lock | ||
| """A lock to ensure that only one operation is performed at a time.""" | ||
| """A lock serializing destructive operations on the dataset.""" |
Collaborator
There was a problem hiding this comment.
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.
Comment on lines
+132
to
+140
| 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 |
Collaborator
There was a problem hiding this comment.
Let's try doing this using unittest.mock.patch, but I won't insist 🙂
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Concurrent dataset pushes were fully serialized - N concurrent pushes cost N sequential API round-trips.
Problem
Two locks wrapped the network I/O; either alone is enough to serialize, so both had to be narrowed:
Actor.push_dataheldcharging_manager.charge_lock()across the whole push + charge sequence.ApifyDatasetClient.push_dataheld its per-client_lockacross the chunkedpush_itemscalls.The charge lock only matters for pay-per-event runs (keeps the limit reservation and charge atomic).
ApifyDatasetClient.push_datamutates no client state -_serialize_chunkis a classmethod,push_itemsis a stateless API call - so outside of charging, neither lock protects anything.Change
charge_lock_if_charging()helper in_charging.py, keyed offcharging_manager_ctx.DatasetClientPpeMixin._charge_lockand a second, divergentis_pay_per_eventpredicate - the "do we need the lock?" decision now lives in one place.Benchmark
push_dataagainst the real API, 10 items/push, median of 5 rounds (raw= same payloads sent directly viapush_items, the network floor with no lock involved):Master's curve is
N x single-push latency(full serialization); this PR flattens onto the raw floor. Re-adding just one lock (either one) aroundpush_datareproduces master's numbers exactly, confirming both had to move.Behavior changes
drop()are no longer mutually exclusive. The lock only ever ordered them within one client instance in one process, so a concurrent drop was never actually safe._lockstill serializesdrop()itself. Verified against the real API: 8 concurrent pushes racing adrop()finish in under a second, with the pushes failingNotFoundErroronce the drop wins, instead of silently completing first.FileSystemDatasetClient._lock, which guards sequential item numbering and metadata and has no network round-trip behind it.Testing
test_concurrent_push_data_overlaps,test_push_data_does_not_take_charge_lock_without_pay_per_event- each fails onmaster.test_concurrent_actor_push_data_stays_within_budget- fails if the charge lock is ever skipped for PPE runs.test_concurrent_multi_chunk_pushes_preserve_per_push_order- locks in per-push ordering on the new concurrent-chunk code path this PR enables.✍️ Drafted by Claude Code