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 lack of eager consumption for streaming collectors #578

Merged
merged 3 commits into from
Feb 11, 2021
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 @@ -5,7 +5,6 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Semaphore;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
Expand Down Expand Up @@ -34,19 +33,23 @@ class ParallelStreamCollector<T, R> implements Collector<T, Stream.Builder<Compl
private final Function<T, R> function;
private final CompletionStrategy<R> completionStrategy;
private final Set<Characteristics> characteristics;
private final Semaphore limiter;
private final Executor executor;
private final Dispatcher<R> dispatcher;

private ParallelStreamCollector(
Function<T, R> function,
CompletionStrategy<R> completionStrategy,
Set<Characteristics> characteristics,
Executor executor, int parallelism) {
Dispatcher<R> dispatcher) {
this.completionStrategy = completionStrategy;
this.characteristics = characteristics;
this.limiter = new Semaphore(parallelism);
this.dispatcher = dispatcher;
this.function = function;
this.executor = executor;
}

private void startConsuming() {
if (!dispatcher.isRunning()) {
dispatcher.start();
}
}

@Override
Expand All @@ -57,19 +60,8 @@ public Supplier<Stream.Builder<CompletableFuture<R>>> supplier() {
@Override
public BiConsumer<Stream.Builder<CompletableFuture<R>>, T> accumulator() {
return (acc, e) -> {
try {
limiter.acquire();
acc.add(CompletableFuture.supplyAsync(() -> {
try {
return function.apply(e);
} finally {
limiter.release();
}
}, executor));
} catch (InterruptedException interruptedException) {
Thread.currentThread().interrupt();
throw new RuntimeException(interruptedException);
}
startConsuming();
acc.add(dispatcher.enqueue(() -> function.apply(e)));
};
}

Expand All @@ -82,7 +74,10 @@ public BinaryOperator<Stream.Builder<CompletableFuture<R>>> combiner() {

@Override
public Function<Stream.Builder<CompletableFuture<R>>, Stream<R>> finisher() {
return acc -> completionStrategy.apply(acc.build());
return acc -> {
dispatcher.stop();
return completionStrategy.apply(acc.build());
};
}

@Override
Expand All @@ -99,9 +94,7 @@ public Set<Characteristics> characteristics() {
requireNonNull(mapper, "mapper can't be null");
requireValidParallelism(parallelism);

return parallelism == 1
? BatchingCollectors.syncCollector(mapper)
: new ParallelStreamCollector<>(mapper, unordered(), UNORDERED, executor, parallelism);
return new ParallelStreamCollector<>(mapper, unordered(), UNORDERED, Dispatcher.of(executor, parallelism));
}

static <T, R> Collector<T, ?, Stream<R>> streamingOrdered(Function<T, R> mapper, Executor executor) {
Expand All @@ -113,9 +106,7 @@ public Set<Characteristics> characteristics() {
requireNonNull(mapper, "mapper can't be null");
requireValidParallelism(parallelism);

return parallelism == 1
? BatchingCollectors.syncCollector(mapper)
: new ParallelStreamCollector<>(mapper, ordered(), emptySet(), executor, parallelism);
return new ParallelStreamCollector<>(mapper, ordered(), emptySet(), Dispatcher.of(executor, parallelism));
}

static final class BatchingCollectors {
Expand Down Expand Up @@ -153,16 +144,14 @@ private BatchingCollectors() {
mapper,
ordered(),
emptySet(),
executor,
parallelism));
Dispatcher.of(executor, parallelism)));
} else {
return partitioned(list, parallelism)
.collect(collectingAndThen(new ParallelStreamCollector<>(
batching(mapper),
ordered(),
emptySet(),
executor,
parallelism),
Dispatcher.of(executor, parallelism)),
s -> s.flatMap(Collection::stream)));
}
});
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/pivovarit/collectors/FunctionalTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pivovarit.collectors;

import com.pivovarit.collectors.ParallelCollectors.Batching;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
Expand All @@ -24,6 +25,7 @@
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;
Expand Down Expand Up @@ -111,6 +113,26 @@ void shouldCollectInCompletionOrder() {
assertThat(result).isSorted();
}

@Test
void shouldCollectEagerlyInCompletionOrder() {
// given
executor = threadPoolExecutor(4);
AtomicBoolean result = new AtomicBoolean(false);
CompletableFuture.runAsync(() -> {
Stream.of(1, 10000, 1, 0)
.collect(parallelToStream(i -> returnWithDelay(i, ofMillis(i)), executor, 2))
.forEach(i -> {
if (i == 1) {
result.set(true);
}
});
});

await()
.atMost(1, SECONDS)
.until(result::get);
}

@Test
void shouldExecuteEagerlyOnProvidedThreadPool() {
ExecutorService executor = Executors.newFixedThreadPool(2);
Expand Down