List of usage examples for com.google.common.util.concurrent ListenableFuture isDone
boolean isDone();
From source file:com.facebook.presto.operator.Driver.java
public ListenableFuture<?> processFor(Duration duration) { checkLockNotHeld("Can not process for a duration while holding the driver lock"); requireNonNull(duration, "duration is null"); long maxRuntime = duration.roundTo(TimeUnit.NANOSECONDS); try (DriverLockResult lockResult = tryLockAndProcessPendingStateChanges(100, TimeUnit.MILLISECONDS)) { if (lockResult.wasAcquired()) { driverContext.startProcessTimer(); try { long start = System.nanoTime(); do { ListenableFuture<?> future = processInternal(); if (!future.isDone()) { return future; }//w w w . j av a 2 s .co m } while (System.nanoTime() - start < maxRuntime && !isFinishedInternal()); } finally { driverContext.recordProcessed(); } } } return NOT_BLOCKED; }
From source file:org.apache.qpid.server.txn.AsyncAutoCommitTransaction.java
private void addEnqueueFuture(final ListenableFuture<Void> future, final Action action, boolean persistent) { if (action != null) { // For persistent messages, do not synchronously invoke postCommit even if the future is completed. // Otherwise, postCommit (which actually does the enqueuing) might be called on successive messages out of order. if (future.isDone() && !persistent && !_strictOrderWithMixedDeliveryMode) { action.postCommit();/* w w w.j a v a 2 s.c om*/ } else { _futureRecorder.recordFuture(future, action); } } }
From source file:com.facebook.presto.operator.Driver.java
private ListenableFuture<?> processInternal() { checkLockHeld("Lock must be held to call processInternal"); try {//w w w .j av a 2 s .co m if (!newSources.isEmpty()) { processNewSources(); } // special handling for drivers with a single operator if (operators.size() == 1) { if (driverContext.isDone()) { return NOT_BLOCKED; } // check if operator is blocked Operator current = operators.get(0); ListenableFuture<?> blocked = isBlocked(current); if (!blocked.isDone()) { current.getOperatorContext().recordBlocked(blocked); return blocked; } // there is only one operator so just finish it current.getOperatorContext().startIntervalTimer(); current.finish(); current.getOperatorContext().recordFinish(); return NOT_BLOCKED; } boolean movedPage = false; for (int i = 0; i < operators.size() - 1 && !driverContext.isDone(); i++) { Operator current = operators.get(i); Operator next = operators.get(i + 1); // skip blocked operators if (!isBlocked(current).isDone()) { continue; } if (!isBlocked(next).isDone()) { continue; } // if the current operator is not finished and next operator needs input... if (!current.isFinished() && next.needsInput()) { // get an output page from current operator current.getOperatorContext().startIntervalTimer(); Page page = current.getOutput(); current.getOperatorContext().recordGetOutput(page); // if we got an output page, add it to the next operator if (page != null) { next.getOperatorContext().startIntervalTimer(); next.addInput(page); next.getOperatorContext().recordAddInput(page); movedPage = true; } } // if current operator is finished... if (current.isFinished()) { // let next operator know there will be no more data next.getOperatorContext().startIntervalTimer(); next.finish(); next.getOperatorContext().recordFinish(); } } // if we did not move any pages, check if we are blocked if (!movedPage) { List<Operator> blockedOperators = new ArrayList<>(); List<ListenableFuture<?>> blockedFutures = new ArrayList<>(); for (Operator operator : operators) { ListenableFuture<?> blocked = isBlocked(operator); if (!blocked.isDone()) { blockedOperators.add(operator); blockedFutures.add(blocked); } } if (!blockedFutures.isEmpty()) { // unblock when the first future is complete ListenableFuture<?> blocked = firstFinishedFuture(blockedFutures); // driver records serial blocked time driverContext.recordBlocked(blocked); // each blocked operator is responsible for blocking the execution // until one of the operators can continue for (Operator operator : blockedOperators) { operator.getOperatorContext().recordBlocked(blocked); } return blocked; } } return NOT_BLOCKED; } catch (Throwable t) { driverContext.failed(t); throw t; } }
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//from w ww . j av a 2 s . 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:org.jclouds.samples.googleappengine.GetAllResourcesController.java
private void blockUntilAllDoneOrCancelOnTimeout(Iterable<? extends ListenableFuture<?>> asyncResources) { try {/*www . j a va 2 s . c om*/ 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:io.prestosql.operator.Driver.java
@GuardedBy("exclusiveLock") private void checkOperatorFinishedRevoking(Operator operator) { ListenableFuture<?> future = revokingOperators.get(operator); if (future.isDone()) { getFutureValue(future); // propagate exception if there was some revokingOperators.remove(operator); operator.finishMemoryRevoke();//from ww w . j a v a2 s.co m operator.getOperatorContext().resetMemoryRevokingRequested(); } }
From source file:io.prestosql.operator.Driver.java
public ListenableFuture<?> processFor(Duration duration) { checkLockNotHeld("Can not process for a duration while holding the driver lock"); requireNonNull(duration, "duration is null"); // if the driver is blocked we don't need to continue SettableFuture<?> blockedFuture = driverBlockedFuture.get(); if (!blockedFuture.isDone()) { return blockedFuture; }/*from www. j a v a 2s .c o m*/ long maxRuntime = duration.roundTo(TimeUnit.NANOSECONDS); Optional<ListenableFuture<?>> result = tryWithLock(100, TimeUnit.MILLISECONDS, () -> { OperationTimer operationTimer = createTimer(); driverContext.startProcessTimer(); driverContext.getYieldSignal().setWithDelay(maxRuntime, driverContext.getYieldExecutor()); try { long start = System.nanoTime(); do { ListenableFuture<?> future = processInternal(operationTimer); if (!future.isDone()) { return updateDriverBlockedFuture(future); } } while (System.nanoTime() - start < maxRuntime && !isFinishedInternal()); } finally { driverContext.getYieldSignal().reset(); driverContext.recordProcessed(operationTimer); } return NOT_BLOCKED; }); return result.orElse(NOT_BLOCKED); }
From source file:org.apache.brooklyn.core.mgmt.persist.BrooklynMementoPersisterToObjectStore.java
protected void visitMemento(final String phase, final BrooklynMementoRawData rawData, final Visitor visitor, final RebindExceptionHandler exceptionHandler) { List<ListenableFuture<?>> futures = Lists.newArrayList(); class VisitorWrapper implements Runnable { private final BrooklynObjectType type; private final Map.Entry<String, String> objectIdAndData; public VisitorWrapper(BrooklynObjectType type, Map.Entry<String, String> objectIdAndData) { this.type = type; this.objectIdAndData = objectIdAndData; }/*from w w w .j av a 2 s.c o m*/ public void run() { try { visitor.visit(type, objectIdAndData.getKey(), objectIdAndData.getValue()); } catch (Exception e) { Exceptions.propagateIfFatal(e); exceptionHandler.onLoadMementoFailed(type, "memento " + objectIdAndData.getKey() + " " + phase + " error", e); } } } for (BrooklynObjectType type : BrooklynPersistenceUtils.STANDARD_BROOKLYN_OBJECT_TYPE_PERSISTENCE_ORDER) { for (final Map.Entry<String, String> entry : rawData.getObjectsOfType(type).entrySet()) { futures.add(executor.submit(new VisitorWrapper(type, entry))); } } try { // Wait for all, failing fast if any exceptions. Futures.allAsList(futures).get(); } catch (Exception e) { Exceptions.propagateIfFatal(e); List<Exception> exceptions = Lists.newArrayList(); for (ListenableFuture<?> future : futures) { if (future.isDone()) { try { future.get(); } catch (InterruptedException e2) { throw Exceptions.propagate(e2); } catch (ExecutionException e2) { LOG.warn("Problem loading memento (" + phase + "): " + e2, e2); exceptions.add(e2); } future.cancel(true); } } if (exceptions.isEmpty()) { throw Exceptions.propagate(e); } else { // Normally there should be at lesat one failure; otherwise all.get() would not have failed. throw new CompoundRuntimeException("Problem loading mementos (" + phase + ")", exceptions); } } }
From source file:com.facebook.presto.server.remotetask.HttpRemoteTask.java
private synchronized void sendUpdate() { TaskStatus taskStatus = getTaskStatus(); // don't update if the task hasn't been started yet or if it is already finished if (!needsUpdate.get() || taskStatus.getState().isDone()) { return;/* w w w . ja v a 2 s. c o m*/ } // if we have an old request outstanding, cancel it if (currentRequest != null && Duration.nanosSince(currentRequestStartNanos).compareTo(requestTimeout) >= 0) { needsUpdate.set(true); currentRequest.cancel(true); currentRequest = null; currentRequestStartNanos = 0; } // if there is a request already running, wait for it to complete if (this.currentRequest != null && !this.currentRequest.isDone()) { return; } // if throttled due to error, asynchronously wait for timeout and try again ListenableFuture<?> errorRateLimit = updateErrorTracker.acquireRequestPermit(); if (!errorRateLimit.isDone()) { errorRateLimit.addListener(this::sendUpdate, executor); return; } List<TaskSource> sources = getSources(); Optional<PlanFragment> fragment = Optional.empty(); if (sendPlan.get()) { fragment = Optional.of(planFragment); } TaskUpdateRequest updateRequest = new TaskUpdateRequest(session.toSessionRepresentation(), fragment, sources, outputBuffers.get()); HttpUriBuilder uriBuilder = getHttpUriBuilder(taskStatus); Request request = preparePost().setUri(uriBuilder.build()) .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString()) .setBodyGenerator(jsonBodyGenerator(taskUpdateRequestCodec, updateRequest)).build(); updateErrorTracker.startRequest(); ListenableFuture<JsonResponse<TaskInfo>> future = httpClient.executeAsync(request, createFullJsonResponseHandler(taskInfoCodec)); currentRequest = future; currentRequestStartNanos = System.nanoTime(); // The needsUpdate flag needs to be set to false BEFORE adding the Future callback since callback might change the flag value // and does so without grabbing the instance lock. needsUpdate.set(false); Futures.addCallback(future, new SimpleHttpResponseHandler<>(new UpdateResponseHandler(sources), request.getUri(), stats), executor); }
From source file:com.facebook.presto.execution.scheduler.FixedSourcePartitionedScheduler.java
@Override public ScheduleResult schedule() { // schedule a task on every node in the distribution List<RemoteTask> newTasks = ImmutableList.of(); if (!scheduledTasks) { newTasks = partitioning.getPartitionToNode().entrySet().stream() .map(entry -> stage.scheduleTask(entry.getValue(), entry.getKey())).collect(toImmutableList()); scheduledTasks = true;/*from ww w. jav a2s . c om*/ } ListenableFuture<?> blocked = immediateFuture(null); ScheduleResult.BlockedReason blockedReason = null; int splitsScheduled = 0; while (!sourcePartitionedSchedulers.isEmpty()) { ScheduleResult schedule = sourcePartitionedSchedulers.peek().schedule(); splitsScheduled += schedule.getSplitsScheduled(); blocked = schedule.getBlocked(); if (schedule.getBlockedReason().isPresent()) { blockedReason = schedule.getBlockedReason().get(); } else { blockedReason = null; } // if the source is not done scheduling, stop scheduling for now if (!blocked.isDone() || !schedule.isFinished()) { break; } sourcePartitionedSchedulers.remove().close(); } if (blockedReason != null) { return new ScheduleResult(sourcePartitionedSchedulers.isEmpty(), newTasks, blocked, blockedReason, splitsScheduled); } else { checkState(blocked.isDone(), "blockedReason not provided when scheduler is blocked"); return new ScheduleResult(sourcePartitionedSchedulers.isEmpty(), newTasks, splitsScheduled); } }