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
scheduled executor can be configured for delayed dispatching
  • Loading branch information
andimarek committed Apr 4, 2025
commit 401d09d40da0964916ed47ad39a35b05b491c8ad
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package graphql.execution.instrumentation.dataloader;

import graphql.ExperimentalApi;
import graphql.GraphQLContext;
import graphql.execution.ExecutionId;
import org.jspecify.annotations.NullMarked;

import java.util.concurrent.ScheduledExecutorService;

@ExperimentalApi
@NullMarked
@FunctionalInterface
public interface DelayedDataLoaderDispatcherExecutorFactory {

/**
* Called once per execution to create the {@link ScheduledExecutorService} for the delayed DataLoader dispatching.
*
* Will only called if needed, i.e. if there are delayed DataLoaders.
*
* @param executionId
* @param graphQLContext
*
* @return
*/
ScheduledExecutorService createExecutor(ExecutionId executionId, GraphQLContext graphQLContext);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@


import graphql.ExperimentalApi;
import org.jspecify.annotations.NullMarked;

@ExperimentalApi
@NullMarked
public final class DispatchingContextKeys {
private DispatchingContextKeys() {
}
Expand All @@ -12,6 +14,18 @@ private DispatchingContextKeys() {
* In nano seconds, the batch window size for delayed DataLoaders.
* That is for DataLoaders, that are not batched as part of the normal per level
* dispatching, because they were created after the level was already dispatched.
*
* Expect Integer values
*
* Default is 500_000 (0.5 ms)
*/
public static final String BATCH_WINDOW_DELAYED_DL_NANO_SECONDS = "__batch_window_delayed_dl_nano_seconds";
public static final String DELAYED_DATA_LOADER_BATCH_WINDOW_SIZE_NANO_SECONDS = "__GJ_delayed_data_loader_batch_window_size_nano_seconds";

/**
* An instance of {@link DelayedDataLoaderDispatcherExecutorFactory} that is used to create the
* {@link java.util.concurrent.ScheduledExecutorService} for the delayed DataLoader dispatching.
*
* Default is one static executor thread pool with a single thread.
*/
public static final String DELAYED_DATA_LOADER_DISPATCHING_EXECUTOR_FACTORY = "__GJ_delayed_data_loader_dispatching_executor_factory";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import graphql.execution.FieldValueInfo;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.util.InterThreadMemoizedSupplier;
import graphql.util.LockKit;
import org.dataloader.DataLoader;
import org.dataloader.DataLoaderRegistry;
Expand All @@ -34,8 +35,12 @@ public class PerLevelDataLoaderDispatchStrategy implements DataLoaderDispatchStr
private final ExecutionContext executionContext;
private final int batchWindowNs;

static final ScheduledExecutorService delayedDLCFBatchWindowScheduler = Executors.newSingleThreadScheduledExecutor();
static final int BATCH_WINDOW_NANO_SECONDS_DEFAULT = 500_000;
private final InterThreadMemoizedSupplier<ScheduledExecutorService> delayedDataLoaderDispatchExecutor;

static final InterThreadMemoizedSupplier<ScheduledExecutorService> defaultDelayedDLCFBatchWindowScheduler
= new InterThreadMemoizedSupplier<>(Executors::newSingleThreadScheduledExecutor);

static final int DEFAULT_BATCH_WINDOW_NANO_SECONDS_DEFAULT = 500_000;


private static class CallStack {
Expand Down Expand Up @@ -154,12 +159,20 @@ public PerLevelDataLoaderDispatchStrategy(ExecutionContext executionContext) {
this.callStack = new CallStack();
this.executionContext = executionContext;

Integer batchWindowNs = executionContext.getGraphQLContext().get(DispatchingContextKeys.BATCH_WINDOW_DELAYED_DL_NANO_SECONDS);
Integer batchWindowNs = executionContext.getGraphQLContext().get(DispatchingContextKeys.DELAYED_DATA_LOADER_BATCH_WINDOW_SIZE_NANO_SECONDS);
if (batchWindowNs != null) {
this.batchWindowNs = batchWindowNs;
} else {
this.batchWindowNs = BATCH_WINDOW_NANO_SECONDS_DEFAULT;
this.batchWindowNs = DEFAULT_BATCH_WINDOW_NANO_SECONDS_DEFAULT;
}

this.delayedDataLoaderDispatchExecutor = new InterThreadMemoizedSupplier<>(() -> {
DelayedDataLoaderDispatcherExecutorFactory delayedDataLoaderDispatcherExecutorFactory = executionContext.getGraphQLContext().get(DispatchingContextKeys.DELAYED_DATA_LOADER_DISPATCHING_EXECUTOR_FACTORY);
if (delayedDataLoaderDispatcherExecutorFactory != null) {
return delayedDataLoaderDispatcherExecutorFactory.createExecutor(executionContext.getExecutionId(), executionContext.getGraphQLContext());
}
return defaultDelayedDLCFBatchWindowScheduler.get();
});
}

@Override
Expand Down Expand Up @@ -320,11 +333,9 @@ private boolean levelReady(int level) {
}

void dispatch(int level) {
System.out.println("dispatching level " + level);
// if we have any DataLoaderCFs => use new Algorithm
Set<ResultPathWithDataLoader> resultPathWithDataLoaders = callStack.levelToResultPathWithDataLoader.get(level);
if (resultPathWithDataLoaders != null) {
System.out.println("dispatching level " + level + " with " + resultPathWithDataLoaders.size() + " DataLoaderCFs" + " this: " + this);
dispatchDLCFImpl(resultPathWithDataLoaders
.stream()
.map(resultPathWithDataLoader -> resultPathWithDataLoader.resultPath)
Expand Down Expand Up @@ -375,22 +386,15 @@ public void dispatchDLCFImpl(Set<String> resultPathsToDispatch, boolean dispatch


public void newDataLoaderCF(String resultPath, int level, DataLoader dataLoader) {
System.out.println("newDataLoaderCF");
ResultPathWithDataLoader resultPathWithDataLoader = new ResultPathWithDataLoader(resultPath, level, dataLoader);
callStack.lock.runLocked(() -> {
callStack.allResultPathWithDataLoader.add(resultPathWithDataLoader);
if (!callStack.dispatchedLevels.contains(level)) {
System.out.println("not finished dispatching level " + level);
callStack.addDataLoaderDFE(level, resultPathWithDataLoader);
} else {
System.out.println("already finished dispatching level " + level);
}
});
if (callStack.dispatchedLevels.contains(level)) {
System.out.println("delayed dispatch");
dispatchDelayedDataLoader(resultPathWithDataLoader);
} else {
System.out.println("normal dispatch");
}


Expand All @@ -408,16 +412,18 @@ private void dispatchDelayedDataLoader(ResultPathWithDataLoader resultPathWithDa
callStack.batchWindowOfDelayedDataLoaderToDispatch.clear();
callStack.batchWindowOpen = false;
});
System.out.println("start dispatch with " + dfesToDispatch.get().size());
dispatchDLCFImpl(dfesToDispatch.get(), false);
};
delayedDLCFBatchWindowScheduler.schedule(runnable, this.batchWindowNs, TimeUnit.NANOSECONDS);
try {
delayedDataLoaderDispatchExecutor.get().schedule(runnable, this.batchWindowNs, TimeUnit.NANOSECONDS);
} catch (Exception e) {
e.printStackTrace();
}
}

});
}


private static class ResultPathWithDataLoader {
final String resultPath;
final int level;
Expand Down
66 changes: 65 additions & 1 deletion src/test/groovy/graphql/ChainedDataLoaderTest.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package graphql


import graphql.execution.ExecutionId
import graphql.execution.instrumentation.dataloader.DelayedDataLoaderDispatcherExecutorFactory
import graphql.execution.instrumentation.dataloader.DispatchingContextKeys
import graphql.schema.DataFetcher
import org.dataloader.BatchLoader
Expand All @@ -8,6 +11,9 @@ import org.dataloader.DataLoaderFactory
import org.dataloader.DataLoaderRegistry
import spock.lang.Specification

import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger

import static graphql.ExecutionInput.newExecutionInput
Expand Down Expand Up @@ -276,7 +282,7 @@ class ChainedDataLoaderTest extends Specification {
def ei = newExecutionInput(query).dataLoaderRegistry(dataLoaderRegistry).build()

// make the window large enough to avoid flaky tests
ei.getGraphQLContext().put(DispatchingContextKeys.BATCH_WINDOW_DELAYED_DL_NANO_SECONDS, 2_000_000)
ei.getGraphQLContext().put(DispatchingContextKeys.DELAYED_DATA_LOADER_BATCH_WINDOW_SIZE_NANO_SECONDS, 2_000_000)

when:
def er = graphQL.execute(ei)
Expand All @@ -285,5 +291,63 @@ class ChainedDataLoaderTest extends Specification {
batchLoadCalls.get() == 1
}

def "executor for delayed dispatching can be configured"() {
given:
def sdl = '''

type Query {
foo: String
bar: String
}
'''
BatchLoader<String, String> batchLoader = { keys ->
return supplyAsync {
Thread.sleep(250)
return keys;
}
}

DataLoader<String, String> nameDataLoader = DataLoaderFactory.newDataLoader(batchLoader);

DataLoaderRegistry dataLoaderRegistry = new DataLoaderRegistry();
dataLoaderRegistry.register("dl", nameDataLoader);

def fooDF = { env ->
return supplyAsync {
Thread.sleep(1000)
return "fooFirstValue"
}.thenCompose {
return env.getDataLoader("dl").load(it)
}
} as DataFetcher


def fetchers = ["Query": ["foo": fooDF]]
def schema = TestUtil.schema(sdl, fetchers)
def graphQL = GraphQL.newGraphQL(schema).build()

def query = "{ foo } "
def ei = newExecutionInput(query).dataLoaderRegistry(dataLoaderRegistry).build()


ScheduledExecutorService scheduledExecutorService = Mock()
ei.getGraphQLContext().put(DispatchingContextKeys.DELAYED_DATA_LOADER_DISPATCHING_EXECUTOR_FACTORY, new DelayedDataLoaderDispatcherExecutorFactory() {
@Override
ScheduledExecutorService createExecutor(ExecutionId executionId, GraphQLContext graphQLContext) {
return scheduledExecutorService
}
})


when:
def er = graphQL.execute(ei)

then:
er.data == [foo: "fooFirstValue"]
1 * scheduledExecutorService.schedule(_ as Runnable, _ as Long, _ as TimeUnit) >> { Runnable runnable, Long delay, TimeUnit timeUnit ->
return Executors.newSingleThreadScheduledExecutor().schedule(runnable, delay, timeUnit)
}

}

}