Search in sources :

Example 21 with SerializedThrowable

use of org.apache.flink.util.SerializedThrowable in project flink by apache.

the class ProducerFailedExceptionTest method testCauseIsSerialized.

@Test
public void testCauseIsSerialized() throws Exception {
    // Tests that the cause is stringified, because it might be an instance
    // of a user level Exception, which can not be deserialized by the
    // remote receiver's system class loader.
    ProducerFailedException e = new ProducerFailedException(new Exception());
    assertNotNull(e.getCause());
    assertTrue(e.getCause() instanceof SerializedThrowable);
}
Also used : CancelTaskException(org.apache.flink.runtime.execution.CancelTaskException) SerializedThrowable(org.apache.flink.util.SerializedThrowable) Test(org.junit.Test)

Example 22 with SerializedThrowable

use of org.apache.flink.util.SerializedThrowable in project flink by apache.

the class SerializedThrowableTest method testSerialization.

@Test
public void testSerialization() {
    try {
        // We need an exception whose class is not in the core class loader
        final ClassLoaderUtils.ObjectAndClassLoader<Exception> outsideClassLoading = ClassLoaderUtils.createExceptionObjectFromNewClassLoader();
        ClassLoader loader = outsideClassLoading.getClassLoader();
        Exception userException = outsideClassLoading.getObject();
        Class<?> clazz = userException.getClass();
        // check that we cannot simply copy the exception
        try {
            byte[] serialized = InstantiationUtil.serializeObject(userException);
            InstantiationUtil.deserializeObject(serialized, getClass().getClassLoader());
            fail("should fail with a class not found exception");
        } catch (ClassNotFoundException e) {
        // as we want it
        }
        // validate that the SerializedThrowable mimics the original exception
        SerializedThrowable serialized = new SerializedThrowable(userException);
        assertEquals(userException.getMessage(), serialized.getMessage());
        assertEquals(userException.toString(), serialized.toString());
        assertEquals(ExceptionUtils.stringifyException(userException), ExceptionUtils.stringifyException(serialized));
        assertArrayEquals(userException.getStackTrace(), serialized.getStackTrace());
        // copy the serialized throwable and make sure everything still works
        SerializedThrowable copy = CommonTestUtils.createCopySerializable(serialized);
        assertEquals(userException.getMessage(), copy.getMessage());
        assertEquals(userException.toString(), copy.toString());
        assertEquals(ExceptionUtils.stringifyException(userException), ExceptionUtils.stringifyException(copy));
        assertArrayEquals(userException.getStackTrace(), copy.getStackTrace());
        // deserialize the proper exception
        Throwable deserialized = copy.deserializeError(loader);
        assertEquals(clazz, deserialized.getClass());
        // deserialization with the wrong classloader does not lead to a failure
        Throwable wronglyDeserialized = copy.deserializeError(getClass().getClassLoader());
        assertEquals(ExceptionUtils.stringifyException(userException), ExceptionUtils.stringifyException(wronglyDeserialized));
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : ClassLoaderUtils(org.apache.flink.testutils.ClassLoaderUtils) SerializedThrowable(org.apache.flink.util.SerializedThrowable) SerializedThrowable(org.apache.flink.util.SerializedThrowable) Test(org.junit.Test)

Example 23 with SerializedThrowable

use of org.apache.flink.util.SerializedThrowable in project flink by apache.

the class SerializedThrowableTest method testCauseChaining.

@Test
public void testCauseChaining() {
    Exception cause2 = new Exception("level2");
    Exception cause1 = new Exception("level1", cause2);
    Exception root = new Exception("level0", cause1);
    SerializedThrowable st = new SerializedThrowable(root);
    assertEquals("level0", st.getMessage());
    assertNotNull(st.getCause());
    assertEquals("level1", st.getCause().getMessage());
    assertNotNull(st.getCause().getCause());
    assertEquals("level2", st.getCause().getCause().getMessage());
}
Also used : SerializedThrowable(org.apache.flink.util.SerializedThrowable) Test(org.junit.Test)

Example 24 with SerializedThrowable

use of org.apache.flink.util.SerializedThrowable in project flink by apache.

the class SerializedThrowableTest method testCyclicCauseChaining.

@Test
public void testCyclicCauseChaining() {
    Exception cause3 = new Exception("level3");
    Exception cause2 = new Exception("level2", cause3);
    Exception cause1 = new Exception("level1", cause2);
    Exception root = new Exception("level0", cause1);
    // introduce a cyclic reference
    cause3.initCause(cause1);
    SerializedThrowable st = new SerializedThrowable(root);
    assertArrayEquals(root.getStackTrace(), st.getStackTrace());
    assertEquals(ExceptionUtils.stringifyException(root), ExceptionUtils.stringifyException(st));
}
Also used : SerializedThrowable(org.apache.flink.util.SerializedThrowable) Test(org.junit.Test)

Example 25 with SerializedThrowable

use of org.apache.flink.util.SerializedThrowable in project flink by apache.

the class CheckpointResourcesCleanupRunnerTest method testRequestJobWithFailure.

@Test
public void testRequestJobWithFailure() {
    final SerializedThrowable expectedError = new SerializedThrowable(new Exception("Expected exception"));
    final JobResult jobResult = createJobResultWithFailure(expectedError);
    testRequestJobExecutionGraph(jobResult, System.currentTimeMillis(), actualExecutionGraph -> Objects.requireNonNull(actualExecutionGraph.getFailureInfo()).getException().equals(expectedError));
}
Also used : JobResult(org.apache.flink.runtime.jobmaster.JobResult) FlinkException(org.apache.flink.util.FlinkException) ExecutionException(java.util.concurrent.ExecutionException) UnavailableDispatcherOperationException(org.apache.flink.runtime.dispatcher.UnavailableDispatcherOperationException) SerializedThrowable(org.apache.flink.util.SerializedThrowable) Test(org.junit.jupiter.api.Test)

Aggregations

SerializedThrowable (org.apache.flink.util.SerializedThrowable)30 Test (org.junit.Test)16 JobGraph (org.apache.flink.runtime.jobgraph.JobGraph)6 FlinkException (org.apache.flink.util.FlinkException)6 ExecutionException (java.util.concurrent.ExecutionException)5 Collection (java.util.Collection)4 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)4 Optional (java.util.Optional)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 UnavailableDispatcherOperationException (org.apache.flink.runtime.dispatcher.UnavailableDispatcherOperationException)3 JobResult (org.apache.flink.runtime.jobmaster.JobResult)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Objects (java.util.Objects)2 Set (java.util.Set)2 RichFlatMapFunction (org.apache.flink.api.common.functions.RichFlatMapFunction)2 RichMapFunction (org.apache.flink.api.common.functions.RichMapFunction)2 ListState (org.apache.flink.api.common.state.ListState)2