Example usage for com.google.common.util.concurrent ListenableFuture cancel

List of usage examples for com.google.common.util.concurrent ListenableFuture cancel

Introduction

In this page you can find the example usage for com.google.common.util.concurrent ListenableFuture cancel.

Prototype

boolean cancel(boolean mayInterruptIfRunning);

Source Link

Document

Attempts to cancel execution of this task.

Usage

From source file:io.druid.query.GroupByMergedQueryRunner.java

private void waitForFutureCompletion(GroupByQuery query, ListenableFuture<?> future,
        IncrementalIndex<?> closeOnFailure) {
    try {/*from   w  w  w .ja  v  a2 s .com*/
        queryWatcher.registerQuery(query, future);
        final Number timeout = query.getContextValue(QueryContextKeys.TIMEOUT, (Number) null);
        if (timeout == null) {
            future.get();
        } else {
            future.get(timeout.longValue(), TimeUnit.MILLISECONDS);
        }
    } catch (InterruptedException e) {
        log.warn(e, "Query interrupted, cancelling pending results, query id [%s]", query.getId());
        future.cancel(true);
        closeOnFailure.close();
        throw new QueryInterruptedException(e);
    } catch (CancellationException e) {
        closeOnFailure.close();
        throw new QueryInterruptedException(e);
    } catch (TimeoutException e) {
        closeOnFailure.close();
        log.info("Query timeout, cancelling pending results for query id [%s]", query.getId());
        future.cancel(true);
        throw new QueryInterruptedException(e);
    } catch (ExecutionException e) {
        closeOnFailure.close();
        throw Throwables.propagate(e.getCause());
    }
}

From source file:io.prestosql.execution.scheduler.SqlQueryScheduler.java

private void schedule() {
    try (SetThreadName ignored = new SetThreadName("Query-%s", queryStateMachine.getQueryId())) {
        Set<StageId> completedStages = new HashSet<>();
        ExecutionSchedule executionSchedule = executionPolicy.createExecutionSchedule(stages.values());
        while (!executionSchedule.isFinished()) {
            List<ListenableFuture<?>> blockedStages = new ArrayList<>();
            for (SqlStageExecution stage : executionSchedule.getStagesToSchedule()) {
                stage.beginScheduling();

                // perform some scheduling work
                ScheduleResult result = stageSchedulers.get(stage.getStageId()).schedule();

                // modify parent and children based on the results of the scheduling
                if (result.isFinished()) {
                    stage.schedulingComplete();
                } else if (!result.getBlocked().isDone()) {
                    blockedStages.add(result.getBlocked());
                }//from   w  w  w.j ava  2s.c  o m
                stageLinkages.get(stage.getStageId()).processScheduleResults(stage.getState(),
                        result.getNewTasks());
                schedulerStats.getSplitsScheduledPerIteration().add(result.getSplitsScheduled());
                if (result.getBlockedReason().isPresent()) {
                    switch (result.getBlockedReason().get()) {
                    case WRITER_SCALING:
                        // no-op
                        break;
                    case WAITING_FOR_SOURCE:
                        schedulerStats.getWaitingForSource().update(1);
                        break;
                    case SPLIT_QUEUES_FULL:
                        schedulerStats.getSplitQueuesFull().update(1);
                        break;
                    case MIXED_SPLIT_QUEUES_FULL_AND_WAITING_FOR_SOURCE:
                    case NO_ACTIVE_DRIVER_GROUP:
                        break;
                    default:
                        throw new UnsupportedOperationException(
                                "Unknown blocked reason: " + result.getBlockedReason().get());
                    }
                }
            }

            // make sure to update stage linkage at least once per loop to catch async state changes (e.g., partial cancel)
            for (SqlStageExecution stage : stages.values()) {
                if (!completedStages.contains(stage.getStageId()) && stage.getState().isDone()) {
                    stageLinkages.get(stage.getStageId()).processScheduleResults(stage.getState(),
                            ImmutableSet.of());
                    completedStages.add(stage.getStageId());
                }
            }

            // wait for a state change and then schedule again
            if (!blockedStages.isEmpty()) {
                try (TimeStat.BlockTimer timer = schedulerStats.getSleepTime().time()) {
                    tryGetFutureValue(whenAnyComplete(blockedStages), 1, SECONDS);
                }
                for (ListenableFuture<?> blockedStage : blockedStages) {
                    blockedStage.cancel(true);
                }
            }
        }

        for (SqlStageExecution stage : stages.values()) {
            StageState state = stage.getState();
            if (state != SCHEDULED && state != RUNNING && !state.isDone()) {
                throw new PrestoException(GENERIC_INTERNAL_ERROR, format(
                        "Scheduling is complete, but stage %s is in state %s", stage.getStageId(), state));
            }
        }
    } catch (Throwable t) {
        queryStateMachine.transitionToFailed(t);
        throw t;
    } finally {
        RuntimeException closeError = new RuntimeException();
        for (StageScheduler scheduler : stageSchedulers.values()) {
            try {
                scheduler.close();
            } catch (Throwable t) {
                queryStateMachine.transitionToFailed(t);
                // Self-suppression not permitted
                if (closeError != t) {
                    closeError.addSuppressed(t);
                }
            }
        }
        if (closeError.getSuppressed().length > 0) {
            throw closeError;
        }
    }
}

From source file:com.yahoo.yqlplus.engine.internal.java.runtime.TimeoutHandler.java

public <T> ListenableFuture<T> withTimeout(final ListenableFuture<T> source, long timeout,
        TimeUnit timeoutUnits) {//from   www  .j  a va2s. c  o  m
    if (timeout != 0) {
        final SettableFuture<T> result = SettableFuture.create();
        final Future<?> scheduledFuture = timers
                .schedule(new TimeoutTask<T>(source, result, timeout, timeoutUnits), timeout, timeoutUnits);
        result.addListener(new Runnable() {
            @Override
            public void run() {
                scheduledFuture.cancel(false);
                if (result.isCancelled()) {
                    source.cancel(true);
                }
            }
        }, MoreExecutors.sameThreadExecutor());
        Futures.addCallback(source, new FutureCallback<T>() {
            @Override
            public void onSuccess(T out) {
                scheduledFuture.cancel(false);
                result.set(out);
            }

            @Override
            public void onFailure(Throwable t) {
                scheduledFuture.cancel(false);
                result.setException(t);
            }
        });
        return scoper.scopeCallbacks(result);
    } else {
        return source;
    }
}

From source file:com.rackspacecloud.blueflood.outputs.handlers.RollupHandler.java

public Map<Locator, MetricData> getRollupByGranularity(final String tenantId, final List<String> metrics,
        final long from, final long to, final Granularity g) {

    final Timer.Context ctx = metrics.size() == 1 ? plotTimers.SPLOT_TIMER.timer.time()
            : plotTimers.MPLOT_TIMER.timer.time();
    Future<List<SearchResult>> unitsFuture = null;
    List<SearchResult> units = null;
    List<Locator> locators = new ArrayList<Locator>();

    Timer.Context c = timerRorCalcUnits.time();

    for (String metric : metrics) {
        locators.add(Locator.createLocatorFromPathComponents(tenantId, metric));
    }//from w  ww. j a  v  a 2  s .co  m

    queriesSizeHist.update(locators.size());

    if (Util.shouldUseESForUnits()) {
        unitsFuture = ESUnitExecutor.submit(new Callable() {

            @Override
            public List<SearchResult> call() throws Exception {
                DiscoveryIO discoveryIO = (DiscoveryIO) ModuleLoader.getInstance(DiscoveryIO.class,
                        CoreConfig.DISCOVERY_MODULES);

                if (discoveryIO == null) {
                    log.warn("USE_ES_FOR_UNITS has been set to true, but no discovery module found."
                            + " Please check your config");
                    return null;
                }
                return discoveryIO.search(tenantId, metrics);
            }
        });
    }

    MetricsRWDelegator delegator = new MetricsRWDelegator();
    final Map<Locator, MetricData> metricDataMap = delegator.getDatapointsForRange(locators,
            new Range(g.snapMillis(from), to), g);

    if (unitsFuture != null) {
        try {
            units = unitsFuture.get();
            for (SearchResult searchResult : units) {
                Locator locator = Locator.createLocatorFromPathComponents(searchResult.getTenantId(),
                        searchResult.getMetricName());
                if (metricDataMap.containsKey(locator))
                    metricDataMap.get(locator).setUnit(searchResult.getUnit());
            }
        } catch (Exception e) {
            log.warn(
                    "Exception encountered while getting units from ES, unit will be set to unknown in query results",
                    e);
        }
    }

    c.stop();

    if (locators.size() == 1) {
        for (final Map.Entry<Locator, MetricData> metricData : metricDataMap.entrySet()) {
            Timer.Context context = rollupsOnReadTimers.RR_SPLOT_TIMER.timer.time();
            repairMetrics(metricData.getKey(), metricData.getValue(), from, to, g);
            context.stop();
        }
    } else if (locators.size() > 1
            && Configuration.getInstance().getBooleanProperty(CoreConfig.TURN_OFF_RR_MPLOT) == false) {
        Timer.Context context = rollupsOnReadTimers.RR_MPLOT_TIMER.timer.time();
        ArrayList<ListenableFuture<Boolean>> futures = new ArrayList<ListenableFuture<Boolean>>();
        for (final Map.Entry<Locator, MetricData> metricData : metricDataMap.entrySet()) {
            futures.add(rollupsOnReadExecutor.submit(new Callable<Boolean>() {
                @Override
                public Boolean call() {
                    return repairMetrics(metricData.getKey(), metricData.getValue(), from, to, g);
                }
            }));
        }
        ListenableFuture<List<Boolean>> aggregateFuture = Futures.allAsList(futures);
        try {
            aggregateFuture.get(rollupOnReadTimeout.getValue(), rollupOnReadTimeout.getUnit());
        } catch (Exception e) {
            aggregateFuture.cancel(true);
            exceededQueryTimeout.mark();
            log.warn("Exception encountered while doing rollups on read, incomplete rollups will be returned.",
                    e);
        }
        context.stop();
    }

    for (MetricData metricData : metricDataMap.values()) {

        // we used to track enum queries here,
        // but since enum is removed, this currently is
        // a no op, doesn't track any queries
        markQueryByRollupType(metricData);
    }

    ctx.stop();
    return metricDataMap;
}

From source file:io.druid.indexing.worker.http.TaskManagementResource.java

/**
 * This endpoint is used by HttpRemoteTaskRunner to keep an up-to-date state of the worker wrt to the tasks it is
 * running, completed etc and other metadata such as its enabled/disabled status.
 *
 * Here is how, this is used.// ww w .  ja  v a 2 s  . c o m
 *
 * (1) Client sends first request /druid/internal/v1/worker?counter=-1&timeout=<timeout>
 * Server responds with current list of running/completed tasks and metadata. And, a <counter,hash> pair.
 *
 * (2) Client sends subsequent requests /druid/internal/v1/worker?counter=<counter>&hash=<hash>&timeout=<timeout>
 * Where <counter,hash> values are used from the last response. Server responds with changes since then.
 *
 * This endpoint makes the client wait till either there is some update or given timeout elapses.
 *
 * So, clients keep on sending next request immediately after receiving the response in order to keep the state of
 * this server up-to-date.
 *
 * @param counter counter received in last response.
 * @param hash hash received in last response.
 * @param timeout after which response is sent even if there are no new segment updates.
 * @param req
 * @throws IOException
 */
@GET
@Produces({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE })
public void getWorkerState(@QueryParam("counter") long counter, @QueryParam("hash") long hash,
        @QueryParam("timeout") long timeout, @Context final HttpServletRequest req) throws IOException {
    if (timeout <= 0) {
        sendErrorResponse(req, HttpServletResponse.SC_BAD_REQUEST, "timeout must be positive.");
        return;
    }

    final ResponseContext context = createContext(req.getHeader("Accept"));

    final ListenableFuture<ChangeRequestsSnapshot<WorkerHistoryItem>> future = workerTaskMonitor
            .getChangesSince(new ChangeRequestHistory.Counter(counter, hash));

    final AsyncContext asyncContext = req.startAsync();

    asyncContext.addListener(new AsyncListener() {
        @Override
        public void onComplete(AsyncEvent event) {
        }

        @Override
        public void onTimeout(AsyncEvent event) {

            // HTTP 204 NO_CONTENT is sent to the client.
            future.cancel(true);
            event.getAsyncContext().complete();
        }

        @Override
        public void onError(AsyncEvent event) {
        }

        @Override
        public void onStartAsync(AsyncEvent event) {
        }
    });

    Futures.addCallback(future, new FutureCallback<ChangeRequestsSnapshot>() {
        @Override
        public void onSuccess(ChangeRequestsSnapshot result) {
            try {
                HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse();
                response.setStatus(HttpServletResponse.SC_OK);
                context.inputMapper.writerWithType(WorkerHolder.WORKER_SYNC_RESP_TYPE_REF)
                        .writeValue(asyncContext.getResponse().getOutputStream(), result);
                asyncContext.complete();
            } catch (Exception ex) {
                log.debug(ex, "Request timed out or closed already.");
            }
        }

        @Override
        public void onFailure(Throwable th) {
            try {
                HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse();
                if (th instanceof IllegalArgumentException) {
                    response.sendError(HttpServletResponse.SC_BAD_REQUEST, th.getMessage());
                } else {
                    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, th.getMessage());
                }
                asyncContext.complete();
            } catch (Exception ex) {
                log.debug(ex, "Request timed out or closed already.");
            }
        }
    });

    asyncContext.setTimeout(timeout);
}

From source file:co.cask.cdap.app.runtime.spark.SparkRuntimeService.java

@Override
protected void triggerShutdown() {
    LOG.debug("Stop requested for Spark Program {}", runtimeContext);
    // Replace the completion future with a cancelled one.
    // Also, try to cancel the current completion future if it exists.
    ListenableFuture<RunId> future = completion.getAndSet(this.<RunId>immediateCancelledFuture());
    if (future != null) {
        future.cancel(true);
    }/* w  w w .j a  v  a2  s .c  o m*/
}

From source file:org.jclouds.samples.googleappengine.GetAllResourcesController.java

private void blockUntilAllDoneOrCancelOnTimeout(Iterable<? extends ListenableFuture<?>> asyncResources) {
    try {/*from   w  w  w .  j a v  a 2  s . c  o  m*/
        for (ListenableFuture<?> asyncResource : asyncResources) {
            if (remainingMillis.get() > 0) {
                try {
                    asyncResource.get(remainingMillis.get(), TimeUnit.MILLISECONDS);
                } catch (Exception e) {
                    logger.info("exception getting resource %s: %s", asyncResource, e.getMessage());
                }
            }
        }
    } finally {
        if (remainingMillis.get() < 0) {
            for (ListenableFuture<?> asyncResource : asyncResources) {
                if (!asyncResource.isDone())
                    asyncResource.cancel(true);
            }
        }
    }

}

From source file:org.gradle.internal.filewatch.FileSystemChangeWaiter.java

@Override
public void execute(FileSystemSubset taskFileSystemInputs, Runnable notifier) {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    final StoppableExecutor executorService = executorFactory.create("quiet period waiter");

    FileWatcher watcher = fileWatcherFactory.watch(taskFileSystemInputs, new Action<Throwable>() {
        @Override/*  w  w  w. j  av  a2s  .c  om*/
        public void execute(Throwable throwable) {
            error.set(throwable);
            latch.countDown();
        }
    }, new FileWatcherListener() {
        private IdleTimeout timeout;

        @Override
        public void onChange(final FileWatcher watcher, FileWatcherEvent event) {
            if (timeout == null) {
                timeout = new IdleTimeout(QUIET_PERIOD, new Runnable() {
                    @Override
                    public void run() {
                        watcher.stop();
                        latch.countDown();
                    }
                });
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        timeout.await();
                    }
                });
            }
            timeout.tick();
        }
    });

    final StoppableExecutor keyboardHandlerExecutor = executorFactory
            .create("Continuous building keyboard handler");
    ListenableFuture<Boolean> keyboardHandlerFuture = submitAsyncKeyboardHandler(
            MoreExecutors.listeningDecorator(keyboardHandlerExecutor), latch);

    try {
        notifier.run();
        latch.await();
        Throwable throwable = error.get();
        if (throwable != null) {
            throw UncheckedException.throwAsUncheckedException(throwable);
        }
    } catch (Exception e) {
        throw UncheckedException.throwAsUncheckedException(e);
    } finally {
        if (!keyboardHandlerFuture.isDone()) {
            keyboardHandlerFuture.cancel(true);
        } else if (Futures.getUnchecked(keyboardHandlerFuture)) {
            cancellationRequested.set(true);
        }
        CompositeStoppable.stoppable(watcher, executorService, keyboardHandlerExecutor).stop();
    }
}

From source file:com.rackspacecloud.blueflood.outputs.handlers.RollupHandler.java

/**
 * This method gets the points from the DB and then rolls them up according to the granularity.
 *
 * Breaks up the number of ranges into buckets based on ROLLUP_ON_READ_REPAIR_SIZE_PER_THREAD and executes
 * the buckets in parallel./*from w  w w  . j  av a2 s. c  om*/
 *
 * @param locator metric key within the DB
 * @param g the granularity
 * @param from the starting timestamp of the range (ms)
 * @param to the ending timestamp of the range (ms)
 *
 * @return a list of rolled-up points
 */
private List<Points.Point> repairRollupsOnRead(final Locator locator, Granularity g, long from, long to) {
    Timer.Context c = timerRepairRollupsOnRead.time();

    List<Points.Point> repairedPoints = new ArrayList<Points.Point>();
    List<ListenableFuture<List<Points.Point>>> futures = new ArrayList<ListenableFuture<List<Points.Point>>>();

    for (final Iterable<Range> ranges : divideRangesByGroup(g, from, to)) {
        futures.add(

                createRepairPointsExecutor.submit(new Callable() {

                    @Override
                    public List<Points.Point> call() throws Exception {
                        return createRepairPoints(ranges, locator);
                    }
                }));
    }

    ListenableFuture<List<List<Points.Point>>> aggregateFuture = Futures.allAsList(futures);

    try {
        for (List<Points.Point> subList : aggregateFuture.get(rollupOnReadTimeout.getValue(),
                rollupOnReadTimeout.getUnit())) {

            repairedPoints.addAll(subList);
        }
    } catch (Exception e) {
        aggregateFuture.cancel(true);
        exceededQueryTimeout.mark();
        log.warn("Exception encountered while doing rollups on read, incomplete rollups will be returned.", e);
    }

    c.stop();

    return repairedPoints;
}

From source file:org.apache.druid.server.http.SegmentListerResource.java

/**
 * This endpoint is used by HttpLoadQueuePeon to assign segment load/drop requests batch. This endpoint makes the
 * client wait till one of the following events occur. Note that this is implemented using async IO so no jetty
 * threads are held while in wait./*  w  ww  .j ava2s  .c om*/
 *
 * (1) Given timeout elapses.
 * (2) Some load/drop request completed.
 *
 * It returns a map of "load/drop request -> SUCCESS/FAILED/PENDING status" for each request in the batch.
 */
@POST
@Path("/changeRequests")
@Produces({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE })
@Consumes({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE })
public void applyDataSegmentChangeRequests(@QueryParam("timeout") long timeout,
        List<DataSegmentChangeRequest> changeRequestList, @Context final HttpServletRequest req)
        throws IOException {
    if (loadDropRequestHandler == null) {
        sendErrorResponse(req, HttpServletResponse.SC_NOT_FOUND, "load/drop handler is not available.");
        return;
    }

    if (timeout <= 0) {
        sendErrorResponse(req, HttpServletResponse.SC_BAD_REQUEST, "timeout must be positive.");
        return;
    }

    if (changeRequestList == null || changeRequestList.isEmpty()) {
        sendErrorResponse(req, HttpServletResponse.SC_BAD_REQUEST, "No change requests provided.");
        return;
    }

    final ResponseContext context = createContext(req.getHeader("Accept"));
    final ListenableFuture<List<SegmentLoadDropHandler.DataSegmentChangeRequestAndStatus>> future = loadDropRequestHandler
            .processBatch(changeRequestList);

    final AsyncContext asyncContext = req.startAsync();

    asyncContext.addListener(new AsyncListener() {
        @Override
        public void onComplete(AsyncEvent event) {
        }

        @Override
        public void onTimeout(AsyncEvent event) {

            // HTTP 204 NO_CONTENT is sent to the client.
            future.cancel(true);
            event.getAsyncContext().complete();
        }

        @Override
        public void onError(AsyncEvent event) {
        }

        @Override
        public void onStartAsync(AsyncEvent event) {
        }
    });

    Futures.addCallback(future,
            new FutureCallback<List<SegmentLoadDropHandler.DataSegmentChangeRequestAndStatus>>() {
                @Override
                public void onSuccess(List<SegmentLoadDropHandler.DataSegmentChangeRequestAndStatus> result) {
                    try {
                        HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse();
                        response.setStatus(HttpServletResponse.SC_OK);
                        context.inputMapper.writerWithType(HttpLoadQueuePeon.RESPONSE_ENTITY_TYPE_REF)
                                .writeValue(asyncContext.getResponse().getOutputStream(), result);
                        asyncContext.complete();
                    } catch (Exception ex) {
                        log.debug(ex, "Request timed out or closed already.");
                    }
                }

                @Override
                public void onFailure(Throwable th) {
                    try {
                        HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse();
                        if (th instanceof IllegalArgumentException) {
                            response.sendError(HttpServletResponse.SC_BAD_REQUEST, th.getMessage());
                        } else {
                            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, th.getMessage());
                        }
                        asyncContext.complete();
                    } catch (Exception ex) {
                        log.debug(ex, "Request timed out or closed already.");
                    }
                }
            });

    asyncContext.setTimeout(timeout);
}