[Java] add exitActor API for java (#10496)

This commit is contained in:
chaokunyang
2020-09-04 10:11:42 +08:00
committed by GitHub
parent 5e4db6ad24
commit cf3875bd8c
11 changed files with 195 additions and 0 deletions
@@ -84,6 +84,11 @@ public class RayDevRuntime extends AbstractRayRuntime {
super.setAsyncContext(asyncContext);
}
@Override
public void exitActor() {
}
private JobId nextJobId() {
return JobId.fromInt(jobCounter.getAndIncrement());
}
@@ -9,6 +9,7 @@ import io.ray.api.runtimecontext.NodeInfo;
import io.ray.runtime.config.RayConfig;
import io.ray.runtime.context.NativeWorkerContext;
import io.ray.runtime.exception.RayException;
import io.ray.runtime.exception.RayIntentionalSystemExitException;
import io.ray.runtime.gcs.GcsClient;
import io.ray.runtime.gcs.GcsClientOptions;
import io.ray.runtime.gcs.RedisClient;
@@ -245,6 +246,16 @@ public final class RayNativeRuntime extends AbstractRayRuntime {
super.setAsyncContext(asyncContext);
}
@Override
public void exitActor() {
if (rayConfig.workerMode != WorkerType.WORKER || runtimeContext.getCurrentActorId().isNil()) {
throw new RuntimeException("This shouldn't be called on a non-actor worker.");
}
LOGGER.info("Actor {} is exiting.", runtimeContext.getCurrentActorId());
throw new RayIntentionalSystemExitException(
String.format("Actor %s is exiting.", runtimeContext.getCurrentActorId()));
}
@Override
public void run() {
Preconditions.checkState(rayConfig.workerMode == WorkerType.WORKER);
@@ -0,0 +1,15 @@
package io.ray.runtime.exception;
/**
* The exception represents that there is an intentional system exit.
*/
public class RayIntentionalSystemExitException extends RuntimeException {
public RayIntentionalSystemExitException(String message) {
super(message);
}
public RayIntentionalSystemExitException(String message, Throwable cause) {
super(message, cause);
}
}
@@ -6,6 +6,7 @@ import io.ray.api.id.JobId;
import io.ray.api.id.TaskId;
import io.ray.api.id.UniqueId;
import io.ray.runtime.RayRuntimeInternal;
import io.ray.runtime.exception.RayIntentionalSystemExitException;
import io.ray.runtime.exception.RayTaskException;
import io.ray.runtime.functionmanager.JavaFunctionDescriptor;
import io.ray.runtime.functionmanager.RayFunction;
@@ -159,6 +160,12 @@ public abstract class TaskExecutor<T extends TaskExecutor.ActorContext> {
}
LOGGER.debug("Finished executing task {}", taskId);
} catch (Throwable e) {
if (e instanceof RayIntentionalSystemExitException) {
// We don't need to fill the `returnObjects` with an exception metadata
// because the node manager or the direct actor task submitter will fill
// the return object with the ACTOR_DIED metadata.
throw (RayIntentionalSystemExitException) e;
}
LOGGER.error("Error executing task " + taskId, e);
if (taskType != TaskType.ACTOR_CREATION_TASK) {
boolean hasReturn = rayFunction != null && rayFunction.hasReturn();
@@ -1,5 +1,6 @@
package io.ray.runtime.util;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.concurrent.locks.ReentrantLock;
@@ -34,4 +35,15 @@ public class SystemUtil {
return pid;
}
public static boolean isProcessAlive(int pid) {
Process process;
try {
process = Runtime.getRuntime().exec(new String[]{"ps", "-p", String.valueOf(pid)});
process.waitFor();
} catch (InterruptedException | IOException e) {
throw new RuntimeException(e);
}
return process.exitValue() == 0;
}
}