Search in sources :

Example 76 with Future

use of java.util.concurrent.Future in project elasticsearch by elastic.

the class UnicastZenPing method resolveHostsLists.

/**
     * Resolves a list of hosts to a list of discovery nodes. Each host is resolved into a transport address (or a collection of addresses
     * if the number of ports is greater than one) and the transport addresses are used to created discovery nodes. Host lookups are done
     * in parallel using specified executor service up to the specified resolve timeout.
     *
     * @param executorService  the executor service used to parallelize hostname lookups
     * @param logger           logger used for logging messages regarding hostname lookups
     * @param hosts            the hosts to resolve
     * @param limitPortCounts  the number of ports to resolve (should be 1 for non-local transport)
     * @param transportService the transport service
     * @param nodeId_prefix    a prefix to use for node ids
     * @param resolveTimeout   the timeout before returning from hostname lookups
     * @return a list of discovery nodes with resolved transport addresses
     */
public static List<DiscoveryNode> resolveHostsLists(final ExecutorService executorService, final Logger logger, final List<String> hosts, final int limitPortCounts, final TransportService transportService, final String nodeId_prefix, final TimeValue resolveTimeout) throws InterruptedException {
    Objects.requireNonNull(executorService);
    Objects.requireNonNull(logger);
    Objects.requireNonNull(hosts);
    Objects.requireNonNull(transportService);
    Objects.requireNonNull(nodeId_prefix);
    Objects.requireNonNull(resolveTimeout);
    if (resolveTimeout.nanos() < 0) {
        throw new IllegalArgumentException("resolve timeout must be non-negative but was [" + resolveTimeout + "]");
    }
    // create tasks to submit to the executor service; we will wait up to resolveTimeout for these tasks to complete
    final List<Callable<TransportAddress[]>> callables = hosts.stream().map(hn -> (Callable<TransportAddress[]>) () -> transportService.addressesFromString(hn, limitPortCounts)).collect(Collectors.toList());
    final List<Future<TransportAddress[]>> futures = executorService.invokeAll(callables, resolveTimeout.nanos(), TimeUnit.NANOSECONDS);
    final List<DiscoveryNode> discoveryNodes = new ArrayList<>();
    final Set<TransportAddress> localAddresses = new HashSet<>();
    localAddresses.add(transportService.boundAddress().publishAddress());
    localAddresses.addAll(Arrays.asList(transportService.boundAddress().boundAddresses()));
    // ExecutorService#invokeAll guarantees that the futures are returned in the iteration order of the tasks so we can associate the
    // hostname with the corresponding task by iterating together
    final Iterator<String> it = hosts.iterator();
    for (final Future<TransportAddress[]> future : futures) {
        final String hostname = it.next();
        if (!future.isCancelled()) {
            assert future.isDone();
            try {
                final TransportAddress[] addresses = future.get();
                logger.trace("resolved host [{}] to {}", hostname, addresses);
                for (int addressId = 0; addressId < addresses.length; addressId++) {
                    final TransportAddress address = addresses[addressId];
                    // no point in pinging ourselves
                    if (localAddresses.contains(address) == false) {
                        discoveryNodes.add(new DiscoveryNode(nodeId_prefix + hostname + "_" + addressId + "#", address, emptyMap(), emptySet(), Version.CURRENT.minimumCompatibilityVersion()));
                    }
                }
            } catch (final ExecutionException e) {
                assert e.getCause() != null;
                final String message = "failed to resolve host [" + hostname + "]";
                logger.warn(message, e.getCause());
            }
        } else {
            logger.warn("timed out after [{}] resolving host [{}]", resolveTimeout, hostname);
        }
    }
    return discoveryNodes;
}
Also used : StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) Arrays(java.util.Arrays) TransportRequest(org.elasticsearch.transport.TransportRequest) Releasables(org.elasticsearch.common.lease.Releasables) Property(org.elasticsearch.common.settings.Setting.Property) ConcurrentCollections(org.elasticsearch.common.util.concurrent.ConcurrentCollections) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ConcurrentCollections.newConcurrentMap(org.elasticsearch.common.util.concurrent.ConcurrentCollections.newConcurrentMap) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) Future(java.util.concurrent.Future) Settings(org.elasticsearch.common.settings.Settings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) PingResponse.readPingResponse(org.elasticsearch.discovery.zen.ZenPing.PingResponse.readPingResponse) Map(java.util.Map) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ClusterName(org.elasticsearch.cluster.ClusterName) CollectionUtils(org.elasticsearch.common.util.CollectionUtils) ThreadFactory(java.util.concurrent.ThreadFactory) Releasable(org.elasticsearch.common.lease.Releasable) Setting(org.elasticsearch.common.settings.Setting) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) KeyedLock(org.elasticsearch.common.util.concurrent.KeyedLock) TransportRequestHandler(org.elasticsearch.transport.TransportRequestHandler) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) Stream(java.util.stream.Stream) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Supplier(org.apache.logging.log4j.util.Supplier) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) Queue(java.util.Queue) TransportException(org.elasticsearch.transport.TransportException) TransportChannel(org.elasticsearch.transport.TransportChannel) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) EsThreadPoolExecutor(org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TimeValue(org.elasticsearch.common.unit.TimeValue) TransportResponse(org.elasticsearch.transport.TransportResponse) TransportService(org.elasticsearch.transport.TransportService) ExecutorService(java.util.concurrent.ExecutorService) ConnectionProfile(org.elasticsearch.transport.ConnectionProfile) Collections.emptyMap(java.util.Collections.emptyMap) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) EsExecutors(org.elasticsearch.common.util.concurrent.EsExecutors) AbstractComponent(org.elasticsearch.common.component.AbstractComponent) Iterator(java.util.Iterator) Collections.emptySet(java.util.Collections.emptySet) IOUtils(org.apache.lucene.util.IOUtils) IOException(java.io.IOException) Connection(org.elasticsearch.transport.Transport.Connection) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NodeNotConnectedException(org.elasticsearch.transport.NodeNotConnectedException) Collections(java.util.Collections) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet)

Example 77 with Future

use of java.util.concurrent.Future in project sonarqube by SonarSource.

the class StopperThread method run.

@Override
public void run() {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future future = executor.submit(new Runnable() {

        @Override
        public void run() {
            monitored.stop();
        }
    });
    try {
        future.get(terminationTimeout, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        LoggerFactory.getLogger(getClass()).error(String.format("Can not stop in %dms", terminationTimeout), e);
    }
    executor.shutdownNow();
    commands.endWatch();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Example 78 with Future

use of java.util.concurrent.Future in project sonarqube by SonarSource.

the class BaseIndexer method index.

public void index(final IndexerTask task) {
    final long requestedAt = system2.now();
    Future submit = executor.submit(() -> {
        if (lastUpdatedAt == -1L) {
            lastUpdatedAt = esClient.getMaxFieldValue(indexType, dateFieldName);
        }
        if (requestedAt > lastUpdatedAt) {
            long l = task.index(lastUpdatedAt);
            // l can be 0 if no documents were indexed
            lastUpdatedAt = Math.max(l, lastUpdatedAt);
        }
    });
    try {
        Uninterruptibles.getUninterruptibly(submit);
    } catch (ExecutionException e) {
        Throwables.propagate(e);
    }
}
Also used : Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Example 79 with Future

use of java.util.concurrent.Future in project disruptor by LMAX-Exchange.

the class ThreeToOneSequencedThroughputTest method runDisruptorPass.

@Override
protected long runDisruptorPass() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    handler.reset(latch, batchEventProcessor.getSequence().get() + ((ITERATIONS / NUM_PUBLISHERS) * NUM_PUBLISHERS));
    Future<?>[] futures = new Future[NUM_PUBLISHERS];
    for (int i = 0; i < NUM_PUBLISHERS; i++) {
        futures[i] = executor.submit(valuePublishers[i]);
    }
    executor.submit(batchEventProcessor);
    long start = System.currentTimeMillis();
    cyclicBarrier.await();
    for (int i = 0; i < NUM_PUBLISHERS; i++) {
        futures[i].get();
    }
    latch.await();
    long opsPerSecond = (ITERATIONS * 1000L) / (System.currentTimeMillis() - start);
    batchEventProcessor.halt();
    return opsPerSecond;
}
Also used : Future(java.util.concurrent.Future) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 80 with Future

use of java.util.concurrent.Future in project disruptor by LMAX-Exchange.

the class ThreeToThreeSequencedThroughputTest method runDisruptorPass.

@Override
protected long runDisruptorPass() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    handler.reset(latch, ITERATIONS);
    Future<?>[] futures = new Future[NUM_PUBLISHERS];
    for (int i = 0; i < NUM_PUBLISHERS; i++) {
        futures[i] = executor.submit(valuePublishers[i]);
    }
    executor.submit(batchEventProcessor);
    long start = System.currentTimeMillis();
    cyclicBarrier.await();
    for (int i = 0; i < NUM_PUBLISHERS; i++) {
        futures[i].get();
    }
    latch.await();
    long opsPerSecond = (ITERATIONS * 1000L * ARRAY_SIZE) / (System.currentTimeMillis() - start);
    batchEventProcessor.halt();
    return opsPerSecond;
}
Also used : Future(java.util.concurrent.Future) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Future (java.util.concurrent.Future)1138 ArrayList (java.util.ArrayList)479 ExecutorService (java.util.concurrent.ExecutorService)445 Test (org.junit.Test)413 ExecutionException (java.util.concurrent.ExecutionException)264 Callable (java.util.concurrent.Callable)206 IOException (java.io.IOException)177 ParallelTest (com.hazelcast.test.annotation.ParallelTest)148 QuickTest (com.hazelcast.test.annotation.QuickTest)148 HashMap (java.util.HashMap)92 List (java.util.List)84 CountDownLatch (java.util.concurrent.CountDownLatch)71 LinkedList (java.util.LinkedList)67 TimeoutException (java.util.concurrent.TimeoutException)63 HashSet (java.util.HashSet)62 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)59 Map (java.util.Map)58 ICompletableFuture (com.hazelcast.core.ICompletableFuture)57 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)53 File (java.io.File)46