Java Code Examples for com.google.common.util.concurrent.Futures#allAsList()

The following examples show how to use com.google.common.util.concurrent.Futures#allAsList() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReplicatorService.java    From c5-replicator with Apache License 2.0 6 votes vote down vote up
private ListenableFuture<Void> getDependedOnModules() {
  SettableFuture<Void> doneFuture = SettableFuture.create();

  List<ListenableFuture<C5Module>> moduleFutures = new ArrayList<>();
  moduleFutures.add(moduleInformationProvider.getModule(ModuleType.Log));
  moduleFutures.add(moduleInformationProvider.getModule(ModuleType.Discovery));

  ListenableFuture<List<C5Module>> compositeModulesFuture = Futures.allAsList(moduleFutures);

  LOG.warn("ReplicatorService now waiting for module dependency on Log & Discovery");

  C5Futures.addCallback(compositeModulesFuture,
      (List<C5Module> modules) -> {
        this.logModule = (LogModule) modules.get(0);
        this.discoveryModule = (DiscoveryModule) modules.get(1);

        doneFuture.set(null);
      },
      this::failModule, fiber);

  return doneFuture;
}
 
Example 2
Source File: FullTraversalConnector.java    From connector-sdk with Apache License 2.0 6 votes vote down vote up
private void processApiOperations(
    Iterable<ApiOperation> allDocs, ExecuteCounter executeCounter, String queueName)
    throws IOException, InterruptedException {
  // Split list of operations into partitions to reduce memory usage
  for (List<ApiOperation> partition : Iterables.partition(allDocs, partitionSize)) {
    List<ListenableFuture<List<GenericJson>>> futures = new ArrayList<>();
    for (ApiOperation operation : partition) {
      // check if abort the traverse based on configuration
      if (executeCounter.getFail() > numToAbort) {
        break;
      }
      ListenableFuture<List<GenericJson>> future =
          listeningExecutorService.submit(
              new ExecuteOperationCallable(operation, executeCounter, numToAbort, queueName));
      futures.add(future);
    }
    ListenableFuture<List<List<GenericJson>>> updates = Futures.allAsList(futures);
    try {
      updates.get();
    } catch (ExecutionException e) {
      throw new IOException(e);
    }
  }
}
 
Example 3
Source File: Futures2.java    From Camera2 with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new joined future from three existing futures and a joining function
 * that combines the resulting outputs of the previous functions into a single
 * result. The resulting future will fail if any of the dependent futures also
 * fail.
 */
public static <T1, T2, T3, TResult> ListenableFuture<TResult> joinAll(
        final ListenableFuture<T1> f1,
        final ListenableFuture<T2> f2,
        final ListenableFuture<T3> f3,
        final AsyncFunction3<T1, T2, T3, TResult> fn)
{
    ListenableFuture<?>[] futures = new ListenableFuture<?>[3];

    futures[0] = f1;
    futures[1] = f2;
    futures[2] = f3;

    // Futures.allAsList is used instead of Futures.successfulAsList because
    // allAsList will propagate the failures instead of null values to the
    // parameters of the supplied function.
    ListenableFuture<List<Object>> result = Futures.<Object>allAsList(futures);
    return Futures.transform(result, new AsyncFunction<List<Object>, TResult>()
    {
        @Override
        public ListenableFuture<TResult> apply(@Nullable List<Object> list) throws Exception
        {
            T1 value1 = (T1) list.get(0);
            T2 value2 = (T2) list.get(1);
            T3 value3 = (T3) list.get(2);

            return fn.apply(value1, value2, value3);
        }
    });
}
 
Example 4
Source File: SPV.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public ListenableFuture<Coin>
validateTx(final PreparedTransaction ptx, final String recipientStr, final Coin amount) {
    Address recipient = null;
    try {
        recipient = Address.fromBase58(mService.getNetworkParameters(), recipientStr);
    } catch (final AddressFormatException e) {
    }

    // 1. Find the change output:
    ListenableFuture<List<Boolean>> changeFn = Futures.immediateFuture(null);

    if (ptx.mDecoded.getOutputs().size() == 2) {
        changeFn = Futures.allAsList(Lists.newArrayList(verifyOutputSpendable(ptx, 0),
                                                        verifyOutputSpendable(ptx, 1)));
    }
    else if (ptx.mDecoded.getOutputs().size() > 2)
        throw new IllegalArgumentException("Verification: Wrong number of transaction outputs.");

    // 2. Verify the main output value and address, if available:
    final Address recipientAddr = recipient;
    return Futures.transform(changeFn, new Function<List<Boolean>, Coin>() {
        @Override
        public Coin apply(final List<Boolean> input) {
            return Verifier.verify(mService, mCountedUtxoValues, ptx, recipientAddr, amount, input);
        }
    });
}
 
Example 5
Source File: JavaIoFileSystemTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * This test has a large number of threads racing to create the same subdirectories.
 *
 * <p>We create N number of distinct directory trees, eg. the tree "0-0/0-1/0-2/0-3/0-4" followed
 * by the tree "1-0/1-1/1-2/1-3/1-4" etc. If there is race we should quickly get a deadlock.
 *
 * <p>A timeout of this test is likely because of a deadlock.
 */
@Test
public void testCreateDirectoriesThreadSafety() throws Exception {
  int threadCount = 200;
  int directoryCreationCount = 500; // We create this many sets of directories
  int subDirectoryCount = 5; // Each directory tree is this deep
  ListeningExecutorService executor =
      MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(threadCount));
  List<ListenableFuture<IOException>> futures = new ArrayList<>();
  for (int threadIndex = 0; threadIndex < threadCount; ++threadIndex) {
    futures.add(
        executor.submit(
            () -> {
              try {
                for (int loopi = 0; loopi < directoryCreationCount; ++loopi) {
                  List<Path> subDirs =
                      getSubDirectories(xEmptyDirectory, loopi, subDirectoryCount);
                  Path lastDir = Iterables.getLast(subDirs);
                  FileSystemUtils.createDirectoryAndParents(lastDir);
                }
              } catch (IOException e) {
                return e;
              }
              return null;
            }));
  }
  ListenableFuture<List<IOException>> all = Futures.allAsList(futures);
  // If the test times out here then there's likely to be a deadlock
  List<IOException> exceptions =
      all.get(TestUtils.WAIT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS);
  Optional<IOException> error = exceptions.stream().filter(Objects::nonNull).findFirst();
  if (error.isPresent()) {
    throw error.get();
  }
}
 
Example 6
Source File: RestartRunnableTestRun.java    From twill with Apache License 2.0 5 votes vote down vote up
@SafeVarargs
private final <V> ListenableFuture<List<V>> allAsList(Future<? extends V>... futures) {
  ImmutableList.Builder<ListenableFuture<? extends V>> listBuilder = ImmutableList.builder();
  for (Future<? extends V> future : futures) {
    listBuilder.add(JdkFutureAdapters.listenInPoolThread(future));
  }
  return Futures.allAsList(listBuilder.build());
}
 
Example 7
Source File: Http2Client.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void maxStreams() throws Exception {
  final int numThreads = 10;

  // Preliminary call to ensure MAX_STREAMS setting is received by the client.
  assertResponseEquals(blockingStub.unaryCall(simpleRequest), goldenResponse);

  threadpool = MoreExecutors.listeningDecorator(newFixedThreadPool(numThreads));
  List<ListenableFuture<?>> workerFutures = new ArrayList<>();
  for (int i = 0; i < numThreads; i++) {
    workerFutures.add(threadpool.submit(new MaxStreamsWorker(i, simpleRequest)));
  }
  ListenableFuture<?> f = Futures.allAsList(workerFutures);
  f.get(timeoutSeconds, TimeUnit.SECONDS);
}
 
Example 8
Source File: AbstractVirtualHost.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private ListenableFuture<Void> closeConnections()
{
    if (LOGGER.isDebugEnabled())
    {
        LOGGER.debug("Closing connection registry : {} connection(s).", _connections.size());
    }
    _acceptsConnections.set(false);
    for(AMQPConnection<?> conn : _connections)
    {
        conn.stopConnection();
    }

    List<ListenableFuture<Void>> connectionCloseFutures = new ArrayList<>();
    while (!_connections.isEmpty())
    {
        Iterator<AMQPConnection<?>> itr = _connections.iterator();
        while(itr.hasNext())
        {
            Connection<?> connection = itr.next();
            try
            {
                connectionCloseFutures.add(connection.closeAsync());
            }
            catch (Exception e)
            {
                LOGGER.warn("Exception closing connection " + connection.getName() + " from " + connection.getRemoteAddress(), e);
            }
            finally
            {
                itr.remove();
            }
        }
    }
    ListenableFuture<List<Void>> combinedFuture = Futures.allAsList(connectionCloseFutures);
    return Futures.transform(combinedFuture, voids -> null, MoreExecutors.directExecutor());
}
 
Example 9
Source File: RollupHandler.java    From blueflood with Apache License 2.0 5 votes vote down vote up
/**
 * 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.
 *
 * @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;
}
 
Example 10
Source File: BGPClusterSingletonService.java    From bgpcep with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public synchronized ListenableFuture<? extends CommitInfo> closeServiceInstance() {
    LOG.info("BGPClusterSingletonService {} close service instance", this.serviceGroupIdentifier.getName());
    this.instantiated.set(false);

    final List<ListenableFuture<? extends CommitInfo>> futurePeerCloseList = this.peers.values().stream()
            .map(PeerBean::closeServiceInstance).collect(Collectors.toList());
    final SettableFuture<? extends CommitInfo> done = SettableFuture.create();

    final ListenableFuture<List<CommitInfo>> futureResult = Futures.allAsList(futurePeerCloseList);
    Futures.addCallback(futureResult, new FutureCallback<List<? extends CommitInfo>>() {
        @Override
        public void onSuccess(final List<? extends CommitInfo> result) {
            synchronized (BGPClusterSingletonService.this) {
                if (BGPClusterSingletonService.this.ribImpl != null) {
                    done.setFuture(Futures.transform(BGPClusterSingletonService.this.ribImpl.closeServiceInstance(),
                        input -> null, MoreExecutors.directExecutor()));
                } else {
                    done.setFuture(Futures.transform(CommitInfo.emptyFluentFuture(),
                        input -> null, MoreExecutors.directExecutor()));
                }
            }
        }

        @Override
        public void onFailure(final Throwable throwable) {
            LOG.warn("Failed removing peers", throwable);
        }
    }, MoreExecutors.directExecutor());
    return done;
}
 
Example 11
Source File: StressTestClient.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
void blockUntilStressTestComplete() throws Exception {
  Preconditions.checkState(!shutdown, "client was shutdown.");

  ListenableFuture<?> f = Futures.allAsList(workerFutures);
  if (durationSecs == -1) {
    // '-1' indicates that the stress test runs until terminated by the user.
    f.get();
  } else {
    f.get(durationSecs + WORKER_GRACE_PERIOD_SECS, SECONDS);
  }
}
 
Example 12
Source File: LegacyIncludeScanner.java    From bazel with Apache License 2.0 5 votes vote down vote up
private ListenableFuture<?> processInclusions(
    Collection<Inclusion> inclusions,
    Artifact source,
    int contextPathPos,
    Kind contextKind,
    Set<Artifact> visited)
    throws IOException, InterruptedException {
  Preconditions.checkNotNull(inclusions, source);
  // Shuffle the inclusions to get better parallelism. See b/62200470.
  // Be careful not to modify the original collection! It's shared between any number of
  // threads.
  // TODO: Maybe we should shuffle before returning it to avoid the copy?
  List<Inclusion> shuffledInclusions = new ArrayList<>(inclusions);
  Collections.shuffle(shuffledInclusions, CONSTANT_SEED_RANDOM);

  // For each inclusion: get or locate its target file & recursively process
  IncludeScannerHelper helper =
      new IncludeScannerHelper(includePaths, quoteIncludePaths, source);
  List<ListenableFuture<?>> allFutures = new ArrayList<>(shuffledInclusions.size());
  for (Inclusion inclusion : shuffledInclusions) {
    allFutures.add(
        findAndProcess(
            helper.createInclusionWithContext(inclusion, contextPathPos, contextKind),
            source,
            visited));
  }
  return Futures.allAsList(allFutures);
}
 
Example 13
Source File: HiveMQNettyBootstrap.java    From hivemq-community-edition with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an aggregated future which allows to wait for all futures at once
 *
 * @param bindInformation a list of futures to aggregate
 * @return a {@link com.google.common.util.concurrent.ListenableFuture} which aggregates
 * all given {@link io.netty.channel.ChannelFuture}s
 */
@NotNull
private ListenableFuture<List<ListenerStartupInformation>> aggregatedFuture(final @NotNull List<BindInformation> bindInformation) {

    final List<ListenableFuture<ListenerStartupInformation>> listenableFutures = Lists.transform(bindInformation, new Function<BindInformation, ListenableFuture<ListenerStartupInformation>>() {
        @Override
        public ListenableFuture<ListenerStartupInformation> apply(final BindInformation input) {
            final SettableFuture<ListenerStartupInformation> objectSettableFuture = SettableFuture.create();
            input.getBindFuture().addListener(updateGivenFuture(objectSettableFuture, input));
            return objectSettableFuture;
        }
    });
    return Futures.allAsList(listenableFutures);
}
 
Example 14
Source File: CachingBuildEngine.java    From buck with Apache License 2.0 5 votes vote down vote up
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));
  }
  return Futures.allAsList(depResults);
}
 
Example 15
Source File: LegacyIncludeScanner.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Processes a given list of includes for a given base file and populates the provided set with
 * the visited includes
 *
 * @param source the source file used as a reference for finding includes
 * @param includes the list of -include option strings to locate and process
 * @param visited the set of files that are transitively included by {@code includes} to
 *     populate
 */
private ListenableFuture<?> processCmdlineIncludesAsync(
    Artifact source, List<String> includes, Set<Artifact> visited)
    throws IOException, ExecException, InterruptedException {
  List<ListenableFuture<?>> allFutures = new ArrayList<>(includes.size());
  for (String incl : includes) {
    InclusionWithContext inclusion = new InclusionWithContext(incl, Kind.QUOTE);
    allFutures.add(findAndProcess(inclusion, source, visited));
  }
  return Futures.allAsList(allFutures);
}
 
Example 16
Source File: BaseTimeseriesService.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<List<Void>> save(EntityId entityId, List<TsKvEntry> tsKvEntries, long ttl) {
    List<ListenableFuture<Void>> futures = Lists.newArrayListWithExpectedSize(tsKvEntries.size() * INSERTS_PER_ENTRY);
    for (TsKvEntry tsKvEntry : tsKvEntries) {
        if (tsKvEntry == null) {
            throw new IncorrectParameterException("Key value entry can't be null");
        }
        saveAndRegisterFutures(futures, entityId, tsKvEntry, ttl);
    }
    return Futures.allAsList(futures);
}
 
Example 17
Source File: StressTestClient.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
void blockUntilStressTestComplete() throws Exception {
  Preconditions.checkState(!shutdown, "client was shutdown.");

  ListenableFuture<?> f = Futures.allAsList(workerFutures);
  if (durationSecs == -1) {
    // '-1' indicates that the stress test runs until terminated by the user.
    f.get();
  } else {
    f.get(durationSecs + WORKER_GRACE_PERIOD_SECS, SECONDS);
  }
}
 
Example 18
Source File: Build.java    From buck with Apache License 2.0 4 votes vote down vote up
private BuildExecutionResult waitForBuildToFinish(
    ImmutableList<BuildRule> rulesToBuild, List<BuildEngine.BuildEngineResult> resultFutures)
    throws ExecutionException, InterruptedException, CleanBuildShutdownException {
  // Get the Future representing the build and then block until everything is built.
  ListenableFuture<List<BuildResult>> buildFuture =
      Futures.allAsList(
          resultFutures.stream()
              .map(BuildEngine.BuildEngineResult::getResult)
              .collect(Collectors.toList()));
  List<BuildResult> results;
  try {
    results = buildFuture.get();
    if (!buildContext.isKeepGoing()) {
      for (BuildResult result : results) {
        if (!result.isSuccess()) {
          if (result.getFailure() instanceof CleanBuildShutdownException) {
            throw (CleanBuildShutdownException) result.getFailure();
          } else {
            throw new BuildExecutionException(result.getFailure(), rulesToBuild, results);
          }
        }
      }
    }
  } catch (ExecutionException | InterruptedException | RuntimeException e) {
    Throwable t = Throwables.getRootCause(e);
    if (e instanceof InterruptedException
        || t instanceof InterruptedException
        || t instanceof ClosedByInterruptException) {
      try {
        LOG.debug("Cancelling all running builds because of InterruptedException");
        buildFuture.cancel(true);
      } catch (CancellationException ex) {
        // Rethrow original InterruptedException instead.
        LOG.warn(ex, "Received CancellationException during processing of InterruptedException");
      }
      Threads.interruptCurrentThread();
      throw new InterruptedException(e.getMessage());
    }
    throw e;
  }
  return createBuildExecutionResult(rulesToBuild, results);
}
 
Example 19
Source File: AbstractVirtualHost.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
private ListenableFuture<Void> doRestart()
{
    createHousekeepingExecutor();

    final VirtualHostStoreUpgraderAndRecoverer virtualHostStoreUpgraderAndRecoverer =
            new VirtualHostStoreUpgraderAndRecoverer((VirtualHostNode<?>) getParent());
    virtualHostStoreUpgraderAndRecoverer.reloadAndRecoverVirtualHost(getDurableConfigurationStore());

    final Collection<VirtualHostAccessControlProvider> accessControlProviders = getChildren(VirtualHostAccessControlProvider.class);
    if (!accessControlProviders.isEmpty())
    {
        accessControlProviders.forEach(child -> child.addChangeListener(_accessControlProviderListener));
    }

    final List<ListenableFuture<Void>> childOpenFutures = new ArrayList<>();

    Subject.doAs(getSubjectWithAddedSystemRights(), (PrivilegedAction<Object>) () ->
    {
        applyToChildren(child ->
                        {
                            final ListenableFuture<Void> childOpenFuture = child.openAsync();
                            childOpenFutures.add(childOpenFuture);

                            addFutureCallback(childOpenFuture, new FutureCallback<Void>()
                            {
                                @Override
                                public void onSuccess(final Void result)
                                {
                                }

                                @Override
                                public void onFailure(final Throwable t)
                                {
                                    LOGGER.error("Exception occurred while opening {} : {}",
                                                  child.getClass().getSimpleName(), child.getName(), t);
                                    onRestartFailure();
                                }

                            }, getTaskExecutor());
                        });
        return null;
    });

    ListenableFuture<List<Void>> combinedFuture = Futures.allAsList(childOpenFutures);
    return Futures.transformAsync(combinedFuture, input -> onActivate(), MoreExecutors.directExecutor());
}
 
Example 20
Source File: BuildEventArtifactUploader.java    From bazel with Apache License 2.0 4 votes vote down vote up
/**
 * Blocks on the completion of pending remote uploads, enforcing the relevant timeout if
 * applicable.
 */
default ListenableFuture<?> waitForRemoteUploads(
    Collection<ListenableFuture<String>> remoteUploads,
    ScheduledExecutorService timeoutExecutor) {
  return Futures.allAsList(remoteUploads);
}