Search in sources :

Example 1 with CancelTaskException

use of org.apache.flink.runtime.execution.CancelTaskException in project flink by apache.

the class BatchTask method run.

protected void run() throws Exception {
    // check for asynchronous canceling
    if (!this.running) {
        return;
    }
    boolean stubOpen = false;
    try {
        // run the data preparation
        try {
            this.driver.prepare();
        } catch (Throwable t) {
            // errors during clean-up are swallowed, because we have already a root exception
            throw new Exception("The data preparation for task '" + this.getEnvironment().getTaskInfo().getTaskName() + "' , caused an error: " + t.getMessage(), t);
        }
        // check for canceling
        if (!this.running) {
            return;
        }
        // start all chained tasks
        BatchTask.openChainedTasks(this.chainedTasks, this);
        // open stub implementation
        if (this.stub != null) {
            try {
                Configuration stubConfig = this.config.getStubParameters();
                FunctionUtils.openFunction(this.stub, stubConfig);
                stubOpen = true;
            } catch (Throwable t) {
                throw new Exception("The user defined 'open()' method caused an exception: " + t.getMessage(), t);
            }
        }
        // run the user code
        this.driver.run();
        // failed.
        if (this.running && this.stub != null) {
            FunctionUtils.closeFunction(this.stub);
            stubOpen = false;
        }
        // close all chained tasks letting them report failure
        BatchTask.closeChainedTasks(this.chainedTasks, this);
        // close the output collector
        this.output.close();
    } catch (Exception ex) {
        // cause
        if (stubOpen) {
            try {
                FunctionUtils.closeFunction(this.stub);
            } catch (Throwable t) {
            // do nothing
            }
        }
        // if resettable driver invoke teardown
        if (this.driver instanceof ResettableDriver) {
            final ResettableDriver<?, ?> resDriver = (ResettableDriver<?, ?>) this.driver;
            try {
                resDriver.teardown();
            } catch (Throwable t) {
                throw new Exception("Error while shutting down an iterative operator: " + t.getMessage(), t);
            }
        }
        BatchTask.cancelChainedTasks(this.chainedTasks);
        ex = ExceptionInChainedStubException.exceptionUnwrap(ex);
        if (ex instanceof CancelTaskException) {
            // forward canceling exception
            throw ex;
        } else if (this.running) {
            // throw only if task was not cancelled. in the case of canceling, exceptions are
            // expected
            BatchTask.logAndThrowException(ex, this);
        }
    } finally {
        this.driver.cleanup();
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) CancelTaskException(org.apache.flink.runtime.execution.CancelTaskException) ExceptionInChainedStubException(org.apache.flink.runtime.operators.chaining.ExceptionInChainedStubException) CancelTaskException(org.apache.flink.runtime.execution.CancelTaskException) IOException(java.io.IOException)

Example 2 with CancelTaskException

use of org.apache.flink.runtime.execution.CancelTaskException in project flink by apache.

the class DataSinkTask method invoke.

@Override
public void invoke() throws Exception {
    // --------------------------------------------------------------------
    // Initialize
    // --------------------------------------------------------------------
    LOG.debug(getLogString("Start registering input and output"));
    // initialize OutputFormat
    initOutputFormat();
    // initialize input readers
    try {
        initInputReaders();
    } catch (Exception e) {
        throw new RuntimeException("Initializing the input streams failed" + (e.getMessage() == null ? "." : ": " + e.getMessage()), e);
    }
    LOG.debug(getLogString("Finished registering input and output"));
    // --------------------------------------------------------------------
    // Invoke
    // --------------------------------------------------------------------
    LOG.debug(getLogString("Starting data sink operator"));
    RuntimeContext ctx = createRuntimeContext();
    final Counter numRecordsIn;
    {
        Counter tmpNumRecordsIn;
        try {
            InternalOperatorIOMetricGroup ioMetricGroup = ((InternalOperatorMetricGroup) ctx.getMetricGroup()).getIOMetricGroup();
            ioMetricGroup.reuseInputMetricsForTask();
            ioMetricGroup.reuseOutputMetricsForTask();
            tmpNumRecordsIn = ioMetricGroup.getNumRecordsInCounter();
        } catch (Exception e) {
            LOG.warn("An exception occurred during the metrics setup.", e);
            tmpNumRecordsIn = new SimpleCounter();
        }
        numRecordsIn = tmpNumRecordsIn;
    }
    if (RichOutputFormat.class.isAssignableFrom(this.format.getClass())) {
        ((RichOutputFormat) this.format).setRuntimeContext(ctx);
        LOG.debug(getLogString("Rich Sink detected. Initializing runtime context."));
    }
    ExecutionConfig executionConfig = getExecutionConfig();
    boolean objectReuseEnabled = executionConfig.isObjectReuseEnabled();
    try {
        // initialize local strategies
        MutableObjectIterator<IT> input1;
        switch(this.config.getInputLocalStrategy(0)) {
            case NONE:
                // nothing to do
                localStrategy = null;
                input1 = reader;
                break;
            case SORT:
                // initialize sort local strategy
                try {
                    // get type comparator
                    TypeComparatorFactory<IT> compFact = this.config.getInputComparator(0, getUserCodeClassLoader());
                    if (compFact == null) {
                        throw new Exception("Missing comparator factory for local strategy on input " + 0);
                    }
                    // initialize sorter
                    Sorter<IT> sorter = ExternalSorter.newBuilder(getEnvironment().getMemoryManager(), this, this.inputTypeSerializerFactory.getSerializer(), compFact.createComparator()).maxNumFileHandles(this.config.getFilehandlesInput(0)).enableSpilling(getEnvironment().getIOManager(), this.config.getSpillingThresholdInput(0)).memoryFraction(this.config.getRelativeMemoryInput(0)).objectReuse(this.getExecutionConfig().isObjectReuseEnabled()).largeRecords(this.config.getUseLargeRecordHandler()).build(this.reader);
                    this.localStrategy = sorter;
                    input1 = sorter.getIterator();
                } catch (Exception e) {
                    throw new RuntimeException("Initializing the input processing failed" + (e.getMessage() == null ? "." : ": " + e.getMessage()), e);
                }
                break;
            default:
                throw new RuntimeException("Invalid local strategy for DataSinkTask");
        }
        // read the reader and write it to the output
        final TypeSerializer<IT> serializer = this.inputTypeSerializerFactory.getSerializer();
        final MutableObjectIterator<IT> input = input1;
        final OutputFormat<IT> format = this.format;
        // check if task has been canceled
        if (this.taskCanceled) {
            return;
        }
        LOG.debug(getLogString("Starting to produce output"));
        // open
        format.open(this.getEnvironment().getTaskInfo().getIndexOfThisSubtask(), this.getEnvironment().getTaskInfo().getNumberOfParallelSubtasks());
        if (objectReuseEnabled) {
            IT record = serializer.createInstance();
            // work!
            while (!this.taskCanceled && ((record = input.next(record)) != null)) {
                numRecordsIn.inc();
                format.writeRecord(record);
            }
        } else {
            IT record;
            // work!
            while (!this.taskCanceled && ((record = input.next()) != null)) {
                numRecordsIn.inc();
                format.writeRecord(record);
            }
        }
        // failed.
        if (!this.taskCanceled) {
            this.format.close();
            this.format = null;
        }
    } catch (Exception ex) {
        // make a best effort to clean up
        try {
            if (!cleanupCalled && format instanceof CleanupWhenUnsuccessful) {
                cleanupCalled = true;
                ((CleanupWhenUnsuccessful) format).tryCleanupOnError();
            }
        } catch (Throwable t) {
            LOG.error("Cleanup on error failed.", t);
        }
        ex = ExceptionInChainedStubException.exceptionUnwrap(ex);
        if (ex instanceof CancelTaskException) {
            // forward canceling exception
            throw ex;
        } else // drop, if the task was canceled
        if (!this.taskCanceled) {
            if (LOG.isErrorEnabled()) {
                LOG.error(getLogString("Error in user code: " + ex.getMessage()), ex);
            }
            throw ex;
        }
    } finally {
        if (this.format != null) {
            // This should only be the case if we had a previous error, or were canceled.
            try {
                this.format.close();
            } catch (Throwable t) {
                if (LOG.isWarnEnabled()) {
                    LOG.warn(getLogString("Error closing the output format"), t);
                }
            }
        }
        // close local strategy if necessary
        if (localStrategy != null) {
            try {
                this.localStrategy.close();
            } catch (Throwable t) {
                LOG.error("Error closing local strategy", t);
            }
        }
        BatchTask.clearReaders(new MutableReader<?>[] { inputReader });
    }
    if (!this.taskCanceled) {
        LOG.debug(getLogString("Finished data sink operator"));
    } else {
        LOG.debug(getLogString("Data sink operator cancelled"));
    }
}
Also used : ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) ExceptionInChainedStubException(org.apache.flink.runtime.operators.chaining.ExceptionInChainedStubException) CancelTaskException(org.apache.flink.runtime.execution.CancelTaskException) InternalOperatorIOMetricGroup(org.apache.flink.runtime.metrics.groups.InternalOperatorIOMetricGroup) SimpleCounter(org.apache.flink.metrics.SimpleCounter) Counter(org.apache.flink.metrics.Counter) SimpleCounter(org.apache.flink.metrics.SimpleCounter) CancelTaskException(org.apache.flink.runtime.execution.CancelTaskException) RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext) RichOutputFormat(org.apache.flink.api.common.io.RichOutputFormat) CleanupWhenUnsuccessful(org.apache.flink.api.common.io.CleanupWhenUnsuccessful)

Example 3 with CancelTaskException

use of org.apache.flink.runtime.execution.CancelTaskException in project flink by apache.

the class RemoteInputChannel method getNextBuffer.

@Override
Optional<BufferAndAvailability> getNextBuffer() throws IOException {
    checkPartitionRequestQueueInitialized();
    final SequenceBuffer next;
    final DataType nextDataType;
    synchronized (receivedBuffers) {
        next = receivedBuffers.poll();
        if (next != null) {
            totalQueueSizeInBytes -= next.buffer.getSize();
        }
        nextDataType = receivedBuffers.peek() != null ? receivedBuffers.peek().buffer.getDataType() : DataType.NONE;
    }
    if (next == null) {
        if (isReleased.get()) {
            throw new CancelTaskException("Queried for a buffer after channel has been released.");
        }
        return Optional.empty();
    }
    NetworkActionsLogger.traceInput("RemoteInputChannel#getNextBuffer", next.buffer, inputGate.getOwningTaskName(), channelInfo, channelStatePersister, next.sequenceNumber);
    numBytesIn.inc(next.buffer.getSize());
    numBuffersIn.inc();
    return Optional.of(new BufferAndAvailability(next.buffer, nextDataType, 0, next.sequenceNumber));
}
Also used : CancelTaskException(org.apache.flink.runtime.execution.CancelTaskException) DataType(org.apache.flink.runtime.io.network.buffer.Buffer.DataType)

Example 4 with CancelTaskException

use of org.apache.flink.runtime.execution.CancelTaskException in project flink by apache.

the class BufferManager method shouldContinueRequest.

private boolean shouldContinueRequest(BufferPool bufferPool) {
    if (bufferPool.addBufferListener(this)) {
        isWaitingForFloatingBuffers = true;
        numRequiredBuffers = 1;
        return false;
    } else if (bufferPool.isDestroyed()) {
        throw new CancelTaskException("Local buffer pool has already been released.");
    } else {
        return true;
    }
}
Also used : CancelTaskException(org.apache.flink.runtime.execution.CancelTaskException)

Example 5 with CancelTaskException

use of org.apache.flink.runtime.execution.CancelTaskException in project flink by apache.

the class TaskTest method testCancelTaskExceptionAfterTaskMarkedFailed.

@Test
public void testCancelTaskExceptionAfterTaskMarkedFailed() throws Exception {
    final Task task = createTaskBuilder().setInvokable(InvokableWithCancelTaskExceptionInInvoke.class).build();
    task.startTaskThread();
    // Wait till the task is in invoke.
    awaitLatch.await();
    task.failExternally(new Exception("external"));
    assertEquals(ExecutionState.FAILED, task.getExecutionState());
    // Either we cause the CancelTaskException or the TaskCanceler
    // by interrupting the invokable.
    triggerLatch.trigger();
    task.getExecutingThread().join();
    assertEquals(ExecutionState.FAILED, task.getExecutionState());
    assertTrue(task.isCanceledOrFailed());
    assertTrue(task.getFailureCause().getMessage().contains("external"));
}
Also used : TimeoutException(java.util.concurrent.TimeoutException) WrappingRuntimeException(org.apache.flink.util.WrappingRuntimeException) CancelTaskException(org.apache.flink.runtime.execution.CancelTaskException) ExpectedTestException(org.apache.flink.runtime.operators.testutils.ExpectedTestException) FlinkException(org.apache.flink.util.FlinkException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) PartitionProducerDisposedException(org.apache.flink.runtime.jobmanager.PartitionProducerDisposedException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

CancelTaskException (org.apache.flink.runtime.execution.CancelTaskException)16 IOException (java.io.IOException)6 Test (org.junit.Test)6 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)4 ResultSubpartitionView (org.apache.flink.runtime.io.network.partition.ResultSubpartitionView)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 TimeoutException (java.util.concurrent.TimeoutException)2 RuntimeContext (org.apache.flink.api.common.functions.RuntimeContext)2 Path (org.apache.flink.core.fs.Path)2 Counter (org.apache.flink.metrics.Counter)2 SimpleCounter (org.apache.flink.metrics.SimpleCounter)2 CheckpointException (org.apache.flink.runtime.checkpoint.CheckpointException)2 Environment (org.apache.flink.runtime.execution.Environment)2 ExecutionState (org.apache.flink.runtime.execution.ExecutionState)2 Buffer (org.apache.flink.runtime.io.network.buffer.Buffer)2 BufferProvider (org.apache.flink.runtime.io.network.buffer.BufferProvider)2