Search in sources :

Example 6 with TimeValue

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

the class ScheduleWithFixedDelayTests method testDoesNotRescheduleUntilExecutionFinished.

public void testDoesNotRescheduleUntilExecutionFinished() throws Exception {
    final TimeValue delay = TimeValue.timeValueMillis(100L);
    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch pauseLatch = new CountDownLatch(1);
    ThreadPool threadPool = mock(ThreadPool.class);
    final Runnable runnable = () -> {
        // notify that the runnable is started
        startLatch.countDown();
        try {
            // wait for other thread to un-pause
            pauseLatch.await();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    };
    ReschedulingRunnable reschedulingRunnable = new ReschedulingRunnable(runnable, delay, Names.GENERIC, threadPool);
    // this call was made during construction of the runnable
    verify(threadPool, times(1)).schedule(delay, Names.GENERIC, reschedulingRunnable);
    // create a thread and start the runnable
    Thread runThread = new Thread() {

        @Override
        public void run() {
            reschedulingRunnable.run();
        }
    };
    runThread.start();
    // wait for the runnable to be started and ensure the runnable hasn't used the threadpool again
    startLatch.await();
    verifyNoMoreInteractions(threadPool);
    // un-pause the runnable and allow it to complete execution
    pauseLatch.countDown();
    runThread.join();
    // validate schedule was called again
    verify(threadPool, times(2)).schedule(delay, Names.GENERIC, reschedulingRunnable);
}
Also used : ReschedulingRunnable(org.elasticsearch.threadpool.ThreadPool.ReschedulingRunnable) ReschedulingRunnable(org.elasticsearch.threadpool.ThreadPool.ReschedulingRunnable) CountDownLatch(java.util.concurrent.CountDownLatch) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 7 with TimeValue

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

the class ConnectionProfileTests method testBuildConnectionProfile.

public void testBuildConnectionProfile() {
    ConnectionProfile.Builder builder = new ConnectionProfile.Builder();
    TimeValue connectTimeout = TimeValue.timeValueMillis(randomIntBetween(1, 10));
    TimeValue handshaketTimeout = TimeValue.timeValueMillis(randomIntBetween(1, 10));
    final boolean setConnectTimeout = randomBoolean();
    if (setConnectTimeout) {
        builder.setConnectTimeout(connectTimeout);
    }
    final boolean setHandshakeTimeout = randomBoolean();
    if (setHandshakeTimeout) {
        builder.setHandshakeTimeout(handshaketTimeout);
    }
    builder.addConnections(1, TransportRequestOptions.Type.BULK);
    builder.addConnections(2, TransportRequestOptions.Type.STATE, TransportRequestOptions.Type.RECOVERY);
    builder.addConnections(3, TransportRequestOptions.Type.PING);
    IllegalStateException illegalStateException = expectThrows(IllegalStateException.class, builder::build);
    assertEquals("not all types are added for this connection profile - missing types: [REG]", illegalStateException.getMessage());
    IllegalArgumentException illegalArgumentException = expectThrows(IllegalArgumentException.class, () -> builder.addConnections(4, TransportRequestOptions.Type.REG, TransportRequestOptions.Type.PING));
    assertEquals("type [PING] is already registered", illegalArgumentException.getMessage());
    builder.addConnections(4, TransportRequestOptions.Type.REG);
    ConnectionProfile build = builder.build();
    if (randomBoolean()) {
        build = new ConnectionProfile.Builder(build).build();
    }
    assertEquals(10, build.getNumConnections());
    if (setConnectTimeout) {
        assertEquals(connectTimeout, build.getConnectTimeout());
    } else {
        assertNull(build.getConnectTimeout());
    }
    if (setHandshakeTimeout) {
        assertEquals(handshaketTimeout, build.getHandshakeTimeout());
    } else {
        assertNull(build.getHandshakeTimeout());
    }
    Integer[] array = new Integer[10];
    for (int i = 0; i < array.length; i++) {
        array[i] = i;
    }
    final int numIters = randomIntBetween(5, 10);
    assertEquals(4, build.getHandles().size());
    assertEquals(0, build.getHandles().get(0).offset);
    assertEquals(1, build.getHandles().get(0).length);
    assertEquals(EnumSet.of(TransportRequestOptions.Type.BULK), build.getHandles().get(0).getTypes());
    Integer channel = build.getHandles().get(0).getChannel(array);
    for (int i = 0; i < numIters; i++) {
        assertEquals(0, channel.intValue());
    }
    assertEquals(1, build.getHandles().get(1).offset);
    assertEquals(2, build.getHandles().get(1).length);
    assertEquals(EnumSet.of(TransportRequestOptions.Type.STATE, TransportRequestOptions.Type.RECOVERY), build.getHandles().get(1).getTypes());
    channel = build.getHandles().get(1).getChannel(array);
    for (int i = 0; i < numIters; i++) {
        assertThat(channel, Matchers.anyOf(Matchers.is(1), Matchers.is(2)));
    }
    assertEquals(3, build.getHandles().get(2).offset);
    assertEquals(3, build.getHandles().get(2).length);
    assertEquals(EnumSet.of(TransportRequestOptions.Type.PING), build.getHandles().get(2).getTypes());
    channel = build.getHandles().get(2).getChannel(array);
    for (int i = 0; i < numIters; i++) {
        assertThat(channel, Matchers.anyOf(Matchers.is(3), Matchers.is(4), Matchers.is(5)));
    }
    assertEquals(6, build.getHandles().get(3).offset);
    assertEquals(4, build.getHandles().get(3).length);
    assertEquals(EnumSet.of(TransportRequestOptions.Type.REG), build.getHandles().get(3).getTypes());
    channel = build.getHandles().get(3).getChannel(array);
    for (int i = 0; i < numIters; i++) {
        assertThat(channel, Matchers.anyOf(Matchers.is(6), Matchers.is(7), Matchers.is(8), Matchers.is(9)));
    }
    assertEquals(3, build.getNumConnectionsPerType(TransportRequestOptions.Type.PING));
    assertEquals(4, build.getNumConnectionsPerType(TransportRequestOptions.Type.REG));
    assertEquals(2, build.getNumConnectionsPerType(TransportRequestOptions.Type.STATE));
    assertEquals(2, build.getNumConnectionsPerType(TransportRequestOptions.Type.RECOVERY));
    assertEquals(1, build.getNumConnectionsPerType(TransportRequestOptions.Type.BULK));
}
Also used : TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 8 with TimeValue

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

the class RestReindexAction method buildRemoteInfo.

static RemoteInfo buildRemoteInfo(Map<String, Object> source) throws IOException {
    @SuppressWarnings("unchecked") Map<String, Object> remote = (Map<String, Object>) source.remove("remote");
    if (remote == null) {
        return null;
    }
    String username = extractString(remote, "username");
    String password = extractString(remote, "password");
    String hostInRequest = requireNonNull(extractString(remote, "host"), "[host] must be specified to reindex from a remote cluster");
    Matcher hostMatcher = HOST_PATTERN.matcher(hostInRequest);
    if (false == hostMatcher.matches()) {
        throw new IllegalArgumentException("[host] must be of the form [scheme]://[host]:[port] but was [" + hostInRequest + "]");
    }
    String scheme = hostMatcher.group("scheme");
    String host = hostMatcher.group("host");
    int port = Integer.parseInt(hostMatcher.group("port"));
    Map<String, String> headers = extractStringStringMap(remote, "headers");
    TimeValue socketTimeout = extractTimeValue(remote, "socket_timeout", RemoteInfo.DEFAULT_SOCKET_TIMEOUT);
    TimeValue connectTimeout = extractTimeValue(remote, "connect_timeout", RemoteInfo.DEFAULT_CONNECT_TIMEOUT);
    if (false == remote.isEmpty()) {
        throw new IllegalArgumentException("Unsupported fields in [remote]: [" + Strings.collectionToCommaDelimitedString(remote.keySet()) + "]");
    }
    return new RemoteInfo(scheme, host, port, queryForRemote(source), username, password, headers, socketTimeout, connectTimeout);
}
Also used : Matcher(java.util.regex.Matcher) RemoteInfo(org.elasticsearch.index.reindex.remote.RemoteInfo) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) TimeValue(org.elasticsearch.common.unit.TimeValue) TimeValue.parseTimeValue(org.elasticsearch.common.unit.TimeValue.parseTimeValue)

Example 9 with TimeValue

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

the class RemoteScrollableHitSource method execute.

private <T> void execute(String method, String uri, Map<String, String> params, HttpEntity entity, BiFunction<XContentParser, XContentType, T> parser, Consumer<? super T> listener) {
    // Preserve the thread context so headers survive after the call
    java.util.function.Supplier<ThreadContext.StoredContext> contextSupplier = threadPool.getThreadContext().newRestorableContext(true);
    class RetryHelper extends AbstractRunnable {

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

        @Override
        protected void doRun() throws Exception {
            client.performRequestAsync(method, uri, params, entity, new ResponseListener() {

                @Override
                public void onSuccess(org.elasticsearch.client.Response response) {
                    // Restore the thread context to get the precious headers
                    try (ThreadContext.StoredContext ctx = contextSupplier.get()) {
                        // eliminates compiler warning
                        assert ctx != null;
                        T parsedResponse;
                        try {
                            HttpEntity responseEntity = response.getEntity();
                            InputStream content = responseEntity.getContent();
                            XContentType xContentType = null;
                            if (responseEntity.getContentType() != null) {
                                final String mimeType = ContentType.parse(responseEntity.getContentType().getValue()).getMimeType();
                                xContentType = XContentType.fromMediaType(mimeType);
                            }
                            if (xContentType == null) {
                                try {
                                    throw new ElasticsearchException("Response didn't include Content-Type: " + bodyMessage(response.getEntity()));
                                } catch (IOException e) {
                                    ElasticsearchException ee = new ElasticsearchException("Error extracting body from response");
                                    ee.addSuppressed(e);
                                    throw ee;
                                }
                            }
                            // EMPTY is safe here because we don't call namedObject
                            try (XContentParser xContentParser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, content)) {
                                parsedResponse = parser.apply(xContentParser, xContentType);
                            } catch (ParsingException e) {
                                /* Because we're streaming the response we can't get a copy of it here. The best we can do is hint that it
                                 * is totally wrong and we're probably not talking to Elasticsearch. */
                                throw new ElasticsearchException("Error parsing the response, remote is likely not an Elasticsearch instance", e);
                            }
                        } catch (IOException e) {
                            throw new ElasticsearchException("Error deserializing response, remote is likely not an Elasticsearch instance", e);
                        }
                        listener.accept(parsedResponse);
                    }
                }

                @Override
                public void onFailure(Exception e) {
                    try (ThreadContext.StoredContext ctx = contextSupplier.get()) {
                        // eliminates compiler warning
                        assert ctx != null;
                        if (e instanceof ResponseException) {
                            ResponseException re = (ResponseException) e;
                            if (RestStatus.TOO_MANY_REQUESTS.getStatus() == re.getResponse().getStatusLine().getStatusCode()) {
                                if (retries.hasNext()) {
                                    TimeValue delay = retries.next();
                                    logger.trace((Supplier<?>) () -> new ParameterizedMessage("retrying rejected search after [{}]", delay), e);
                                    countSearchRetry.run();
                                    threadPool.schedule(delay, ThreadPool.Names.SAME, RetryHelper.this);
                                    return;
                                }
                            }
                            e = wrapExceptionToPreserveStatus(re.getResponse().getStatusLine().getStatusCode(), re.getResponse().getEntity(), re);
                        } else if (e instanceof ContentTooLongException) {
                            e = new IllegalArgumentException("Remote responded with a chunk that was too large. Use a smaller batch size.", e);
                        }
                        fail.accept(e);
                    }
                }
            });
        }

        @Override
        public void onFailure(Exception t) {
            fail.accept(t);
        }
    }
    new RetryHelper().run();
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) HttpEntity(org.apache.http.HttpEntity) ResponseException(org.elasticsearch.client.ResponseException) ElasticsearchException(org.elasticsearch.ElasticsearchException) XContentType(org.elasticsearch.common.xcontent.XContentType) ParsingException(org.elasticsearch.common.ParsingException) Iterator(java.util.Iterator) Supplier(org.apache.logging.log4j.util.Supplier) TimeValue(org.elasticsearch.common.unit.TimeValue) InputStream(java.io.InputStream) ContentTooLongException(org.apache.http.ContentTooLongException) ResponseListener(org.elasticsearch.client.ResponseListener) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ResponseException(org.elasticsearch.client.ResponseException) ContentTooLongException(org.apache.http.ContentTooLongException) ParsingException(org.elasticsearch.common.ParsingException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) IOException(java.io.IOException) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 10 with TimeValue

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

the class SimpleNetty4TransportTests method nettyFromThreadPool.

public static MockTransportService nettyFromThreadPool(Settings settings, ThreadPool threadPool, final Version version, ClusterSettings clusterSettings, boolean doHandshake) {
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList());
    Transport transport = new Netty4Transport(settings, threadPool, new NetworkService(settings, Collections.emptyList()), BigArrays.NON_RECYCLING_INSTANCE, namedWriteableRegistry, new NoneCircuitBreakerService()) {

        @Override
        protected Version executeHandshake(DiscoveryNode node, Channel channel, TimeValue timeout) throws IOException, InterruptedException {
            if (doHandshake) {
                return super.executeHandshake(node, channel, timeout);
            } else {
                return version.minimumCompatibilityVersion();
            }
        }

        @Override
        protected Version getCurrentVersion() {
            return version;
        }
    };
    MockTransportService mockTransportService = MockTransportService.createNewService(Settings.EMPTY, transport, version, threadPool, clusterSettings);
    mockTransportService.start();
    return mockTransportService;
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) Channel(io.netty.channel.Channel) NetworkService(org.elasticsearch.common.network.NetworkService) Transport(org.elasticsearch.transport.Transport) TimeValue(org.elasticsearch.common.unit.TimeValue) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService)

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