Search in sources :

Example 6 with UserCodeException

use of org.apache.beam.sdk.util.UserCodeException in project beam by apache.

the class SimpleParDoFnTest method testErrorPropagation.

@Test
public void testErrorPropagation() throws Exception {
    TestErrorDoFn fn = new TestErrorDoFn();
    DoFnInfo<?, ?> fnInfo = DoFnInfo.forFn(fn, WindowingStrategy.globalDefault(), null, /* side input views */
    null, /* input coder */
    MAIN_OUTPUT, DoFnSchemaInformation.create(), Collections.emptyMap());
    TestReceiver receiver = new TestReceiver();
    ParDoFn userParDoFn = new SimpleParDoFn<>(options, DoFnInstanceManagers.singleInstance(fnInfo), new EmptySideInputReader(), MAIN_OUTPUT, ImmutableMap.of(MAIN_OUTPUT, 0), BatchModeExecutionContext.forTesting(options, "testStage").getStepContext(operationContext), operationContext, DoFnSchemaInformation.create(), Collections.emptyMap(), SimpleDoFnRunnerFactory.INSTANCE);
    try {
        userParDoFn.startBundle(receiver);
        userParDoFn.processElement(null);
        fail("should have failed");
    } catch (Exception exn) {
        // Because we're calling this from inside the SDK and not from a
        // user's program (e.g. through Pipeline.run), the error should
        // be thrown as a UserCodeException. The cause of the
        // UserCodeError shouldn't contain any of the stack from within
        // the SDK, since we don't want to overwhelm users with stack
        // frames outside of their control.
        assertThat(exn, instanceOf(UserCodeException.class));
        // Stack trace of the cause should contain three frames:
        // TestErrorDoFn.nestedFunctionBeta
        // TestErrorDoFn.nestedFunctionAlpha
        // TestErrorDoFn.startBundle
        assertThat(stackTraceFrameStrings(exn.getCause()), contains(containsString("TestErrorDoFn.nestedFunctionBeta"), containsString("TestErrorDoFn.nestedFunctionAlpha"), containsString("TestErrorDoFn.startBundle")));
        assertThat(exn.toString(), containsString("test error in initialize"));
    }
    try {
        userParDoFn.processElement(WindowedValue.valueInGlobalWindow(3));
        fail("should have failed");
    } catch (Exception exn) {
        // Exception should be a UserCodeException since we're calling
        // from inside the SDK.
        assertThat(exn, instanceOf(UserCodeException.class));
        // Stack trace of the cause should contain two frames:
        // TestErrorDoFn.nestedFunctionBeta
        // TestErrorDoFn.processElement
        assertThat(stackTraceFrameStrings(exn.getCause()), contains(containsString("TestErrorDoFn.nestedFunctionBeta"), containsString("TestErrorDoFn.processElement")));
        assertThat(exn.toString(), containsString("test error in process"));
    }
    try {
        userParDoFn.finishBundle();
        fail("should have failed");
    } catch (Exception exn) {
        // Exception should be a UserCodeException since we're calling
        // from inside the SDK.
        assertThat(exn, instanceOf(UserCodeException.class));
        // Stack trace should only contain a single frame:
        // TestErrorDoFn.finishBundle
        assertThat(stackTraceFrameStrings(exn.getCause()), contains(containsString("TestErrorDoFn.finishBundle")));
        assertThat(exn.toString(), containsString("test error in finalize"));
    }
}
Also used : ParDoFn(org.apache.beam.runners.dataflow.worker.util.common.worker.ParDoFn) UserCodeException(org.apache.beam.sdk.util.UserCodeException) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Aggregations

UserCodeException (org.apache.beam.sdk.util.UserCodeException)6 Status (com.google.api.services.dataflow.model.Status)2 WorkItemStatus (com.google.api.services.dataflow.model.WorkItemStatus)2 IOException (java.io.IOException)2 KvCoder (org.apache.beam.sdk.coders.KvCoder)2 WindowedValueCoder (org.apache.beam.sdk.util.WindowedValue.WindowedValueCoder)2 Instant (org.joda.time.Instant)2 CounterStructuredName (com.google.api.services.dataflow.model.CounterStructuredName)1 CounterUpdate (com.google.api.services.dataflow.model.CounterUpdate)1 MapTask (com.google.api.services.dataflow.model.MapTask)1 StreamingComputationConfig (com.google.api.services.dataflow.model.StreamingComputationConfig)1 StreamingConfigTask (com.google.api.services.dataflow.model.StreamingConfigTask)1 WorkItem (com.google.api.services.dataflow.model.WorkItem)1 AutoValue (com.google.auto.value.AutoValue)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 File (java.io.File)1 PrintWriter (java.io.PrintWriter)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1