Repository navigation
Fix reactive streams batch cursor regression. - #652
Conversation
The BatchCursorPublisher now waits for demand to be signalled before creating the cursor. The batch cursor publisher also internally respects the batch size. Its either the configured batch size if set or the amount requested. JAVA-3973
jyemin
left a comment
There was a problem hiding this comment.
I'll think more about how the concurrency issues might be addressed. @stIncMale might also have some ideas about it.
| } | ||
| } | ||
| Integer originalBatchSize = getBatchSize(); | ||
| new BatchCursorFlux<>(this, originalBatchSize, () -> batchSize = originalBatchSize).subscribe(subscriber); |
There was a problem hiding this comment.
This resetting of the batch size seems sketchy. Can you explain the intention? If we really need to do this, we need to make it thread safe, and this way it's not because batchSize is not protected my any mutex.
There was a problem hiding this comment.
This was how it was done prior to reactor. The issue is the batchSize is used as part of the operation creation, so if a user doesn't set it on the Publisher we shouldn't set it on the publisher.
I'll look to refactor around that and make it stable.
| sink.onRequest(l -> { | ||
| if (demandDelta.addAndGet(l) > 0 && inProgress.compareAndSet(false, true)) { | ||
| if (batchCursor == null) { | ||
| batchCursorPublisher.batchSize(calculateBatchSize()); |
There was a problem hiding this comment.
It seems dangerous for BatchCursorFlux to be mutating the state of the BatchCursorPublisher, since that may be both observed and changed by the application concurrrently with this call.
Perhaps another way of accomplishing the goal is for this class to instead tell the publisher what batch size to use. This could be done by adding a batchSize parameter to BatchCursorPublish.batchCursor().
| Flux.<T>create(sink -> { | ||
| this.sink = sink; | ||
| sink.onRequest(l -> { | ||
| if (demandDelta.addAndGet(l) > 0 && inProgress.compareAndSet(false, true)) { |
There was a problem hiding this comment.
Do we need to check for overflow of demandDelta?
| Flux.<T>create(sink -> { | ||
| this.sink = sink; | ||
| sink.onRequest(l -> { | ||
| if (demandDelta.addAndGet(l) > 0 && inProgress.compareAndSet(false, true)) { |
There was a problem hiding this comment.
I think there is a concurrency issue here. Consider the case where an application calls Subscription.request multiple times. The first call we will get demand and set inProgress to true. Now imagine that the second call occurs just after line 57. We've set the batchCursor but haven't recursed yet. So the second call will observe demand and inProgress of false and will enter the conditional, then set inProgress to false and call recurseCursor. Meanwhile the first call will also, concurrently, call recurseCursor.
Also, batchCursor is not protected by a mutex and so I think it's not safe to assume that writes to it will be observed.
There was a problem hiding this comment.
Multiple calls to recurseCursor is fine as it contains a locking mechanism around inProgress - so if there is a race calling it via multiple calls to request(n) only one thread can action it.
batchCursor is not protected by a mutex and so I think it's not safe to assume that writes to it will be observed.
Not sure I follow here, batchCursor is also protected by the inprogress check..
There was a problem hiding this comment.
Scenario expressed by Jeff
Consider the case where an application calls Subscription.request multiple times. The first call we will get demand and set inProgress to true. Now imagine that the second call occurs just after line 57. We've set the batchCursor but haven't recursed yet. So the second call will observe demand and inProgress of false and will enter the conditional, then set inProgress to false and call recurseCursor. Meanwhile the first call will also, concurrently, call recurseCursor.
@jyemin Yes, the method recurseCursor may be called concurrently in the scenario you describe. However, this method protects most of its body from being executed concurrently by using the same approach:
if (inProgress.compareAndSet(false, true)) {//start executing mutually exclusive code in thread X
...
doOnSuccess(results -> {
...
inProgress.set(false);//end executing mutually exclusive code in thread Y
//(may not be the same as X, depending on how Reactor/BatchCursor work)
})
}A similar code in the subscribe method is meant to guarantee that:
batchCursoris initialized not more than oncew_in_subscribe(batchCursor, <initial value>)happens-before (HB) anyr_in_recurseCursor(batchCursor).
HB(w_in_subscribe(batchCursor, initial value), r_in_closeCursor(batchCursor))?
The usage of inProgress does not provide HB between w_in_subscribe(batchCursor, <initial value>) and r_in_closeCursor(batchCursor). This HB may only be provided by the Reactor. @rozza Could you please confirm this HB relation is guaranteed?
There was a problem hiding this comment.
closeCursor can be called at any time via sink::onDispose & sink::onCancel - so the batch cursor may not be set.
Looking at the code there is a chance of the sink being cancelled before the batchCursor is set which currently wouldn't close the cursor:
batchCursorPublisher.batchCursor(batchSize).subscribe(bc -> {
batchCursor = bc; // sink.isCancelled could be true at this point in time.
inProgress.set(false);
recurseCursor();
}, sink::error);
So I've added a check in recurseCursor - as closeCursor is idempotent it doesn't matter if it gets called multiple times.
There was a problem hiding this comment.
closeCursorcan be called at any time viasink::onDispose&sink::onCancel
And closeCursor may be called concurrently with the code we pass to the onRequest method, right? If that is the case, then making sure executions do not have data race due to writing to and reading from batchCursor is on us. The simplest way of achieving this is by marking batchCursor as volatile.
stIncMale
left a comment
There was a problem hiding this comment.
It is worth saying that due not not having any knowledge of Project Reactor, I cannot assess the correctness of the core that uses Reactor. I expressed mostly thoughts about other aspects of this PR.
stIncMale
left a comment
There was a problem hiding this comment.
👍
I marked all conversations started by me as resolved.
The BatchCursorPublisher now waits for demand to be signalled before creating the cursor. The batch cursor publisher also internally respects the batch size. Its either the configured batch size if set or the amount requested. JAVA-3973
The BatchCursorPublisher now waits for demand to be signalled before creating the cursor. The batch cursor publisher also internally respects the batch size. Its either the configured batch size if set or the amount requested. JAVA-3973
This reverts commit b9133d1.
The BatchCursorPublisher now waits for demand to be signalled before
creating the cursor.
The batch cursor publisher also internally respects the batch size.
Its either the configured batch size if set or the amount requested.
JAVA-3973