|
| 1 | +package org.dataloader; |
| 2 | + |
| 3 | +import org.awaitility.Duration; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.reactivestreams.Publisher; |
| 6 | +import org.reactivestreams.Subscriber; |
| 7 | +import org.reactivestreams.Subscription; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Collection; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.concurrent.CompletableFuture; |
| 14 | +import java.util.concurrent.atomic.AtomicInteger; |
| 15 | +import java.util.concurrent.atomic.AtomicLong; |
| 16 | + |
| 17 | +import static java.util.Arrays.asList; |
| 18 | +import static org.awaitility.Awaitility.await; |
| 19 | +import static org.dataloader.DataLoaderFactory.newMappedPublisherDataLoader; |
| 20 | +import static org.dataloader.DataLoaderFactory.newPublisherDataLoader; |
| 21 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 22 | +import static org.hamcrest.Matchers.equalTo; |
| 23 | + |
| 24 | +/** |
| 25 | + * Reproduces <a href="https://github.com/graphql-java/java-dataloader/issues/273">issue #273</a>. |
| 26 | + * <p> |
| 27 | + * A reactive publisher only emits while it has outstanding demand. The reactive subscribers used |
| 28 | + * to request {@code keys.size()} exactly once and then wait for {@code onComplete}. A publisher |
| 29 | + * that emits values lazily as more demand arrives - for example one that emits an unrelated value |
| 30 | + * before the one that actually matches a key - would therefore leave the data loader blocked |
| 31 | + * forever, because the matching value was never requested and the publisher never completed. |
| 32 | + */ |
| 33 | +public class ReactiveBackpressureTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void mapped_loader_recovers_from_a_lazy_backpressured_publisher_that_never_completes() { |
| 37 | + List<Collection<String>> loadCalls = new ArrayList<>(); |
| 38 | + |
| 39 | + DataLoader<String, String> loader = newMappedPublisherDataLoader((keys, subscriber) -> { |
| 40 | + loadCalls.add(new ArrayList<>(keys)); |
| 41 | + |
| 42 | + List<Map.Entry<String, String>> items = new ArrayList<>(); |
| 43 | + // an entry that does not match any requested key - the original code consumed the one |
| 44 | + // and only window of demand on this value and then blocked |
| 45 | + items.add(Map.entry("an-unrelated-key", "an-unrelated-value")); |
| 46 | + for (String key : keys) { |
| 47 | + items.add(Map.entry(key, "value-" + key)); |
| 48 | + } |
| 49 | + // note: this publisher NEVER calls onComplete - it only emits as demand arrives |
| 50 | + new BackpressuredPublisher<>(items, false).subscribe(subscriber); |
| 51 | + }); |
| 52 | + |
| 53 | + CompletableFuture<String> cfA = loader.load("a"); |
| 54 | + CompletableFuture<String> cfB = loader.load("b"); |
| 55 | + CompletableFuture<List<String>> dispatch = loader.dispatch(); |
| 56 | + |
| 57 | + // before the fix the matching values were never re-requested and dispatch never completed |
| 58 | + await().atMost(Duration.FIVE_SECONDS).until(() -> cfA.isDone() && cfB.isDone() && dispatch.isDone()); |
| 59 | + |
| 60 | + assertThat(cfA.join(), equalTo("value-a")); |
| 61 | + assertThat(cfB.join(), equalTo("value-b")); |
| 62 | + // we got everything we asked for so we completed early rather than waiting on the publisher |
| 63 | + assertThat(dispatch.join(), equalTo(asList("value-a", "value-b"))); |
| 64 | + assertThat(loadCalls, equalTo(List.of(asList("a", "b")))); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void list_loader_recovers_from_a_lazy_backpressured_publisher_that_never_completes() { |
| 69 | + List<Collection<String>> loadCalls = new ArrayList<>(); |
| 70 | + |
| 71 | + DataLoader<String, String> loader = newPublisherDataLoader((keys, subscriber) -> { |
| 72 | + loadCalls.add(new ArrayList<>(keys)); |
| 73 | + |
| 74 | + List<String> items = new ArrayList<>(); |
| 75 | + for (String key : keys) { |
| 76 | + items.add("value-" + key); |
| 77 | + } |
| 78 | + new BackpressuredPublisher<>(items, false).subscribe(subscriber); |
| 79 | + }); |
| 80 | + |
| 81 | + CompletableFuture<String> cfA = loader.load("a"); |
| 82 | + CompletableFuture<String> cfB = loader.load("b"); |
| 83 | + CompletableFuture<List<String>> dispatch = loader.dispatch(); |
| 84 | + |
| 85 | + await().atMost(Duration.FIVE_SECONDS).until(() -> cfA.isDone() && cfB.isDone() && dispatch.isDone()); |
| 86 | + |
| 87 | + assertThat(cfA.join(), equalTo("value-a")); |
| 88 | + assertThat(cfB.join(), equalTo("value-b")); |
| 89 | + assertThat(dispatch.join(), equalTo(asList("value-a", "value-b"))); |
| 90 | + assertThat(loadCalls, equalTo(List.of(asList("a", "b")))); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * A minimal reactive-streams {@link Publisher} that strictly honours backpressure: it only ever |
| 95 | + * emits an item when there is outstanding demand for it, and (optionally) never signals |
| 96 | + * {@code onComplete}. It uses the standard work-in-progress drain loop so it behaves correctly |
| 97 | + * even when {@code request} is called re-entrantly from within {@code onNext}. |
| 98 | + */ |
| 99 | + static final class BackpressuredPublisher<T> implements Publisher<T> { |
| 100 | + private final List<T> items; |
| 101 | + private final boolean completeWhenDrained; |
| 102 | + |
| 103 | + BackpressuredPublisher(List<T> items, boolean completeWhenDrained) { |
| 104 | + this.items = items; |
| 105 | + this.completeWhenDrained = completeWhenDrained; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void subscribe(Subscriber<? super T> subscriber) { |
| 110 | + subscriber.onSubscribe(new Subscription() { |
| 111 | + final AtomicLong demand = new AtomicLong(); |
| 112 | + final AtomicInteger wip = new AtomicInteger(); |
| 113 | + volatile boolean cancelled; |
| 114 | + int idx; |
| 115 | + boolean completed; |
| 116 | + |
| 117 | + @Override |
| 118 | + public void request(long n) { |
| 119 | + if (n <= 0) { |
| 120 | + return; |
| 121 | + } |
| 122 | + demand.addAndGet(n); |
| 123 | + drain(); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void cancel() { |
| 128 | + cancelled = true; |
| 129 | + } |
| 130 | + |
| 131 | + private void drain() { |
| 132 | + if (wip.getAndIncrement() != 0) { |
| 133 | + // a drain is already in progress (possibly our own re-entrant caller) - it will |
| 134 | + // pick up the extra demand we just registered |
| 135 | + return; |
| 136 | + } |
| 137 | + int missed = 1; |
| 138 | + for (; ; ) { |
| 139 | + while (!cancelled && demand.get() > 0 && idx < items.size()) { |
| 140 | + T item = items.get(idx++); |
| 141 | + demand.decrementAndGet(); |
| 142 | + subscriber.onNext(item); |
| 143 | + } |
| 144 | + if (!cancelled && completeWhenDrained && !completed && idx >= items.size()) { |
| 145 | + completed = true; |
| 146 | + subscriber.onComplete(); |
| 147 | + } |
| 148 | + missed = wip.addAndGet(-missed); |
| 149 | + if (missed == 0) { |
| 150 | + break; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + }); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments