List of usage examples for com.google.common.util.concurrent Futures allAsList
@Beta @CheckReturnValue public static <V> ListenableFuture<List<V>> allAsList( Iterable<? extends ListenableFuture<? extends V>> futures)
From source file:org.apache.streams.gplus.provider.AbstractGPlusProvider.java
@Override public boolean isRunning() { if (datumQueue.isEmpty() && executor.isTerminated() && Futures.allAsList(futures).isDone()) { LOGGER.info("Completed"); isComplete.set(true);//from w ww. j a va 2 s.co m LOGGER.info("Exiting"); } return !isComplete.get(); }
From source file:flipkart.lego.engine.Lego.java
private long waitUntilAvailableOrTimeout(Map<String, ListenableFuture> requiredFutureHashMap, Map<String, ListenableFuture> optionalFutureHashMap, Request request, long elementTimeout) throws TimeoutException { //requiredFuture is only realized if all the futures are realized List<ListenableFuture<Object>> requireFutureList = new ArrayList<>(); for (Map.Entry<String, ListenableFuture> listenableFutureEntry : requiredFutureHashMap.entrySet()) { requireFutureList.add(listenableFutureEntry.getValue()); }/*from w ww.ja va2s . c o m*/ ListenableFuture<List<Object>> requiredFuture = Futures.allAsList(requireFutureList); //requiredFuture is only realized if all the futures are realized List<ListenableFuture<Object>> optionalFutureList = new ArrayList<>(); for (Map.Entry<String, ListenableFuture> listenableFutureEntry : optionalFutureHashMap.entrySet()) { optionalFutureList.add(listenableFutureEntry.getValue()); } ListenableFuture<List<Object>> optionalFuture = Futures.successfulAsList(optionalFutureList); //used to calculate remaining time for timeout Stopwatch requiredDSStopWatch = Stopwatch.createStarted(); //Wait until timeout to see if required data is realized, if it times out throw internalErrorException try { requiredFuture.get(elementTimeout, TimeUnit.MILLISECONDS); } catch (TimeoutException timeoutException) { exceptionLogger.error("TimeOutException: required data sources timed out {}, Timeout:{}, Exception:{}", request, elementTimeout, timeoutException); requiredFuture.cancel(true); cancelFutures((Collection) requireFutureList); throw timeoutException; } catch (InterruptedException interruptedException) { exceptionLogger.error( "InterruptedException: required data sources were interrupted{}, Message:{}, Exception:{}", request, interruptedException.getMessage(), interruptedException); requiredFuture.cancel(true); cancelFutures((Collection) requireFutureList); throwTimeoutException(interruptedException); } catch (ExecutionException executionException) { exceptionLogger.error("ExcecutionException: {}", executionException); requiredFuture.cancel(true); cancelFutures((Collection) requireFutureList); throwTimeoutException(executionException); } //if time is still remaining before timeout wait until timeout for optional data to realize itself requiredDSStopWatch.stop(); long remainingTimeForTimeout = elementTimeout - requiredDSStopWatch.elapsed(TimeUnit.MILLISECONDS); //calculates milliseconds remaining before elementTimeout Stopwatch optionalDsStopWatch = Stopwatch.createStarted(); if (remainingTimeForTimeout > 0) { try { optionalFuture.get(1, TimeUnit.MILLISECONDS); } catch (Exception exception) { optionalFuture.cancel(true); cancelFutures((Collection) optionalFutureList); exceptionLogger.warn("Optional Data Sources Were Not Realized {}, Exception: {}", request, exception); } } optionalDsStopWatch.stop(); remainingTimeForTimeout = remainingTimeForTimeout - optionalDsStopWatch.elapsed(TimeUnit.MILLISECONDS); //calculate time remaining for execution of response filters return remainingTimeForTimeout > 0 ? remainingTimeForTimeout : 0; }
From source file:com.google.cloud.dataflow.sdk.io.FileBasedSource.java
private static long getExactTotalSizeOfFiles(Collection<String> files, IOChannelFactory ioChannelFactory) throws Exception { List<ListenableFuture<Long>> futures = new ArrayList<>(); ListeningExecutorService service = MoreExecutors .listeningDecorator(Executors.newFixedThreadPool(THREAD_POOL_SIZE)); long totalSize = 0; try {/*from ww w .ja v a2 s . co m*/ for (String file : files) { futures.add(createFutureForSizeEstimation(file, ioChannelFactory, service)); } for (Long val : Futures.allAsList(futures).get()) { totalSize += val; } return totalSize; } finally { service.shutdown(); } }
From source file:org.opendaylight.controller.md.sal.dom.broker.impl.DOMConcurrentDataCommitCoordinator.java
private void handleException(final AsyncNotifyingSettableFuture clientSubmitFuture, final DOMDataWriteTransaction transaction, final Iterable<DOMStoreThreePhaseCommitCohort> cohorts, int cohortSize, final String phase, final TransactionCommitFailedExceptionMapper exMapper, final Throwable t) { if (clientSubmitFuture.isDone()) { // We must have had failures from multiple cohorts. return;//from ww w. j av a2 s. c om } LOG.warn("Tx: {} Error during phase {}, starting Abort", transaction.getIdentifier(), phase, t); Exception e; if (t instanceof Exception) { e = (Exception) t; } else { e = new RuntimeException("Unexpected error occurred", t); } final TransactionCommitFailedException clientException = exMapper.apply(e); // Transaction failed - tell all cohorts to abort. @SuppressWarnings("unchecked") ListenableFuture<Void>[] canCommitFutures = new ListenableFuture[cohortSize]; int i = 0; for (DOMStoreThreePhaseCommitCohort cohort : cohorts) { canCommitFutures[i++] = cohort.abort(); } ListenableFuture<List<Void>> combinedFuture = Futures.allAsList(canCommitFutures); Futures.addCallback(combinedFuture, new FutureCallback<List<Void>>() { @Override public void onSuccess(List<Void> notUsed) { // Propagate the original exception to the client. clientSubmitFuture.setException(clientException); } @Override public void onFailure(Throwable t) { LOG.error("Tx: {} Error during Abort.", transaction.getIdentifier(), t); // Propagate the original exception as that is what caused the Tx to fail and is // what's interesting to the client. clientSubmitFuture.setException(clientException); } }, internalFutureCallbackExecutor); }
From source file:com.microsoft.intellij.forms.WebSiteDeployForm.java
private void fillList() { if (fillListTaskHandle != null && !fillListTaskHandle.isFinished()) { fillListTaskHandle.cancel();//w w w. j a v a 2s .c o m } webSiteJList.setModel(getMessageListModel("(Loading Web Apps...)")); for (ListSelectionListener selectionListener : webSiteJList.getListSelectionListeners()) { webSiteJList.removeListSelectionListener(selectionListener); } ProjectDescriptor projectDescriptor = new ProjectDescriptor(project.getName(), project.getBasePath() == null ? "" : project.getBasePath()); try { fillListTaskHandle = DefaultLoader.getIdeHelper().runInBackground(projectDescriptor, "Retrieving web apps info...", null, new CancellableTask() { final AzureManager manager = AzureManagerImpl.getManager(); final Object lock = new Object(); CancellationHandle cancellationHandle; Map<WebSite, WebSiteConfiguration> webSiteConfigMap; @Override public synchronized void run(final CancellationHandle cancellationHandle) throws Throwable { this.cancellationHandle = cancellationHandle; subscriptionList = manager.getSubscriptionList(); webSiteConfigMap = new HashMap<WebSite, WebSiteConfiguration>(); if (subscriptionList.size() > 0) { if (manager.authenticated()) { // authenticated using AD. Proceed for Web Apps retrieval List<ListenableFuture<Void>> subscriptionFutures = new ArrayList<ListenableFuture<Void>>(); for (final Subscription subscription : subscriptionList) { if (cancellationHandle.isCancelled()) { return; } final SettableFuture<Void> subscriptionFuture = SettableFuture.create(); DefaultLoader.getIdeHelper().executeOnPooledThread(new Runnable() { @Override public void run() { loadWebSiteConfigurations(subscription, subscriptionFuture); } }); subscriptionFutures.add(subscriptionFuture); } try { Futures.allAsList(subscriptionFutures).get(); } catch (InterruptedException e) { throw new AzureCmdException(e.getMessage(), e); } catch (ExecutionException e) { throw new AzureCmdException(e.getCause().getMessage(), e.getCause()); } } else { // imported publish settings file. Clear subscription manager.clearImportedPublishSettingsFiles(); WizardCacheManager.clearSubscriptions(); subscriptionList = manager.getSubscriptionList(); } } } @Override public void onCancel() { } @Override public void onSuccess() { DefaultLoader.getIdeHelper().invokeLater(new Runnable() { @Override public void run() { if (subscriptionList.isEmpty()) { setMessages( "Please sign in to import your Azure subscriptions. The credentials in a publish settings file are not sufficient for the web app functionality."); selectedWebSite = null; editSubscriptions(true); } else if (webSiteConfigMap.isEmpty()) { setMessages("There are no Azure web apps in the imported subscriptions."); selectedWebSite = null; } else { setWebApps(webSiteConfigMap); } } }); } @Override public void onError(@NotNull Throwable throwable) { DefaultLoader.getUIHelper().showException( "An error occurred while trying to load the web apps info", throwable, "Azure Services Explorer - Error Loading Web Apps Info", false, true); } private void loadWebSiteConfigurations(final Subscription subscription, final SettableFuture<Void> subscriptionFuture) { try { List<ListenableFuture<Void>> webSpaceFutures = new ArrayList<ListenableFuture<Void>>(); for (final String webSpace : manager.getResourceGroupNames(subscription.getId())) { if (cancellationHandle.isCancelled()) { subscriptionFuture.set(null); return; } final SettableFuture<Void> webSpaceFuture = SettableFuture.create(); DefaultLoader.getIdeHelper().executeOnPooledThread(new Runnable() { @Override public void run() { loadWebSiteConfigurations(subscription, webSpace, webSpaceFuture); } }); webSpaceFutures.add(webSpaceFuture); } Futures.addCallback(Futures.allAsList(webSpaceFutures), new FutureCallback<List<Void>>() { @Override public void onSuccess(List<Void> voids) { subscriptionFuture.set(null); } @Override public void onFailure(Throwable throwable) { subscriptionFuture.setException(throwable); } }); } catch (AzureCmdException ex) { subscriptionFuture.setException(ex); } } private void loadWebSiteConfigurations(final Subscription subscription, final String webSpace, final SettableFuture<Void> webSpaceFuture) { try { List<ListenableFuture<Void>> webSiteFutures = new ArrayList<ListenableFuture<Void>>(); for (final WebSite webSite : manager.getWebSites(subscription.getId(), webSpace)) { if (cancellationHandle.isCancelled()) { webSpaceFuture.set(null); return; } final SettableFuture<Void> webSiteFuture = SettableFuture.create(); DefaultLoader.getIdeHelper().executeOnPooledThread(new Runnable() { @Override public void run() { loadWebSiteConfigurations(subscription, webSpace, webSite, webSiteFuture); } }); webSiteFutures.add(webSiteFuture); } Futures.addCallback(Futures.allAsList(webSiteFutures), new FutureCallback<List<Void>>() { @Override public void onSuccess(List<Void> voids) { webSpaceFuture.set(null); } @Override public void onFailure(Throwable throwable) { webSpaceFuture.setException(throwable); } }); } catch (AzureCmdException ex) { webSpaceFuture.setException(ex); } } private void loadWebSiteConfigurations(final Subscription subscription, final String webSpace, final WebSite webSite, final SettableFuture<Void> webSiteFuture) { WebSiteConfiguration webSiteConfiguration; try { webSiteConfiguration = AzureManagerImpl.getManager().getWebSiteConfiguration( webSite.getSubscriptionId(), webSite.getWebSpaceName(), webSite.getName()); } catch (Throwable ignore) { webSiteConfiguration = new WebSiteConfiguration(webSpace, webSite.getName(), subscription.getId()); } synchronized (lock) { webSiteConfigMap.put(webSite, webSiteConfiguration); } webSiteFuture.set(null); } }); } catch (AzureCmdException e) { selectedWebSite = null; DefaultLoader.getUIHelper().showException("An error occurred while trying to load the web apps info", e, "Azure Services Explorer - Error Loading Web Apps Info", false, true); } }
From source file:io.druid.server.coordinator.CostBalancerStrategy.java
/** * For assignment, we want to move to the lowest cost server that isn't already serving the segment. * * @param proposalSegment A DataSegment that we are proposing to move. * @param serverHolders An iterable of ServerHolders for a particular tier. * * @return A ServerHolder with the new home for a segment. *//*from w ww. jav a2s.c o m*/ protected Pair<Double, ServerHolder> chooseBestServer(final DataSegment proposalSegment, final Iterable<ServerHolder> serverHolders, final boolean includeCurrentServer) { Pair<Double, ServerHolder> bestServer = Pair.of(Double.POSITIVE_INFINITY, null); ListeningExecutorService service = MoreExecutors .listeningDecorator(Executors.newFixedThreadPool(threadCount)); List<ListenableFuture<Pair<Double, ServerHolder>>> futures = Lists.newArrayList(); for (final ServerHolder server : serverHolders) { futures.add(service.submit(new Callable<Pair<Double, ServerHolder>>() { @Override public Pair<Double, ServerHolder> call() throws Exception { return Pair.of(computeCost(proposalSegment, server, includeCurrentServer), server); } })); } final ListenableFuture<List<Pair<Double, ServerHolder>>> resultsFuture = Futures.allAsList(futures); try { for (Pair<Double, ServerHolder> server : resultsFuture.get()) { if (server.lhs < bestServer.lhs) { bestServer = server; } } } catch (Exception e) { log.makeAlert(e, "Cost Balancer Multithread strategy wasn't able to complete cost computation.").emit(); } service.shutdown(); return bestServer; }
From source file:org.opendaylight.controller.cluster.datastore.ConcurrentDOMDataBroker.java
private void handleException(final AsyncNotifyingSettableFuture clientSubmitFuture, final DOMDataWriteTransaction transaction, final Collection<DOMStoreThreePhaseCommitCohort> cohorts, final String phase, final TransactionCommitFailedExceptionMapper exMapper, final Throwable t) { if (clientSubmitFuture.isDone()) { // We must have had failures from multiple cohorts. return;/*from w ww.ja va 2s . c o m*/ } LOG.warn("Tx: {} Error during phase {}, starting Abort", transaction.getIdentifier(), phase, t); Exception e; if (t instanceof Exception) { e = (Exception) t; } else { e = new RuntimeException("Unexpected error occurred", t); } final TransactionCommitFailedException clientException = exMapper.apply(e); // Transaction failed - tell all cohorts to abort. @SuppressWarnings("unchecked") ListenableFuture<Void>[] canCommitFutures = new ListenableFuture[cohorts.size()]; int i = 0; for (DOMStoreThreePhaseCommitCohort cohort : cohorts) { canCommitFutures[i++] = cohort.abort(); } ListenableFuture<List<Void>> combinedFuture = Futures.allAsList(canCommitFutures); Futures.addCallback(combinedFuture, new FutureCallback<List<Void>>() { @Override public void onSuccess(List<Void> notUsed) { // Propagate the original exception to the client. clientSubmitFuture.setException(clientException); } @Override public void onFailure(Throwable t) { LOG.error("Tx: {} Error during Abort.", transaction.getIdentifier(), t); // Propagate the original exception as that is what caused the Tx to fail and is // what's interesting to the client. clientSubmitFuture.setException(clientException); } }, internalFutureCallbackExecutor); }
From source file:org.opendaylight.controller.md.sal.dom.broker.impl.CommitCoordinationTask.java
/** * Invokes abort on underlying cohorts and returns future which * completes once all abort on cohorts are completed. * * @return Future which will complete once all cohorts completed * abort./*from w w w . j av a 2 s. c om*/ */ @SuppressWarnings({ "unchecked", "rawtypes" }) private ListenableFuture<Void> abortAsyncAll() { final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohorts.size()]; int i = 0; for (final DOMStoreThreePhaseCommitCohort cohort : cohorts) { ops[i++] = cohort.abort(); } /* * We are returning all futures as list, not only succeeded ones in * order to fail composite future if any of them failed. * See Futures.allAsList for this description. */ return (ListenableFuture) Futures.allAsList(ops); }
From source file:com.facebook.buck.maven.Resolver.java
private ImmutableSetMultimap<Path, Prebuilt> downloadArtifacts(final MutableDirectedGraph<Artifact> graph) throws ExecutionException, InterruptedException { ListeningExecutorService exec = MoreExecutors .listeningDecorator(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(), new MostExecutors.NamedThreadFactory("artifact download"))); @SuppressWarnings("unchecked") List<ListenableFuture<Map.Entry<Path, Prebuilt>>> results = (List<ListenableFuture<Map.Entry<Path, Prebuilt>>>) (List<?>) exec .invokeAll(graph.getNodes().stream().map( artifact -> (Callable<Map.Entry<Path, Prebuilt>>) () -> downloadArtifact(artifact, graph)) .collect(MoreCollectors.toImmutableList())); try {//www . j a v a2 s . com return ImmutableSetMultimap.<Path, Prebuilt>builder().orderValuesBy(Ordering.natural()) .putAll(Futures.allAsList(results).get()).build(); } finally { exec.shutdown(); } }
From source file:org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker.java
private static void handleException(final AsyncNotifyingSettableFuture clientSubmitFuture, final DOMDataWriteTransaction transaction, final Collection<DOMStoreThreePhaseCommitCohort> cohorts, final String phase, final TransactionCommitFailedExceptionMapper exMapper, final Throwable t) { if (clientSubmitFuture.isDone()) { // We must have had failures from multiple cohorts. return;/* www . ja v a2 s . com*/ } LOG.warn("Tx: {} Error during phase {}, starting Abort", transaction.getIdentifier(), phase, t); final Exception e; if (t instanceof NoShardLeaderException || t instanceof ShardLeaderNotRespondingException) { e = new DataStoreUnavailableException(t.getMessage(), t); } else if (t instanceof Exception) { e = (Exception) t; } else { e = new RuntimeException("Unexpected error occurred", t); } final TransactionCommitFailedException clientException = exMapper.apply(e); // Transaction failed - tell all cohorts to abort. @SuppressWarnings("unchecked") ListenableFuture<Void>[] canCommitFutures = new ListenableFuture[cohorts.size()]; int i = 0; for (DOMStoreThreePhaseCommitCohort cohort : cohorts) { canCommitFutures[i++] = cohort.abort(); } ListenableFuture<List<Void>> combinedFuture = Futures.allAsList(canCommitFutures); Futures.addCallback(combinedFuture, new FutureCallback<List<Void>>() { @Override public void onSuccess(List<Void> notUsed) { // Propagate the original exception to the client. clientSubmitFuture.setException(clientException); } @Override public void onFailure(Throwable t) { LOG.error("Tx: {} Error during Abort.", transaction.getIdentifier(), t); // Propagate the original exception as that is what caused the Tx to fail and is // what's interesting to the client. clientSubmitFuture.setException(clientException); } }, MoreExecutors.directExecutor()); }