Class ExecutorInstrumentation
- java.lang.Object
-
- graphql.execution.instrumentation.SimplePerformantInstrumentation
-
- graphql.execution.instrumentation.threadpools.ExecutorInstrumentation
-
- All Implemented Interfaces:
Instrumentation
@Beta public class ExecutorInstrumentation extends SimplePerformantInstrumentation
This instrumentation can be used to control on what thread calls toDataFetcher
s happen on.If your data fetching is inherently IO bound then you could use a IO oriented thread pool for your fetches and transfer control back to a CPU oriented thread pool and allow graphql-java code to run the post-processing of results there.
An IO oriented thread pool is typically a multiple of
Runtime.availableProcessors()
while a CPU oriented thread pool is typically no more thanRuntime.availableProcessors()
.The instrumentation will use the
Instrumentation.instrumentDataFetcher(DataFetcher, InstrumentationFieldFetchParameters, InstrumentationState)
method to change your data fetchers, so they are executed on a thread pool dedicated to fetching (if you provide one).Once the data fetcher value is returns it will transfer control back to a processing thread pool (if you provide one).
This code uses
CompletableFuture.supplyAsync(Supplier, Executor)
andCompletableFuture.thenApplyAsync(Function, Executor)
to transfer control between thread pools.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
ExecutorInstrumentation.Action
This describes what action is currently being done.static class
ExecutorInstrumentation.Builder
-
Field Summary
Fields Modifier and Type Field Description private java.util.function.Consumer<ExecutorInstrumentation.Action>
actionObserver
private java.util.concurrent.Executor
fetchExecutor
private static java.util.function.Consumer<ExecutorInstrumentation.Action>
NOOP
private java.util.concurrent.Executor
processingExecutor
-
Fields inherited from class graphql.execution.instrumentation.SimplePerformantInstrumentation
INSTANCE
-
-
Constructor Summary
Constructors Modifier Constructor Description private
ExecutorInstrumentation(java.util.concurrent.Executor fetchExecutor, java.util.concurrent.Executor processingExecutor, java.util.function.Consumer<ExecutorInstrumentation.Action> actionObserver)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.concurrent.Executor
getFetchExecutor()
java.util.concurrent.Executor
getProcessingExecutor()
@NotNull DataFetcher<?>
instrumentDataFetcher(DataFetcher<?> originalDataFetcher, InstrumentationFieldFetchParameters parameters, InstrumentationState state)
This is called to instrument aDataFetcher
just before it is used to fetch a field, allowing you to adjust what information is passed back or record information about specific data fetches.private java.util.function.Supplier<java.util.concurrent.CompletionStage<?>>
invokedAsync(DataFetcher<?> originalDataFetcher, DataFetchingEnvironment environment)
private java.util.concurrent.CompletableFuture<java.util.concurrent.CompletionStage<?>>
invokedSync(DataFetcher<?> originalDataFetcher, DataFetchingEnvironment environment)
private java.util.concurrent.CompletionStage<?>
invokeOriginalDF(DataFetcher<?> originalDataFetcher, DataFetchingEnvironment environment)
static ExecutorInstrumentation.Builder
newThreadPoolExecutionInstrumentation()
private java.util.function.Function<java.util.concurrent.CompletionStage<?>,java.util.concurrent.CompletionStage<?>>
processingControl()
-
Methods inherited from class graphql.execution.instrumentation.SimplePerformantInstrumentation
beginExecuteOperation, beginExecuteOperation, beginExecution, beginExecution, beginExecutionStrategy, beginExecutionStrategy, beginField, beginField, beginFieldComplete, beginFieldComplete, beginFieldFetch, beginFieldFetch, beginFieldListComplete, beginFieldListComplete, beginParse, beginParse, beginSubscribedFieldEvent, beginSubscribedFieldEvent, beginValidation, beginValidation, createState, createState, createStateAsync, instrumentDataFetcher, instrumentDocumentAndVariables, instrumentDocumentAndVariables, instrumentExecutionContext, instrumentExecutionContext, instrumentExecutionInput, instrumentExecutionInput, instrumentExecutionResult, instrumentExecutionResult, instrumentSchema, instrumentSchema
-
-
-
-
Field Detail
-
NOOP
private static final java.util.function.Consumer<ExecutorInstrumentation.Action> NOOP
-
fetchExecutor
private final java.util.concurrent.Executor fetchExecutor
-
processingExecutor
private final java.util.concurrent.Executor processingExecutor
-
actionObserver
private final java.util.function.Consumer<ExecutorInstrumentation.Action> actionObserver
-
-
Constructor Detail
-
ExecutorInstrumentation
private ExecutorInstrumentation(java.util.concurrent.Executor fetchExecutor, java.util.concurrent.Executor processingExecutor, java.util.function.Consumer<ExecutorInstrumentation.Action> actionObserver)
-
-
Method Detail
-
getFetchExecutor
public java.util.concurrent.Executor getFetchExecutor()
-
getProcessingExecutor
public java.util.concurrent.Executor getProcessingExecutor()
-
newThreadPoolExecutionInstrumentation
public static ExecutorInstrumentation.Builder newThreadPoolExecutionInstrumentation()
-
instrumentDataFetcher
@NotNull public @NotNull DataFetcher<?> instrumentDataFetcher(DataFetcher<?> originalDataFetcher, InstrumentationFieldFetchParameters parameters, InstrumentationState state)
Description copied from interface:Instrumentation
This is called to instrument aDataFetcher
just before it is used to fetch a field, allowing you to adjust what information is passed back or record information about specific data fetches. Note the same data fetcher instance maybe presented to you many times and that data fetcher implementations widely vary.- Specified by:
instrumentDataFetcher
in interfaceInstrumentation
- Overrides:
instrumentDataFetcher
in classSimplePerformantInstrumentation
- Parameters:
originalDataFetcher
- the data fetcher about to be usedparameters
- the parameters describing the field to be fetchedstate
- the state created during the call toInstrumentation.createState(InstrumentationCreateStateParameters)
- Returns:
- a non null instrumented DataFetcher, the default is to return to the same object
-
invokedAsync
private java.util.function.Supplier<java.util.concurrent.CompletionStage<?>> invokedAsync(DataFetcher<?> originalDataFetcher, DataFetchingEnvironment environment)
-
invokedSync
private java.util.concurrent.CompletableFuture<java.util.concurrent.CompletionStage<?>> invokedSync(DataFetcher<?> originalDataFetcher, DataFetchingEnvironment environment)
-
processingControl
private java.util.function.Function<java.util.concurrent.CompletionStage<?>,java.util.concurrent.CompletionStage<?>> processingControl()
-
invokeOriginalDF
private java.util.concurrent.CompletionStage<?> invokeOriginalDF(DataFetcher<?> originalDataFetcher, DataFetchingEnvironment environment)
-
-