Skip to content

Commit 378eb2b

Browse files
authored
Merge pull request #4055 from graphql-java/simplify-delay-dataloader
simplify delayed dataloader dispatching by not using a batch window
2 parents 2d19c78 + 1dbd50e commit 378eb2b

File tree

11 files changed

+133
-365
lines changed

11 files changed

+133
-365
lines changed

src/main/java/graphql/GraphQLUnusualConfiguration.java

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
package graphql;
22

33
import graphql.execution.ResponseMapFactory;
4-
import graphql.execution.instrumentation.dataloader.DelayedDataLoaderDispatcherExecutorFactory;
54
import graphql.introspection.GoodFaithIntrospection;
65
import graphql.parser.ParserOptions;
76
import graphql.schema.PropertyDataFetcherHelper;
87

9-
import java.time.Duration;
10-
118
import static graphql.Assert.assertNotNull;
12-
import static graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys.DELAYED_DATA_LOADER_BATCH_WINDOW_SIZE_NANO_SECONDS;
13-
import static graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys.DELAYED_DATA_LOADER_DISPATCHING_EXECUTOR_FACTORY;
149
import static graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_CHAINING;
1510

1611
/**
@@ -364,42 +359,6 @@ public DataloaderConfig enableDataLoaderChaining(boolean enable) {
364359
return this;
365360
}
366361

367-
/**
368-
* @return the batch window duration size for delayed DataLoaders.
369-
*/
370-
public Duration delayedDataLoaderBatchWindowSize() {
371-
Long d = contextConfig.get(DELAYED_DATA_LOADER_BATCH_WINDOW_SIZE_NANO_SECONDS);
372-
return d != null ? Duration.ofNanos(d) : null;
373-
}
374-
375-
/**
376-
* Sets the batch window duration size for delayed DataLoaders.
377-
* That is for DataLoaders, that are not batched as part of the normal per level
378-
* dispatching, because they were created after the level was already dispatched.
379-
*/
380-
@ExperimentalApi
381-
public DataloaderConfig delayedDataLoaderBatchWindowSize(Duration batchWindowSize) {
382-
contextConfig.put(DELAYED_DATA_LOADER_BATCH_WINDOW_SIZE_NANO_SECONDS, batchWindowSize.toNanos());
383-
return this;
384-
}
385-
386-
/**
387-
* @return the instance of {@link DelayedDataLoaderDispatcherExecutorFactory} that is used to create the
388-
* {@link java.util.concurrent.ScheduledExecutorService} for the delayed DataLoader dispatching.
389-
*/
390-
public DelayedDataLoaderDispatcherExecutorFactory delayedDataLoaderExecutorFactory() {
391-
return contextConfig.get(DELAYED_DATA_LOADER_DISPATCHING_EXECUTOR_FACTORY);
392-
}
393-
394-
/**
395-
* Sets the instance of {@link DelayedDataLoaderDispatcherExecutorFactory} that is used to create the
396-
* {@link java.util.concurrent.ScheduledExecutorService} for the delayed DataLoader dispatching.
397-
*/
398-
@ExperimentalApi
399-
public DataloaderConfig delayedDataLoaderExecutorFactory(DelayedDataLoaderDispatcherExecutorFactory delayedDataLoaderDispatcherExecutorFactory) {
400-
contextConfig.put(DELAYED_DATA_LOADER_DISPATCHING_EXECUTOR_FACTORY, delayedDataLoaderDispatcherExecutorFactory);
401-
return this;
402-
}
403362
}
404363

405364
public static class ResponseMapFactoryConfig extends BaseContextConfig {

src/main/java/graphql/Profiler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ default void oldStrategyDispatchingAll(int level) {
4646

4747
default void batchLoadedOldStrategy(String name, int level, int count) {
4848

49-
5049
}
5150

52-
default void batchLoadedNewStrategy(String dataLoaderName, @Nullable Integer level, int count) {
51+
52+
default void batchLoadedNewStrategy(String dataLoaderName, Integer level, int count, boolean delayed, boolean chained) {
5353

5454
}
5555

src/main/java/graphql/ProfilerImpl.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,22 @@ public void oldStrategyDispatchingAll(int level) {
147147

148148
@Override
149149
public void batchLoadedOldStrategy(String name, int level, int count) {
150-
profilerResult.addDispatchEvent(name, level, count, ProfilerResult.DispatchEventType.STRATEGY_DISPATCH);
150+
profilerResult.addDispatchEvent(name, level, count, ProfilerResult.DispatchEventType.LEVEL_STRATEGY_DISPATCH);
151151
}
152152

153153
@Override
154-
public void batchLoadedNewStrategy(String dataLoaderName, @Nullable Integer level, int count) {
155-
profilerResult.addDispatchEvent(dataLoaderName, level, count, ProfilerResult.DispatchEventType.STRATEGY_DISPATCH);
154+
public void batchLoadedNewStrategy(String dataLoaderName, Integer level, int count, boolean delayed, boolean chained) {
155+
ProfilerResult.DispatchEventType dispatchEventType = null;
156+
if (delayed && !chained) {
157+
dispatchEventType = ProfilerResult.DispatchEventType.DELAYED_DISPATCH;
158+
} else if (delayed) {
159+
dispatchEventType = ProfilerResult.DispatchEventType.CHAINED_DELAYED_DISPATCH;
160+
} else if (!chained) {
161+
dispatchEventType = ProfilerResult.DispatchEventType.LEVEL_STRATEGY_DISPATCH;
162+
} else {
163+
dispatchEventType = ProfilerResult.DispatchEventType.CHAINED_STRATEGY_DISPATCH;
164+
}
165+
profilerResult.addDispatchEvent(dataLoaderName, level, count, dispatchEventType);
156166
}
157167

158168
@Override

src/main/java/graphql/ProfilerResult.java

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,20 @@ public void setInstrumentationClasses(List<String> instrumentationClasses) {
6464

6565

6666
public enum DispatchEventType {
67-
STRATEGY_DISPATCH,
67+
LEVEL_STRATEGY_DISPATCH,
68+
CHAINED_STRATEGY_DISPATCH,
69+
DELAYED_DISPATCH,
70+
CHAINED_DELAYED_DISPATCH,
6871
MANUAL_DISPATCH,
6972
}
7073

7174
public static class DispatchEvent {
7275
final String dataLoaderName;
73-
final @Nullable Integer level; // is null for delayed dispatching
76+
final Integer level; // is null for delayed dispatching
7477
final int keyCount; // how many
7578
final DispatchEventType type;
7679

77-
public DispatchEvent(String dataLoaderName, @Nullable Integer level, int keyCount, DispatchEventType type) {
80+
public DispatchEvent(String dataLoaderName, Integer level, int keyCount, DispatchEventType type) {
7881
this.dataLoaderName = dataLoaderName;
7982
this.level = level;
8083
this.keyCount = keyCount;
@@ -85,7 +88,7 @@ public String getDataLoaderName() {
8588
return dataLoaderName;
8689
}
8790

88-
public @Nullable Integer getLevel() {
91+
public Integer getLevel() {
8992
return level;
9093
}
9194

@@ -175,7 +178,7 @@ void oldStrategyDispatchingAll(int level) {
175178
}
176179

177180

178-
void addDispatchEvent(String dataLoaderName, @Nullable Integer level, int count, DispatchEventType type) {
181+
void addDispatchEvent(String dataLoaderName, Integer level, int count, DispatchEventType type) {
179182
dispatchEvents.add(new DispatchEvent(dataLoaderName, level, count, type));
180183
}
181184

@@ -316,34 +319,13 @@ public Map<String, Object> shortSummaryMap() {
316319
}
317320

318321

319-
private String printDispatchEvents() {
320-
if (dispatchEvents.isEmpty()) {
321-
return "[]";
322-
}
323-
StringBuilder sb = new StringBuilder();
324-
sb.append("[");
325-
int i = 0;
326-
for (DispatchEvent event : dispatchEvents) {
327-
sb.append("dataLoader=")
328-
.append(event.getDataLoaderName())
329-
.append(", level=")
330-
.append(event.getLevel())
331-
.append(", count=").append(event.getKeyCount());
332-
if (i++ < dispatchEvents.size() - 1) {
333-
sb.append("; ");
334-
}
335-
}
336-
sb.append("]");
337-
return sb.toString();
338-
}
339-
340322
public List<Map<String, Object>> getDispatchEventsAsMap() {
341323
List<Map<String, Object>> result = new ArrayList<>();
342324
for (DispatchEvent event : dispatchEvents) {
343325
Map<String, Object> eventMap = new LinkedHashMap<>();
344326
eventMap.put("type", event.getType().name());
345327
eventMap.put("dataLoader", event.getDataLoaderName());
346-
eventMap.put("level", event.getLevel() != null ? event.getLevel() : "delayed");
328+
eventMap.put("level", event.getLevel());
347329
eventMap.put("keyCount", event.getKeyCount());
348330
result.add(eventMap);
349331
}

src/main/java/graphql/execution/instrumentation/dataloader/DataLoaderDispatchingContextKeys.java

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import graphql.Internal;
66
import org.jspecify.annotations.NullMarked;
77

8-
import java.time.Duration;
9-
108
/**
119
* GraphQLContext keys related to DataLoader dispatching.
1210
*/
@@ -16,26 +14,6 @@ public final class DataLoaderDispatchingContextKeys {
1614
private DataLoaderDispatchingContextKeys() {
1715
}
1816

19-
/**
20-
* In nano seconds, the batch window size for delayed DataLoaders.
21-
* That is for DataLoaders, that are not batched as part of the normal per level
22-
* dispatching, because they were created after the level was already dispatched.
23-
* <p>
24-
* Expect Long values
25-
* <p>
26-
* Default is 500_000 (0.5 ms)
27-
*/
28-
public static final String DELAYED_DATA_LOADER_BATCH_WINDOW_SIZE_NANO_SECONDS = "__GJ_delayed_data_loader_batch_window_size_nano_seconds";
29-
30-
/**
31-
* An instance of {@link DelayedDataLoaderDispatcherExecutorFactory} that is used to create the
32-
* {@link java.util.concurrent.ScheduledExecutorService} for the delayed DataLoader dispatching.
33-
* <p>
34-
* Default is one static executor thread pool with a single thread.
35-
*/
36-
public static final String DELAYED_DATA_LOADER_DISPATCHING_EXECUTOR_FACTORY = "__GJ_delayed_data_loader_dispatching_executor_factory";
37-
38-
3917
/**
4018
* Enables the ability to chain DataLoader dispatching.
4119
* <p>
@@ -57,27 +35,4 @@ public static void setEnableDataLoaderChaining(GraphQLContext graphQLContext, bo
5735
}
5836

5937

60-
/**
61-
* Sets nanoseconds the batch window duration size for delayed DataLoaders.
62-
* That is for DataLoaders, that are not batched as part of the normal per level
63-
* dispatching, because they were created after the level was already dispatched.
64-
*
65-
* @param graphQLContext
66-
* @param batchWindowSize
67-
*/
68-
public static void setDelayedDataLoaderBatchWindowSize(GraphQLContext graphQLContext, Duration batchWindowSize) {
69-
graphQLContext.put(DELAYED_DATA_LOADER_BATCH_WINDOW_SIZE_NANO_SECONDS, batchWindowSize.toNanos());
70-
}
71-
72-
/**
73-
* Sets the instance of {@link DelayedDataLoaderDispatcherExecutorFactory} that is used to create the
74-
* {@link java.util.concurrent.ScheduledExecutorService} for the delayed DataLoader dispatching.
75-
* <p>
76-
*
77-
* @param graphQLContext
78-
* @param delayedDataLoaderDispatcherExecutorFactory
79-
*/
80-
public static void setDelayedDataLoaderDispatchingExecutorFactory(GraphQLContext graphQLContext, DelayedDataLoaderDispatcherExecutorFactory delayedDataLoaderDispatcherExecutorFactory) {
81-
graphQLContext.put(DELAYED_DATA_LOADER_DISPATCHING_EXECUTOR_FACTORY, delayedDataLoaderDispatcherExecutorFactory);
82-
}
8338
}

0 commit comments

Comments
 (0)