fix(cursor): make eachAsync()
avoid modifying batch when mixing parallel
and batchSize
#12716
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.
Fix #12652
Summary
#12652 points out that there is a race condition in
eachAsync()
whenparallel > 1
andbatchSize
is set. Given that we run a bunch of parallel queues,documentsBatch
can change while theeachAsync
callback is running.This fixes part of the issue: makes it so that at least every document gets processed, and each
eachAsync()
callback gets a consistent batch of documents that doesn't change.However, there is still the potential issue that the individual documents in the batch can be in any order when
parallel > 1
. Because we runnext()
in parallel, you can end up with surprising behavior like document 9613 in the same batch as documents 9900-9999. Or aneachAsync()
callback running with a batch of size 67 followed by a batch of 33 even though you havebatchSize = 100
and there's 10000 documents in the result set.We should consider running
next()
in a queue, getting documents in order and building up the batch, and then building the next batch whileeachAsync()
runs. Rather than running the whole "build up a batch and then call theeachAsync()
callback" process in parallel. What do you think @hasezoey ?Examples