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

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

Introduction

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

Prototype

@GwtIncompatible("TODO")
public static <V> V getUnchecked(Future<V> future) 

Source Link

Document

Returns the result of calling Future#get() uninterruptibly on a task known not to throw a checked exception.

Usage

From source file:org.onosproject.cluster.impl.MastershipManager.java

@Override
public void balanceRoles() {
    List<ControllerNode> nodes = newArrayList(clusterService.getNodes());
    Map<ControllerNode, Set<DeviceId>> controllerDevices = new HashMap<>();
    int deviceCount = 0;

    // Create buckets reflecting current ownership.
    for (ControllerNode node : nodes) {
        if (clusterService.getState(node.id()) == ACTIVE) {
            Set<DeviceId> devicesOf = new HashSet<>(getDevicesOf(node.id()));
            deviceCount += devicesOf.size();
            controllerDevices.put(node, devicesOf);
            log.info("Node {} has {} devices.", node.id(), devicesOf.size());
        }//  w w  w  .j  av  a 2 s .  co m
    }

    // Now re-balance the buckets until they are roughly even.
    List<CompletableFuture<Void>> balanceBucketsFutures = Lists.newLinkedList();
    int rounds = controllerDevices.keySet().size();
    for (int i = 0; i < rounds; i++) {
        // Iterate over the buckets and find the smallest and the largest.
        ControllerNode smallest = findBucket(true, controllerDevices);
        ControllerNode largest = findBucket(false, controllerDevices);
        balanceBucketsFutures.add(balanceBuckets(smallest, largest, controllerDevices, deviceCount));
    }
    CompletableFuture<Void> balanceRolesFuture = CompletableFuture
            .allOf(balanceBucketsFutures.toArray(new CompletableFuture[balanceBucketsFutures.size()]));

    Futures.getUnchecked(balanceRolesFuture);
}

From source file:co.cask.tigon.internal.app.runtime.distributed.AbstractProgramTwillRunnable.java

@Override
public void run() {
    LOG.info("Starting metrics service");
    Futures.getUnchecked(Services.chainStart(zkClientService, metricsCollectionService, resourceReporter));

    LOG.info("Starting runnable: {}", name);
    controller = injector.getInstance(getProgramClass()).run(program, programOpts);
    final SettableFuture<ProgramController.State> state = SettableFuture.create();
    controller.addListener(new AbstractListener() {
        @Override/* w ww .j a  v  a 2s  .  co  m*/
        public void stopped() {
            state.set(ProgramController.State.STOPPED);
        }

        @Override
        public void error(Throwable cause) {
            LOG.error("Program runner error out.", cause);
            state.setException(cause);
        }
    }, MoreExecutors.sameThreadExecutor());

    runlatch.countDown();
    try {
        state.get();
        LOG.info("Program stopped.");
    } catch (Throwable t) {
        LOG.error("Program terminated due to error.", t);
        throw Throwables.propagate(t);
    }
}

From source file:co.cask.cdap.data.runtime.main.MasterServiceMain.java

@Override
public void start() {
    try {/*from w  w w  .  ja  v a2  s .  co m*/
        // Workaround for release of file descriptors opened by URLClassLoader - https://issues.cask.co/browse/CDAP-2841
        URLConnections.setDefaultUseCaches(false);
    } catch (IOException e) {
        LOG.error("Could not disable caching of URLJarFiles. This may lead to 'too many open files` exception.",
                e);
    }

    createSystemHBaseNamespace();
    updateConfigurationTable();

    LogAppenderInitializer logAppenderInitializer = baseInjector.getInstance(LogAppenderInitializer.class);
    logAppenderInitializer.initialize();

    zkClient.startAndWait();
    // Tries to create the ZK root node (which can be namespaced through the zk connection string)
    Futures.getUnchecked(ZKOperations.ignoreError(zkClient.create("/", null, CreateMode.PERSISTENT),
            KeeperException.NodeExistsException.class, null));

    twillRunner.start();

    kafkaClient.startAndWait();
    metricsCollectionService.startAndWait();
    serviceStore.startAndWait();
    leaderElection.startAndWait();
}

From source file:co.cask.cdap.internal.app.runtime.distributed.ServiceTwillRunnable.java

@Override
public void initialize(TwillContext context) {
    name = context.getSpecification().getName();
    Map<String, String> configs = context.getSpecification().getConfigs();

    LOG.info("Initialize runnable: " + name);
    try {//from w  w  w.j a v  a  2s.  com
        CommandLine cmdLine = parseArgs(context.getApplicationArguments());

        // Loads configurations
        hConf = new Configuration();
        hConf.clear();
        hConf.addResource(new File(configs.get("hConf")).toURI().toURL());

        UserGroupInformation.setConfiguration(hConf);

        cConf = CConfiguration.create();
        cConf.clear();
        cConf.addResource(new File(configs.get("cConf")).toURI().toURL());

        injector = Guice.createInjector(createModule(context));

        zkClientService = injector.getInstance(ZKClientService.class);
        kafkaClientService = injector.getInstance(KafkaClientService.class);
        metricsCollectionService = injector.getInstance(MetricsCollectionService.class);

        // Initialize log appender
        logAppenderInitializer = injector.getInstance(LogAppenderInitializer.class);
        logAppenderInitializer.initialize();

        transactionSystemClient = injector.getInstance(TransactionSystemClient.class);
        datasetFramework = injector.getInstance(DatasetFramework.class);
        discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);

        try {
            program = injector.getInstance(ProgramFactory.class)
                    .create(cmdLine.getOptionValue(RunnableOptions.JAR));
        } catch (IOException e) {
            throw Throwables.propagate(e);
        }

        Arguments runtimeArguments = new Gson().fromJson(cmdLine.getOptionValue(RunnableOptions.RUNTIME_ARGS),
                BasicArguments.class);
        programOpts = new SimpleProgramOptions(name, createProgramArguments(context, configs),
                runtimeArguments);
        resourceReporter = new ProgramRunnableResourceReporter(program, metricsCollectionService, context);

        // These services need to be starting before initializing the delegate since they are used in
        // AbstractContext's constructor to create datasets.
        Futures.getUnchecked(Services.chainStart(zkClientService, kafkaClientService, metricsCollectionService,
                resourceReporter));

        ApplicationSpecification appSpec = program.getSpecification();
        String processorName = program.getName();
        runnableName = programOpts.getName();

        Arguments arguments = programOpts.getArguments();
        RunId runId = arguments.hasOption(ProgramOptionConstants.RUN_ID)
                ? RunIds.fromString(arguments.getOption(ProgramOptionConstants.RUN_ID))
                : RunIds.generate();

        ServiceSpecification serviceSpec = appSpec.getServices().get(processorName);
        final RuntimeSpecification runtimeSpec = serviceSpec.getRunnables().get(runnableName);
        String className = runtimeSpec.getRunnableSpecification().getClassName();
        LOG.info("Getting class : {}", program.getMainClass().getName());
        Class<?> clz = Class.forName(className, true, program.getClassLoader());
        Preconditions.checkArgument(TwillRunnable.class.isAssignableFrom(clz), "%s is not a TwillRunnable.",
                clz);

        if (clz.isAssignableFrom(HttpServiceTwillRunnable.class)) {
            // Special case for running http services since we need to instantiate the http service
            // using the program classloader.
            delegate = new HttpServiceTwillRunnable(program, runId, cConf, runnableName,
                    metricsCollectionService, discoveryServiceClient, datasetFramework,
                    transactionSystemClient);
        } else if (clz.isAssignableFrom(ServiceWorkerTwillRunnable.class)) {
            delegate = new ServiceWorkerTwillRunnable(program, runId, runnableName, program.getClassLoader(),
                    cConf, metricsCollectionService, datasetFramework, transactionSystemClient,
                    discoveryServiceClient);
        } else {
            delegate = (TwillRunnable) new InstantiatorFactory(false).get(TypeToken.of(clz)).create();
        }

        Reflections.visit(delegate, TypeToken.of(delegate.getClass()),
                new MetricsFieldSetter(new ServiceRunnableMetrics(metricsCollectionService,
                        program.getApplicationId(), program.getName(), runnableName, context.getInstanceId())),
                new PropertyFieldSetter(runtimeSpec.getRunnableSpecification().getConfigs()));

        final String[] argArray = RuntimeArguments.toPosixArray(programOpts.getUserArguments());
        LoggingContextAccessor.setLoggingContext(new UserServiceLoggingContext(program.getAccountId(),
                program.getApplicationId(), program.getName(), runnableName));
        delegate.initialize(new ForwardingTwillContext(context) {
            @Override
            public String[] getApplicationArguments() {
                return argArray;
            }

            @Override
            public TwillRunnableSpecification getSpecification() {
                return runtimeSpec.getRunnableSpecification();
            }

            @Override
            public Cancellable announce(String runnable, int port) {
                String serviceContextPath = String.format("%s.%s.%s.%s",
                        ProgramType.SERVICE.name().toLowerCase(), program.getAccountId(),
                        program.getApplicationId(), program.getName());
                // Currently ignoring the runnable's name (the param passed into announce), and simply announcing by the name
                // of the service it belongs to (reasoning: the primary runnable of a service will be identified by the
                // service's name, and the other runnables within the service are not discoverable externally).
                return super.announce(serviceContextPath, port);
            }
        });

        LOG.info("Runnable initialized: " + name);
    } catch (Throwable t) {
        LOG.error(t.getMessage(), t);
        throw Throwables.propagate(t);
    }
}

From source file:com.facebook.buck.core.graph.transformation.DefaultAsyncTransformationEngine.java

@Override
public final ImmutableMap<ComputeKey, ComputeResult> computeAllUnchecked(Iterable<ComputeKey> keys) {
    return Futures.getUnchecked(collectFutures(computeAll(keys)));
}

From source file:ninja.leaping.permissionsex.extrabackends.groupmanager.GroupManagerDataStore.java

@Override
public Iterable<Map.Entry<Map.Entry<String, String>, ImmutableSubjectData>> getAll() {
    return Iterables.transform(
            Iterables.concat(//from   w ww . java2  s  .  c  o  m
                    Iterables.transform(getAllIdentifiers(SUBJECTS_USER),
                            name -> Maps.immutableEntry(SUBJECTS_USER, name)),
                    Iterables.transform(getAllIdentifiers(SUBJECTS_GROUP),
                            name -> Maps.immutableEntry(SUBJECTS_GROUP, name))),
            input -> Maps.immutableEntry(input,
                    Futures.getUnchecked(getData(input.getKey(), input.getValue(), null))));
}

From source file:org.onosproject.store.link.impl.ECLinkStore.java

@Override
public LinkEvent createOrUpdateLink(ProviderId providerId, LinkDescription linkDescription) {
    final DeviceId dstDeviceId = linkDescription.dst().deviceId();
    final NodeId dstNodeId = mastershipService.getMasterFor(dstDeviceId);

    // Process link update only if we're the master of the destination node,
    // otherwise signal the actual master.
    if (clusterService.getLocalNode().id().equals(dstNodeId)) {
        LinkKey linkKey = linkKey(linkDescription.src(), linkDescription.dst());
        Provided<LinkKey> internalLinkKey = getProvided(linkKey, providerId);
        if (internalLinkKey == null) {
            return null;
        }/*  w w w  .  j  a  va  2  s.  c om*/
        linkDescriptions.compute(internalLinkKey, (k, v) -> createOrUpdateLinkInternal(v, linkDescription));
        return refreshLinkCache(linkKey);
    } else {
        if (dstNodeId == null) {
            return null;
        }
        return Futures
                .getUnchecked(clusterCommunicator.sendAndReceive(new Provided<>(linkDescription, providerId),
                        LINK_INJECT_MESSAGE, SERIALIZER::encode, SERIALIZER::decode, dstNodeId));
    }
}

From source file:com.jordanwilliams.heftydb.compact.Compactor.java

public synchronized Future<?> scheduleCompaction() {
    final int id = compactionId.incrementAndGet();
    logger.debug("Starting compaction " + id);

    CompactionPlan compactionPlan = compactionPlanner.planCompaction();

    if (compactionPlan == null) {
        logger.debug("No compaction tasks present " + id);
        logger.debug("Finishing compaction " + id);
        return Futures.immediateFuture(null);
    }/*from w  w w .java2  s  . co m*/

    final List<Future<?>> taskFutures = new ArrayList<Future<?>>();
    Throttle compactionThrottle = new Throttle(config.maxCompactionRate());

    for (CompactionTask task : compactionPlan) {
        logger.debug("Compaction " + id + "  task : " + task);

        for (Table table : task.tables()) {
            compactionTables.markAsCompacted(table);
        }

        ThreadPoolExecutor taskExecutor = task.priority().equals(CompactionTask.Priority.HIGH)
                ? highPriorityCompactionTaskExecutor
                : compactionTaskExecutor;

        taskFutures.add(taskExecutor.submit(new Task(task, compactionThrottle)));
    }

    metrics.histogram("compactor.concurrentTasks").update(
            highPriorityCompactionTaskExecutor.getActiveCount() + compactionTaskExecutor.getActiveCount());

    return compactionExecutor.submit(new Runnable() {
        @Override
        public void run() {
            for (Future<?> taskFuture : taskFutures) {
                Futures.getUnchecked(taskFuture);
            }

            logger.debug("Finishing compaction " + id);
        }
    });
}

From source file:org.apache.twill.discovery.ZKDiscoveryService.java

/**
 * Registers a {@link Discoverable} in zookeeper.
 * <p>//from w  ww. j a  v  a2s .c  om
 *   Registering a {@link Discoverable} will create a node &lt;base&gt;/&lt;service-name&gt;
 *   in zookeeper as a ephemeral node. If the node already exists (timeout associated with emphemeral node creation), 
 *   then a runtime exception is thrown to make sure that a service with an intent to register is not started without 
 *   registering. 
 *   When a runtime exception is thrown, expectation is that the process being started will fail and would be started 
 *   again by the monitoring service.
 * </p>
 * @param discoverable Information of the service provider that could be discovered.
 * @return An instance of {@link Cancellable}
 */
@Override
public Cancellable register(final Discoverable discoverable) {
    if (closed.get()) {
        throw new IllegalStateException("Cannot register discoverable through a closed ZKDiscoveryService");
    }

    final SettableFuture<String> future = SettableFuture.create();
    final DiscoveryCancellable cancellable = new DiscoveryCancellable(discoverable);

    // Create the zk ephemeral node.
    Futures.addCallback(doRegister(discoverable), new FutureCallback<String>() {
        @Override
        public void onSuccess(String result) {
            // Set the sequence node path to cancellable for future cancellation.
            cancellable.setPath(result);
            lock.lock();
            try {
                if (!closed.get()) {
                    discoverables.put(discoverable, cancellable);
                } else {
                    cancellable.asyncCancel();
                }
            } finally {
                lock.unlock();
            }
            LOG.debug("Service registered: {} {}", discoverable, result);
            future.set(result);
        }

        @Override
        public void onFailure(Throwable t) {
            if (t instanceof KeeperException.NodeExistsException) {
                handleRegisterFailure(discoverable, future, this, t);
            } else {
                LOG.warn("Failed to register: {}", discoverable, t);
                future.setException(t);
            }
        }
    }, Threads.SAME_THREAD_EXECUTOR);

    Futures.getUnchecked(future);
    return cancellable;
}

From source file:zipkin.storage.cassandra3.Cassandra3Storage.java

/** Truncates all the column families, or throws on any failure. */
@VisibleForTesting/*from ww  w  .j  ava2 s  .c  o m*/
void clear() {
    List<ListenableFuture<?>> futures = new LinkedList<>();
    for (String cf : ImmutableList.of(Schema.TABLE_TRACES, Schema.TABLE_TRACE_BY_SERVICE_SPAN,
            Schema.TABLE_SERVICE_SPANS, Schema.TABLE_DEPENDENCIES)) {
        futures.add(session.get().executeAsync(format("TRUNCATE %s", cf)));
    }
    Futures.getUnchecked(Futures.allAsList(futures));
}