forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core] Support default actor lifetime. (ray-project#21283)
Support the ability to specify a default lifetime for actors which are not specified lifetime when creating. This is a job level configuration item. #### API Change The Python API looks like: ```python ray.init(job_config=JobConfig(default_actor_lifetime="detached")) ``` Java API looks like: ```java System.setProperty("ray.job.default-actor-lifetime", defaultActorLifetime.name()); Ray.init(); ``` One example usage is: ```python ray.init(job_config=JobConfig(default_actor_lifetime="detached")) a1 = A.options(lifetime="non_detached").remote() # a1 is a non-detached actor. a2 = A.remote() # a2 is a non-detached actor. ``` Co-authored-by: Kai Yang <[email protected]> Co-authored-by: Qing Wang <[email protected]>
- Loading branch information
1 parent
b00385f
commit a37d9a2
Showing
20 changed files
with
328 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
java/test/src/main/java/io/ray/test/DefaultActorLifetimeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package io.ray.test; | ||
|
||
import io.ray.api.ActorHandle; | ||
import io.ray.api.Ray; | ||
import io.ray.api.options.ActorLifetime; | ||
import io.ray.runtime.exception.RayActorException; | ||
import io.ray.runtime.util.SystemUtil; | ||
import java.io.IOException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Supplier; | ||
import org.testng.Assert; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
@Test(groups = "cluster") | ||
public class DefaultActorLifetimeTest { | ||
|
||
private static class OwnerActor { | ||
private ActorHandle<ChildActor> childActor; | ||
|
||
public ActorHandle<ChildActor> createChildActor(ActorLifetime childActorLifetime) { | ||
if (childActorLifetime == null) { | ||
childActor = Ray.actor(ChildActor::new).remote(); | ||
} else { | ||
childActor = Ray.actor(ChildActor::new).setLifetime(childActorLifetime).remote(); | ||
} | ||
if ("ok".equals(childActor.task(ChildActor::ready).remote().get())) { | ||
return childActor; | ||
} | ||
return null; | ||
} | ||
|
||
int getPid() { | ||
return SystemUtil.pid(); | ||
} | ||
|
||
String ready() { | ||
return "ok"; | ||
} | ||
} | ||
|
||
private static class ChildActor { | ||
String ready() { | ||
return "ok"; | ||
} | ||
} | ||
|
||
@Test( | ||
groups = {"cluster"}, | ||
dataProvider = "parameters") | ||
public void testDefaultActorLifetime( | ||
ActorLifetime defaultActorLifetime, ActorLifetime childActorLifetime) | ||
throws IOException, InterruptedException { | ||
if (defaultActorLifetime != null) { | ||
System.setProperty("ray.job.default-actor-lifetime", defaultActorLifetime.name()); | ||
} | ||
try { | ||
System.setProperty("ray.job.num-java-workers-per-process", "1"); | ||
Ray.init(); | ||
|
||
/// 1. create owner and invoke createChildActor. | ||
ActorHandle<OwnerActor> owner = Ray.actor(OwnerActor::new).remote(); | ||
ActorHandle<ChildActor> child = | ||
owner.task(OwnerActor::createChildActor, childActorLifetime).remote().get(); | ||
Assert.assertEquals("ok", child.task(ChildActor::ready).remote().get()); | ||
int ownerPid = owner.task(OwnerActor::getPid).remote().get(); | ||
|
||
/// 2. Kill owner and make sure it's dead. | ||
Runtime.getRuntime().exec("kill -9 " + ownerPid); | ||
Supplier<Boolean> isOwnerDead = | ||
() -> { | ||
try { | ||
owner.task(OwnerActor::ready).remote().get(); | ||
return false; | ||
} catch (RayActorException e) { | ||
return true; | ||
} | ||
}; | ||
Assert.assertTrue(TestUtils.waitForCondition(isOwnerDead, 3000)); | ||
|
||
/// 3. Assert child state. | ||
Supplier<Boolean> isChildDead = | ||
() -> { | ||
try { | ||
child.task(ChildActor::ready).remote().get(); | ||
return false; | ||
} catch (RayActorException e) { | ||
return true; | ||
} | ||
}; | ||
ActorLifetime actualLifetime = defaultActorLifetime; | ||
if (childActorLifetime != null) { | ||
actualLifetime = childActorLifetime; | ||
} | ||
Assert.assertNotNull(actualLifetime); | ||
if (actualLifetime == ActorLifetime.DETACHED) { | ||
TimeUnit.SECONDS.sleep(5); | ||
Assert.assertFalse(isChildDead.get()); | ||
} else { | ||
Assert.assertTrue(TestUtils.waitForCondition(isChildDead, 5000)); | ||
} | ||
} finally { | ||
Ray.shutdown(); | ||
} | ||
} | ||
|
||
@DataProvider | ||
public static Object[][] parameters() { | ||
Object[] defaultEnums = new Object[] {ActorLifetime.DETACHED, ActorLifetime.NON_DETACHED}; | ||
Object[] enums = new Object[] {null, ActorLifetime.DETACHED, ActorLifetime.NON_DETACHED}; | ||
Object[][] params = new Object[6][2]; | ||
for (int i = 0; i < 6; ++i) { | ||
params[i][0] = defaultEnums[i / 3]; | ||
params[i][1] = enums[i % 3]; | ||
} | ||
return params; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.