diff --git a/src/jmh/java/performance/PerLevelDispatchOverheadPerformance.java b/src/jmh/java/performance/PerLevelDispatchOverheadPerformance.java new file mode 100644 index 0000000000..193680a180 --- /dev/null +++ b/src/jmh/java/performance/PerLevelDispatchOverheadPerformance.java @@ -0,0 +1,145 @@ +package performance; + +import graphql.Assert; +import graphql.ExecutionInput; +import graphql.ExecutionResult; +import graphql.GraphQL; +import graphql.schema.DataFetcher; +import graphql.schema.GraphQLSchema; +import graphql.schema.idl.RuntimeWiring; +import graphql.schema.idl.SchemaGenerator; +import graphql.schema.idl.SchemaParser; +import org.dataloader.DataLoaderFactory; +import org.dataloader.DataLoaderRegistry; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; +import org.openjdk.jmh.annotations.Warmup; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +/** + * Isolates the total cost of {@code PerLevelDataLoaderDispatchStrategy} bookkeeping. + *

+ * The same wide query runs with identical data fetchers (none of them use a DataLoader) in two + * modes: {@code none} executes without a {@code DataLoaderRegistry}, so the engine uses the no-op + * dispatch strategy; {@code perLevel} passes a registry holding one unused loader, which switches + * the engine to {@code PerLevelDataLoaderDispatchStrategy}. The difference between the two scores + * is everything the strategy costs for this query shape. + */ +@State(Scope.Benchmark) +@Warmup(iterations = 3, time = 3) +@Measurement(iterations = 5, time = 3) +@Fork(2) +public class PerLevelDispatchOverheadPerformance { + + private static final String SDL = "" + + "type Query { items(count: Int!): [Item!]! }\n" + + "type Item { id: ID! a: String b: String c: String d: String e: String child: Child }\n" + + "type Child { id: ID! x: String y: String grand: [Grand!]! }\n" + + "type Grand { v: String w: String }\n"; + + private static final int GRANDCHILDREN_PER_CHILD = 5; + + @Param({"250"}) + public int count; + + @Param({"false", "true"}) + public boolean async; + + @Param({"none", "perLevel"}) + public String strategy; + + private GraphQL graphQL; + private String query; + private ExecutorService pool; + + @Setup(Level.Trial) + public void setup() { + pool = Executors.newFixedThreadPool(4); + + DataFetcher itemsDf = env -> { + int n = env.getArgument("count"); + List> items = new ArrayList<>(n); + for (int i = 0; i < n; i++) { + Map item = new LinkedHashMap<>(); + item.put("id", "I-" + i); + item.put("a", "a" + i); + item.put("b", "b" + i); + item.put("c", "c" + i); + item.put("d", "d" + i); + item.put("e", "e" + i); + items.add(item); + } + return items; + }; + DataFetcher asyncScalarDf = env -> { + Map item = env.getSource(); + Object value = item.get("a"); + return async ? CompletableFuture.supplyAsync(() -> value, pool) : value; + }; + DataFetcher childDf = env -> { + Map item = env.getSource(); + return Map.of("id", "C-" + item.get("id"), "x", "x", "y", "y"); + }; + DataFetcher grandDf = env -> { + List> grand = new ArrayList<>(GRANDCHILDREN_PER_CHILD); + for (int i = 0; i < GRANDCHILDREN_PER_CHILD; i++) { + grand.add(Map.of("v", "v" + i, "w", "w" + i)); + } + return grand; + }; + + RuntimeWiring wiring = RuntimeWiring.newRuntimeWiring() + .type("Query", b -> b.dataFetcher("items", itemsDf)) + .type("Item", b -> b.dataFetcher("a", asyncScalarDf).dataFetcher("child", childDf)) + .type("Child", b -> b.dataFetcher("grand", grandDf)) + .build(); + GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(new SchemaParser().parse(SDL), wiring); + graphQL = GraphQL.newGraphQL(schema).build(); + query = "{ items(count: " + count + ") { id a b c d e child { id x y grand { v w } } } }"; + + Map> data = execute().getData(); + Assert.assertTrue(data.get("items").size() == count, () -> "items: " + data); + } + + @TearDown(Level.Trial) + public void tearDown() { + pool.shutdownNow(); + } + + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MICROSECONDS) + public ExecutionResult executeWideQuery() { + return execute(); + } + + private ExecutionResult execute() { + ExecutionInput.Builder input = ExecutionInput.newExecutionInput().query(query); + if (strategy.equals("perLevel")) { + input.dataLoaderRegistry(DataLoaderRegistry.newRegistry() + .register("unused", DataLoaderFactory.newDataLoader(keys -> CompletableFuture.completedFuture(keys))) + .build()); + } + ExecutionResult result = graphQL.execute(input.build()); + Assert.assertTrue(result.getErrors().isEmpty(), () -> "errors: " + result.getErrors()); + return result; + } +} diff --git a/src/jmh/java/performance/PerLevelDispatchPerformance.java b/src/jmh/java/performance/PerLevelDispatchPerformance.java new file mode 100644 index 0000000000..58c383368e --- /dev/null +++ b/src/jmh/java/performance/PerLevelDispatchPerformance.java @@ -0,0 +1,164 @@ +package performance; + +import graphql.Assert; +import graphql.ExecutionInput; +import graphql.ExecutionResult; +import graphql.GraphQL; +import graphql.schema.DataFetcher; +import graphql.schema.GraphQLSchema; +import graphql.schema.idl.RuntimeWiring; +import graphql.schema.idl.SchemaGenerator; +import graphql.schema.idl.SchemaParser; +import org.dataloader.BatchLoader; +import org.dataloader.DataLoader; +import org.dataloader.DataLoaderFactory; +import org.dataloader.DataLoaderRegistry; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.infra.Blackhole; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +/** + * Measures the per-field bookkeeping of {@code PerLevelDataLoaderDispatchStrategy}, which is the + * strategy graphql-java uses whenever a {@code DataLoaderRegistry} is present. + *

+ * The query is wide rather than deep: {@code count} list items, each with scalar fields, a + * DataLoader-backed child object and a list of grandchildren, so the strategy's + * {@code executeObject} / {@code onCompletionFinished} / readiness checks run thousands of times + * per request while the data fetchers themselves do almost nothing. + *

+ * With {@code async = true} a scalar field on every item and the child batch loader complete on a + * small thread pool, so object completions for the same level race on the strategy's per-level + * state from several threads, as they do in a server whose fetchers resume on worker threads. + */ +@State(Scope.Benchmark) +@Warmup(iterations = 3, time = 3) +@Measurement(iterations = 5, time = 3) +@Fork(2) +public class PerLevelDispatchPerformance { + + private static final String SDL = "" + + "type Query { items(count: Int!): [Item!]! }\n" + + "type Item { id: ID! a: String b: String c: String d: String e: String child: Child }\n" + + "type Child { id: ID! x: String y: String grand: [Grand!]! }\n" + + "type Grand { v: String w: String }\n"; + + private static final int GRANDCHILDREN_PER_CHILD = 5; + + @Param({"50", "250"}) + public int count; + + @Param({"false", "true"}) + public boolean async; + + private GraphQL graphQL; + private String query; + private ExecutorService pool; + + @Setup(Level.Trial) + public void setup() { + pool = Executors.newFixedThreadPool(4); + + DataFetcher itemsDf = env -> { + int n = env.getArgument("count"); + List> items = new ArrayList<>(n); + for (int i = 0; i < n; i++) { + Map item = new LinkedHashMap<>(); + item.put("id", "I-" + i); + item.put("a", "a" + i); + item.put("b", "b" + i); + item.put("c", "c" + i); + item.put("d", "d" + i); + item.put("e", "e" + i); + items.add(item); + } + return items; + }; + DataFetcher asyncScalarDf = env -> { + Map item = env.getSource(); + Object value = item.get("a"); + return async ? CompletableFuture.supplyAsync(() -> value, pool) : value; + }; + DataFetcher childDf = env -> { + Map item = env.getSource(); + DataLoader> loader = env.getDataLoader("child"); + return loader.load((String) item.get("id")); + }; + DataFetcher grandDf = env -> { + List> grand = new ArrayList<>(); + for (int i = 0; i < GRANDCHILDREN_PER_CHILD; i++) { + grand.add(Map.of("v", "v" + i, "w", "w" + i)); + } + return grand; + }; + + RuntimeWiring wiring = RuntimeWiring.newRuntimeWiring() + .type("Query", b -> b.dataFetcher("items", itemsDf)) + .type("Item", b -> b.dataFetcher("a", asyncScalarDf).dataFetcher("child", childDf)) + .type("Child", b -> b.dataFetcher("grand", grandDf)) + .build(); + GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(new SchemaParser().parse(SDL), wiring); + graphQL = GraphQL.newGraphQL(schema).build(); + query = "{ items(count: " + count + ") { id a b c d e child { id x y grand { v w } } } }"; + + Map>>>> data = execute().getData(); + List>>> items = data.get("items"); + Assert.assertTrue(items.size() == count, () -> "items: " + items.size()); + Assert.assertTrue(items.get(count - 1).get("child").get("grand").size() == GRANDCHILDREN_PER_CHILD, + () -> "grandchildren missing: " + items.get(count - 1)); + } + + @TearDown(Level.Trial) + public void tearDown() { + pool.shutdownNow(); + } + + private BatchLoader> childBatchLoader() { + return keys -> { + List> children = new ArrayList<>(keys.size()); + for (String key : keys) { + children.add(Map.of("id", "C-" + key, "x", "x", "y", "y")); + } + return async ? CompletableFuture.supplyAsync(() -> children, pool) : CompletableFuture.completedFuture(children); + }; + } + + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MICROSECONDS) + public void executeWideQuery(Blackhole blackhole) { + blackhole.consume(execute()); + } + + private ExecutionResult execute() { + DataLoaderRegistry registry = DataLoaderRegistry.newRegistry() + .register("child", DataLoaderFactory.newDataLoader(childBatchLoader())) + .build(); + ExecutionInput input = ExecutionInput.newExecutionInput() + .query(query) + .dataLoaderRegistry(registry) + .build(); + ExecutionResult result = graphQL.execute(input); + Assert.assertTrue(result.getErrors().isEmpty(), () -> "errors: " + result.getErrors()); + return result; + } +} diff --git a/src/main/java/graphql/execution/instrumentation/dataloader/PerLevelDataLoaderDispatchStrategy.java b/src/main/java/graphql/execution/instrumentation/dataloader/PerLevelDataLoaderDispatchStrategy.java index 93d6a3b2cf..58cde65b08 100644 --- a/src/main/java/graphql/execution/instrumentation/dataloader/PerLevelDataLoaderDispatchStrategy.java +++ b/src/main/java/graphql/execution/instrumentation/dataloader/PerLevelDataLoaderDispatchStrategy.java @@ -18,12 +18,14 @@ import org.jspecify.annotations.Nullable; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; @@ -219,7 +221,19 @@ public StateForLevel increaseHappenedExecuteObjectCalls() { private final AtomicInteger happenedFirstLevelFetchCount = new AtomicInteger(); - private final Map> stateForLevelMap = new ConcurrentHashMap<>(); + /* + * The two counters of a level packed into one long: happenedCompletionFinishedCount in the + * high 32 bits, happenedExecuteObjectCalls in the low 32 bits. Both are read in one + * consistent snapshot, and each increment is a single atomic add instead of a + * compare-and-set retry loop that allocates a new StateForLevel. + * + * Levels are indexed directly in an array that grows on demand (query depth bounds it); + * the slow path that creates a level is synchronized, the hot path is a volatile read. + */ + private static final long COMPLETION_FINISHED_ONE = 1L << 32; + private static final int INITIAL_LEVELS = 16; + + private volatile AtomicLong[] stateForLevels = new AtomicLong[INITIAL_LEVELS]; @VisibleForTesting final Set dispatchedLevels = ConcurrentHashMap.newKeySet(); @@ -232,20 +246,74 @@ public CallStack() { } + private AtomicLong stateForLevel(int level) { + AtomicLong[] levels = stateForLevels; + if (level < levels.length) { + AtomicLong state = levels[level]; + if (state != null) { + return state; + } + } + return createStateForLevel(level); + } + + private synchronized AtomicLong createStateForLevel(int level) { + AtomicLong[] levels = stateForLevels; + if (level >= levels.length) { + levels = Arrays.copyOf(levels, Math.max(level + 1, levels.length * 2)); + } + AtomicLong state = levels[level]; + if (state == null) { + state = new AtomicLong(); + levels[level] = state; + } + stateForLevels = levels; + return state; + } + + private static long pack(StateForLevel state) { + return ((long) state.happenedCompletionFinishedCount << 32) | (state.happenedExecuteObjectCalls & 0xFFFFFFFFL); + } + + private static int completionFinishedCount(long packed) { + return (int) (packed >>> 32); + } + + private static int executeObjectCalls(long packed) { + return (int) packed; + } + public StateForLevel get(int level) { - AtomicReference dataPerLevelAtomicReference = stateForLevelMap.computeIfAbsent(level, __ -> new AtomicReference<>(new StateForLevel())); - return Assert.assertNotNull(dataPerLevelAtomicReference.get()); + long packed = stateForLevel(level).get(); + return new StateForLevel(completionFinishedCount(packed), executeObjectCalls(packed)); } public boolean tryUpdateLevel(int level, StateForLevel oldData, StateForLevel newData) { - AtomicReference dataPerLevelAtomicReference = Assert.assertNotNull(stateForLevelMap.get(level)); - return dataPerLevelAtomicReference.compareAndSet(oldData, newData); + return stateForLevel(level).compareAndSet(pack(oldData), pack(newData)); } + void increaseHappenedExecuteObjectCalls(int level) { + stateForLevel(level).getAndIncrement(); + } - public void clear() { + void increaseHappenedCompletionFinishedCount(int level) { + stateForLevel(level).getAndAdd(COMPLETION_FINISHED_ONE); + } + + /** + * @return true if at least one execute object call happened on the level and every one of + * them has a matching completion, read from a single snapshot of both counters + */ + boolean allExecuteObjectCallsCompleted(int level) { + long packed = stateForLevel(level).get(); + int happenedExecuteObjectCalls = executeObjectCalls(packed); + return happenedExecuteObjectCalls > 0 && happenedExecuteObjectCalls == completionFinishedCount(packed); + } + + + public synchronized void clear() { dispatchedLevels.clear(); - stateForLevelMap.clear(); + stateForLevels = new AtomicLong[INITIAL_LEVELS]; expectedFirstLevelFetchCount = 0; happenedFirstLevelFetchCount.set(0); deferredFragmentRootFieldsCompleted.set(0); @@ -301,12 +369,7 @@ public void executionStrategyOnFieldValuesException(Throwable t, ExecutionStrate public void executeObject(ExecutionContext executionContext, ExecutionStrategyParameters parameters, int fieldCount) { CallStack callStack = getCallStack(parameters); int curLevel = parameters.getPath().getLevel(); - while (true) { - CallStack.StateForLevel currentState = callStack.get(curLevel); - if (callStack.tryUpdateLevel(curLevel, currentState, currentState.increaseHappenedExecuteObjectCalls())) { - return; - } - } + callStack.increaseHappenedExecuteObjectCalls(curLevel); } @Override @@ -325,12 +388,7 @@ public void executeObjectOnFieldValuesException(Throwable t, ExecutionStrategyPa private void onCompletionFinished(int level, CallStack callStack) { - while (true) { - CallStack.StateForLevel currentState = callStack.get(level); - if (callStack.tryUpdateLevel(level, currentState, currentState.increaseHappenedCompletionFinishedCount())) { - break; - } - } + callStack.increaseHappenedCompletionFinishedCount(level); // due to synchronous DataFetcher the completion calls on higher levels // can happen before the completion calls on lower level @@ -386,12 +444,7 @@ public void subscriptionEventCompletionDone(AlternativeCallContext alternativeCa // this means the single root field is completed (it was never "fetched" because it is // the event payload) and we can mark level 1 (root fields) as dispatched and level 0 as completed callStack.dispatchedLevels.add(1); - while (true) { - CallStack.StateForLevel currentState = callStack.get(0); - if (callStack.tryUpdateLevel(0, currentState, currentState.increaseHappenedExecuteObjectCalls())) { - break; - } - } + callStack.increaseHappenedExecuteObjectCalls(0); onCompletionFinished(0, callStack); } @@ -468,9 +521,7 @@ private boolean isLevelReady(int level, CallStack callStack) { Assert.assertTrue(level > 1); // we expect that parent has been dispatched and that all parents fields are completed // all parent fields completed means all parent parent on completions finished calls must have happened - int happenedExecuteObjectCalls = callStack.get(level - 2).happenedExecuteObjectCalls; - return callStack.dispatchedLevels.contains(level - 1) && - happenedExecuteObjectCalls > 0 && happenedExecuteObjectCalls == callStack.get(level - 2).happenedCompletionFinishedCount; + return callStack.dispatchedLevels.contains(level - 1) && callStack.allExecuteObjectCallsCompleted(level - 2); } diff --git a/src/test/groovy/graphql/execution/instrumentation/dataloader/PerLevelDataLoaderDispatchStrategyTest.groovy b/src/test/groovy/graphql/execution/instrumentation/dataloader/PerLevelDataLoaderDispatchStrategyTest.groovy index 5491945237..a20628734b 100644 --- a/src/test/groovy/graphql/execution/instrumentation/dataloader/PerLevelDataLoaderDispatchStrategyTest.groovy +++ b/src/test/groovy/graphql/execution/instrumentation/dataloader/PerLevelDataLoaderDispatchStrategyTest.groovy @@ -196,6 +196,66 @@ class PerLevelDataLoaderDispatchStrategyTest extends Specification { strategy.initialCallStack.get(0).happenedCompletionFinishedCount > 0 } + def "call stack keeps per-level counters beyond its initial capacity"() { + given: + def callStack = new PerLevelDataLoaderDispatchStrategy.CallStack() + + when: + callStack.increaseHappenedExecuteObjectCalls(3) + callStack.increaseHappenedExecuteObjectCalls(40) + callStack.increaseHappenedExecuteObjectCalls(40) + callStack.increaseHappenedCompletionFinishedCount(40) + + then: + callStack.get(3).happenedExecuteObjectCalls == 1 + callStack.get(40).happenedExecuteObjectCalls == 2 + callStack.get(40).happenedCompletionFinishedCount == 1 + !callStack.allExecuteObjectCallsCompleted(40) + + when: + callStack.increaseHappenedCompletionFinishedCount(40) + + then: + callStack.allExecuteObjectCallsCompleted(40) + !callStack.allExecuteObjectCallsCompleted(41) + + when: + callStack.clear() + + then: + callStack.get(3).happenedExecuteObjectCalls == 0 + callStack.get(40).happenedExecuteObjectCalls == 0 + } + + def "call stack counters do not lose concurrent increments"() { + given: + def callStack = new PerLevelDataLoaderDispatchStrategy.CallStack() + def threads = 8 + def incrementsPerThread = 10_000 + def startLatch = new CountDownLatch(1) + def executor = Executors.newFixedThreadPool(threads) + + when: + def futures = (1..threads).collect { thread -> + executor.submit({ + startLatch.await() + for (int i = 0; i < incrementsPerThread; i++) { + int level = (thread + i) % 20 + callStack.increaseHappenedExecuteObjectCalls(level) + callStack.increaseHappenedCompletionFinishedCount(level) + } + } as Runnable) + } + startLatch.countDown() + futures*.get(10, TimeUnit.SECONDS) + executor.shutdown() + + then: + (0..<20).sum { callStack.get(it).happenedExecuteObjectCalls } == threads * incrementsPerThread + (0..<20).sum { callStack.get(it).happenedCompletionFinishedCount } == threads * incrementsPerThread + (0..<20).every { callStack.allExecuteObjectCallsCompleted(it) } + } + private Map alternativeCallContextMap() { def field = PerLevelDataLoaderDispatchStrategy.getDeclaredField("alternativeCallContextMap") field.accessible = true