Search in sources :

Example 16 with ListenableFuture

use of com.google.common.util.concurrent.ListenableFuture in project hive by apache.

the class HostExecutor method execInstances.

private List<ListenableFuture<RemoteCommandResult>> execInstances(List<Drone> drones, final String cmd) throws InterruptedException, IOException {
    List<ListenableFuture<RemoteCommandResult>> result = Lists.newArrayList();
    for (final Drone drone : ImmutableList.copyOf(drones)) {
        result.add(mExecutor.submit(new Callable<RemoteCommandResult>() {

            @Override
            public RemoteCommandResult call() throws Exception {
                Map<String, String> templateVariables = Maps.newHashMap(mTemplateDefaults);
                templateVariables.put("instanceName", drone.getInstanceName());
                templateVariables.put("localDir", drone.getLocalDirectory());
                String command = Templates.getTemplateResult(cmd, templateVariables);
                SSHResult result = new SSHCommand(mSSHCommandExecutor, drone.getPrivateKey(), drone.getUser(), drone.getHost(), drone.getInstance(), command, true).call();
                if (result.getExitCode() != Constants.EXIT_CODE_SUCCESS) {
                    // return value not checked due to concurrent access
                    mDrones.remove(drone);
                    mLogger.error("Aborting drone during exec " + command, new AbortDroneException("Drone " + drone + " exited with " + result.getExitCode() + ": " + result));
                    return null;
                } else {
                    return result;
                }
            }
        }));
    }
    return result;
}
Also used : SSHResult(org.apache.hive.ptest.execution.ssh.SSHResult) SSHCommand(org.apache.hive.ptest.execution.ssh.SSHCommand) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Callable(java.util.concurrent.Callable)

Example 17 with ListenableFuture

use of com.google.common.util.concurrent.ListenableFuture in project hive by apache.

the class Phase method initalizeHosts.

protected List<RemoteCommandResult> initalizeHosts() throws Exception {
    List<ListenableFuture<List<RemoteCommandResult>>> futures = Lists.newArrayList();
    ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(hostExecutors.size()));
    try {
        for (final HostExecutor hostExecutor : hostExecutors) {
            futures.add(executor.submit(new Callable<List<RemoteCommandResult>>() {

                @Override
                public List<RemoteCommandResult> call() throws Exception {
                    return initalizeHost(hostExecutor);
                }
            }));
        }
        List<RemoteCommandResult> results = Lists.newArrayList();
        for (ListenableFuture<List<RemoteCommandResult>> future : futures) {
            List<RemoteCommandResult> result = future.get();
            if (result != null) {
                results.addAll(result);
            }
        }
        executor.shutdown();
        return results;
    } finally {
        if (executor.isShutdown()) {
            executor.shutdownNow();
        }
    }
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) RemoteCommandResult(org.apache.hive.ptest.execution.ssh.RemoteCommandResult) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) List(java.util.List) Callable(java.util.concurrent.Callable)

Example 18 with ListenableFuture

use of com.google.common.util.concurrent.ListenableFuture in project weave by continuuity.

the class RunningContainers method stopAll.

/**
   * Stops all running services. Only called when the AppMaster stops.
   */
void stopAll() {
    containerLock.lock();
    try {
        // Stop it one by one in reverse order of start sequence
        Iterator<String> itor = startSequence.descendingIterator();
        List<ListenableFuture<ServiceController.State>> futures = Lists.newLinkedList();
        while (itor.hasNext()) {
            String runnableName = itor.next();
            LOG.info("Stopping all instances of " + runnableName);
            futures.clear();
            // Parallel stops all running containers of the current runnable.
            for (WeaveContainerController controller : containers.row(runnableName).values()) {
                futures.add(controller.stop());
            }
            // Wait for containers to stop. Assumes the future returned by Futures.successfulAsList won't throw exception.
            Futures.getUnchecked(Futures.successfulAsList(futures));
            LOG.info("Terminated all instances of " + runnableName);
        }
        containers.clear();
        runnableInstances.clear();
    } finally {
        containerLock.unlock();
    }
}
Also used : WeaveContainerController(com.continuuity.weave.internal.WeaveContainerController) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ServiceController(com.continuuity.weave.api.ServiceController)

Example 19 with ListenableFuture

use of com.google.common.util.concurrent.ListenableFuture in project buck by facebook.

the class DistBuildClientExecutor method executeAndPrintFailuresToEventBus.

public int executeAndPrintFailuresToEventBus(final WeightedListeningExecutorService executorService, ProjectFilesystem projectFilesystem, FileHashCache fileHashCache, BuckEventBus eventBus) throws IOException, InterruptedException {
    BuildJob job = distBuildService.createBuild();
    final StampedeId id = job.getStampedeId();
    LOG.info("Created job. Build id = " + id.getId());
    logDebugInfo(job);
    List<ListenableFuture<Void>> asyncJobs = new LinkedList<>();
    LOG.info("Uploading local changes.");
    asyncJobs.add(distBuildService.uploadMissingFiles(buildJobState.fileHashes, executorService));
    LOG.info("Uploading target graph.");
    asyncJobs.add(distBuildService.uploadTargetGraph(buildJobState, id, executorService));
    LOG.info("Uploading buck dot-files.");
    asyncJobs.add(distBuildService.uploadBuckDotFiles(id, projectFilesystem, fileHashCache, executorService));
    try {
        Futures.allAsList(asyncJobs).get();
    } catch (ExecutionException e) {
        LOG.error("Upload failed.");
        throw new RuntimeException(e);
    }
    distBuildService.setBuckVersion(id, buckVersion);
    LOG.info("Set Buck Version. Build status: " + job.getStatus().toString());
    job = distBuildService.startBuild(id);
    LOG.info("Started job. Build status: " + job.getStatus().toString());
    logDebugInfo(job);
    Stopwatch stopwatch = Stopwatch.createStarted();
    // Keep polling until the build is complete or failed.
    do {
        job = distBuildService.getCurrentBuildJobState(id);
        LOG.info("Got build status: " + job.getStatus().toString());
        DistBuildStatus distBuildStatus = prepareStatusFromJob(job).setETAMillis(MAX_BUILD_DURATION_MILLIS - stopwatch.elapsed(TimeUnit.MILLISECONDS)).build();
        eventBus.post(new DistBuildStatusEvent(distBuildStatus));
        List<LogLineBatchRequest> newLogLineRequests = distBuildLogStateTracker.createRealtimeLogRequests(job.getSlaveInfoByRunId().values());
        MultiGetBuildSlaveRealTimeLogsResponse slaveLogsResponse = distBuildService.fetchSlaveLogLines(job.stampedeId, newLogLineRequests);
        distBuildLogStateTracker.processStreamLogs(slaveLogsResponse.getMultiStreamLogs());
        try {
            // TODO(shivanker): Get rid of sleeps in methods which we want to unit test
            Thread.sleep(millisBetweenStatusPoll);
        } catch (InterruptedException e) {
            LOG.error(e, "BuildStatus polling sleep call has been interrupted unexpectedly.");
        }
    } while (!(job.getStatus().equals(BuildStatus.FINISHED_SUCCESSFULLY) || job.getStatus().equals(BuildStatus.FAILED)));
    try {
        MultiGetBuildSlaveLogDirResponse logDirsResponse = distBuildService.fetchBuildSlaveLogDir(job.stampedeId, distBuildLogStateTracker.runIdsToMaterializeLogDirsFor(job.slaveInfoByRunId.values()));
        distBuildLogStateTracker.materializeLogDirs(logDirsResponse.getLogDirs());
    } catch (IOException ex) {
        LOG.error(ex);
    }
    LOG.info("Build was " + (job.getStatus().equals(BuildStatus.FINISHED_SUCCESSFULLY) ? "" : "not ") + "successful!");
    logDebugInfo(job);
    DistBuildStatus distBuildStatus = prepareStatusFromJob(job).setETAMillis(0).build();
    eventBus.post(new DistBuildStatusEvent(distBuildStatus));
    return job.getStatus().equals(BuildStatus.FINISHED_SUCCESSFULLY) ? 0 : 1;
}
Also used : LogLineBatchRequest(com.facebook.buck.distributed.thrift.LogLineBatchRequest) MultiGetBuildSlaveRealTimeLogsResponse(com.facebook.buck.distributed.thrift.MultiGetBuildSlaveRealTimeLogsResponse) MultiGetBuildSlaveLogDirResponse(com.facebook.buck.distributed.thrift.MultiGetBuildSlaveLogDirResponse) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) LinkedList(java.util.LinkedList) StampedeId(com.facebook.buck.distributed.thrift.StampedeId) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) BuildJob(com.facebook.buck.distributed.thrift.BuildJob) ExecutionException(java.util.concurrent.ExecutionException)

Example 20 with ListenableFuture

use of com.google.common.util.concurrent.ListenableFuture in project buck by facebook.

the class DistBuildFileHashes method ruleKeyComputation.

private static ListenableFuture<ImmutableMap<BuildRule, RuleKey>> ruleKeyComputation(ActionGraph actionGraph, final LoadingCache<ProjectFilesystem, DefaultRuleKeyFactory> ruleKeyFactories, ListeningExecutorService executorService) {
    List<ListenableFuture<Map.Entry<BuildRule, RuleKey>>> ruleKeyEntries = new ArrayList<>();
    for (final BuildRule rule : actionGraph.getNodes()) {
        ruleKeyEntries.add(executorService.submit(() -> Maps.immutableEntry(rule, ruleKeyFactories.get(rule.getProjectFilesystem()).build(rule))));
    }
    ListenableFuture<List<Map.Entry<BuildRule, RuleKey>>> ruleKeyComputation = Futures.allAsList(ruleKeyEntries);
    return Futures.transform(ruleKeyComputation, new Function<List<Map.Entry<BuildRule, RuleKey>>, ImmutableMap<BuildRule, RuleKey>>() {

        @Override
        public ImmutableMap<BuildRule, RuleKey> apply(List<Map.Entry<BuildRule, RuleKey>> input) {
            return ImmutableMap.copyOf(input);
        }
    }, executorService);
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) ArrayList(java.util.ArrayList) ImmutableMap(com.google.common.collect.ImmutableMap) BuildJobStateFileHashEntry(com.facebook.buck.distributed.thrift.BuildJobStateFileHashEntry) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) BuildRule(com.facebook.buck.rules.BuildRule) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

ListenableFuture (com.google.common.util.concurrent.ListenableFuture)566 ArrayList (java.util.ArrayList)292 List (java.util.List)175 Test (org.junit.Test)158 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)111 ExecutionException (java.util.concurrent.ExecutionException)111 Map (java.util.Map)108 Futures (com.google.common.util.concurrent.Futures)93 IOException (java.io.IOException)77 WriteTransaction (org.opendaylight.controller.md.sal.binding.api.WriteTransaction)71 HashMap (java.util.HashMap)64 ImmutableList (com.google.common.collect.ImmutableList)63 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)62 ImmutableMap (com.google.common.collect.ImmutableMap)59 Set (java.util.Set)59 BigInteger (java.math.BigInteger)57 File (java.io.File)56 Logger (org.slf4j.Logger)56 Nullable (javax.annotation.Nullable)55 LoggerFactory (org.slf4j.LoggerFactory)55