Repository navigation
Description
I use graphql-java with Play Framework 2.8.19 and Java 11 (was trying also with Java 8, same error)
in build.sbt i add dependency like this:
libraryDependencies ++= "com.graphql-java" % "graphql-java" % "20.0"
When application is started, and after i send my graphQL request, i've got an following error in console
If i set my dependency version to 18.3, everything is fine and i've got an result like:
{
"data": {
"productById": {
"id": "1",
"name": "ProductName",
"details": [
{
"id": "1",
"description": "The product detailed description.",
"display": "display",
"name": "product details"
}
]
}
}
}
Actually trying to start this example, but with upgraded library to 20.0, latest, version: https://github.com/manojgodara/GraphQLPlayFramework
ERROR message on version 20.0:
ERROR notprivacysafe.graphql.GraphQL Execution '4e7d7de4-89ce-4518-a389-5a3ab105101c' threw exception when executing : query : 'query test($id: String!) {
productById(id: $id) {
id
name
details {
id
description
display
name
}
}
}'. variables '{id=1}'
java.util.concurrent.CompletionException: java.lang.NoClassDefFoundError: models/Product
at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292)
at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:308)
at java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:783)
at java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:792)
at java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2153)
at graphql.execution.Execution.executeOperation(Execution.java:171)
at graphql.execution.Execution.execute(Execution.java:102)
at graphql.GraphQL.execute(GraphQL.java:557)
at graphql.GraphQL.lambda$parseValidateAndExecute$11(GraphQL.java:476)
at java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:995)
at java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2137)
at graphql.GraphQL.parseValidateAndExecute(GraphQL.java:471)
at graphql.GraphQL.executeAsync(GraphQL.java:439)
at controllers.GraphQLController.query(GraphQLController.java:118)
at controllers.GraphQLController.postHandler(GraphQLController.java:86)
at router.Routes$$anonfun$routes$1.$anonfun$applyOrElse$3(Routes.scala:73)
at play.core.routing.HandlerInvokerFactory$$anon$9.resultCall(HandlerInvoker.scala:154)
at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$3$$anon$4$$anon$5.invocation(HandlerInvoker.scala:115)
at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:119)
at play.http.DefaultActionCreator$1.call(DefaultActionCreator.java:33)
at play.core.j.JavaAction.$anonfun$apply$8(JavaAction.scala:175)
at scala.concurrent.Future$.$anonfun$apply$1(Future.scala:659)
at scala.util.Success.$anonfun$map$1(Try.scala:255)
at scala.util.Success.map(Try.scala:213)
at scala.concurrent.Future.$anonfun$map$1(Future.scala:292)
at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:33)
at scala.concurrent.impl.Promise.$anonfun$transform$1(Promise.scala:33)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64)
at play.core.j.HttpExecutionContext.$anonfun$execute$1(HttpExecutionContext.scala:64)
at play.api.libs.streams.Execution$trampoline$.execute(Execution.scala:70)
at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:59)
at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:72)
at scala.concurrent.impl.Promise$KeptPromise$Kept.onComplete(Promise.scala:372)
at scala.concurrent.impl.Promise$KeptPromise$Kept.onComplete$(Promise.scala:371)
at scala.concurrent.impl.Promise$KeptPromise$Successful.onComplete(Promise.scala:379)
at scala.concurrent.impl.Promise.transform(Promise.scala:33)
at scala.concurrent.impl.Promise.transform$(Promise.scala:31)
at scala.concurrent.impl.Promise$KeptPromise$Successful.transform(Promise.scala:379)
at scala.concurrent.Future.map(Future.scala:292)
at scala.concurrent.Future.map$(Future.scala:292)
at scala.concurrent.impl.Promise$KeptPromise$Successful.map(Promise.scala:379)
at scala.concurrent.Future$.apply(Future.scala:659)
at play.core.j.JavaAction.apply(JavaAction.scala:176)
at play.api.mvc.Action.$anonfun$apply$4(Action.scala:82)
at scala.concurrent.Future.$anonfun$flatMap$1(Future.scala:307)
at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:41)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64)
at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:63)
at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:100)
at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:85)
at akka.dispatch.BatchingExecutor$BlockableBatch.run(BatchingExecutor.scala:100)
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:49)
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(ForkJoinExecutorConfigurator.scala:48)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1067)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1703)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:172)
Caused by: java.lang.NoClassDefFoundError: models/Product
at graphql.schema.PropertyFetchingImpl.getPropertyValue(PropertyFetchingImpl.java:114)
at graphql.schema.PropertyDataFetcherHelper.getPropertyValue(PropertyDataFetcherHelper.java:22)
at graphql.schema.PropertyDataFetcher.getImpl(PropertyDataFetcher.java:131)
at graphql.schema.PropertyDataFetcher.get(PropertyDataFetcher.java:112)
at graphql.execution.ExecutionStrategy.invokeDataFetcher(ExecutionStrategy.java:307)
at graphql.execution.ExecutionStrategy.fetchField(ExecutionStrategy.java:286)
at graphql.execution.ExecutionStrategy.resolveFieldWithInfo(ExecutionStrategy.java:212)
at graphql.execution.AsyncExecutionStrategy.execute(AsyncExecutionStrategy.java:55)
at graphql.execution.ExecutionStrategy.completeValueForObject(ExecutionStrategy.java:680)
at graphql.execution.ExecutionStrategy.completeValue(ExecutionStrategy.java:472)
at graphql.execution.ExecutionStrategy.completeField(ExecutionStrategy.java:422)
at graphql.execution.ExecutionStrategy.lambda$resolveFieldWithInfo$1(ExecutionStrategy.java:214)
at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:616)
at java.util.concurrent.CompletableFuture.uniApplyStage(CompletableFuture.java:628)
at java.util.concurrent.CompletableFuture.thenApply(CompletableFuture.java:1996)
at graphql.execution.ExecutionStrategy.resolveFieldWithInfo(ExecutionStrategy.java:213)
at graphql.execution.AsyncExecutionStrategy.execute(AsyncExecutionStrategy.java:55)
at graphql.execution.Execution.executeOperation(Execution.java:156)
... 52 common frames omitted
Caused by: java.lang.ClassNotFoundException: models.Product
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 70 common frames omitted
Activity
bbakerman commentedon Feb 4, 2023
java.lang.NoClassDefFoundErrormeans that something went wrong during the "class definition" phase of loading the class.Typically this is because some "static" initializer threw an exception and hence the class definition is invalid and the JVM will mark the class as so and throw
java.lang.NoClassDefFoundErrorfrom then on.I this root cause here is the
java.lang.ClassNotFoundException: models.Product- it cant find that classThe graphql standard graphql "property fetcher" is using a new mechanism to find getter methods
See
https://github.com/graphql-java/graphql-java/releases/tag/v20.0
The
PropertyDataFetcherclass is the most common data fetcher used in graphql-java. It uses Java reflection to get field values from objects based on field name.This was logically the following
with the method lookup cached for performance reasons.
However there is mechanism in the JVM that provides even faster object reflective access.
See
https://wttech.blog/blog/2020/method-handles-and-lambda-metafactory/
https://www.optaplanner.org/blog/2018/01/09/JavaReflectionButMuchFaster.html
java.lang.invoke.LambdaMetafactory#metafactoryis an arcane mechanism that can be used to create virtual method lambdas that give fast access to call object methods. It turns out to be significantly faster that Java reflection and only marginally slower that directly invoking a method.Somehow this is failing on your system - perhaps because of class loader challenges between where the graphql-java classes are and where the
models.Productclasses are?Are you using funky Java modules or OSGI perchance?
You can disable this new
LambdaMetafactorymechanism JVM wide. Callgraphql.schema.PropertyDataFetcherHelper#setUseLambdaFactorywith a false value and this strategy wont be used.This might help you debug what's going on and we would love to know more about why this is not working.
I suspect "funky class loaders" between where graphql-java classes are and your model classes are but its hard to know from this distance
SandQuattro commentedon Feb 5, 2023
by the way, tried latest version on maven repository: 230521-nf-execution: "com.graphql-java" % "graphql-java" % "230521-nf-execution",
no errors at all, works fine, got the result, problem only on 20.0
dondonz commentedon Feb 5, 2023
The version
230521-nf-executionis a rather old one from 2021, I tried to delete it but Maven doesn't make that easy sadly.Our main releases are listed on GitHub, and our latest build releases start with
0.0.0coolersham commentedon Feb 13, 2023
Facing the exact same problem in a Quarkus Kotlin environment. Disabling the new
LambdaMetafactoryapproach as mentioned by @bbakerman resolves the problem. However, this is not really a satisfactory solution because the performance benefit is lost.@SandQuattro did you come across a solution?
SandQuattro commentedon Feb 13, 2023
not yet. Downgraded to 18.3 version and it's fine for me
bbakerman commentedon Feb 18, 2023
Can the people affected by this issue please outline more debugging information.
It would appear that
java.lang.invoke.LambdaMetafactory#metafactoryperformance trick is not working for some class loader set ups.Its is certainly not a common mechanism so its more than possible that specific "class loader" setups such as OSGI or ones with fancy security managers could pose a problem.
As mentioned above this mechanism can be turned off via
graphql.schema.PropertyDataFetcherHelper#setUseLambdaFactorywhich statically turns it off for the JVM (static boolean).I really need more details on class loaders and JVMs used. I suspect strongly its some more "complex" JVM setups in play here.
For example in the Quarkus case - is Graal Native being used?
bbakerman commentedon Feb 19, 2023
This is what I think is happening
There is a
SomeTarget.classout there had has a methods we are going to invoke for a property fetch. And we have asomeTargetobject of that class to invoke.The
java.lang.invoke.LambdaMetafactory#metafactorycreates a mechanism that looks like thisThe property fetcher code then invokes that function that is really a way to invoke some proxy code on the
someTarget.However for some people above it is blowing up with
LinkageErrors. This part I dont know know why.And to complicate it - the code is able to use Reflection onto
someTargetand have that succeed.The class is not found in the Lambda case
Yet the same class CAN be found via Reflection which is the part that puzzles me.
So I need a reproducible case showing how this is happening - why one technique fails yet reflection works in class loader terms.
I started on a PR that makes it resilient to
LinkageErrors : #3095 - but this is just working around. I have no more insight on why this is happening.SandQuattro commentedon Feb 19, 2023
curl 'http://localhost:9000/graphql' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'Origin: altair://-' --data-binary '{"query":"query productById($id: String!) {\n productById(id: $id) {\n categoryName\n }\n}\n","variables":{"id":"1"}}' --compressed
here is our error:
[error] n.g.GraphQL - Execution '7d58df18-10eb-4841-8f3c-330793db87b7' threw exception when executing : query : 'query productById($id: String!) {
productById(id: $id) {
categoryName
}
}
'. variables '{id=1}'
java.util.concurrent.CompletionException: java.lang.NoClassDefFoundError: entity/Product
at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292)
at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:308)
at java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:783)
at java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:792)
at java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2153)
at graphql.execution.Execution.executeOperation(Execution.java:171)
at graphql.execution.Execution.execute(Execution.java:102)
at graphql.GraphQL.execute(GraphQL.java:557)
at graphql.GraphQL.lambda$parseValidateAndExecute$11(GraphQL.java:476)
at java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:995)
Caused by: java.lang.NoClassDefFoundError: entity/Product
at graphql.schema.PropertyFetchingImpl.getPropertyValue(PropertyFetchingImpl.java:114)
at graphql.schema.PropertyDataFetcherHelper.getPropertyValue(PropertyDataFetcherHelper.java:22)
at graphql.schema.PropertyDataFetcher.getImpl(PropertyDataFetcher.java:131)
at graphql.schema.PropertyDataFetcher.get(PropertyDataFetcher.java:112)
at graphql.execution.ExecutionStrategy.invokeDataFetcher(ExecutionStrategy.java:307)
at graphql.execution.ExecutionStrategy.fetchField(ExecutionStrategy.java:286)
at graphql.execution.ExecutionStrategy.resolveFieldWithInfo(ExecutionStrategy.java:212)
at graphql.execution.AsyncExecutionStrategy.execute(AsyncExecutionStrategy.java:55)
at graphql.execution.ExecutionStrategy.completeValueForObject(ExecutionStrategy.java:680)
at graphql.execution.ExecutionStrategy.completeValue(ExecutionStrategy.java:472)
Caused by: java.lang.ClassNotFoundException: entity.Product
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at graphql.schema.PropertyFetchingImpl.getPropertyValue(PropertyFetchingImpl.java:114)
at graphql.schema.PropertyDataFetcherHelper.getPropertyValue(PropertyDataFetcherHelper.java:22)
at graphql.schema.PropertyDataFetcher.getImpl(PropertyDataFetcher.java:131)
at graphql.schema.PropertyDataFetcher.get(PropertyDataFetcher.java:112)
at graphql.execution.ExecutionStrategy.invokeDataFetcher(ExecutionStrategy.java:307)
at graphql.execution.ExecutionStrategy.fetchField(ExecutionStrategy.java:286)
at graphql.execution.ExecutionStrategy.resolveFieldWithInfo(ExecutionStrategy.java:212)
[error] c.GraphQLController - Error while executing the query java.util.concurrent.CompletionException: java.lang.NoClassDefFoundError: entity/Product
It's just a simple entity, like:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Product {
}
bbakerman commentedon Feb 22, 2023
@SandQuattro
This is definitely class loader based
the object passed into the graphql-java code is a
class entity.Productand its class loader is
but the graphql java 20 fetching code is running inside
LambdaFetchingSupport.class.getClassLoader()and its of class loader type
eg a different class loader
and calling
LambdaFetchingSupport.class.getClassLoader().loadClass("entity.Product")fails with class not foundAnd I think this is what is happening inside the
java.lang.invoke.LambdaMetafactory#metafactorycode - its using the class loader of the current code to get to the entity class - but the two cant see it eachIt seems that we can "reflect" and "reflect invoke" the method but we cant use the much faster
LambdaMetafactoryto create a call site to it with the Play framework class loaders set up like this.Its great I have a reproduction here - what do do about it is challenging but I can now see it happening
bbakerman commentedon Feb 23, 2023
This PR is an attempt to make it more resilient
#3095
There is a Java 9 fix that would make this go away I think but we are likely to stay on Java 8 until April so I will leave it at this for the moment
schaefa commentedon Feb 24, 2023
I used the latest code (master) with Felix, Sling and Adobe AEM and it runs into the same issue with Pagination.
Up and to version 19.3 it is working just fine. So far something is going wrong inside the Bundle Wiring but I don't think that is the cause. I will check with 19.3 what is the difference inside Felix but I think in 19.3 it is not obtain the class from the graphql-java class loader but the Sling bundle class loader.
bbakerman commentedon Feb 24, 2023
@schaefa - can you try the branch I have posted to see if its more resilient to class loader challenges
schaefa commentedon Feb 28, 2023
@bbakerman Yes, that fixed my issue