Skip to content

Commit fcef4ed

Browse files
jovany-wangrobertnishihara
authored andcommitted
[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.
1 parent b45bed4 commit fcef4ed

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

java/runtime/src/main/java/org/ray/runtime/functionmanager/RayFunction.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,17 @@ public FunctionDescriptor getFunctionDescriptor() {
5858
}
5959

6060
public RayRemote getRayRemoteAnnotation() {
61-
RayRemote rayRemote = executable.getAnnotation(RayRemote.class);
62-
if (rayRemote == null) {
63-
// If the method doesn't have a annotation, get the annotation from
64-
// its wrapping class.
61+
RayRemote rayRemote;
62+
63+
// If this method is a constructor, the task of it should be a actorCreationTask.
64+
// And the annotation of actorCreationTask should inherit from class.
65+
// Otherwise, it's a normal method, and it shouldn't inherit annotation from class.
66+
if (isConstructor()) {
6567
rayRemote = executable.getDeclaringClass().getAnnotation(RayRemote.class);
68+
} else {
69+
rayRemote = executable.getAnnotation(RayRemote.class);
6670
}
71+
6772
return rayRemote;
6873
}
6974

java/runtime/src/test/java/org/ray/runtime/functionmanager/FunctionManagerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void testGetFunctionFromRayFunc() {
7474
func = functionManager.getFunction(UniqueId.NIL, barFunc);
7575
Assert.assertFalse(func.isConstructor());
7676
Assert.assertEquals(func.getFunctionDescriptor(), barDescriptor);
77-
Assert.assertNotNull(func.getRayRemoteAnnotation());
77+
Assert.assertNull(func.getRayRemoteAnnotation());
7878

7979
// Test actor constructor
8080
func = functionManager.getFunction(UniqueId.NIL, barConstructor);
@@ -95,7 +95,7 @@ public void testGetFunctionFromFunctionDescriptor() {
9595
func = functionManager.getFunction(UniqueId.NIL, barDescriptor);
9696
Assert.assertFalse(func.isConstructor());
9797
Assert.assertEquals(func.getFunctionDescriptor(), barDescriptor);
98-
Assert.assertNotNull(func.getRayRemoteAnnotation());
98+
Assert.assertNull(func.getRayRemoteAnnotation());
9999

100100
// Test actor constructor
101101
func = functionManager.getFunction(UniqueId.NIL, barConstructorDescriptor);

java/test/src/main/java/org/ray/api/test/ResourcesManagementTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ public Integer echo(Integer number) {
4545
}
4646
}
4747

48+
@RayRemote(resources = {@ResourceItem(name = "RES-A", value = 4)})
49+
public static class Echo3 {
50+
public Integer echo(Integer number) {
51+
return number;
52+
}
53+
}
54+
4855
@Test
4956
public void testMethods() {
5057
// This is a case that can satisfy required resources.
@@ -75,5 +82,14 @@ public void testActors() {
7582
Assert.assertEquals(1, waitResult.getUnready().size());
7683
}
7784

85+
@Test
86+
public void testActorAndMemberMethods() {
87+
// Note(qwang): This case depends on the following line.
88+
// https://github.com/ray-project/ray/blob/master/java/test/src/main/java/org/ray/api/test/TestListener.java#L13
89+
// If we change the static resources configuration item, this case may not pass.
90+
// Then we should change this case too.
91+
RayActor<Echo3> echo3 = Ray.createActor(Echo3::new);
92+
Assert.assertEquals(100, (int) Ray.call(Echo3::echo, echo3, 100).get());
93+
}
7894
}
7995

java/test/src/main/java/org/ray/api/test/TestListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class TestListener extends RunListener {
1010
@Override
1111
public void testRunStarted(Description description) {
1212
System.setProperty("ray.home", "../..");
13-
System.setProperty("ray.resources", "CPU:4");
13+
System.setProperty("ray.resources", "CPU:4,RES-A:4");
1414
Ray.init();
1515
}
1616

0 commit comments

Comments
 (0)