Search in sources :

Example 1 with FlinkJobTerminatedWithoutCancellationException

use of org.apache.flink.runtime.messages.FlinkJobTerminatedWithoutCancellationException in project flink by apache.

the class Dispatcher method cancelJob.

@Override
public CompletableFuture<Acknowledge> cancelJob(JobID jobId, Time timeout) {
    Optional<JobManagerRunner> maybeJob = getJobManagerRunner(jobId);
    if (maybeJob.isPresent()) {
        return maybeJob.get().cancel(timeout);
    }
    final ExecutionGraphInfo executionGraphInfo = executionGraphInfoStore.get(jobId);
    if (executionGraphInfo != null) {
        final JobStatus jobStatus = executionGraphInfo.getArchivedExecutionGraph().getState();
        if (jobStatus == JobStatus.CANCELED) {
            return CompletableFuture.completedFuture(Acknowledge.get());
        } else {
            return FutureUtils.completedExceptionally(new FlinkJobTerminatedWithoutCancellationException(jobId, jobStatus));
        }
    }
    log.debug("Dispatcher is unable to cancel job {}: not found", jobId);
    return FutureUtils.completedExceptionally(new FlinkJobNotFoundException(jobId));
}
Also used : JobStatus(org.apache.flink.api.common.JobStatus) ExecutionGraphInfo(org.apache.flink.runtime.scheduler.ExecutionGraphInfo) FlinkJobNotFoundException(org.apache.flink.runtime.messages.FlinkJobNotFoundException) FlinkJobTerminatedWithoutCancellationException(org.apache.flink.runtime.messages.FlinkJobTerminatedWithoutCancellationException) JobManagerRunner(org.apache.flink.runtime.jobmaster.JobManagerRunner)

Example 2 with FlinkJobTerminatedWithoutCancellationException

use of org.apache.flink.runtime.messages.FlinkJobTerminatedWithoutCancellationException in project flink by apache.

the class JobCancellationHandler method handleRequest.

@Override
public CompletableFuture<EmptyResponseBody> handleRequest(HandlerRequest<EmptyRequestBody> request, RestfulGateway gateway) throws RestHandlerException {
    final JobID jobId = request.getPathParameter(JobIDPathParameter.class);
    final List<TerminationModeQueryParameter.TerminationMode> terminationModes = request.getQueryParameter(TerminationModeQueryParameter.class);
    final TerminationModeQueryParameter.TerminationMode terminationMode;
    if (terminationModes.isEmpty()) {
        terminationMode = defaultTerminationMode;
    } else {
        // picking the first termination mode value
        terminationMode = terminationModes.get(0);
    }
    final CompletableFuture<Acknowledge> terminationFuture;
    switch(terminationMode) {
        case CANCEL:
            terminationFuture = gateway.cancelJob(jobId, timeout);
            break;
        case STOP:
            throw new RestHandlerException("The termination mode \"stop\" has been removed. For " + "an ungraceful shutdown, please use \"cancel\" instead. For a graceful shutdown, " + "please use \"jobs/:jobId/stop\" instead.", HttpResponseStatus.PERMANENT_REDIRECT);
        default:
            terminationFuture = FutureUtils.completedExceptionally(new RestHandlerException("Unknown termination mode " + terminationMode + '.', HttpResponseStatus.BAD_REQUEST));
    }
    return terminationFuture.handle((Acknowledge ack, Throwable throwable) -> {
        if (throwable != null) {
            Throwable error = ExceptionUtils.stripCompletionException(throwable);
            if (error instanceof FlinkJobTerminatedWithoutCancellationException) {
                throw new CompletionException(new RestHandlerException(String.format("Job cancellation failed because the job has already reached another terminal state (%s).", ((FlinkJobTerminatedWithoutCancellationException) error).getJobStatus()), HttpResponseStatus.CONFLICT));
            } else if (error instanceof TimeoutException) {
                throw new CompletionException(new RestHandlerException("Job cancellation timed out.", HttpResponseStatus.REQUEST_TIMEOUT, error));
            } else if (error instanceof FlinkJobNotFoundException) {
                throw new CompletionException(new RestHandlerException("Job could not be found.", HttpResponseStatus.NOT_FOUND, error));
            } else {
                throw new CompletionException(new RestHandlerException("Job cancellation failed: " + error.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR, error));
            }
        } else {
            return EmptyResponseBody.getInstance();
        }
    });
}
Also used : Acknowledge(org.apache.flink.runtime.messages.Acknowledge) FlinkJobNotFoundException(org.apache.flink.runtime.messages.FlinkJobNotFoundException) FlinkJobTerminatedWithoutCancellationException(org.apache.flink.runtime.messages.FlinkJobTerminatedWithoutCancellationException) TerminationModeQueryParameter(org.apache.flink.runtime.rest.messages.TerminationModeQueryParameter) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) CompletionException(java.util.concurrent.CompletionException) JobID(org.apache.flink.api.common.JobID) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

FlinkJobNotFoundException (org.apache.flink.runtime.messages.FlinkJobNotFoundException)2 FlinkJobTerminatedWithoutCancellationException (org.apache.flink.runtime.messages.FlinkJobTerminatedWithoutCancellationException)2 CompletionException (java.util.concurrent.CompletionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 JobID (org.apache.flink.api.common.JobID)1 JobStatus (org.apache.flink.api.common.JobStatus)1 JobManagerRunner (org.apache.flink.runtime.jobmaster.JobManagerRunner)1 Acknowledge (org.apache.flink.runtime.messages.Acknowledge)1 RestHandlerException (org.apache.flink.runtime.rest.handler.RestHandlerException)1 TerminationModeQueryParameter (org.apache.flink.runtime.rest.messages.TerminationModeQueryParameter)1 ExecutionGraphInfo (org.apache.flink.runtime.scheduler.ExecutionGraphInfo)1