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);
}
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);
}
}
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();
}
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"));
}
}
}
}
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;
}
Aggregations