[Java] Support wasCurrentActorRestarted in actor task. (#13120)

* Remove check.

* Add test

* fix lint

* lint

* Fix spotless lint

* Address comments.

* Fix lint

Co-authored-by: Qing Wang <jovany.wq@antgroup.com>
This commit is contained in:
Qing Wang
2021-01-02 11:31:08 +08:00
committed by GitHub
parent 710615c228
commit d3dd5b87ce
3 changed files with 29 additions and 27 deletions
@@ -22,7 +22,7 @@ public class ActorRestartTest extends BaseTest {
wasCurrentActorRestarted = Ray.getRuntimeContext().wasCurrentActorRestarted();
}
public boolean wasCurrentActorRestarted() {
public boolean checkWasCurrentActorRestartedInActorCreationTask() {
return wasCurrentActorRestarted;
}
@@ -31,6 +31,10 @@ public class ActorRestartTest extends BaseTest {
return value;
}
public boolean checkWasCurrentActorRestartedInActorTask() {
return Ray.getRuntimeContext().wasCurrentActorRestarted();
}
public int getPid() {
return SystemUtil.pid();
}
@@ -43,30 +47,38 @@ public class ActorRestartTest extends BaseTest {
actor.task(Counter::increase).remote().get();
}
Assert.assertFalse(actor.task(Counter::wasCurrentActorRestarted).remote().get());
// Check if actor was restarted.
Assert.assertFalse(
actor.task(Counter::checkWasCurrentActorRestartedInActorCreationTask).remote().get());
Assert.assertFalse(
actor.task(Counter::checkWasCurrentActorRestartedInActorTask).remote().get());
// Kill the actor process.
int pid = actor.task(Counter::getPid).remote().get();
Runtime.getRuntime().exec("kill -9 " + pid);
// Wait for the actor to be killed.
TimeUnit.SECONDS.sleep(1);
killActorProcess(actor);
int value = actor.task(Counter::increase).remote().get();
Assert.assertEquals(value, 1);
Assert.assertTrue(actor.task(Counter::wasCurrentActorRestarted).remote().get());
// Check if actor was restarted again.
Assert.assertTrue(
actor.task(Counter::checkWasCurrentActorRestartedInActorCreationTask).remote().get());
Assert.assertTrue(actor.task(Counter::checkWasCurrentActorRestartedInActorTask).remote().get());
// Kill the actor process again.
pid = actor.task(Counter::getPid).remote().get();
Runtime.getRuntime().exec("kill -9 " + pid);
TimeUnit.SECONDS.sleep(1);
killActorProcess(actor);
// Try calling increase on this actor again and this should fail.
try {
actor.task(Counter::increase).remote().get();
Assert.fail("The above task didn't fail.");
} catch (RayActorException e) {
// We should receive a RayActorException because the actor is dead.
}
Assert.assertThrows(
RayActorException.class, () -> actor.task(Counter::increase).remote().get());
}
/** The helper to kill a counter actor. */
private static void killActorProcess(ActorHandle<Counter> actor)
throws IOException, InterruptedException {
// Kill the actor process.
int pid = actor.task(Counter::getPid).remote().get();
Process p = Runtime.getRuntime().exec("kill -9 " + pid);
// Wait for the actor to be killed.
TimeUnit.SECONDS.sleep(1);
}
}