Search in sources :

Example 66 with TimeValue

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

the class RemoteClusterService method initializeRemoteClusters.

/**
     * Connects to all remote clusters in a blocking fashion. This should be called on node startup to establish an initial connection
     * to all configured seed nodes.
     */
void initializeRemoteClusters() {
    final TimeValue timeValue = REMOTE_INITIAL_CONNECTION_TIMEOUT_SETTING.get(settings);
    final PlainActionFuture<Void> future = new PlainActionFuture<>();
    Map<String, List<DiscoveryNode>> seeds = buildRemoteClustersSeeds(settings);
    updateRemoteClusters(seeds, future);
    try {
        future.get(timeValue.millis(), TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (TimeoutException ex) {
        logger.warn("failed to connect to remote clusters within {}", timeValue.toString());
    } catch (Exception e) {
        throw new IllegalStateException("failed to connect to remote clusters", e);
    }
}
Also used : PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ArrayList(java.util.ArrayList) List(java.util.List) TimeValue(org.elasticsearch.common.unit.TimeValue) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) TransportException(org.elasticsearch.transport.TransportException) TimeoutException(java.util.concurrent.TimeoutException)

Example 67 with TimeValue

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

the class AbstractBulkByScrollRequest method readFrom.

@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    searchRequest = new SearchRequest();
    searchRequest.readFrom(in);
    abortOnVersionConflict = in.readBoolean();
    size = in.readVInt();
    refresh = in.readBoolean();
    timeout = new TimeValue(in);
    activeShardCount = ActiveShardCount.readFrom(in);
    retryBackoffInitialTime = new TimeValue(in);
    maxRetries = in.readVInt();
    requestsPerSecond = in.readFloat();
    if (in.getVersion().onOrAfter(Version.V_5_1_1_UNRELEASED)) {
        slices = in.readVInt();
    } else {
        slices = 1;
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 68 with TimeValue

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

the class ClientScrollableHitSource method searchWithRetry.

/**
     * Run a search action and call onResponse when a the response comes in, retrying if the action fails with an exception caused by
     * rejected execution.
     *
     * @param action consumes a listener and starts the action. The listener it consumes is rigged to retry on failure.
     * @param onResponse consumes the response from the action
     */
private void searchWithRetry(Consumer<ActionListener<SearchResponse>> action, Consumer<SearchResponse> onResponse) {
    /*
         * RetryHelper is both an AbstractRunnable and an ActionListener<SearchResponse> - meaning that it both starts the search and
         * handles reacts to the results. The complexity is all in onFailure which either adapts the failure to the "fail" listener or
         * retries the search. Since both AbstractRunnable and ActionListener define the onFailure method it is called for either failure
         * to run the action (either while running or before starting) and for failure on the response from the action.
         */
    class RetryHelper extends AbstractRunnable implements ActionListener<SearchResponse> {

        private final Iterator<TimeValue> retries = backoffPolicy.iterator();

        /**
             * The runnable to run that retries in the same context as the original call.
             */
        private Runnable retryWithContext;

        private volatile int retryCount = 0;

        @Override
        protected void doRun() throws Exception {
            action.accept(this);
        }

        @Override
        public void onResponse(SearchResponse response) {
            onResponse.accept(response);
        }

        @Override
        public void onFailure(Exception e) {
            if (ExceptionsHelper.unwrap(e, EsRejectedExecutionException.class) != null) {
                if (retries.hasNext()) {
                    retryCount += 1;
                    TimeValue delay = retries.next();
                    logger.trace((Supplier<?>) () -> new ParameterizedMessage("retrying rejected search after [{}]", delay), e);
                    countSearchRetry.run();
                    threadPool.schedule(delay, ThreadPool.Names.SAME, retryWithContext);
                } else {
                    logger.warn((Supplier<?>) () -> new ParameterizedMessage("giving up on search because we retried [{}] times without success", retryCount), e);
                    fail.accept(e);
                }
            } else {
                logger.warn("giving up on search because it failed with a non-retryable exception", e);
                fail.accept(e);
            }
        }
    }
    RetryHelper helper = new RetryHelper();
    // Wrap the helper in a runnable that preserves the current context so we keep it on retry.
    helper.retryWithContext = threadPool.getThreadContext().preserveContext(helper);
    helper.run();
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) ActionListener(org.elasticsearch.action.ActionListener) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) Iterator(java.util.Iterator) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchResponse(org.elasticsearch.action.search.SearchResponse) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Example 69 with TimeValue

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

the class WorkingBulkByScrollTask method delayPrepareBulkRequest.

/**
     * Schedule prepareBulkRequestRunnable to run after some delay. This is where throttling plugs into reindexing so the request can be
     * rescheduled over and over again.
     */
void delayPrepareBulkRequest(ThreadPool threadPool, TimeValue lastBatchStartTime, int lastBatchSize, AbstractRunnable prepareBulkRequestRunnable) {
    // Synchronize so we are less likely to schedule the same request twice.
    synchronized (delayedPrepareBulkRequestReference) {
        TimeValue delay = throttleWaitTime(lastBatchStartTime, lastBatchSize);
        delayedPrepareBulkRequestReference.set(new DelayedPrepareBulkRequest(threadPool, getRequestsPerSecond(), delay, new RunOnce(prepareBulkRequestRunnable)));
    }
}
Also used : TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 70 with TimeValue

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

the class BulkRequest method readFrom.

@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    waitForActiveShards = ActiveShardCount.readFrom(in);
    int size = in.readVInt();
    for (int i = 0; i < size; i++) {
        requests.add(DocWriteRequest.readDocumentRequest(in));
    }
    refreshPolicy = RefreshPolicy.readFrom(in);
    timeout = new TimeValue(in);
}
Also used : TimeValue(org.elasticsearch.common.unit.TimeValue)

Aggregations

TimeValue (org.elasticsearch.common.unit.TimeValue)139 ClusterState (org.elasticsearch.cluster.ClusterState)26 IOException (java.io.IOException)24 CountDownLatch (java.util.concurrent.CountDownLatch)18 ArrayList (java.util.ArrayList)17 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)17 Settings (org.elasticsearch.common.settings.Settings)17 Supplier (org.apache.logging.log4j.util.Supplier)16 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 SearchResponse (org.elasticsearch.action.search.SearchResponse)15 AbstractRunnable (org.elasticsearch.common.util.concurrent.AbstractRunnable)13 Matchers.containsString (org.hamcrest.Matchers.containsString)13 Map (java.util.Map)12 TimeUnit (java.util.concurrent.TimeUnit)11 ThreadPool (org.elasticsearch.threadpool.ThreadPool)11 List (java.util.List)10 HashMap (java.util.HashMap)9 Iterator (java.util.Iterator)8 ExecutionException (java.util.concurrent.ExecutionException)8