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:com.facebook.buck.android.AdbHelper.java
/** * Execute an {@link AdbCallable} for all matching devices. This functions performs device * filtering based on three possible arguments: * * -e (emulator-only) - only emulators are passing the filter * -d (device-only) - only real devices are passing the filter * -s (serial) - only device/emulator with specific serial number are passing the filter * * If more than one device matches the filter this function will fail unless multi-install * mode is enabled (-x). This flag is used as a marker that user understands that multiple * devices will be used to install the apk if needed. *//*from www. j av a 2s.c o m*/ @SuppressWarnings("PMD.EmptyCatchBlock") @SuppressForbidden public boolean adbCall(AdbCallable adbCallable, boolean quiet) throws InterruptedException { List<IDevice> devices; try (SimplePerfEvent.Scope ignored = SimplePerfEvent.scope(buckEventBus, "set_up_adb_call")) { devices = getDevices(quiet); if (devices.size() == 0) { return false; } } int adbThreadCount = options.getAdbThreadCount(); if (adbThreadCount <= 0) { adbThreadCount = devices.size(); } // Start executions on all matching devices. List<ListenableFuture<Boolean>> futures = Lists.newArrayList(); ListeningExecutorService executorService = listeningDecorator( newMultiThreadExecutor(new CommandThreadFactory(getClass().getSimpleName()), adbThreadCount)); for (final IDevice device : devices) { futures.add(executorService.submit(adbCallable.forDevice(device))); } // Wait for all executions to complete or fail. List<Boolean> results = null; try { results = Futures.allAsList(futures).get(); } catch (ExecutionException ex) { console.printBuildFailure("Failed: " + adbCallable); ex.printStackTrace(console.getStdErr()); return false; } catch (InterruptedException e) { try { Futures.allAsList(futures).cancel(true); } catch (CancellationException ignored) { // Rethrow original InterruptedException instead. } Thread.currentThread().interrupt(); throw e; } finally { MostExecutors.shutdownOrThrow(executorService, 10, TimeUnit.MINUTES, new InterruptionFailedException("Failed to shutdown ExecutorService.")); } int successCount = 0; for (Boolean result : results) { if (result) { successCount++; } } int failureCount = results.size() - successCount; // Report results. if (successCount > 0 && !quiet) { console.printSuccess(String.format("Successfully ran %s on %d device(s)", adbCallable, successCount)); } if (failureCount > 0) { console.printBuildFailure(String.format("Failed to %s on %d device(s).", adbCallable, failureCount)); } return failureCount == 0; }
From source file:com.facebook.buck.util.network.offline.OfflineScribeLogger.java
private synchronized void sendStoredLogs() { ImmutableSortedSet<Path> logsPaths; try {//from ww w .jav a2 s . c o m if (!filesystem.isDirectory(logDir)) { // No logs to submit to Scribe. return; } logsPaths = filesystem.getMtimeSortedMatchingDirectoryContents(logDir, LOGFILE_PATTERN); } catch (Exception e) { LOG.error(e, "Fetching stored logs list failed."); return; } long totalBytesToSend = 0; for (Path logPath : logsPaths) { // Sending should be ceased if storing has been initiated or closing was started. if (startedStoring || startedClosing) { break; } // Get iterator. Iterator<ScribeData> it; File logFile; try { logFile = logPath.toFile(); totalBytesToSend += logFile.length(); if (totalBytesToSend > maxScribeOfflineLogsBytes) { LOG.warn("Total size of offline logs exceeds the limit. Ceasing to send them to Scribe."); return; } InputStream logFileStream; try { logFileStream = new BufferedInputStream(new FileInputStream(logFile), BUFFER_SIZE); } catch (FileNotFoundException e) { LOG.info(e, "There was a problem getting stream for logfile: %s. Likely logfile was resent and" + "deleted by a concurrent Buck command.", logPath); continue; } it = new ObjectMapper().readValues(new JsonFactory().createParser(logFileStream), ScribeData.class); } catch (Exception e) { LOG.error(e, "Failed to initiate reading from: %s. File may be corrupted.", logPath); continue; } // Read and submit. int scribeLinesInFile = 0; List<ListenableFuture<Void>> logFutures = new LinkedList<>(); Map<String, CategoryData> logReadData = new HashMap<>(); try { boolean interrupted = false; // Read data and build per category clusters - dispatch if needed. while (it.hasNext()) { if (startedStoring || startedClosing) { interrupted = true; break; } ScribeData newData = it.next(); // Prepare map entry for new data (dispatch old data if needed). if (!logReadData.containsKey(newData.getCategory())) { logReadData.put(newData.getCategory(), new CategoryData()); } CategoryData categoryData = logReadData.get(newData.getCategory()); if (categoryData.getLinesBytes() > CLUSTER_DISPATCH_SIZE) { logFutures.add(scribeLogger.log(newData.getCategory(), categoryData.getLines())); categoryData.clearData(); } // Add new data to the cluster for the category. for (String line : newData.getLines()) { categoryData.addLine(line); scribeLinesInFile++; } } // Send remaining data from per category clusters. if (!interrupted) { for (Map.Entry<String, CategoryData> logReadDataEntry : logReadData.entrySet()) { if (startedStoring || startedClosing) { interrupted = true; break; } List<String> categoryLines = logReadDataEntry.getValue().getLines(); if (categoryLines.size() > 0) { logFutures.add(scribeLogger.log(logReadDataEntry.getKey(), categoryLines)); } } } if (interrupted) { LOG.info("Stopped while sending from offline log (it will not be removed): %s.", logPath); logFutures.clear(); break; } } catch (Exception e) { LOG.error(e, "Error while reading offline log from: %s. This log will not be removed now. If this " + "error reappears in further runs, the file may be corrupted and should be deleted. ", logPath); logFutures.clear(); continue; } finally { logReadData.clear(); } // Confirm data was successfully sent and remove logfile. try { Futures.allAsList(logFutures).get(LOG_TIMEOUT, LOG_TIMEOUT_UNIT); totalBytesResent.inc(logFile.length()); totalLinesResent.inc(scribeLinesInFile); logfilesResent.inc(); try { filesystem.deleteFileAtPathIfExists(logPath); } catch (Exception e) { LOG.error(e, "Failed to remove successfully resent offline log. Stopping sending."); break; } } catch (Exception e) { LOG.info("Failed to send all data from offline log: %s. Log will not be removed.", logPath); // Do not attempt to send data from further logfiles - likely there are network issues. break; } finally { logFutures.clear(); } } }
From source file:com.google.cloud.dataflow.sdk.util.GcsUtil.java
private static void executeBatches(List<BatchRequest> batches) throws IOException { ListeningExecutorService executor = MoreExecutors.listeningDecorator( MoreExecutors.getExitingExecutorService(new ThreadPoolExecutor(MAX_CONCURRENT_BATCHES, MAX_CONCURRENT_BATCHES, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()))); List<ListenableFuture<Void>> futures = new LinkedList<>(); for (final BatchRequest batch : batches) { futures.add(executor.submit(new Callable<Void>() { public Void call() throws IOException { batch.execute();//from www . j av a2 s . c o m return null; } })); } try { Futures.allAsList(futures).get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException("Interrupted while executing batch GCS request", e); } catch (ExecutionException e) { throw new IOException("Error executing batch GCS request", e); } finally { executor.shutdown(); } }
From source file:org.hawkular.alerts.engine.impl.CassActionsServiceImpl.java
private void updateActionHistory(Action action) { if (action.getResult() == null) { action.setResult(UNKNOWN_RESULT); }/*from w ww .j a v a 2 s.c o m*/ try { Action oldActionHistory = selectActionHistory(action.getTenantId(), action.getActionPlugin(), action.getActionId(), action.getEvent().getId(), action.getCtime()); if (oldActionHistory == null) { insertActionHistory(action); return; } String oldResult = oldActionHistory.getResult(); PreparedStatement deleteActionHistoryResult = CassStatement.get(session, CassStatement.DELETE_ACTION_HISTORY_RESULT); PreparedStatement insertActionHistoryResult = CassStatement.get(session, CassStatement.INSERT_ACTION_HISTORY_RESULT); PreparedStatement updateActionHistory = CassStatement.get(session, CassStatement.UPDATE_ACTION_HISTORY); List<ResultSetFuture> futures = new ArrayList<>(); futures.add(session.executeAsync(deleteActionHistoryResult.bind(action.getTenantId(), oldResult, action.getActionPlugin(), action.getActionId(), action.getEvent().getId(), action.getCtime()))); futures.add(session.executeAsync(insertActionHistoryResult.bind(action.getTenantId(), action.getResult(), action.getActionPlugin(), action.getActionId(), action.getEvent().getId(), action.getCtime()))); futures.add(session.executeAsync(updateActionHistory.bind(JsonUtil.toJson(action), action.getTenantId(), action.getActionPlugin(), action.getActionId(), action.getEvent().getId(), action.getCtime()))); Futures.allAsList(futures).get(); } catch (Exception e) { msgLog.errorDatabaseException(e.getMessage()); } }
From source file:org.opendaylight.vbd.impl.VbdBridgeDomain.java
private ListenableFuture<Void> deleteBridgeDomain() { LOG.debug("Deleting entire bridge domain {}", bridgeDomainName); final Collection<KeyedInstanceIdentifier<Node, NodeKey>> vppNodes = nodesToVpps.values(); final List<ListenableFuture<Void>> deleteBdTask = new ArrayList<>(); vppNodes.forEach((vppNode) -> deleteBdTask.add(vppModifier.deleteBridgeDomainFromVppNode(vppNode))); nodesToVpps.clear();/* w w w. j a v a 2s.co m*/ final ListenableFuture<List<Void>> cumulativeDeleteBdTask = Futures.allAsList(deleteBdTask); return transform(cumulativeDeleteBdTask); }
From source file:org.opendaylight.openflowplugin.applications.frsync.impl.strategy.SyncPlanPushStrategyIncrementalImpl.java
ListenableFuture<RpcResult<Void>> removeRedundantMeters(final NodeId nodeId, final InstanceIdentifier<FlowCapableNode> nodeIdent, final ItemSyncBox<Meter> meterRemovalPlan, final SyncCrudCounters counters) { if (meterRemovalPlan.isEmpty()) { LOG.trace("no meters on device for node: {} -> SKIPPING", nodeId.getValue()); return RpcResultBuilder.<Void>success().buildFuture(); }//from w w w. j a v a2s . c o m final CrudCounts meterCrudCounts = counters.getMeterCrudCounts(); final List<ListenableFuture<RpcResult<RemoveMeterOutput>>> allResults = new ArrayList<>(); for (Meter meter : meterRemovalPlan.getItemsToPush()) { LOG.trace("removing meter {} - absent in config {}", meter.getMeterId(), nodeId); final KeyedInstanceIdentifier<Meter, MeterKey> meterIdent = nodeIdent.child(Meter.class, meter.getKey()); allResults .add(JdkFutureAdapters.listenInPoolThread(meterForwarder.remove(meterIdent, meter, nodeIdent))); meterCrudCounts.incRemoved(); } return Futures.transform(Futures.allAsList(allResults), ReconcileUtil.<RemoveMeterOutput>createRpcResultCondenser("meter remove")); }
From source file:io.druid.query.aggregation.atomcube.AtomCubeQueryRunner.java
private ListenableFuture<List<ImmutableBitmap>> prepareFutures(final AtomCubeQuery atomQ) { final Map<String, Query> queries = atomQ.getQueries(); Iterable<Query> queryIterables = Iterables .unmodifiableIterable(Iterables.filter(queries.values(), Predicates.notNull())); return Futures.allAsList( Iterables.transform(queryIterables, new Function<Query, ListenableFuture<ImmutableBitmap>>() { @Nullable/* w w w .j av a 2 s. c o m*/ @Override public ListenableFuture<ImmutableBitmap> apply(@Nullable final Query input) { if (input == null) { throw new ISE("Null query!??"); } final String queryName = getQueryName(queries, input); final StringBuilder debugsb = new StringBuilder(); debugsb.append("** add query to list, query:").append(queryName).append("\n"); final Query query = input; int priority = BaseQuery.getContextPriority(query, 0); return ((ListeningExecutorService) executorService) .submit(new AbstractPrioritizedCallable<ImmutableBitmap>(priority) { @Override public ImmutableBitmap call() { debugsb.append("** running query:").append(queryName).append("\n"); long begin = System.currentTimeMillis(); Yielder yielder = null; try { yielder = Query(query, debugsb); } catch (IOException e) { e.printStackTrace(); log.error(e.getMessage()); debugsb.append("** subquery failed:").append(e.getMessage()) .append("\n"); } catch (Throwable throwable) { debugsb.append("** * subquery failed:").append(throwable.getMessage()) .append("\n"); } debugsb.append("** query done:").append(queryName).append(" time:") .append(System.currentTimeMillis() - begin).append("\n"); log.debug("--- inner query use " + (System.currentTimeMillis() - begin) + "millis"); ImmutableBitmap immutableBitmap = mm.get(query.getType()).apply(query, yielder); if (immutableBitmap == null) { immutableBitmap = AtomCubeAggregatorFactory.BITMAP_FACTORY .makeEmptyImmutableBitmap(); } debugsb.append("** done query:").append(queryName).append(" time:") .append(System.currentTimeMillis() - begin).append("\n"); queue.offer(Pair.of(queryName, immutableBitmap)); queryStack.put(queryName, debugsb); if (immutableBitmap.isEmpty()) { log.warn("returned bitmap is empty:\n" + debugsb.toString()); } return immutableBitmap; } }); } })); }
From source file:org.opendaylight.openflowplugin.impl.util.DeviceInitializationUtils.java
private static ListenableFuture<List<RpcResult<List<MultipartReply>>>> createDeviceFeaturesForOF13( final DeviceContext deviceContext, final DeviceState deviceState, final boolean switchFeaturesMandatory) { final ListenableFuture<RpcResult<List<MultipartReply>>> replyDesc = getNodeStaticInfo( MultipartType.OFPMPDESC, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());/*w w w . ja v a 2 s . c om*/ //first process description reply, write data to DS and write consequent data if successful return Futures.transform(replyDesc, new AsyncFunction<RpcResult<List<MultipartReply>>, List<RpcResult<List<MultipartReply>>>>() { @Override public ListenableFuture<List<RpcResult<List<MultipartReply>>>> apply( final RpcResult<List<MultipartReply>> rpcResult) throws Exception { translateAndWriteReply(MultipartType.OFPMPDESC, deviceContext, deviceState.getNodeInstanceIdentifier(), rpcResult.getResult()); final ListenableFuture<RpcResult<List<MultipartReply>>> replyMeterFeature = getNodeStaticInfo( MultipartType.OFPMPMETERFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion()); createSuccessProcessingCallback(MultipartType.OFPMPMETERFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), replyMeterFeature); final ListenableFuture<RpcResult<List<MultipartReply>>> replyGroupFeatures = getNodeStaticInfo( MultipartType.OFPMPGROUPFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion()); createSuccessProcessingCallback(MultipartType.OFPMPGROUPFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), replyGroupFeatures); final ListenableFuture<RpcResult<List<MultipartReply>>> replyTableFeatures = getNodeStaticInfo( MultipartType.OFPMPTABLEFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion()); createSuccessProcessingCallback(MultipartType.OFPMPTABLEFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), replyTableFeatures); final ListenableFuture<RpcResult<List<MultipartReply>>> replyPortDescription = getNodeStaticInfo( MultipartType.OFPMPPORTDESC, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion()); createSuccessProcessingCallback(MultipartType.OFPMPPORTDESC, deviceContext, deviceState.getNodeInstanceIdentifier(), replyPortDescription); if (switchFeaturesMandatory) { return Futures.allAsList(Arrays.asList(replyMeterFeature, replyGroupFeatures, replyTableFeatures, replyPortDescription)); } else { return Futures.successfulAsList(Arrays.asList(replyMeterFeature, replyGroupFeatures, replyTableFeatures, replyPortDescription)); } } }); }
From source file:com.facebook.buck.core.build.engine.impl.CachingBuildEngine.java
private ListenableFuture<List<BuildResult>> getDepResults(BuildRule rule, BuildEngineBuildContext buildContext, ExecutionContext executionContext) { List<ListenableFuture<BuildResult>> depResults = new ArrayList<>( SortedSets.sizeEstimate(rule.getBuildDeps())); for (BuildRule dep : shuffled(rule.getBuildDeps())) { depResults.add(getBuildRuleResultWithRuntimeDeps(dep, buildContext, executionContext)); }//from w w w.j ava 2 s . co m return Futures.allAsList(depResults); }
From source file:org.hawkular.alerts.engine.impl.CassAlertsServiceImpl.java
@Override public void addAlertTags(String tenantId, Collection<String> alertIds, Map<String, String> tags) throws Exception { if (isEmpty(tenantId)) { throw new IllegalArgumentException("TenantId must be not null"); }//www. j av a2 s . co m if (isEmpty(alertIds)) { throw new IllegalArgumentException("AlertIds must be not null"); } if (isEmpty(tags)) { throw new IllegalArgumentException("Tags must be not null"); } // Only tag existing alerts AlertsCriteria criteria = new AlertsCriteria(); criteria.setAlertIds(alertIds); Page<Alert> existingAlerts = getAlerts(tenantId, criteria, null); PreparedStatement updateAlert = CassStatement.get(session, CassStatement.UPDATE_ALERT); PreparedStatement insertTag = CassStatement.get(session, CassStatement.INSERT_TAG); try { List<ResultSetFuture> futures = new ArrayList<>(); BatchStatement batch = new BatchStatement(batchType); int i = 0; for (Alert a : existingAlerts) { tags.entrySet().stream().forEach(tag -> { a.addTag(tag.getKey(), tag.getValue()); batch.add(insertTag.bind(tenantId, TagType.ALERT.name(), tag.getKey(), tag.getValue(), a.getId())); }); batch.add(updateAlert.bind(JsonUtil.toJson(a), tenantId, a.getAlertId())); i += batch.size(); if (i > batchSize) { futures.add(session.executeAsync(batch)); batch.clear(); i = 0; } } if (batch.size() > 0) { futures.add(session.executeAsync(batch)); } Futures.allAsList(futures).get(); } catch (Exception e) { msgLog.errorDatabaseException(e.getMessage()); throw e; } }