Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
0a27e76
introducing of a special CompletableFuture enabling scheduling of nes…
andimarek Mar 30, 2025
f655893
replace countdown latch with an async solution
andimarek Mar 30, 2025
3675d8a
wip
andimarek Mar 30, 2025
c2d0676
refactor
andimarek Mar 31, 2025
36b2d64
refactor
andimarek Mar 31, 2025
eb8f9bb
naming
andimarek Mar 31, 2025
d1df820
comment
andimarek Mar 31, 2025
2c5da50
fix test
andimarek Mar 31, 2025
62325eb
green tests without any chain
andimarek Apr 1, 2025
89f2c48
cleanup
andimarek Apr 1, 2025
67df3a9
use ResultPath to identify a field instead DFE
andimarek Apr 1, 2025
dc8ac9a
fix a bug in the PerLevel dispatcher regarding which level to dispatch
andimarek Apr 1, 2025
3be8443
fix test
andimarek Apr 1, 2025
0686385
don't put ExecutionStrategy into context
andimarek Apr 1, 2025
bcfeff8
introduce toInternal method in DFE
andimarek Apr 1, 2025
bf0b730
Merge branch 'master' into dataloader-cfs
andimarek Apr 2, 2025
89b8c13
use new DataLoaderDelegate
andimarek Apr 2, 2025
6d02760
Merge branch 'master' into dataloader-cfs
andimarek Apr 4, 2025
5e00964
update dataloader
andimarek Apr 4, 2025
ce4d3f6
make batch window configurable and make one test more stable
andimarek Apr 4, 2025
401d09d
scheduled executor can be configured for delayed dispatching
andimarek Apr 4, 2025
2a29686
new handling of Dataloders can be disabled
andimarek Apr 4, 2025
e08ca66
Merge branch 'master' into dataloader-cfs
andimarek Apr 7, 2025
42d1d1d
more stuff
andimarek Apr 7, 2025
24d4687
Merge branch 'master' into dataloader-cfs
andimarek Apr 7, 2025
51efbfc
fix mutations with DataLoader
andimarek Apr 7, 2025
6e0da52
testing and cleanup
andimarek Apr 7, 2025
12fe329
green tests
andimarek Apr 8, 2025
e3539c7
disable new chaining algo by default
andimarek Apr 8, 2025
2369db5
cleanup
andimarek Apr 8, 2025
861a317
cleanup
andimarek Apr 8, 2025
1f4d18e
some performance,naming,cleanup, docs
andimarek Apr 9, 2025
efa8fb0
fix an engine state edge case
andimarek Apr 10, 2025
f70630d
fix an engine state edge case
andimarek Apr 10, 2025
e759137
change list implementation
andimarek Apr 10, 2025
c20ed90
Merge branch 'master' into dataloader-cfs
andimarek Apr 11, 2025
bcfb9ef
fix dispatching edge case
andimarek Apr 11, 2025
572fbdf
data loader performance test with chaining on
andimarek Apr 11, 2025
6c3d14c
data loader performance test with chaining on
andimarek Apr 11, 2025
ee1541d
make result path to String faster
andimarek Apr 14, 2025
0d76d31
Merge branch 'master' into dataloader-cfs
andimarek Apr 15, 2025
47300cb
fix merge problem
andimarek Apr 15, 2025
fed7456
Merge branch 'master' into dataloader-cfs
andimarek May 1, 2025
d57d4fd
naming
andimarek May 1, 2025
34bc3d7
add helper methods to configure new dispatching strategy
andimarek May 1, 2025
604f679
implement toInternal by default
andimarek May 1, 2025
6328062
formatting
andimarek May 1, 2025
65cc42a
cleanup
andimarek May 1, 2025
51f9344
cleanup
andimarek May 2, 2025
d1384e1
cleanup
andimarek May 2, 2025
5b04714
cleanup
andimarek May 2, 2025
a622d0d
PR feedback
andimarek May 2, 2025
cdb335f
PR feedback
andimarek May 2, 2025
81c4b09
PR feedback
andimarek May 2, 2025
4e0c26a
Merge branch 'master' into dataloader-cfs
andimarek May 16, 2025
c9a3ff3
merging
andimarek May 16, 2025
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
Prev Previous commit
Next Next commit
more stuff
  • Loading branch information
andimarek committed Apr 7, 2025
commit 42d1d1d28a2e27dad219f23db8af8e97c48eb34f
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,18 @@ private static class CallStack {

private final Set<Integer> dispatchedLevels = ConcurrentHashMap.newKeySet();

// fields only relevant when a DataLoaderCF is involved
private final List<ResultPathWithDataLoader> allResultPathWithDataLoader = new CopyOnWriteArrayList<>();


//TODO: maybe this should be cleaned up once the CF returned by these fields are completed
// otherwise this will stick around until the whole request is finished

private final List<ResultPathWithDataLoader> allResultPathWithDataLoader = new CopyOnWriteArrayList<>();
// used for per level dispatching
private final Map<Integer, Set<ResultPathWithDataLoader>> levelToResultPathWithDataLoader = new ConcurrentHashMap<>();

private final Set<Integer> dispatchingStartedPerLevel = ConcurrentHashMap.newKeySet();
private final Set<Integer> dispatchingFinishedPerLevel = ConcurrentHashMap.newKeySet();
// Set of ResultPath
private final Set<String> batchWindowOfDelayedDataLoaderToDispatch = ConcurrentHashMap.newKeySet();

private boolean batchWindowOpen = false;
Expand All @@ -73,7 +79,7 @@ public CallStack() {
expectedExecuteObjectCallsPerLevel.set(1, 1);
}

public void addDataLoaderDFE(int level, ResultPathWithDataLoader resultPathWithDataLoader) {
public void addResultPathWithDataLoader(int level, ResultPathWithDataLoader resultPathWithDataLoader) {
levelToResultPathWithDataLoader.computeIfAbsent(level, k -> new LinkedHashSet<>()).add(resultPathWithDataLoader);
}

Expand Down Expand Up @@ -347,19 +353,24 @@ void dispatch(int level) {

Set<ResultPathWithDataLoader> resultPathWithDataLoaders = callStack.levelToResultPathWithDataLoader.get(level);
if (resultPathWithDataLoaders != null) {
dispatchDLCFImpl(resultPathWithDataLoaders
.stream()
.map(resultPathWithDataLoader -> resultPathWithDataLoader.resultPath)
.collect(Collectors.toSet()), true);
Set<String> resultPathToDispatch = callStack.lock.callLocked(() -> {
callStack.dispatchingStartedPerLevel.add(level);
return resultPathWithDataLoaders
.stream()
.map(resultPathWithDataLoader -> resultPathWithDataLoader.resultPath)
.collect(Collectors.toSet());
});
dispatchDLCFImpl(resultPathToDispatch, true, level);
} else {
// TODO: this is questionable if we should do that: we didn't find any DataLoaders in that level
callStack.dispatchingFinishedPerLevel.add(level);
// TODO: this is questionable if we should do that: we didn't find any DataLoaders in that level,
DataLoaderRegistry dataLoaderRegistry = executionContext.getDataLoaderRegistry();
dataLoaderRegistry.dispatchAll();
}
}


public void dispatchDLCFImpl(Set<String> resultPathsToDispatch, boolean dispatchAll) {
public void dispatchDLCFImpl(Set<String> resultPathsToDispatch, boolean dispatchAll, Integer level) {

// filter out all DataLoaderCFS that are matching the fields we want to dispatch
List<ResultPathWithDataLoader> relevantResultPathWithDataLoader = new ArrayList<>();
Expand All @@ -371,43 +382,52 @@ public void dispatchDLCFImpl(Set<String> resultPathsToDispatch, boolean dispatch
// we are cleaning up the list of all DataLoadersCFs
callStack.allResultPathWithDataLoader.removeAll(relevantResultPathWithDataLoader);

Set<Integer> levelsToDispatch = relevantResultPathWithDataLoader.stream()
.map(resultPathWithDataLoader -> resultPathWithDataLoader.level)
.collect(Collectors.toSet());


// means we are all done dispatching the fields
if (relevantResultPathWithDataLoader.size() == 0) {
if (level != null) {
callStack.dispatchingFinishedPerLevel.add(level);
}
return;
}
List<CompletableFuture> allDispatchedCFs = new ArrayList<>();
if (dispatchAll) {
// if we have a mixed world with old and new DataLoaderCFs we dispatch all DataLoaders to retain compatibility
for (DataLoader dl : executionContext.getDataLoaderRegistry().getDataLoaders()) {
allDispatchedCFs.add(dl.dispatch());
}
} else {
// Only dispatching relevant data loaders
for (ResultPathWithDataLoader resultPathWithDataLoader : relevantResultPathWithDataLoader) {
allDispatchedCFs.add(resultPathWithDataLoader.dataLoader.dispatch());
}
}
CompletableFuture.allOf(allDispatchedCFs.toArray(new CompletableFuture[0]))
.whenComplete((unused, throwable) -> {
dispatchDLCFImpl(resultPathsToDispatch, false);
dispatchDLCFImpl(resultPathsToDispatch, false, level);
}
);

}


public void newDataLoaderCF(String resultPath, int level, DataLoader dataLoader) {
public void newDataLoaderLoadCall(String resultPath, int level, DataLoader dataLoader) {
if (disableNewDataLoaderDispatching) {
return;
}
ResultPathWithDataLoader resultPathWithDataLoader = new ResultPathWithDataLoader(resultPath, level, dataLoader);
callStack.lock.runLocked(() -> {
boolean levelFinished = callStack.lock.callLocked(() -> {
boolean finished = callStack.dispatchingFinishedPerLevel.contains(level);
callStack.allResultPathWithDataLoader.add(resultPathWithDataLoader);
if (!callStack.dispatchedLevels.contains(level)) {
callStack.addDataLoaderDFE(level, resultPathWithDataLoader);
// only add to the list of DataLoader for this level if we are not already dispatching
if (!callStack.dispatchingStartedPerLevel.contains(level)) {
callStack.addResultPathWithDataLoader(level, resultPathWithDataLoader);
}
return finished;
});
if (callStack.dispatchedLevels.contains(level)) {
if (levelFinished) {
dispatchDelayedDataLoader(resultPathWithDataLoader);
}

Expand All @@ -426,13 +446,9 @@ private void dispatchDelayedDataLoader(ResultPathWithDataLoader resultPathWithDa
callStack.batchWindowOfDelayedDataLoaderToDispatch.clear();
callStack.batchWindowOpen = false;
});
dispatchDLCFImpl(dfesToDispatch.get(), false);
dispatchDLCFImpl(dfesToDispatch.get(), false, null);
};
try {
delayedDataLoaderDispatchExecutor.get().schedule(runnable, this.batchWindowNs, TimeUnit.NANOSECONDS);
} catch (Exception e) {
e.printStackTrace();
}
delayedDataLoaderDispatchExecutor.get().schedule(runnable, this.batchWindowNs, TimeUnit.NANOSECONDS);
}

});
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/graphql/schema/DataLoaderWithContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CompletableFuture<V> load(@NonNull K key, @Nullable Object keyContext) {
String path = dfe.getExecutionStepInfo().getPath().toString();
DataFetchingEnvironmentImpl.DFEInternalState dfeInternalState = (DataFetchingEnvironmentImpl.DFEInternalState) dfeImpl.toInternal();
if (dfeInternalState.getDataLoaderDispatchStrategy() instanceof PerLevelDataLoaderDispatchStrategy) {
((PerLevelDataLoaderDispatchStrategy) dfeInternalState.dataLoaderDispatchStrategy).newDataLoaderCF(path, level, delegate);
((PerLevelDataLoaderDispatchStrategy) dfeInternalState.dataLoaderDispatchStrategy).newDataLoaderLoadCall(path, level, delegate);
}
return super.load(key, keyContext);
}
Expand Down