use of org.apache.flink.util.FlinkException in project flink by apache.
the class ResourceManager method registerJobMaster.
// ------------------------------------------------------------------------
// RPC methods
// ------------------------------------------------------------------------
@Override
public CompletableFuture<RegistrationResponse> registerJobMaster(final JobMasterId jobMasterId, final ResourceID jobManagerResourceId, final String jobManagerAddress, final JobID jobId, final Time timeout) {
checkNotNull(jobMasterId);
checkNotNull(jobManagerResourceId);
checkNotNull(jobManagerAddress);
checkNotNull(jobId);
if (!jobLeaderIdService.containsJob(jobId)) {
try {
jobLeaderIdService.addJob(jobId);
} catch (Exception e) {
ResourceManagerException exception = new ResourceManagerException("Could not add the job " + jobId + " to the job id leader service.", e);
onFatalError(exception);
log.error("Could not add job {} to job leader id service.", jobId, e);
return FutureUtils.completedExceptionally(exception);
}
}
log.info("Registering job manager {}@{} for job {}.", jobMasterId, jobManagerAddress, jobId);
CompletableFuture<JobMasterId> jobMasterIdFuture;
try {
jobMasterIdFuture = jobLeaderIdService.getLeaderId(jobId);
} catch (Exception e) {
// we cannot check the job leader id so let's fail
// TODO: Maybe it's also ok to skip this check in case that we cannot check the leader
// id
ResourceManagerException exception = new ResourceManagerException("Cannot obtain the " + "job leader id future to verify the correct job leader.", e);
onFatalError(exception);
log.debug("Could not obtain the job leader id future to verify the correct job leader.");
return FutureUtils.completedExceptionally(exception);
}
CompletableFuture<JobMasterGateway> jobMasterGatewayFuture = getRpcService().connect(jobManagerAddress, jobMasterId, JobMasterGateway.class);
CompletableFuture<RegistrationResponse> registrationResponseFuture = jobMasterGatewayFuture.thenCombineAsync(jobMasterIdFuture, (JobMasterGateway jobMasterGateway, JobMasterId leadingJobMasterId) -> {
if (Objects.equals(leadingJobMasterId, jobMasterId)) {
return registerJobMasterInternal(jobMasterGateway, jobId, jobManagerAddress, jobManagerResourceId);
} else {
final String declineMessage = String.format("The leading JobMaster id %s did not match the received JobMaster id %s. " + "This indicates that a JobMaster leader change has happened.", leadingJobMasterId, jobMasterId);
log.debug(declineMessage);
return new RegistrationResponse.Failure(new FlinkException(declineMessage));
}
}, getMainThreadExecutor());
// handle exceptions which might have occurred in one of the futures inputs of combine
return registrationResponseFuture.handleAsync((RegistrationResponse registrationResponse, Throwable throwable) -> {
if (throwable != null) {
if (log.isDebugEnabled()) {
log.debug("Registration of job manager {}@{} failed.", jobMasterId, jobManagerAddress, throwable);
} else {
log.info("Registration of job manager {}@{} failed.", jobMasterId, jobManagerAddress);
}
return new RegistrationResponse.Failure(throwable);
} else {
return registrationResponse;
}
}, ioExecutor);
}
use of org.apache.flink.util.FlinkException in project flink by apache.
the class FutureUtilsTest method testForwardExceptionally.
@Test
public void testForwardExceptionally() {
final CompletableFuture<String> source = new CompletableFuture<>();
final CompletableFuture<String> target = new CompletableFuture<>();
FutureUtils.forward(source, target);
assertThat(target.isDone(), is(source.isDone()));
source.completeExceptionally(new FlinkException("foobar"));
assertThat(target.isDone(), is(source.isDone()));
Throwable targetException = getThrowable(target);
Throwable actualException = getThrowable(source);
assertThat(targetException, is(equalTo(actualException)));
}
use of org.apache.flink.util.FlinkException in project flink by apache.
the class FutureUtilsTest method testComposeAfterwardsBothExceptional.
@Test
public void testComposeAfterwardsBothExceptional() throws InterruptedException {
final CompletableFuture<Void> inputFuture = new CompletableFuture<>();
final FlinkException testException1 = new FlinkException("Test exception1");
final FlinkException testException2 = new FlinkException("Test exception2");
final OneShotLatch composeLatch = new OneShotLatch();
final CompletableFuture<Void> composeFuture = FutureUtils.composeAfterwards(inputFuture, () -> {
composeLatch.trigger();
return FutureUtils.completedExceptionally(testException2);
});
assertThat(composeLatch.isTriggered(), is(false));
assertThat(composeFuture.isDone(), is(false));
inputFuture.completeExceptionally(testException1);
assertThat(composeLatch.isTriggered(), is(true));
assertThat(composeFuture.isDone(), is(true));
// check that this future is not exceptionally completed
try {
composeFuture.get();
fail("Expected an exceptional completion");
} catch (ExecutionException ee) {
final Throwable actual = ExceptionUtils.stripExecutionException(ee);
assertThat(actual, is(testException1));
assertThat(actual.getSuppressed(), arrayWithSize(1));
assertThat(actual.getSuppressed()[0], is(testException2));
}
}
use of org.apache.flink.util.FlinkException in project flink by apache.
the class FutureUtilsTest method testStopAtNonRetryableException.
/**
* Test that {@link FutureUtils#retry} should stop at non-retryable exception.
*/
@Test
public void testStopAtNonRetryableException() {
final int retries = 10;
final int notRetry = 3;
final AtomicInteger atomicInteger = new AtomicInteger(0);
final FlinkRuntimeException nonRetryableException = new FlinkRuntimeException("Non-retryable exception");
CompletableFuture<Boolean> retryFuture = FutureUtils.retry(() -> CompletableFuture.supplyAsync(() -> {
if (atomicInteger.incrementAndGet() == notRetry) {
// throw non-retryable exception
throw new CompletionException(nonRetryableException);
} else {
throw new CompletionException(new FlinkException("Test exception"));
}
}, TestingUtils.defaultExecutor()), retries, throwable -> ExceptionUtils.findThrowable(throwable, FlinkException.class).isPresent(), TestingUtils.defaultExecutor());
try {
retryFuture.get();
fail("Exception should be thrown.");
} catch (Exception ex) {
assertThat(ex, FlinkMatchers.containsCause(nonRetryableException));
}
assertThat(atomicInteger.get(), is(notRetry));
}
use of org.apache.flink.util.FlinkException in project flink by apache.
the class FutureUtilsTest method testRetryWithDelayFixedArgs.
/**
* Tests that the delay is respected between subsequent retries of a retry future with retry
* delay.
*/
@Test
public void testRetryWithDelayFixedArgs() throws Exception {
final int retries = 4;
final Time delay = Time.milliseconds(5L);
final AtomicInteger countDown = new AtomicInteger(retries);
long start = System.currentTimeMillis();
CompletableFuture<Boolean> retryFuture = FutureUtils.retryWithDelay(() -> {
if (countDown.getAndDecrement() == 0) {
return CompletableFuture.completedFuture(true);
} else {
return FutureUtils.completedExceptionally(new FlinkException("Test exception."));
}
}, retries, delay, TestingUtils.defaultScheduledExecutor());
Boolean result = retryFuture.get();
long completionTime = System.currentTimeMillis() - start;
assertTrue(result);
assertTrue("The completion time should be at least retries times delay between retries.", completionTime >= retries * delay.toMilliseconds());
}
Aggregations