Search in sources :

Example 1 with DispatcherThreadFactory

use of org.apache.flink.runtime.taskmanager.DispatcherThreadFactory in project flink by apache.

the class StreamTask method invoke.

@Override
public final void invoke() throws Exception {
    boolean disposed = false;
    try {
        // -------- Initialize ---------
        LOG.debug("Initializing {}.", getName());
        asyncOperationsThreadPool = Executors.newCachedThreadPool();
        configuration = new StreamConfig(getTaskConfiguration());
        stateBackend = createStateBackend();
        accumulatorMap = getEnvironment().getAccumulatorRegistry().getUserMap();
        // if the clock is not already set, then assign a default TimeServiceProvider
        if (timerService == null) {
            ThreadFactory timerThreadFactory = new DispatcherThreadFactory(TRIGGER_THREAD_GROUP, "Time Trigger for " + getName());
            timerService = new SystemProcessingTimeService(this, getCheckpointLock(), timerThreadFactory);
        }
        operatorChain = new OperatorChain<>(this);
        headOperator = operatorChain.getHeadOperator();
        // task specific initialization
        init();
        // save the work of reloading state, etc, if the task is already canceled
        if (canceled) {
            throw new CancelTaskException();
        }
        // -------- Invoke --------
        LOG.debug("Invoking {}", getName());
        // executed before all operators are opened
        synchronized (lock) {
            // both the following operations are protected by the lock
            // so that we avoid race conditions in the case that initializeState()
            // registers a timer, that fires before the open() is called.
            initializeState();
            openAllOperators();
        }
        // final check to exit early before starting to run
        if (canceled) {
            throw new CancelTaskException();
        }
        // let the task do its work
        isRunning = true;
        run();
        // make sure the "clean shutdown" is not attempted
        if (canceled) {
            throw new CancelTaskException();
        }
        // make sure all timers finish and no new timers can come
        timerService.quiesceAndAwaitPending();
        LOG.debug("Finished task {}", getName());
        // at the same time, this makes sure that during any "regular" exit where still
        synchronized (lock) {
            isRunning = false;
            // this is part of the main logic, so if this fails, the task is considered failed
            closeAllOperators();
        }
        LOG.debug("Closed operators for task {}", getName());
        // make sure all buffered data is flushed
        operatorChain.flushOutputs();
        // make an attempt to dispose the operators such that failures in the dispose call
        // still let the computation fail
        tryDisposeAllOperators();
        disposed = true;
    } finally {
        // clean up everything we initialized
        isRunning = false;
        // stop all timers and threads
        if (timerService != null) {
            try {
                timerService.shutdownService();
            } catch (Throwable t) {
                // catch and log the exception to not replace the original exception
                LOG.error("Could not shut down timer service", t);
            }
        }
        // stop all asynchronous checkpoint threads
        try {
            cancelables.close();
            shutdownAsyncThreads();
        } catch (Throwable t) {
            // catch and log the exception to not replace the original exception
            LOG.error("Could not shut down async checkpoint threads", t);
        }
        // we must! perform this cleanup
        try {
            cleanup();
        } catch (Throwable t) {
            // catch and log the exception to not replace the original exception
            LOG.error("Error during cleanup of stream task", t);
        }
        // if the operators were not disposed before, do a hard dispose
        if (!disposed) {
            disposeAllOperators();
        }
        // release the output resources. this method should never fail.
        if (operatorChain != null) {
            operatorChain.releaseOutputs();
        }
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) DispatcherThreadFactory(org.apache.flink.runtime.taskmanager.DispatcherThreadFactory) DispatcherThreadFactory(org.apache.flink.runtime.taskmanager.DispatcherThreadFactory) CancelTaskException(org.apache.flink.runtime.execution.CancelTaskException) StreamConfig(org.apache.flink.streaming.api.graph.StreamConfig)

Aggregations

ThreadFactory (java.util.concurrent.ThreadFactory)1 CancelTaskException (org.apache.flink.runtime.execution.CancelTaskException)1 DispatcherThreadFactory (org.apache.flink.runtime.taskmanager.DispatcherThreadFactory)1 StreamConfig (org.apache.flink.streaming.api.graph.StreamConfig)1