Skip to content

Implement recursive dispatching #111

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
20 changes: 18 additions & 2 deletions src/main/java/org/dataloader/DataLoaderRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

Expand Down Expand Up @@ -131,7 +132,13 @@ public Set<String> getKeys() {
* {@link org.dataloader.DataLoader}s
*/
public void dispatchAll() {
getDataLoaders().forEach(DataLoader::dispatch);
CompletableFuture<?>[] futuresToDispatch = getDataLoaders().stream()
.filter(dataLoader -> dataLoader.dispatchDepth() > 0)
.map(DataLoader::dispatch)
.toArray(CompletableFuture[]::new);
if (futuresToDispatch.length > 0) {
CompletableFuture.allOf(futuresToDispatch).whenComplete((__, throwable) -> dispatchAll());
}
}

/**
Expand All @@ -142,8 +149,17 @@ public void dispatchAll() {
*/
public int dispatchAllWithCount() {
int sum = 0;
List<CompletableFuture<?>> futuresToDispatch = new ArrayList<>();
for (DataLoader<?, ?> dataLoader : getDataLoaders()) {
sum += dataLoader.dispatchWithCounts().getKeysCount();
if (dataLoader.dispatchDepth() > 0) {
DispatchResult<?> dispatchResult = dataLoader.dispatchWithCounts();
sum += dispatchResult.getKeysCount();
futuresToDispatch.add(dispatchResult.getPromisedResults());
}
}
if (futuresToDispatch.size() > 0) {
CompletableFuture.allOf(futuresToDispatch.toArray(new CompletableFuture[0])).join();
Copy link
Member

Choose a reason for hiding this comment

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

join() is typically in an async system. You going to make them sync onto each other

sum += dispatchAllWithCount();
}
return sum;
}
Expand Down
78 changes: 78 additions & 0 deletions src/test/java/org/dataloader/DataLoaderRegistryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.Test;

import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import static java.util.Arrays.asList;
import static org.dataloader.DataLoaderFactory.newDataLoader;
Expand All @@ -14,6 +15,8 @@

public class DataLoaderRegistryTest {
final BatchLoader<Object, Object> identityBatchLoader = CompletableFuture::completedFuture;
final BatchLoader<Integer, Integer> incrementalBatchLoader =
v -> CompletableFuture.supplyAsync(() -> v.stream().map(i -> ++i).collect(Collectors.toList()));

@Test
public void registration_works() {
Expand Down Expand Up @@ -159,6 +162,81 @@ public void dispatch_counts_are_maintained() {
assertThat(dispatchDepth, equalTo(0));
}

@Test
public void composed_dispatch_counts_are_maintained() {

DataLoaderRegistry registry = new DataLoaderRegistry();

DataLoader<Integer, Integer> dlA = newDataLoader(incrementalBatchLoader);
DataLoader<Integer, Integer> dlB = newDataLoader(incrementalBatchLoader);

registry.register("a", dlA);
registry.register("b", dlB);

CompletableFuture<Integer> test1 = dlA.load(10)
.thenCompose(dlA::load)
.thenCompose(dlB::load)
.thenCompose(dlB::load);
CompletableFuture<Integer> test2 = dlB.load(20)
.thenCompose(dlB::load)
.thenCompose(dlA::load)
.thenCompose(dlA::load);

int dispatchDepth = registry.dispatchDepth();
assertThat(dispatchDepth, equalTo(2));

int dispatchedCount = registry.dispatchAllWithCount();
dispatchDepth = registry.dispatchDepth();
assertThat(dispatchedCount, equalTo(8));
assertThat(dispatchDepth, equalTo(0));
assertThat(test1.join(), equalTo(14));
assertThat(test2.join(), equalTo(24));
}

@Test
public void composed_stats_can_be_collected() {

DataLoaderRegistry registry = new DataLoaderRegistry();

DataLoader<Integer, Integer> dlA = newDataLoader(incrementalBatchLoader);
DataLoader<Integer, Integer> dlB = newDataLoader(incrementalBatchLoader);
DataLoader<Integer, Integer> dlC = newDataLoader(incrementalBatchLoader);

registry.register("a", dlA).register("b", dlB).register("c", dlC);

CompletableFuture<Integer> test1 = dlA.load(10)
.thenCompose(dlB::load)
.thenCompose(dlC::load);
CompletableFuture<Integer> test2 = dlC.load(20)
.thenCompose(dlB::load)
.thenCompose(dlA::load);

registry.dispatchAll();
CompletableFuture.allOf(test1, test2).join(); // wait for composed dispatches to settle

CompletableFuture<Integer> test3 = dlA.load(10)
.thenCompose(dlB::load)
.thenCompose(dlC::load);
CompletableFuture<Integer> test4 = dlC.load(20)
.thenCompose(dlB::load)
.thenCompose(dlA::load);

registry.dispatchAll();
CompletableFuture.allOf(test3, test4).join(); // wait for composed dispatches to settle

Statistics statistics = registry.getStatistics();

assertThat(statistics.getLoadCount(), equalTo(12L));
assertThat(statistics.getBatchLoadCount(), equalTo(6L));
assertThat(statistics.getCacheHitCount(), equalTo(6L));
assertThat(statistics.getLoadErrorCount(), equalTo(0L));
assertThat(statistics.getBatchLoadExceptionCount(), equalTo(0L));
assertThat(test1.join(), equalTo(13));
assertThat(test2.join(), equalTo(23));
assertThat(test3.join(), equalTo(13));
assertThat(test4.join(), equalTo(23));
}

@Test
public void builder_works() {
DataLoader<Object, Object> dlA = newDataLoader(identityBatchLoader);
Expand Down