Example usage for com.google.common.util.concurrent Futures allAsList

List of usage examples for com.google.common.util.concurrent Futures allAsList

Introduction

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

Prototype

@Beta
@CheckReturnValue
public static <V> ListenableFuture<List<V>> allAsList(
        Iterable<? extends ListenableFuture<? extends V>> futures) 

Source Link

Document

Creates a new ListenableFuture whose value is a list containing the values of all its input futures, if all succeed.

Usage

From source file:org.hawkular.metrics.core.impl.cassandra.MetricsServiceCassandra.java

@Override
public Observable<Void> createTenant(final Tenant tenant) {
    return dataAccess.insertTenant(tenant).flatMap(resultSet -> {
        if (!resultSet.wasApplied()) {
            throw new TenantAlreadyExistsException(tenant.getId());
        }//from   w w w .java  2  s  . co  m
        Map<MetricType, Set<Retention>> retentionsMap = new HashMap<>();
        for (RetentionSettings.RetentionKey key : tenant.getRetentionSettings().keySet()) {
            Set<Retention> retentions = retentionsMap.get(key.metricType);
            if (retentions == null) {
                retentions = new HashSet<>();
            }
            Interval interval = key.interval == null ? Interval.NONE : key.interval;
            Hours hours = hours(tenant.getRetentionSettings().get(key));
            retentions.add(new Retention(new MetricId("[" + key.metricType.getText() + "]", interval),
                    hours.toStandardSeconds().getSeconds()));
            retentionsMap.put(key.metricType, retentions);
        }
        if (retentionsMap.isEmpty()) {
            return Observable.from(Collections.singleton(null));
        }
        List<ResultSetFuture> updateRetentionFutures = new ArrayList<>();

        for (Map.Entry<MetricType, Set<Retention>> metricTypeSetEntry : retentionsMap.entrySet()) {
            updateRetentionFutures.add(dataAccess.updateRetentionsIndex(tenant.getId(),
                    metricTypeSetEntry.getKey(), metricTypeSetEntry.getValue()));

            for (Retention r : metricTypeSetEntry.getValue()) {
                dataRetentions.put(new DataRetentionKey(tenant.getId(), metricTypeSetEntry.getKey()),
                        r.getValue());
            }
        }

        ListenableFuture<List<ResultSet>> updateRetentionsFuture = Futures.allAsList(updateRetentionFutures);
        ListenableFuture<Void> transform = Futures.transform(updateRetentionsFuture, Functions.TO_VOID,
                metricsTasks);
        return RxUtil.from(transform, metricsTasks);
    });
}

From source file:io.druid.segment.realtime.appenderator.AppenderatorImpl.java

@Override
public void clear() throws InterruptedException {
    // Drop commit metadata, then abandon all segments.

    try {//from   w w w .j  a v a 2s  . co m
        final ListenableFuture<?> uncommitFuture = persistExecutor.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                objectMapper.writeValue(computeCommitFile(), Committed.nil());
                return null;
            }
        });

        // Await uncommit.
        uncommitFuture.get();

        // Drop everything.
        final List<ListenableFuture<?>> futures = Lists.newArrayList();
        for (Map.Entry<SegmentIdentifier, Sink> entry : sinks.entrySet()) {
            futures.add(abandonSegment(entry.getKey(), entry.getValue(), true));
        }

        // Await dropping.
        Futures.allAsList(futures).get();
    } catch (ExecutionException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.google.idea.blaze.base.sync.aspects.BlazeIdeInterfaceAspectsImpl.java

@Nullable
static State updateState(BlazeContext parentContext, @Nullable State prevState,
        ImmutableMap<File, Long> fileState, WorkspaceLanguageSettings workspaceLanguageSettings,
        ArtifactLocationDecoder artifactLocationDecoder, AspectStrategy aspectStrategy, List<File> newFiles,
        List<File> removedFiles, boolean mergeWithOldState) {
    Result<State> result = Scope.push(parentContext, (ScopedFunction<Result<State>>) context -> {
        context.push(new TimingScope("UpdateTargetMap"));

        // If we're not removing we have to merge the old state
        // into the new one or we'll miss file removes next time
        ImmutableMap<File, Long> nextFileState = fileState;
        if (mergeWithOldState && prevState != null) {
            ImmutableMap.Builder<File, Long> fileStateBuilder = ImmutableMap.<File, Long>builder()
                    .putAll(fileState);/*from  w w w . j av a2  s. c  om*/
            for (Map.Entry<File, Long> entry : prevState.fileState.entrySet()) {
                if (!fileState.containsKey(entry.getKey())) {
                    fileStateBuilder.put(entry);
                }
            }
            nextFileState = fileStateBuilder.build();
        }

        State state = new State();
        state.fileState = nextFileState;
        state.workspaceLanguageSettings = workspaceLanguageSettings;
        state.aspectStrategyName = aspectStrategy.getName();

        Map<TargetKey, TargetIdeInfo> targetMap = Maps.newHashMap();
        Map<TargetKey, TargetIdeInfo> updatedTargets = Maps.newHashMap();
        if (prevState != null) {
            targetMap.putAll(prevState.targetMap.map());
            state.fileToTargetMapKey.putAll(prevState.fileToTargetMapKey);
        }

        // Update removed unless we're merging with the old state
        if (!mergeWithOldState) {
            for (File removedFile : removedFiles) {
                TargetKey key = state.fileToTargetMapKey.remove(removedFile);
                if (key != null) {
                    targetMap.remove(key);
                }
            }
        }

        AtomicLong totalSizeLoaded = new AtomicLong(0);

        ListeningExecutorService executor = BlazeExecutor.getInstance().getExecutor();

        // Read protos from any new files
        List<ListenableFuture<TargetFilePair>> futures = Lists.newArrayList();
        for (File file : newFiles) {
            futures.add(executor.submit(() -> {
                totalSizeLoaded.addAndGet(file.length());
                try (InputStream inputStream = getAspectInputStream(file)) {
                    IntellijIdeInfo.TargetIdeInfo ruleProto = aspectStrategy.readAspectFile(inputStream);
                    TargetIdeInfo target = IdeInfoFromProtobuf.makeTargetIdeInfo(workspaceLanguageSettings,
                            ruleProto);
                    return new TargetFilePair(file, target);
                }
            }));
        }

        // Update state with result from proto files
        int duplicateTargetLabels = 0;
        try {
            for (TargetFilePair targetFilePairs : Futures.allAsList(futures).get()) {
                if (targetFilePairs.target != null) {
                    File file = targetFilePairs.file;
                    TargetKey key = targetFilePairs.target.key;
                    TargetIdeInfo previousTarget = updatedTargets.putIfAbsent(key, targetFilePairs.target);
                    if (previousTarget == null) {
                        state.fileToTargetMapKey.put(file, key);
                    } else {
                        duplicateTargetLabels++;
                    }
                }
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return Result.error(null);
        } catch (ExecutionException e) {
            return Result.error(e);
        }
        targetMap.putAll(updatedTargets);

        context.output(PrintOutput.log(String.format("Loaded %d aspect files, total size %dkB", newFiles.size(),
                totalSizeLoaded.get() / 1024)));
        if (duplicateTargetLabels > 0) {
            context.output(new PerformanceWarning(String.format(
                    "There were %d duplicate rules. "
                            + "You may be including multiple configurations in your build. "
                            + "Your IDE sync is slowed down by ~%d%%.",
                    duplicateTargetLabels, (100 * duplicateTargetLabels / targetMap.size()))));
        }

        state.targetMap = new TargetMap(ImmutableMap.copyOf(targetMap));
        return Result.of(state);
    });

    if (result.error != null) {
        LOG.error(result.error);
        return null;
    }
    return result.result;
}

From source file:zipkin.storage.cassandra.CassandraStorage.java

/** Truncates all the column families, or throws on any failure. */
@VisibleForTesting//from   ww  w  .  j  a v a  2s.  c om
void clear() {
    guavaSpanConsumer().clear();
    List<ListenableFuture<?>> futures = new LinkedList<>();
    for (String cf : ImmutableList.of("traces", "dependencies", Tables.SERVICE_NAMES, Tables.SPAN_NAMES,
            Tables.SERVICE_NAME_INDEX, Tables.SERVICE_SPAN_NAME_INDEX, Tables.ANNOTATIONS_INDEX)) {
        futures.add(session.get().executeAsync(format("TRUNCATE %s", cf)));
    }
    Futures.getUnchecked(Futures.allAsList(futures));
}

From source file:org.opendaylight.openflowplugin.impl.util.DeviceInitializationUtils.java

private static ListenableFuture<List<RpcResult<List<MultipartReply>>>> createDeviceFeaturesForOF10(
        final DeviceContext deviceContext, final DeviceState deviceState) {
    final ListenableFuture<RpcResult<List<MultipartReply>>> replyDesc = getNodeStaticInfo(
            MultipartType.OFPMPDESC, deviceContext, deviceState.getNodeInstanceIdentifier(),
            deviceState.getVersion());//from   w ww.j  a v a 2s  .  c o  m

    return Futures.allAsList(Arrays.asList(replyDesc));
}

From source file:org.hawkular.alerts.engine.impl.CassActionsServiceImpl.java

private void insertActionHistory(Action action) {
    if (action.getResult() == null) {
        action.setResult(WAITING_RESULT);
    }//from   w w  w.j  av a2 s .  c o  m
    try {
        PreparedStatement insertActionHistory = CassStatement.get(session, CassStatement.INSERT_ACTION_HISTORY);
        PreparedStatement insertActionHistoryAction = CassStatement.get(session,
                CassStatement.INSERT_ACTION_HISTORY_ACTION);
        PreparedStatement insertActionHistoryAlert = CassStatement.get(session,
                CassStatement.INSERT_ACTION_HISTORY_ALERT);
        PreparedStatement insertActionHistoryCtime = CassStatement.get(session,
                CassStatement.INSERT_ACTION_HISTORY_CTIME);
        PreparedStatement insertActionHistoryResult = CassStatement.get(session,
                CassStatement.INSERT_ACTION_HISTORY_RESULT);

        List<ResultSetFuture> futures = new ArrayList<>();

        futures.add(session.executeAsync(
                insertActionHistory.bind(action.getTenantId(), action.getActionPlugin(), action.getActionId(),
                        action.getEvent().getId(), action.getCtime(), JsonUtil.toJson(action))));
        futures.add(session.executeAsync(insertActionHistoryAction.bind(action.getTenantId(),
                action.getActionId(), action.getActionPlugin(), action.getEvent().getId(), action.getCtime())));
        futures.add(session.executeAsync(insertActionHistoryAlert.bind(action.getTenantId(),
                action.getEvent().getId(), action.getActionPlugin(), action.getActionId(), action.getCtime())));
        futures.add(session.executeAsync(insertActionHistoryCtime.bind(action.getTenantId(), action.getCtime(),
                action.getActionPlugin(), action.getActionId(), action.getEvent().getId())));
        futures.add(session.executeAsync(insertActionHistoryResult.bind(action.getTenantId(),
                action.getResult(), action.getActionPlugin(), action.getActionId(), action.getEvent().getId(),
                action.getCtime())));

        Futures.allAsList(futures).get();
    } catch (Exception e) {
        msgLog.errorDatabaseException(e.getMessage());
    }
}

From source file:com.facebook.buck.core.build.engine.impl.CachingBuildEngine.java

@Override
public void close() {
    try {/* ww w  .j  a v  a 2s  .co  m*/
        if (customBuildRuleStrategy.isPresent()) {
            customBuildRuleStrategy.get().close();
        }
        terminateBuildWithFailure(new CancellationException("Cancelling due to engine shutdown."));
        Futures.allAsList(asyncCallbacks).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (IOException | ExecutionException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.hawkular.alerts.engine.impl.CassAlertsServiceImpl.java

@Override
public void persistEvents(Collection<Event> events) throws Exception {
    if (events == null) {
        throw new IllegalArgumentException("Events must be not null");
    }/*from w  w  w .ja  v a 2s .  co m*/
    if (events.isEmpty()) {
        return;
    }
    if (log.isDebugEnabled()) {
        log.debug("Adding " + events.size() + " events");
    }
    PreparedStatement insertEvent = CassStatement.get(session, CassStatement.INSERT_EVENT);
    PreparedStatement insertEventCategory = CassStatement.get(session, CassStatement.INSERT_EVENT_CATEGORY);
    PreparedStatement insertEventCtime = CassStatement.get(session, CassStatement.INSERT_EVENT_CTIME);
    PreparedStatement insertEventTrigger = CassStatement.get(session, CassStatement.INSERT_EVENT_TRIGGER);
    PreparedStatement insertTag = CassStatement.get(session, CassStatement.INSERT_TAG);

    try {
        List<ResultSetFuture> futures = new ArrayList<>();
        BatchStatement batch = new BatchStatement(batchType);
        int i = 0;
        for (Event e : events) {
            batch.add(insertEvent.bind(e.getTenantId(), e.getId(), JsonUtil.toJson(e)));
            batch.add(insertEventCategory.bind(e.getTenantId(), e.getCategory(), e.getId()));
            batch.add(insertEventCtime.bind(e.getTenantId(), e.getCtime(), e.getId()));
            if (null != e.getTrigger()) {
                batch.add(insertEventTrigger.bind(e.getTenantId(), e.getTrigger().getId(), e.getId()));
            }
            e.getTags().entrySet().stream().forEach(tag -> {
                batch.add(insertTag.bind(e.getTenantId(), TagType.EVENT.name(), tag.getKey(), tag.getValue(),
                        e.getId()));
            });
            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;
    }
}

From source file:com.google.gapid.widgets.ImagePanel.java

private void loadLevel(int level) {
    if (image.getLevelCount() == 0) {
        clearImage();//from  w  w w.  j a  v  a2  s  . c om
        loading.showMessage(Info, Messages.NO_IMAGE_DATA);
        if (saveItem != null) {
            saveItem.setEnabled(false);
        }
        return;
    }

    level = Math.min(image.getLevelCount() - 1, level);
    loading.startLoading();

    ListenableFuture<Image>[] layers = new ListenableFuture[image.getLayerCount()];
    for (int layer = 0; layer < layers.length; layer++) {
        layers[layer] = image.getImage(layer, level);
    }
    imageRequestController.start().listen(Futures.allAsList(layers),
            new UiErrorCallback<List<Image>, List<Image>, Loadable.Message>(this, LOG) {
                @Override
                protected ResultOrError<List<Image>, Loadable.Message> onRpcThread(
                        Rpc.Result<List<Image>> result) throws RpcException, ExecutionException {
                    try {
                        return success(result.get());
                    } catch (DataUnavailableException e) {
                        return error(Loadable.Message.info(e));
                    } catch (RpcException e) {
                        return error(Loadable.Message.error(e));
                    }
                }

                @Override
                protected void onUiThreadSuccess(List<Image> levels) {
                    updateLayers(levels);
                }

                @Override
                protected void onUiThreadError(Loadable.Message message) {
                    clearImage();
                    loading.showMessage(message);
                }
            });
}

From source file:com.facebook.buck.distributed.build_slave.CacheOptimizedBuildTargetsQueueFactory.java

/**
 * Upload the smallest set of cachable {@link BuildRule}s from the dir-cache, which can help the
 * remote servers in finishing the build faster.
 *
 * @param targetsToBuild Top-level targets which this build needs to optimize for.
 * @param clientStatsTracker For tracking some timing/perf metrics for the Stampede client.
 * @return Future to track the progress of the uploads.
 *//*  ww  w .  j  a v  a  2 s .  c  o  m*/
public ListenableFuture<?> uploadCriticalNodesFromLocalCache(Iterable<BuildTarget> targetsToBuild,
        ClientStatsTracker clientStatsTracker) {
    clientStatsTracker.startTimer(ClientStatsTracker.DistBuildClientStat.LOCAL_UPLOAD_FROM_DIR_CACHE);

    traverseGraphFromTopLevelUsingAvailableCaches(targetsToBuild);
    return Futures.transform(Futures.allAsList(artifactCache.getAllUploadRuleFutures()), results -> {
        clientStatsTracker.stopTimer(ClientStatsTracker.DistBuildClientStat.LOCAL_UPLOAD_FROM_DIR_CACHE);
        clientStatsTracker.setMissingRulesUploadedFromDirCacheCount(results.size());
        return null;
    }, MoreExecutors.directExecutor());
}