[Java] Fix the required-resources issue of actor member function in Java worker. (#3002)

This fixes a bug in which Java actor methods inherit the resource requirements of the actor creation task.
This commit is contained in:
Wang Qing
2018-10-02 03:56:36 +08:00
committed by Robert Nishihara
parent b45bed4bce
commit fcef4edd46
4 changed files with 28 additions and 7 deletions
@@ -58,12 +58,17 @@ public class RayFunction {
}
public RayRemote getRayRemoteAnnotation() {
RayRemote rayRemote = executable.getAnnotation(RayRemote.class);
if (rayRemote == null) {
// If the method doesn't have a annotation, get the annotation from
// its wrapping class.
RayRemote rayRemote;
// If this method is a constructor, the task of it should be a actorCreationTask.
// And the annotation of actorCreationTask should inherit from class.
// Otherwise, it's a normal method, and it shouldn't inherit annotation from class.
if (isConstructor()) {
rayRemote = executable.getDeclaringClass().getAnnotation(RayRemote.class);
} else {
rayRemote = executable.getAnnotation(RayRemote.class);
}
return rayRemote;
}
@@ -74,7 +74,7 @@ public class FunctionManagerTest {
func = functionManager.getFunction(UniqueId.NIL, barFunc);
Assert.assertFalse(func.isConstructor());
Assert.assertEquals(func.getFunctionDescriptor(), barDescriptor);
Assert.assertNotNull(func.getRayRemoteAnnotation());
Assert.assertNull(func.getRayRemoteAnnotation());
// Test actor constructor
func = functionManager.getFunction(UniqueId.NIL, barConstructor);
@@ -95,7 +95,7 @@ public class FunctionManagerTest {
func = functionManager.getFunction(UniqueId.NIL, barDescriptor);
Assert.assertFalse(func.isConstructor());
Assert.assertEquals(func.getFunctionDescriptor(), barDescriptor);
Assert.assertNotNull(func.getRayRemoteAnnotation());
Assert.assertNull(func.getRayRemoteAnnotation());
// Test actor constructor
func = functionManager.getFunction(UniqueId.NIL, barConstructorDescriptor);
@@ -45,6 +45,13 @@ public class ResourcesManagementTest {
}
}
@RayRemote(resources = {@ResourceItem(name = "RES-A", value = 4)})
public static class Echo3 {
public Integer echo(Integer number) {
return number;
}
}
@Test
public void testMethods() {
// This is a case that can satisfy required resources.
@@ -75,5 +82,14 @@ public class ResourcesManagementTest {
Assert.assertEquals(1, waitResult.getUnready().size());
}
@Test
public void testActorAndMemberMethods() {
// Note(qwang): This case depends on the following line.
// https://github.com/ray-project/ray/blob/master/java/test/src/main/java/org/ray/api/test/TestListener.java#L13
// If we change the static resources configuration item, this case may not pass.
// Then we should change this case too.
RayActor<Echo3> echo3 = Ray.createActor(Echo3::new);
Assert.assertEquals(100, (int) Ray.call(Echo3::echo, echo3, 100).get());
}
}
@@ -10,7 +10,7 @@ public class TestListener extends RunListener {
@Override
public void testRunStarted(Description description) {
System.setProperty("ray.home", "../..");
System.setProperty("ray.resources", "CPU:4");
System.setProperty("ray.resources", "CPU:4,RES-A:4");
Ray.init();
}