Cross language exception (#10023)

This commit is contained in:
fyrestone
2020-08-26 10:46:05 +08:00
committed by GitHub
parent 1e99b814f0
commit 08adbb371f
30 changed files with 441 additions and 137 deletions
@@ -3,7 +3,7 @@ package io.ray.test;
import io.ray.api.ActorHandle;
import io.ray.api.Checkpointable;
import io.ray.api.Ray;
import io.ray.api.exception.RayActorException;
import io.ray.runtime.exception.RayActorException;
import io.ray.api.id.ActorId;
import io.ray.api.id.UniqueId;
import io.ray.runtime.util.SystemUtil;
@@ -5,7 +5,7 @@ import io.ray.api.ActorHandle;
import io.ray.api.ObjectRef;
import io.ray.api.PyActorHandle;
import io.ray.api.Ray;
import io.ray.api.exception.UnreconstructableException;
import io.ray.runtime.exception.UnreconstructableException;
import io.ray.api.id.ActorId;
import io.ray.api.id.UniqueId;
import java.util.Collections;
@@ -11,6 +11,9 @@ import io.ray.api.function.PyActorMethod;
import io.ray.api.function.PyFunction;
import io.ray.runtime.actor.NativeActorHandle;
import io.ray.runtime.actor.NativePyActorHandle;
import io.ray.runtime.exception.CrossLanguageException;
import io.ray.runtime.exception.RayException;
import io.ray.runtime.generated.Common.Language;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -19,14 +22,11 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.Test;
public class CrossLanguageInvocationTest extends BaseMultiLanguageTest {
private static final Logger LOGGER = LoggerFactory.getLogger(CrossLanguageInvocationTest.class);
private static final String PYTHON_MODULE = "test_cross_language_invocation";
@Override
@@ -151,7 +151,6 @@ public class CrossLanguageInvocationTest extends BaseMultiLanguageTest {
PyFunction.of(PYTHON_MODULE, "py_func_call_java_actor", byte[].class),
"1".getBytes()).remote();
Assert.assertEquals(res.get(), "Counter1".getBytes());
}
@Test
@@ -188,6 +187,91 @@ public class CrossLanguageInvocationTest extends BaseMultiLanguageTest {
Assert.assertEquals(res.get(), "3".getBytes());
}
@Test
public void testExceptionSerialization() throws IOException {
try {
throw new RayException("Test Exception");
} catch (RayException e) {
String formattedException = org.apache.commons.lang3.exception.ExceptionUtils
.getStackTrace(e);
io.ray.runtime.generated.Common.RayException exception = io.ray.runtime.generated.Common.RayException
.parseFrom(e.toBytes());
Assert.assertEquals(exception.getFormattedExceptionString(), formattedException);
}
}
@Test
public void testRaiseExceptionFromPython() {
ObjectRef<Object> res = Ray.task(PyFunction.of(
PYTHON_MODULE, "py_func_python_raise_exception", Object.class)).remote();
try {
res.get();
} catch (RuntimeException ex) {
// ex is a Python exception(py_func_python_raise_exception) with no cause.
Assert.assertTrue(ex instanceof CrossLanguageException);
CrossLanguageException e = (CrossLanguageException) ex;
Assert.assertEquals(e.getLanguage(), Language.PYTHON);
// ex.cause is null.
Assert.assertNull(ex.getCause());
Assert.assertTrue(ex.getMessage().contains("ZeroDivisionError: division by zero"),
ex.getMessage());
return;
}
Assert.fail();
}
@Test
public void testThrowExceptionFromJava() {
ObjectRef<Object> res = Ray.task(PyFunction.of(
PYTHON_MODULE, "py_func_java_throw_exception", Object.class)).remote();
try {
res.get();
} catch (RuntimeException ex) {
final String message = ex.getMessage();
Assert.assertTrue(message.contains("py_func_java_throw_exception"), message);
Assert.assertTrue(message.contains("io.ray.test.CrossLanguageInvocationTest.throwException"),
message);
Assert.assertTrue(message.contains("java.lang.ArithmeticException: / by zero"), message);
return;
}
Assert.fail();
}
@Test
public void testRaiseExceptionFromNestPython() {
ObjectRef<Object> res = Ray.task(
PyFunction.of(PYTHON_MODULE, "py_func_nest_python_raise_exception", Object.class)).remote();
try {
res.get();
} catch (RuntimeException ex) {
final String message = ex.getMessage();
Assert.assertTrue(message.contains("py_func_nest_python_raise_exception"), message);
Assert.assertTrue(message.contains("io.ray.runtime.task.TaskExecutor.execute"), message);
Assert.assertTrue(message.contains("py_func_python_raise_exception"), message);
Assert.assertTrue(message.contains("ZeroDivisionError: division by zero"), message);
return;
}
Assert.fail();
}
@Test
public void testThrowExceptionFromNestJava() {
ObjectRef<Object> res = Ray.task(
PyFunction.of(PYTHON_MODULE, "py_func_nest_java_throw_exception", Object.class)).remote();
try {
res.get();
} catch (RuntimeException ex) {
final String message = ex.getMessage();
Assert.assertTrue(message.contains("py_func_nest_java_throw_exception"), message);
Assert.assertEquals(org.apache.commons.lang3.StringUtils
.countMatches(message, "io.ray.runtime.exception.RayTaskException"), 2);
Assert.assertTrue(message.contains("py_func_java_throw_exception"), message);
Assert.assertTrue(message.contains("java.lang.ArithmeticException: / by zero"), message);
return;
}
Assert.fail();
}
public static Object[] pack(int i, String s, double f, Object[] o) {
// This function will be called from test_cross_language_invocation.py
return new Object[]{i, s, f, o};
@@ -227,6 +311,23 @@ public class CrossLanguageInvocationTest extends BaseMultiLanguageTest {
return (byte[]) res.get();
}
@SuppressWarnings("ConstantOverflow")
public static Object throwException() {
return 1 / 0;
}
public static Object throwJavaException() {
ObjectRef<Object> res = Ray.task(
PyFunction.of(PYTHON_MODULE, "py_func_java_throw_exception", Object.class)).remote();
return res.get();
}
public static Object raisePythonException() {
ObjectRef<Object> res = Ray.task(
PyFunction.of(PYTHON_MODULE, "py_func_python_raise_exception", Object.class)).remote();
return res.get();
}
public static class TestActor {
public TestActor(byte[] v) {
@@ -3,11 +3,10 @@ package io.ray.test;
import io.ray.api.ActorHandle;
import io.ray.api.ObjectRef;
import io.ray.api.Ray;
import io.ray.api.exception.RayActorException;
import io.ray.api.exception.RayException;
import io.ray.api.exception.RayTaskException;
import io.ray.api.exception.RayWorkerException;
import io.ray.runtime.exception.RayActorException;
import io.ray.runtime.exception.RayWorkerException;
import io.ray.api.function.RayFunc0;
import io.ray.runtime.exception.RayTaskException;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
@@ -138,7 +137,7 @@ public class FailureTest extends BaseTest {
try {
Ray.get(Arrays.asList(obj1, obj2));
Assert.fail("Should throw RayException.");
} catch (RayException e) {
} catch (RuntimeException e) {
Instant end = Instant.now();
long duration = Duration.between(start, end).toMillis();
Assert.assertTrue(duration < 5000, "Should fail quickly. " +
@@ -4,7 +4,7 @@ import com.google.common.collect.ImmutableList;
import io.ray.api.ActorHandle;
import io.ray.api.ObjectRef;
import io.ray.api.Ray;
import io.ray.api.exception.RayActorException;
import io.ray.runtime.exception.RayActorException;
import java.util.function.BiConsumer;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
@@ -5,7 +5,6 @@ import io.ray.api.ActorHandle;
import io.ray.api.ObjectRef;
import io.ray.api.Ray;
import io.ray.api.WaitResult;
import io.ray.api.exception.RayException;
import io.ray.api.id.ActorId;
import java.util.ArrayList;
import java.util.List;
@@ -189,7 +188,7 @@ public class MultiThreadingTest extends BaseTest {
try {
// It wouldn't be OK to run them in another thread if not wrapped the runnable.
for (Runnable runnable : runnables) {
Assert.expectThrows(RayException.class, runnable::run);
Assert.expectThrows(RuntimeException.class, runnable::run);
}
} catch (Throwable ex) {
throwable[0] = ex;
@@ -83,6 +83,35 @@ def py_func_pass_python_actor_handle():
return ray.get(r)
@ray.remote
def py_func_python_raise_exception():
1 / 0
@ray.remote
def py_func_java_throw_exception():
f = ray.java_function("io.ray.test.CrossLanguageInvocationTest",
"throwException")
r = f.remote()
return ray.get(r)
@ray.remote
def py_func_nest_python_raise_exception():
f = ray.java_function("io.ray.test.CrossLanguageInvocationTest",
"raisePythonException")
r = f.remote()
return ray.get(r)
@ray.remote
def py_func_nest_java_throw_exception():
f = ray.java_function("io.ray.test.CrossLanguageInvocationTest",
"throwJavaException")
r = f.remote()
return ray.get(r)
@ray.remote
class Counter(object):
def __init__(self, value):