Skip to content

Commit fa1e720

Browse files
committed
Rename and doc
1 parent 4c922c5 commit fa1e720

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

src/main/java/org/dataloader/strategy/BreadthFirstChainedDispatchStrategy.java renamed to src/main/java/org/dataloader/strategy/GreedyLevelByLevelChainedDispatchStrategy.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,20 @@
1212
import java.util.concurrent.TimeUnit;
1313
import java.util.concurrent.atomic.AtomicInteger;
1414

15-
public class BreadthFirstChainedDispatchStrategy implements DispatchStrategy {
15+
/**
16+
* A {@link DispatchStrategy} which balances batching and performance by dispatching level by level with minimal waiting.
17+
* <p>
18+
* We use a fallback {@link ScheduledExecutorService} to handle when work is stuck due to async calls in the chain for
19+
* chained dataloaders. This minimizes the amount of threads spawned to only be used when there is no known work to be
20+
* done and the chain is not finished.
21+
* <p>
22+
* Due to the concept of 'known' work we greedily walk the chain instead of waiting for async calls to finish before
23+
* kicking off the next level.
24+
* <p>
25+
* In practice this will greedily fill up DataLoader keys while walking the chain to provide a nice balance of
26+
* batching/dedupe/caching while not needing to worry about manually dispatching the tree.
27+
*/
28+
public class GreedyLevelByLevelChainedDispatchStrategy implements DispatchStrategy {
1629

1730
private static final Duration DEFAULT_FALLBACK_TIMEOUT = Duration.ofMillis(30);
1831

@@ -29,7 +42,7 @@ public class BreadthFirstChainedDispatchStrategy implements DispatchStrategy {
2942

3043
@Nullable private Runnable dispatchCallback;
3144

32-
private BreadthFirstChainedDispatchStrategy(Builder builder) {
45+
private GreedyLevelByLevelChainedDispatchStrategy(Builder builder) {
3346
this.scheduledExecutorService = builder.scheduledExecutorService;
3447
this.fallbackTimeout = builder.fallbackTimeout;
3548
}
@@ -141,8 +154,8 @@ public Builder setFallbackTimeout(Duration fallbackTimeout) {
141154
return this;
142155
}
143156

144-
public BreadthFirstChainedDispatchStrategy build() {
145-
return new BreadthFirstChainedDispatchStrategy(this);
157+
public GreedyLevelByLevelChainedDispatchStrategy build() {
158+
return new GreedyLevelByLevelChainedDispatchStrategy(this);
146159
}
147160

148161
}

src/test/java/org/dataloader/strategy/BreadthFirstChainedDispatchStrategyStressTest.java renamed to src/test/java/org/dataloader/strategy/GreedyLevelByLevelChainedDispatchStrategyStressTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
import java.util.concurrent.CompletableFuture;
1313
import java.util.concurrent.CountDownLatch;
1414
import java.util.concurrent.Executors;
15-
import java.util.function.Function;
1615
import java.util.stream.Collectors;
1716

1817
import static org.hamcrest.MatcherAssert.assertThat;
1918
import static org.hamcrest.Matchers.equalTo;
2019

21-
public class BreadthFirstChainedDispatchStrategyStressTest {
20+
public class GreedyLevelByLevelChainedDispatchStrategyStressTest {
2221

2322
private int iterationCount;
2423
private List<List<String>> dispatchOrder;
@@ -62,12 +61,12 @@ public void setup() {
6261
gCompleted = new CountDownLatch(1);
6362
iCompleted = new CountDownLatch(1);
6463
iterationCount = 1;
65-
BreadthFirstChainedDispatchStrategy breadthFirstChainedDispatchStrategy =
66-
new BreadthFirstChainedDispatchStrategy.Builder(Executors.newSingleThreadScheduledExecutor())
64+
GreedyLevelByLevelChainedDispatchStrategy greedyLevelByLevelChainedDispatchStrategy =
65+
new GreedyLevelByLevelChainedDispatchStrategy.Builder(Executors.newSingleThreadScheduledExecutor())
6766
.setFallbackTimeout(Duration.ofMillis(300)).build();
68-
breadthFirstChainedDispatchStrategy.onIteration(() -> iterationCount += 1);
67+
greedyLevelByLevelChainedDispatchStrategy.onIteration(() -> iterationCount += 1);
6968
registry = DataLoaderRegistry.newRegistry()
70-
.dispatchStrategy(breadthFirstChainedDispatchStrategy)
69+
.dispatchStrategy(greedyLevelByLevelChainedDispatchStrategy)
7170
.build();
7271

7372

src/test/java/org/dataloader/strategy/BreadthFirstChainedDispatchStrategyTest.java renamed to src/test/java/org/dataloader/strategy/GreedyLevelByLevelChainedDispatchStrategyTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import static org.hamcrest.MatcherAssert.assertThat;
2121
import static org.hamcrest.Matchers.equalTo;
2222

23-
public class BreadthFirstChainedDispatchStrategyTest {
23+
public class GreedyLevelByLevelChainedDispatchStrategyTest {
2424

2525
private ScheduledExecutorService scheduledExecutorService;
2626

@@ -38,7 +38,7 @@ public void cleanUp() {
3838
void singleDepthLoadSucceeds() throws Exception {
3939
DataLoaderRegistry registry = DataLoaderRegistry.newRegistry()
4040
.register(SimpleLoader.class.getSimpleName(), DataLoaderFactory.newDataLoader(new SimpleLoader()))
41-
.dispatchStrategy(new BreadthFirstChainedDispatchStrategy.Builder(scheduledExecutorService).build())
41+
.dispatchStrategy(new GreedyLevelByLevelChainedDispatchStrategy.Builder(scheduledExecutorService).build())
4242
.build();
4343
DataLoader<Integer, Integer> loader = registry.getDataLoader(SimpleLoader.class.getSimpleName());
4444
CompletableFuture<Integer> result = loader.load(1);
@@ -50,7 +50,7 @@ void singleDepthLoadSucceeds() throws Exception {
5050
void singleDepthLoadSucceedsMultipleTimes() throws Exception {
5151
DataLoaderRegistry registry = DataLoaderRegistry.newRegistry()
5252
.register(SimpleLoader.class.getSimpleName(), DataLoaderFactory.newDataLoader(new SimpleLoader()))
53-
.dispatchStrategy(new BreadthFirstChainedDispatchStrategy.Builder(scheduledExecutorService).build())
53+
.dispatchStrategy(new GreedyLevelByLevelChainedDispatchStrategy.Builder(scheduledExecutorService).build())
5454
.build();
5555
DataLoader<Integer, Integer> loader = registry.getDataLoader(SimpleLoader.class.getSimpleName());
5656
CompletableFuture<Integer> result = loader.load(1);
@@ -66,7 +66,7 @@ void singleDepthLoadSucceedsMultipleTimes() throws Exception {
6666
@Test
6767
void chainedLoaderSucceeds() throws Exception {
6868
DataLoaderRegistry registry = DataLoaderRegistry.newRegistry()
69-
.dispatchStrategy(new BreadthFirstChainedDispatchStrategy.Builder(scheduledExecutorService).build())
69+
.dispatchStrategy(new GreedyLevelByLevelChainedDispatchStrategy.Builder(scheduledExecutorService).build())
7070
.build();
7171
registry.register(SimpleLoader.class.getSimpleName(), DataLoaderFactory.newDataLoader(new SimpleLoader()));
7272
registry.register(ChainedLoader.class.getSimpleName(), DataLoaderFactory.newDataLoader(new ChainedLoader(registry)));
@@ -80,7 +80,7 @@ void chainedLoaderSucceeds() throws Exception {
8080
void chainedAsyncLoaderSucceeds() {
8181
CountDownLatch latch = new CountDownLatch(1);
8282
DataLoaderRegistry registry = DataLoaderRegistry.newRegistry()
83-
.dispatchStrategy(new BreadthFirstChainedDispatchStrategy.Builder(scheduledExecutorService).build())
83+
.dispatchStrategy(new GreedyLevelByLevelChainedDispatchStrategy.Builder(scheduledExecutorService).build())
8484
.build();
8585
registry.register(SimpleLoader.class.getSimpleName(), DataLoaderFactory.newDataLoader(new SimpleLoader()));
8686
registry.register(ChainedAsyncLoader.class.getSimpleName(), DataLoaderFactory.newDataLoader(new ChainedAsyncLoader(registry, latch)));
@@ -100,7 +100,7 @@ void chainedAsyncLoaderSucceeds() {
100100
@Test
101101
void dispatchGoesByLevel() throws Exception {
102102
DataLoaderRegistry registry = DataLoaderRegistry.newRegistry()
103-
.dispatchStrategy(new BreadthFirstChainedDispatchStrategy.Builder(scheduledExecutorService).build())
103+
.dispatchStrategy(new GreedyLevelByLevelChainedDispatchStrategy.Builder(scheduledExecutorService).build())
104104
.build();
105105
List<List<Integer>> leafLevelSeenKeys = new ArrayList<>();
106106
BatchLoader<Integer, Integer> leaf = keys -> {

0 commit comments

Comments
 (0)