Search in sources :

Example 6 with LifecycleStop

use of io.druid.java.util.common.lifecycle.LifecycleStop in project druid by druid-io.

the class ThreadPoolTaskRunner method stop.

@Override
@LifecycleStop
public void stop() {
    stopping = true;
    for (Map.Entry<Integer, ListeningExecutorService> entry : exec.entrySet()) {
        try {
            entry.getValue().shutdown();
        } catch (SecurityException ex) {
            log.wtf(ex, "I can't control my own threads!");
        }
    }
    for (ThreadPoolTaskRunnerWorkItem item : runningItems) {
        final Task task = item.getTask();
        final long start = System.currentTimeMillis();
        final boolean graceful;
        final long elapsed;
        boolean error = false;
        if (taskConfig.isRestoreTasksOnRestart() && task.canRestore()) {
            // Attempt graceful shutdown.
            graceful = true;
            log.info("Starting graceful shutdown of task[%s].", task.getId());
            try {
                task.stopGracefully();
                final TaskStatus taskStatus = item.getResult().get(new Interval(new DateTime(start), taskConfig.getGracefulShutdownTimeout()).toDurationMillis(), TimeUnit.MILLISECONDS);
                // Ignore status, it doesn't matter for graceful shutdowns.
                log.info("Graceful shutdown of task[%s] finished in %,dms.", task.getId(), System.currentTimeMillis() - start);
                TaskRunnerUtils.notifyStatusChanged(listeners, task.getId(), taskStatus);
            } catch (Exception e) {
                log.makeAlert(e, "Graceful task shutdown failed: %s", task.getDataSource()).addData("taskId", task.getId()).addData("dataSource", task.getDataSource()).emit();
                log.warn(e, "Graceful shutdown of task[%s] aborted with exception.", task.getId());
                error = true;
                TaskRunnerUtils.notifyStatusChanged(listeners, task.getId(), TaskStatus.failure(task.getId()));
            }
        } else {
            graceful = false;
            TaskRunnerUtils.notifyStatusChanged(listeners, task.getId(), TaskStatus.failure(task.getId()));
        }
        elapsed = System.currentTimeMillis() - start;
        final ServiceMetricEvent.Builder metricBuilder = ServiceMetricEvent.builder().setDimension("task", task.getId()).setDimension("dataSource", task.getDataSource()).setDimension("graceful", String.valueOf(graceful)).setDimension("error", String.valueOf(error));
        emitter.emit(metricBuilder.build("task/interrupt/count", 1L));
        emitter.emit(metricBuilder.build("task/interrupt/elapsed", elapsed));
    }
    // Ok, now interrupt everything.
    for (Map.Entry<Integer, ListeningExecutorService> entry : exec.entrySet()) {
        try {
            entry.getValue().shutdownNow();
        } catch (SecurityException ex) {
            log.wtf(ex, "I can't control my own threads!");
        }
    }
}
Also used : Task(io.druid.indexing.common.task.Task) TaskStatus(io.druid.indexing.common.TaskStatus) DateTime(org.joda.time.DateTime) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ServiceMetricEvent(com.metamx.emitter.service.ServiceMetricEvent) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Interval(org.joda.time.Interval) LifecycleStop(io.druid.java.util.common.lifecycle.LifecycleStop)

Aggregations

LifecycleStop (io.druid.java.util.common.lifecycle.LifecycleStop)6 Closer (com.google.common.io.Closer)3 IOException (java.io.IOException)2 Map (java.util.Map)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 KeeperException (org.apache.zookeeper.KeeperException)2 DateTime (org.joda.time.DateTime)2 Interval (org.joda.time.Interval)2 ListenableScheduledFuture (com.google.common.util.concurrent.ListenableScheduledFuture)1 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 ServiceMetricEvent (com.metamx.emitter.service.ServiceMetricEvent)1 TaskStatus (io.druid.indexing.common.TaskStatus)1 Task (io.druid.indexing.common.task.Task)1 MalformedURLException (java.net.MalformedURLException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 CuratorTransaction (org.apache.curator.framework.api.transaction.CuratorTransaction)1 CuratorTransactionFinal (org.apache.curator.framework.api.transaction.CuratorTransactionFinal)1 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)1