Search in sources :

Example 21 with TimeValue

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.unit.TimeValue in project elasticsearch by elastic.

the class BulkByScrollResponse method readFrom.

@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    took = new TimeValue(in);
    status = new BulkByScrollTask.Status(in);
    bulkFailures = in.readList(Failure::new);
    searchFailures = in.readList(SearchFailure::new);
    timedOut = in.readBoolean();
}
Also used : TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 22 with TimeValue

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.unit.TimeValue in project elasticsearch by elastic.

the class MetaDataIndexTemplateService method putTemplate.

public void putTemplate(final PutRequest request, final PutListener listener) {
    Settings.Builder updatedSettingsBuilder = Settings.builder();
    updatedSettingsBuilder.put(request.settings).normalizePrefix(IndexMetaData.INDEX_SETTING_PREFIX);
    request.settings(updatedSettingsBuilder.build());
    if (request.name == null) {
        listener.onFailure(new IllegalArgumentException("index_template must provide a name"));
        return;
    }
    if (request.indexPatterns == null) {
        listener.onFailure(new IllegalArgumentException("index_template must provide a template"));
        return;
    }
    try {
        validate(request);
    } catch (Exception e) {
        listener.onFailure(e);
        return;
    }
    final IndexTemplateMetaData.Builder templateBuilder = IndexTemplateMetaData.builder(request.name);
    clusterService.submitStateUpdateTask("create-index-template [" + request.name + "], cause [" + request.cause + "]", new ClusterStateUpdateTask(Priority.URGENT) {

        @Override
        public TimeValue timeout() {
            return request.masterTimeout;
        }

        @Override
        public void onFailure(String source, Exception e) {
            listener.onFailure(e);
        }

        @Override
        public ClusterState execute(ClusterState currentState) throws Exception {
            if (request.create && currentState.metaData().templates().containsKey(request.name)) {
                throw new IllegalArgumentException("index_template [" + request.name + "] already exists");
            }
            validateAndAddTemplate(request, templateBuilder, indicesService, xContentRegistry);
            for (Alias alias : request.aliases) {
                AliasMetaData aliasMetaData = AliasMetaData.builder(alias.name()).filter(alias.filter()).indexRouting(alias.indexRouting()).searchRouting(alias.searchRouting()).build();
                templateBuilder.putAlias(aliasMetaData);
            }
            for (Map.Entry<String, IndexMetaData.Custom> entry : request.customs.entrySet()) {
                templateBuilder.putCustom(entry.getKey(), entry.getValue());
            }
            IndexTemplateMetaData template = templateBuilder.build();
            MetaData.Builder builder = MetaData.builder(currentState.metaData()).put(template);
            return ClusterState.builder(currentState).metaData(builder).build();
        }

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
            listener.onResponse(new PutResponse(true, templateBuilder.build()));
        }
    });
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) ValidationException(org.elasticsearch.common.ValidationException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) InvalidIndexTemplateException(org.elasticsearch.indices.InvalidIndexTemplateException) IndexTemplateMissingException(org.elasticsearch.indices.IndexTemplateMissingException) Alias(org.elasticsearch.action.admin.indices.alias.Alias) Settings(org.elasticsearch.common.settings.Settings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 23 with TimeValue

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.unit.TimeValue in project elasticsearch by elastic.

the class ClusterStateObserver method waitForNextChange.

/**
     * Wait for the next cluster state which satisfies statePredicate
     *
     * @param listener        callback listener
     * @param statePredicate predicate to check whether cluster state changes are relevant and the callback should be called
     * @param timeOutValue    a timeout for waiting. If null the global observer timeout will be used.
     */
public void waitForNextChange(Listener listener, Predicate<ClusterState> statePredicate, @Nullable TimeValue timeOutValue) {
    listener = new ContextPreservingListener(listener, contextHolder.newRestorableContext(false));
    if (observingContext.get() != null) {
        throw new ElasticsearchException("already waiting for a cluster state change");
    }
    Long timeoutTimeLeftMS;
    if (timeOutValue == null) {
        timeOutValue = this.timeOutValue;
        if (timeOutValue != null) {
            long timeSinceStartMS = TimeValue.nsecToMSec(System.nanoTime() - startTimeNS);
            timeoutTimeLeftMS = timeOutValue.millis() - timeSinceStartMS;
            if (timeoutTimeLeftMS <= 0L) {
                // things have timeout while we were busy -> notify
                logger.trace("observer timed out. notifying listener. timeout setting [{}], time since start [{}]", timeOutValue, new TimeValue(timeSinceStartMS));
                // update to latest, in case people want to retry
                timedOut = true;
                lastObservedState.set(new StoredState(clusterService.state()));
                listener.onTimeout(timeOutValue);
                return;
            }
        } else {
            timeoutTimeLeftMS = null;
        }
    } else {
        this.startTimeNS = System.nanoTime();
        this.timeOutValue = timeOutValue;
        timeoutTimeLeftMS = timeOutValue.millis();
        timedOut = false;
    }
    // sample a new state. This state maybe *older* than the supplied state if we are called from an applier,
    // which wants to wait for something else to happen
    ClusterState newState = clusterService.state();
    if (lastObservedState.get().isOlderOrDifferentMaster(newState) && statePredicate.test(newState)) {
        // good enough, let's go.
        logger.trace("observer: sampled state accepted by predicate ({})", newState);
        lastObservedState.set(new StoredState(newState));
        listener.onNewClusterState(newState);
    } else {
        logger.trace("observer: sampled state rejected by predicate ({}). adding listener to ClusterService", newState);
        final ObservingContext context = new ObservingContext(listener, statePredicate);
        if (!observingContext.compareAndSet(null, context)) {
            throw new ElasticsearchException("already waiting for a cluster state change");
        }
        clusterService.addTimeoutListener(timeoutTimeLeftMS == null ? null : new TimeValue(timeoutTimeLeftMS), clusterStateListener);
    }
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 24 with TimeValue

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.unit.TimeValue in project elasticsearch by elastic.

the class UnicastZenPing method sendPings.

protected void sendPings(final TimeValue timeout, final PingingRound pingingRound) {
    final UnicastPingRequest pingRequest = new UnicastPingRequest();
    pingRequest.id = pingingRound.id();
    pingRequest.timeout = timeout;
    DiscoveryNodes discoNodes = contextProvider.nodes();
    pingRequest.pingResponse = createPingResponse(discoNodes);
    Set<DiscoveryNode> nodesFromResponses = temporalResponses.stream().map(pingResponse -> {
        assert clusterName.equals(pingResponse.clusterName()) : "got a ping request from a different cluster. expected " + clusterName + " got " + pingResponse.clusterName();
        return pingResponse.node();
    }).collect(Collectors.toSet());
    // dedup by address
    final Map<TransportAddress, DiscoveryNode> uniqueNodesByAddress = Stream.concat(pingingRound.getSeedNodes().stream(), nodesFromResponses.stream()).collect(Collectors.toMap(DiscoveryNode::getAddress, Function.identity(), (n1, n2) -> n1));
    // resolve what we can via the latest cluster state
    final Set<DiscoveryNode> nodesToPing = uniqueNodesByAddress.values().stream().map(node -> {
        DiscoveryNode foundNode = discoNodes.findByAddress(node.getAddress());
        if (foundNode == null) {
            return node;
        } else {
            return foundNode;
        }
    }).collect(Collectors.toSet());
    nodesToPing.forEach(node -> sendPingRequestToNode(node, timeout, pingingRound, pingRequest));
}
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) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 25 with TimeValue

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.unit.TimeValue 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)

Aggregations

TimeValue (org.elasticsearch.common.unit.TimeValue)169 SearchResponse (org.elasticsearch.action.search.SearchResponse)37 ArrayList (java.util.ArrayList)28 IOException (java.io.IOException)27 ClusterState (org.elasticsearch.cluster.ClusterState)26 SearchHit (org.elasticsearch.search.SearchHit)26 CountDownLatch (java.util.concurrent.CountDownLatch)18 Settings (org.elasticsearch.common.settings.Settings)18 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)17 Map (java.util.Map)16 Supplier (org.apache.logging.log4j.util.Supplier)16 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)16 List (java.util.List)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 AbstractRunnable (org.elasticsearch.common.util.concurrent.AbstractRunnable)13 Matchers.containsString (org.hamcrest.Matchers.containsString)13 TimeUnit (java.util.concurrent.TimeUnit)11 ThreadPool (org.elasticsearch.threadpool.ThreadPool)11 HashMap (java.util.HashMap)10 ByteSizeValue (org.elasticsearch.common.unit.ByteSizeValue)10