Search in sources :

Example 6 with FlinkException

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);
}
Also used : JobMasterId(org.apache.flink.runtime.jobmaster.JobMasterId) ResourceManagerException(org.apache.flink.runtime.resourcemanager.exceptions.ResourceManagerException) JobMasterGateway(org.apache.flink.runtime.jobmaster.JobMasterGateway) RegistrationResponse(org.apache.flink.runtime.registration.RegistrationResponse) TimeoutException(java.util.concurrent.TimeoutException) CompletionException(java.util.concurrent.CompletionException) FlinkException(org.apache.flink.util.FlinkException) ResourceManagerException(org.apache.flink.runtime.resourcemanager.exceptions.ResourceManagerException) UnknownTaskExecutorException(org.apache.flink.runtime.resourcemanager.exceptions.UnknownTaskExecutorException) FlinkException(org.apache.flink.util.FlinkException)

Example 7 with FlinkException

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)));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) FlinkException(org.apache.flink.util.FlinkException) Test(org.junit.Test)

Example 8 with FlinkException

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));
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) ExecutionException(java.util.concurrent.ExecutionException) FlinkException(org.apache.flink.util.FlinkException) Test(org.junit.Test)

Example 9 with FlinkException

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));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletionException(java.util.concurrent.CompletionException) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FlinkException(org.apache.flink.util.FlinkException) FlinkException(org.apache.flink.util.FlinkException) TimeoutException(java.util.concurrent.TimeoutException) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) CompletionException(java.util.concurrent.CompletionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 10 with FlinkException

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Time(org.apache.flink.api.common.time.Time) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FlinkException(org.apache.flink.util.FlinkException) Test(org.junit.Test)

Aggregations

FlinkException (org.apache.flink.util.FlinkException)197 Test (org.junit.Test)91 CompletableFuture (java.util.concurrent.CompletableFuture)59 IOException (java.io.IOException)38 ExecutionException (java.util.concurrent.ExecutionException)26 ArrayList (java.util.ArrayList)25 JobID (org.apache.flink.api.common.JobID)24 Collection (java.util.Collection)22 CompletionException (java.util.concurrent.CompletionException)22 Configuration (org.apache.flink.configuration.Configuration)21 TimeoutException (java.util.concurrent.TimeoutException)19 FutureUtils (org.apache.flink.util.concurrent.FutureUtils)19 Time (org.apache.flink.api.common.time.Time)16 OneShotLatch (org.apache.flink.core.testutils.OneShotLatch)16 ResourceID (org.apache.flink.runtime.clusterframework.types.ResourceID)16 JobGraph (org.apache.flink.runtime.jobgraph.JobGraph)15 AllocationID (org.apache.flink.runtime.clusterframework.types.AllocationID)14 Collections (java.util.Collections)13 List (java.util.List)13 ExecutorService (java.util.concurrent.ExecutorService)13