Remove a race condition that caused done to sometimes be executed twice … #429
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.
…for the same set of input metrics.
Given two threads in a race and an initial count of 2.
shortLatch.countDown()(count = 1)shortLatch.countDown()(count = 0)done()done()shortLatch.getCount()(count = 0)shortLatch.getCount()(count = 0)As you can see in this scenario both threads will execute the body of the if statement in the
done().The below pull request address this issue by looking at what the if statement is actually doing (and what the latch is actually guarding against.)
It appears that aside from stopping
actualWriteCtxwhich should only be done once, the other conditions apply to individual batches and the latch was used just to prevent logging more than one similar event.I don't think there is very much value in that, so I've moved them inline to the work (thus also removing AtomicBoolean) and moved the stopping of
actualWriteCtxto a listener on the aggregate Future.So it'll only be stopped once when all futures of all batches have a result.