Search in sources :

Example 26 with SerializedThrowable

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

the class SerializedThrowableSerializerTest method testSerializationDeserialization.

@Test
public void testSerializationDeserialization() throws Exception {
    Exception cause = new Exception("cause");
    Exception root = new Exception("message", cause);
    Exception suppressed = new Exception("suppressed");
    root.addSuppressed(suppressed);
    final SerializedThrowable serializedThrowable = new SerializedThrowable(root);
    final String json = objectMapper.writeValueAsString(serializedThrowable);
    final SerializedThrowable deserializedSerializedThrowable = objectMapper.readValue(json, SerializedThrowable.class);
    assertEquals("message", deserializedSerializedThrowable.getMessage());
    assertEquals(serializedThrowable.getFullStringifiedStackTrace(), deserializedSerializedThrowable.getFullStringifiedStackTrace());
    assertEquals("cause", deserializedSerializedThrowable.getCause().getMessage());
    assertTrue(deserializedSerializedThrowable.getCause() instanceof SerializedThrowable);
    assertEquals(1, deserializedSerializedThrowable.getSuppressed().length);
    assertEquals("suppressed", deserializedSerializedThrowable.getSuppressed()[0].getMessage());
    assertTrue(deserializedSerializedThrowable.getSuppressed()[0] instanceof SerializedThrowable);
}
Also used : SerializedThrowable(org.apache.flink.util.SerializedThrowable) Test(org.junit.Test)

Example 27 with SerializedThrowable

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

the class JobResultDeserializer method deserialize.

@Override
public JobResult deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException {
    JobID jobId = null;
    ApplicationStatus applicationStatus = ApplicationStatus.UNKNOWN;
    long netRuntime = -1;
    SerializedThrowable serializedThrowable = null;
    Map<String, SerializedValue<OptionalFailure<Object>>> accumulatorResults = null;
    while (true) {
        final JsonToken jsonToken = p.nextToken();
        assertNotEndOfInput(p, jsonToken);
        if (jsonToken == JsonToken.END_OBJECT) {
            break;
        }
        final String fieldName = p.getValueAsString();
        switch(fieldName) {
            case JobResultSerializer.FIELD_NAME_JOB_ID:
                assertNextToken(p, JsonToken.VALUE_STRING);
                jobId = jobIdDeserializer.deserialize(p, ctxt);
                break;
            case JobResultSerializer.FIELD_NAME_APPLICATION_STATUS:
                assertNextToken(p, JsonToken.VALUE_STRING);
                applicationStatus = ApplicationStatus.valueOf(p.getValueAsString().toUpperCase());
                break;
            case JobResultSerializer.FIELD_NAME_NET_RUNTIME:
                assertNextToken(p, JsonToken.VALUE_NUMBER_INT);
                netRuntime = p.getLongValue();
                break;
            case JobResultSerializer.FIELD_NAME_ACCUMULATOR_RESULTS:
                assertNextToken(p, JsonToken.START_OBJECT);
                accumulatorResults = parseAccumulatorResults(p, ctxt);
                break;
            case JobResultSerializer.FIELD_NAME_FAILURE_CAUSE:
                assertNextToken(p, JsonToken.START_OBJECT);
                serializedThrowable = serializedThrowableDeserializer.deserialize(p, ctxt);
                break;
            default:
        }
    }
    try {
        return new JobResult.Builder().jobId(jobId).applicationStatus(applicationStatus).netRuntime(netRuntime).accumulatorResults(accumulatorResults).serializedThrowable(serializedThrowable).build();
    } catch (final RuntimeException e) {
        throw new JsonMappingException(null, "Could not deserialize " + JobResult.class.getSimpleName(), e);
    }
}
Also used : JobResult(org.apache.flink.runtime.jobmaster.JobResult) SerializedValue(org.apache.flink.util.SerializedValue) ApplicationStatus(org.apache.flink.runtime.clusterframework.ApplicationStatus) JsonMappingException(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonMappingException) JsonToken(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken) JobID(org.apache.flink.api.common.JobID) SerializedThrowable(org.apache.flink.util.SerializedThrowable)

Example 28 with SerializedThrowable

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

the class JobResultSerializer method serialize.

@Override
public void serialize(final JobResult result, final JsonGenerator gen, final SerializerProvider provider) throws IOException {
    gen.writeStartObject();
    gen.writeFieldName(FIELD_NAME_JOB_ID);
    jobIdSerializer.serialize(result.getJobId(), gen, provider);
    gen.writeFieldName(FIELD_NAME_APPLICATION_STATUS);
    gen.writeString(result.getApplicationStatus().name());
    gen.writeFieldName(FIELD_NAME_ACCUMULATOR_RESULTS);
    gen.writeStartObject();
    final Map<String, SerializedValue<OptionalFailure<Object>>> accumulatorResults = result.getAccumulatorResults();
    for (final Map.Entry<String, SerializedValue<OptionalFailure<Object>>> nameValue : accumulatorResults.entrySet()) {
        final String name = nameValue.getKey();
        final SerializedValue<OptionalFailure<Object>> value = nameValue.getValue();
        gen.writeFieldName(name);
        serializedValueSerializer.serialize(value, gen, provider);
    }
    gen.writeEndObject();
    gen.writeNumberField(FIELD_NAME_NET_RUNTIME, result.getNetRuntime());
    if (result.getSerializedThrowable().isPresent()) {
        gen.writeFieldName(FIELD_NAME_FAILURE_CAUSE);
        final SerializedThrowable serializedThrowable = result.getSerializedThrowable().get();
        serializedThrowableSerializer.serialize(serializedThrowable, gen, provider);
    }
    gen.writeEndObject();
}
Also used : OptionalFailure(org.apache.flink.util.OptionalFailure) SerializedValue(org.apache.flink.util.SerializedValue) Map(java.util.Map) SerializedThrowable(org.apache.flink.util.SerializedThrowable)

Example 29 with SerializedThrowable

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

the class RestClusterClientTest method testSubmitJobAndWaitForExecutionResult.

@Test
public void testSubmitJobAndWaitForExecutionResult() throws Exception {
    final TestJobExecutionResultHandler testJobExecutionResultHandler = new TestJobExecutionResultHandler(new RestHandlerException("should trigger retry", HttpResponseStatus.SERVICE_UNAVAILABLE), JobExecutionResultResponseBody.inProgress(), // On an UNKNOWN JobResult it should be retried
    JobExecutionResultResponseBody.created(new JobResult.Builder().applicationStatus(ApplicationStatus.UNKNOWN).jobId(jobId).netRuntime(Long.MAX_VALUE).accumulatorResults(Collections.singletonMap("testName", new SerializedValue<>(OptionalFailure.of(1.0)))).build()), JobExecutionResultResponseBody.created(new JobResult.Builder().applicationStatus(ApplicationStatus.SUCCEEDED).jobId(jobId).netRuntime(Long.MAX_VALUE).accumulatorResults(Collections.singletonMap("testName", new SerializedValue<>(OptionalFailure.of(1.0)))).build()), JobExecutionResultResponseBody.created(new JobResult.Builder().applicationStatus(ApplicationStatus.FAILED).jobId(jobId).netRuntime(Long.MAX_VALUE).serializedThrowable(new SerializedThrowable(new RuntimeException("expected"))).build()));
    // Fail the first JobExecutionResult HTTP polling attempt, which should not be a problem
    // because of the retries.
    final AtomicBoolean firstExecutionResultPollFailed = new AtomicBoolean(false);
    // Fail the first JobSubmit HTTP request, which should not be a problem because of the
    // retries.
    final AtomicBoolean firstSubmitRequestFailed = new AtomicBoolean(false);
    failHttpRequest = (messageHeaders, messageParameters, requestBody) -> {
        if (messageHeaders instanceof JobExecutionResultHeaders) {
            return !firstExecutionResultPollFailed.getAndSet(true);
        }
        if (messageHeaders instanceof JobSubmitHeaders) {
            return !firstSubmitRequestFailed.getAndSet(true);
        }
        return false;
    };
    try (TestRestServerEndpoint restServerEndpoint = createRestServerEndpoint(testJobExecutionResultHandler, new TestJobSubmitHandler())) {
        try (RestClusterClient<?> restClusterClient = createRestClusterClient(restServerEndpoint.getServerAddress().getPort())) {
            final JobExecutionResult jobExecutionResult = restClusterClient.submitJob(jobGraph).thenCompose(restClusterClient::requestJobResult).get().toJobExecutionResult(ClassLoader.getSystemClassLoader());
            assertTrue(firstExecutionResultPollFailed.get());
            assertTrue(firstSubmitRequestFailed.get());
            assertThat(jobExecutionResult.getJobID(), equalTo(jobId));
            assertThat(jobExecutionResult.getNetRuntime(), equalTo(Long.MAX_VALUE));
            assertThat(jobExecutionResult.getAllAccumulatorResults(), equalTo(Collections.singletonMap("testName", 1.0)));
            try {
                restClusterClient.submitJob(jobGraph).thenCompose(restClusterClient::requestJobResult).get().toJobExecutionResult(ClassLoader.getSystemClassLoader());
                fail("Expected exception not thrown.");
            } catch (final Exception e) {
                final Optional<RuntimeException> cause = ExceptionUtils.findThrowable(e, RuntimeException.class);
                assertThat(cause.isPresent(), is(true));
                assertThat(cause.get().getMessage(), equalTo("expected"));
            }
        }
    }
}
Also used : Optional(java.util.Optional) JobResult(org.apache.flink.runtime.jobmaster.JobResult) JobExecutionResultHeaders(org.apache.flink.runtime.rest.messages.job.JobExecutionResultHeaders) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) FlinkException(org.apache.flink.util.FlinkException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RestClientException(org.apache.flink.runtime.rest.util.RestClientException) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) ConfigurationException(org.apache.flink.util.ConfigurationException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JobExecutionResult(org.apache.flink.api.common.JobExecutionResult) TestRestServerEndpoint(org.apache.flink.runtime.rest.util.TestRestServerEndpoint) JobSubmitHeaders(org.apache.flink.runtime.rest.messages.job.JobSubmitHeaders) SerializedThrowable(org.apache.flink.util.SerializedThrowable) Test(org.junit.Test)

Example 30 with SerializedThrowable

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

the class TestJobExecutor method assertFinishedSuccessfully.

public TestJobExecutor assertFinishedSuccessfully() throws Exception {
    LOG.debug("assertFinishedSuccessfully");
    JobStatus jobStatus = miniClusterResource.getClusterClient().getJobStatus(jobID).get();
    if (!jobStatus.equals(FINISHED)) {
        String message = String.format("Job didn't finish successfully, status: %s", jobStatus);
        Optional<SerializedThrowable> throwable = miniClusterResource.getClusterClient().requestJobResult(jobID).get().getSerializedThrowable();
        if (throwable.isPresent()) {
            throw new AssertionError(message, throwable.get());
        } else {
            fail(message);
        }
    }
    return this;
}
Also used : JobStatus(org.apache.flink.api.common.JobStatus) SerializedThrowable(org.apache.flink.util.SerializedThrowable)

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