Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix concat map request race condition which resulted in flaky test #1341

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.concurrent.Flow.Publisher;
import java.util.concurrent.Flow.Subscription;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
Expand Down Expand Up @@ -78,6 +79,8 @@ public static final class ConcatMapMainSubscriber<I, O> implements MultiSubscrib

final ConcatMapInner<O> inner;

private final AtomicBoolean deferredUpstreamRequest = new AtomicBoolean(false);

ConcatMapMainSubscriber(
MultiSubscriber<? super O> downstream,
Function<? super I, ? extends Publisher<? extends O>> mapper,
Expand All @@ -93,13 +96,16 @@ public void request(long n) {
if (n > 0) {
if (state.compareAndSet(STATE_NEW, STATE_READY)) {
upstream.request(1);
// No outstanding requests from inner, forward the request to upstream
} else if (state.get() == STATE_READY && inner.requested() == 0) {
}
if (deferredUpstreamRequest.compareAndSet(true, false)) {
upstream.request(1);
}
inner.request(n);
if (inner.requested() != 0L && deferredUpstreamRequest.compareAndSet(true, false)) {
upstream.request(1);
}
} else {
downstream.onFailure(new IllegalArgumentException("Invalid requests, must be greater than 0"));
downstream.onFailure(Subscriptions.getInvalidRequestException());
}
}

Expand Down Expand Up @@ -187,26 +193,17 @@ public synchronized void tryEmit(O value) {
}

public void innerComplete(long emitted) {
while (true) {
int state = this.state.get();
if (state == STATE_EMITTING) {
if (this.state.compareAndSet(state, STATE_READY)) {
// Inner completed but there are outstanding requests from inner,
// Or the inner completed without producing any items
// Request new item from upstream
if (inner.requested() != 0L || emitted == 0) {
upstream.request(1);
}
return;
}
} else if (state == STATE_OUTER_TERMINATED) {
if (this.state.compareAndSet(state, STATE_TERMINATED)) {
terminateDownstream();
return;
}
if (this.state.compareAndSet(STATE_EMITTING, STATE_READY)) {
// Inner completed but there are outstanding requests from inner,
// Or the inner completed without producing any items
// Request new item from upstream
if (inner.requested() != 0L || emitted == 0) {
upstream.request(1);
} else {
return;
deferredUpstreamRequest.set(true);
}
} else if (this.state.compareAndSet(STATE_OUTER_TERMINATED, STATE_TERMINATED)) {
terminateDownstream();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -22,7 +21,6 @@
import io.smallrye.mutiny.helpers.test.AssertSubscriber;
import io.smallrye.mutiny.infrastructure.Infrastructure;

@DisabledIfEnvironmentVariable(named = "CI", matches = "true") // TODO Disabled in CI until https://github.com/smallrye/smallrye-mutiny/issues/1254 has been fixed
class MultiConcatMapNoPrefetchTest {

AtomicInteger upstreamRequestCount;
Expand Down Expand Up @@ -70,9 +68,9 @@ void testTransformToMulti(boolean prefetch, int[] upstreamRequests) {
Multi<Integer> result = upstream.onItem()
.transformToMulti(i -> Multi.createFrom().items(i, i))
.concatenate(prefetch);
AssertSubscriber<Integer> ts = new AssertSubscriber<>(5);
AssertSubscriber<Integer> ts = new AssertSubscriber<>();
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for digging into that bug @ozangunalp 👍

Minor question: why did you update the test case here? The split 5 upfront then 5 requests scheme had a race condition fixed before, but why not keep it in this test, still?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The ts.awaitSubscription(); in the other test reproduced the test case more consistently.
For this one, during bug fix tests, I had a dropped item, even with this change. So I kept it like that.

Copy link
Member

Choose a reason for hiding this comment

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

Ok thanks!

result.runSubscriptionOn(Infrastructure.getDefaultExecutor()).subscribe(ts);
ts.request(5);
ts.request(10);
ts.awaitItems(10);
assertThat(upstreamRequestCount).hasValue(upstreamRequests[0]);
ts.request(1);
Expand Down Expand Up @@ -137,6 +135,7 @@ void testNoPrefetchWithConcatMapContainingEmpty() {
.concatenate();
AssertSubscriber<Integer> ts = new AssertSubscriber<>(5);
result.runSubscriptionOn(Infrastructure.getDefaultExecutor()).subscribe(ts);
ts.awaitSubscription();
ts.request(5);
ts.awaitItems(10);
assertThat(upstreamRequestCount).hasValueGreaterThan(10);
Expand Down