Skip to content

OperatorGroupBy - check for request overflow and don't decrement when at Long.MAX_VALUE #2950

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

Merged
merged 1 commit into from
May 15, 2015
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
16 changes: 13 additions & 3 deletions src/main/java/rx/internal/operators/OperatorGroupBy.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void onError(Throwable e) {
// If we already have items queued when a request comes in we vend those and decrement the outstanding request count

void requestFromGroupedObservable(long n, GroupState<K, T> group) {
group.requested.getAndAdd(n);
BackpressureUtils.getAndAddRequest(group.requested, n);
if (group.count.getAndIncrement() == 0) {
pollQueue(group);
}
Expand Down Expand Up @@ -330,13 +330,19 @@ private void cleanupGroup(Object key) {
private void emitItem(GroupState<K, T> groupState, Object item) {
Queue<Object> q = groupState.buffer;
AtomicLong keyRequested = groupState.requested;
//don't need to check for requested being Long.MAX_VALUE because this
//field is capped at MAX_QUEUE_SIZE
REQUESTED.decrementAndGet(this);
// short circuit buffering
if (keyRequested != null && keyRequested.get() > 0 && (q == null || q.isEmpty())) {
@SuppressWarnings("unchecked")
Observer<Object> obs = (Observer<Object>)groupState.getObserver();
nl.accept(obs, item);
keyRequested.decrementAndGet();
if (keyRequested.get() != Long.MAX_VALUE) {
// best endeavours check (no CAS loop here) because we mainly care about
// the initial request being Long.MAX_VALUE and that value being conserved.
keyRequested.decrementAndGet();
}
} else {
q.add(item);
BUFFERED_COUNT.incrementAndGet(this);
Expand Down Expand Up @@ -381,7 +387,11 @@ private void drainIfPossible(GroupState<K, T> groupState) {
@SuppressWarnings("unchecked")
Observer<Object> obs = (Observer<Object>)groupState.getObserver();
nl.accept(obs, t);
groupState.requested.decrementAndGet();
if (groupState.requested.get()!=Long.MAX_VALUE) {
// best endeavours check (no CAS loop here) because we mainly care about
// the initial request being Long.MAX_VALUE and that value being conserved.
groupState.requested.decrementAndGet();
}
BUFFERED_COUNT.decrementAndGet(this);

// if we have used up all the events we requested from upstream then figure out what to ask for this time based on the empty space in the buffer
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/rx/internal/operators/OperatorGroupByTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -1454,4 +1455,50 @@ public Integer call(Integer i) {
assertEquals(Arrays.asList(e), inner1.getOnErrorEvents());
assertEquals(Arrays.asList(e), inner2.getOnErrorEvents());
}

@Test
public void testRequestOverflow() {
final AtomicBoolean completed = new AtomicBoolean(false);
Observable
.just(1, 2, 3)
// group into one group
.groupBy(new Func1<Integer, Integer>() {
@Override
public Integer call(Integer t) {
return 1;
}
})
// flatten
.concatMap(new Func1<GroupedObservable<Integer, Integer>, Observable<Integer>>() {
@Override
public Observable<Integer> call(GroupedObservable<Integer, Integer> g) {
return g;
}
})
.subscribe(new Subscriber<Integer>() {

@Override
public void onStart() {
request(2);
}

@Override
public void onCompleted() {
completed.set(true);

}

@Override
public void onError(Throwable e) {

}

@Override
public void onNext(Integer t) {
System.out.println(t);
//provoke possible request overflow
request(Long.MAX_VALUE-1);
}});
assertTrue(completed.get());
}
}