Skip to content

Commit 975df61

Browse files
committed
fix Amb backpressure problems
1 parent 1a85656 commit 975df61

File tree

2 files changed

+97
-23
lines changed

2 files changed

+97
-23
lines changed

src/main/java/rx/internal/operators/OnSubscribeAmb.java

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Collection;
2020
import java.util.List;
2121
import java.util.concurrent.ConcurrentLinkedQueue;
22+
import java.util.concurrent.atomic.AtomicBoolean;
2223
import java.util.concurrent.atomic.AtomicReference;
2324

2425
import rx.Observable;
@@ -343,10 +344,13 @@ public void unsubscribeOthers(AmbSubscriber<T> notThis) {
343344
}
344345

345346
}
346-
347-
private final Iterable<? extends Observable<? extends T>> sources;
348-
private final Selection<T> selection = new Selection<T>();
349-
347+
348+
//give default access instead of private as a micro-optimization
349+
//for access from anonymous classes below
350+
final Iterable<? extends Observable<? extends T>> sources;
351+
final Selection<T> selection = new Selection<T>();
352+
final AtomicReference<AmbSubscriber<T>> choice = selection.choice;
353+
350354
private OnSubscribeAmb(Iterable<? extends Observable<? extends T>> sources) {
351355
this.sources = sources;
352356
}
@@ -357,41 +361,63 @@ public void call(final Subscriber<? super T> subscriber) {
357361

358362
@Override
359363
public void call() {
360-
if (selection.choice.get() != null) {
364+
AmbSubscriber<T> c;
365+
if ((c = choice.get()) != null) {
361366
// there is a single winner so we unsubscribe it
362-
selection.choice.get().unsubscribe();
367+
c.unsubscribe();
363368
}
364369
// if we are racing with others still existing, we'll also unsubscribe them
365-
if(!selection.ambSubscribers.isEmpty()) {
366-
for (AmbSubscriber<T> other : selection.ambSubscribers) {
370+
Collection<AmbSubscriber<T>> ambSubs = selection.ambSubscribers;
371+
if(!ambSubs.isEmpty()) {
372+
for (AmbSubscriber<T> other : ambSubs) {
367373
other.unsubscribe();
368374
}
369-
selection.ambSubscribers.clear();
375+
ambSubs.clear();
370376
}
371377
}
372378

373379
}));
380+
//need to subscribe to all the sources
381+
for (Observable<? extends T> source : sources) {
382+
if (subscriber.isUnsubscribed()) {
383+
return;
384+
}
385+
AmbSubscriber<T> ambSubscriber = new AmbSubscriber<T>(0, subscriber, selection);
386+
selection.ambSubscribers.add(ambSubscriber);
387+
// check again if choice has been made so can stop subscribing
388+
// if all sources were backpressure aware then this check
389+
// would be pointless given that 0 was requested above from each ambSubscriber
390+
AmbSubscriber<T> c;
391+
if ((c = choice.get()) != null) {
392+
// Already chose one, the rest can be skipped and we can clean up
393+
selection.unsubscribeOthers(c);
394+
return;
395+
}
396+
source.unsafeSubscribe(ambSubscriber);
397+
}
374398
subscriber.setProducer(new Producer() {
375399

376400
@Override
377401
public void request(long n) {
378-
if (selection.choice.get() != null) {
402+
final AmbSubscriber<T> c;
403+
if ((c = choice.get()) != null) {
379404
// propagate the request to that single Subscriber that won
380-
selection.choice.get().requestMore(n);
405+
c.requestMore(n);
381406
} else {
382-
for (Observable<? extends T> source : sources) {
383-
if (subscriber.isUnsubscribed()) {
384-
break;
385-
}
386-
AmbSubscriber<T> ambSubscriber = new AmbSubscriber<T>(n, subscriber, selection);
387-
selection.ambSubscribers.add(ambSubscriber);
388-
// possible race condition in previous lines ... a choice may have been made so double check (instead of synchronizing)
389-
if (selection.choice.get() != null) {
390-
// Already chose one, the rest can be skipped and we can clean up
391-
selection.unsubscribeOthers(selection.choice.get());
392-
break;
407+
//propagate the request to all the amb subscribers
408+
for (AmbSubscriber<T> ambSubscriber: selection.ambSubscribers) {
409+
if (!ambSubscriber.isUnsubscribed()) {
410+
// make a best endeavours check to not waste requests
411+
// if first emission has already occurred
412+
if (choice.get() == ambSubscriber) {
413+
ambSubscriber.requestMore(n);
414+
// don't need to request from other subscribers because choice has been made
415+
// and request has gone to choice
416+
return;
417+
} else {
418+
ambSubscriber.requestMore(n);
419+
}
393420
}
394-
source.unsafeSubscribe(ambSubscriber);
395421
}
396422
}
397423
}

src/test/java/rx/internal/operators/OnSubscribeAmbTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static rx.internal.operators.OnSubscribeAmb.amb;
2323

2424
import java.io.IOException;
25+
import java.util.Arrays;
2526
import java.util.concurrent.TimeUnit;
2627
import java.util.concurrent.atomic.AtomicLong;
2728

@@ -219,4 +220,51 @@ public void testBackpressure() {
219220
ts.assertNoErrors();
220221
assertEquals(RxRingBuffer.SIZE * 2, ts.getOnNextEvents().size());
221222
}
223+
224+
225+
@Test
226+
public void testSubscriptionOnlyHappensOnce() throws InterruptedException {
227+
final AtomicLong count = new AtomicLong();
228+
Action0 incrementer = new Action0() {
229+
@Override
230+
public void call() {
231+
count.incrementAndGet();
232+
}
233+
};
234+
//this aync stream should emit first
235+
Observable<Integer> o1 = Observable.just(1).doOnSubscribe(incrementer)
236+
.delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
237+
//this stream emits second
238+
Observable<Integer> o2 = Observable.just(1).doOnSubscribe(incrementer)
239+
.delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
240+
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
241+
Observable.amb(o1, o2).subscribe(ts);
242+
ts.requestMore(1);
243+
ts.awaitTerminalEvent(5, TimeUnit.SECONDS);
244+
ts.assertNoErrors();
245+
assertEquals(2, count.get());
246+
}
247+
248+
@Test
249+
public void testSecondaryRequestsPropagatedToChildren() throws InterruptedException {
250+
//this aync stream should emit first
251+
Observable<Integer> o1 = Observable.from(Arrays.asList(1, 2, 3))
252+
.delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
253+
//this stream emits second
254+
Observable<Integer> o2 = Observable.from(Arrays.asList(4, 5, 6))
255+
.delay(200, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
256+
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() {
257+
@Override
258+
public void onStart() {
259+
request(1);
260+
}};
261+
Observable.amb(o1, o2).subscribe(ts);
262+
// before first emission request 20 more
263+
// this request should suffice to emit all
264+
ts.requestMore(20);
265+
//ensure stream does not hang
266+
ts.awaitTerminalEvent(5, TimeUnit.SECONDS);
267+
ts.assertNoErrors();
268+
}
269+
222270
}

0 commit comments

Comments
 (0)