[Serve]Fix classloader bug in Java Deployment (#27899)

We have encountered `java.lang.ClassNotFoundException` when deploying Java Ray Serve deployments. The property `ray.job.code-search-path` which specifies the search path of user's classes is not working. The reason is that `ray.job.code-search-path` is loaded in an independent classloader in Ray context, but Serve Replica initialized user class with `AppClassLoader`. We need to change the classloader used to construct user classes to the one in Ray context.
This commit is contained in:
liuyang-my 2022-08-16 15:22:00 +08:00 committed by GitHub
parent c2abfdb2f7
commit 1c4b3879a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -89,7 +89,12 @@ public class RayServeWrappedReplica implements RayServeReplica {
deploymentWrapper.getConfig()); deploymentWrapper.getConfig());
// Instantiate the object defined by deploymentDef. // Instantiate the object defined by deploymentDef.
Class deploymentClass = Class.forName(deploymentWrapper.getDeploymentDef()); Class deploymentClass =
Class.forName(
deploymentWrapper.getDeploymentDef(),
true,
Optional.ofNullable(Thread.currentThread().getContextClassLoader())
.orElse(getClass().getClassLoader()));
Object callable = Object callable =
ReflectUtil.getConstructor(deploymentClass, deploymentWrapper.getInitArgs()) ReflectUtil.getConstructor(deploymentClass, deploymentWrapper.getInitArgs())
.newInstance(deploymentWrapper.getInitArgs()); .newInstance(deploymentWrapper.getInitArgs());