Skip to content

Fix reactive streams batch cursor regression. - #652

Merged
rozza merged 13 commits into
mongodb:masterfrom
rozza:j3973
Feb 3, 2021
Merged

rozza merged 13 commits into
mongodb:masterfrom
rozza:j3973

Conversation

@rozza

@rozza rozza commented Jan 29, 2021

Copy link
Copy Markdown
Member

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
@rozza
rozza requested review from jyemin and stIncMale January 29, 2021 13:46

@jyemin jyemin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Flux.<T>create(sink -> {
this.sink = sink;
sink.onRequest(l -> {
if (demandDelta.addAndGet(l) > 0 && inProgress.compareAndSet(false, true)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check for overflow of demandDelta?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Flux.<T>create(sink -> {
this.sink = sink;
sink.onRequest(l -> {
if (demandDelta.addAndGet(l) > 0 && inProgress.compareAndSet(false, true)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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..

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stIncMale can you check my reasoning here?

@stIncMale stIncMale Feb 1, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. batchCursor is initialized not more than once
  2. w_in_subscribe(batchCursor, <initial value>) happens-before (HB) any r_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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closeCursor can be called at any time via sink::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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@stIncMale stIncMale left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rozza
rozza requested review from jyemin and stIncMale February 1, 2021 14:51
@rozza
rozza requested review from jyemin and stIncMale February 2, 2021 16:02
@rozza
rozza requested a review from stIncMale February 2, 2021 16:35

@stIncMale stIncMale left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
I marked all conversations started by me as resolved.

@rozza
rozza merged commit 4af9e9f into mongodb:master Feb 3, 2021
@rozza
rozza deleted the j3973 branch February 3, 2021 17:13
rozza added a commit to rozza/mongo-java-driver that referenced this pull request Feb 4, 2021
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
jfitzu pushed a commit to nextworld-tools/mongo-java-driver that referenced this pull request Mar 3, 2021
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
jfitzu added a commit to nextworld-tools/mongo-java-driver that referenced this pull request Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants