Search in sources :

Example 81 with TimeValue

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

the class TcpTransport method openConnection.

@Override
public final NodeChannels openConnection(DiscoveryNode node, ConnectionProfile connectionProfile) throws IOException {
    if (node == null) {
        throw new ConnectTransportException(null, "can't open connection to a null node");
    }
    boolean success = false;
    NodeChannels nodeChannels = null;
    connectionProfile = resolveConnectionProfile(connectionProfile, defaultConnectionProfile);
    // ensure we don't open connections while we are closing
    globalLock.readLock().lock();
    try {
        ensureOpen();
        try {
            nodeChannels = connectToChannels(node, connectionProfile);
            // one channel is guaranteed by the connection profile
            final Channel channel = nodeChannels.getChannels().get(0);
            final TimeValue connectTimeout = connectionProfile.getConnectTimeout() == null ? defaultConnectionProfile.getConnectTimeout() : connectionProfile.getConnectTimeout();
            final TimeValue handshakeTimeout = connectionProfile.getHandshakeTimeout() == null ? connectTimeout : connectionProfile.getHandshakeTimeout();
            final Version version = executeHandshake(node, channel, handshakeTimeout);
            transportServiceAdapter.onConnectionOpened(node);
            // clone the channels - we now have the correct version
            nodeChannels = new NodeChannels(nodeChannels, version);
            success = true;
            return nodeChannels;
        } catch (ConnectTransportException e) {
            throw e;
        } catch (Exception e) {
            // only relevant exceptions are logged on the caller end.. this is the same as in connectToNode
            throw new ConnectTransportException(node, "general node connection failure", e);
        } finally {
            if (success == false) {
                IOUtils.closeWhileHandlingException(nodeChannels);
            }
        }
    } finally {
        globalLock.readLock().unlock();
    }
}
Also used : Version(org.elasticsearch.Version) TimeValue(org.elasticsearch.common.unit.TimeValue) ElasticsearchException(org.elasticsearch.ElasticsearchException) NotCompressedException(org.elasticsearch.common.compress.NotCompressedException) StreamCorruptedException(java.io.StreamCorruptedException) CancelledKeyException(java.nio.channels.CancelledKeyException) NetworkExceptionHelper.isCloseConnectionException(org.elasticsearch.common.transport.NetworkExceptionHelper.isCloseConnectionException) BindException(java.net.BindException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) NetworkExceptionHelper.isConnectException(org.elasticsearch.common.transport.NetworkExceptionHelper.isConnectException)

Example 82 with TimeValue

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

the class SettingTests method testDefault.

public void testDefault() {
    TimeValue defaultValue = TimeValue.timeValueMillis(randomIntBetween(0, 1000000));
    Setting<TimeValue> setting = Setting.positiveTimeSetting("my.time.value", defaultValue, Property.NodeScope);
    assertFalse(setting.isGroupSetting());
    String aDefault = setting.getDefaultRaw(Settings.EMPTY);
    assertEquals(defaultValue.millis() + "ms", aDefault);
    assertEquals(defaultValue.millis(), setting.get(Settings.EMPTY).millis());
    assertEquals(defaultValue, setting.getDefault(Settings.EMPTY));
    Setting<String> secondaryDefault = new Setting<>("foo.bar", (s) -> s.get("old.foo.bar", "some_default"), Function.identity(), Property.NodeScope);
    assertEquals("some_default", secondaryDefault.get(Settings.EMPTY));
    assertEquals("42", secondaryDefault.get(Settings.builder().put("old.foo.bar", 42).build()));
    Setting<String> secondaryDefaultViaSettings = new Setting<>("foo.bar", secondaryDefault, Function.identity(), Property.NodeScope);
    assertEquals("some_default", secondaryDefaultViaSettings.get(Settings.EMPTY));
    assertEquals("42", secondaryDefaultViaSettings.get(Settings.builder().put("old.foo.bar", 42).build()));
    // It gets more complicated when there are two settings objects....
    Settings hasFallback = Settings.builder().put("foo.bar", "o").build();
    Setting<String> fallsback = new Setting<>("foo.baz", secondaryDefault, Function.identity(), Property.NodeScope);
    assertEquals("o", fallsback.get(hasFallback));
    assertEquals("some_default", fallsback.get(Settings.EMPTY));
    assertEquals("some_default", fallsback.get(Settings.EMPTY, Settings.EMPTY));
    assertEquals("o", fallsback.get(Settings.EMPTY, hasFallback));
    assertEquals("o", fallsback.get(hasFallback, Settings.EMPTY));
    assertEquals("a", fallsback.get(Settings.builder().put("foo.bar", "a").build(), Settings.builder().put("foo.bar", "b").build()));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 83 with TimeValue

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

the class SettingTests method testTimeValue.

public void testTimeValue() {
    final TimeValue random = TimeValue.parseTimeValue(randomTimeValue(), "test");
    Setting<TimeValue> setting = Setting.timeSetting("foo", random);
    assertThat(setting.get(Settings.EMPTY), equalTo(random));
    final int factor = randomIntBetween(1, 10);
    setting = Setting.timeSetting("foo", (s) -> TimeValue.timeValueMillis(random.getMillis() * factor), TimeValue.ZERO);
    assertThat(setting.get(Settings.builder().put("foo", "12h").build()), equalTo(TimeValue.timeValueHours(12)));
    assertThat(setting.get(Settings.EMPTY).getMillis(), equalTo(random.getMillis() * factor));
}
Also used : ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) JvmInfo(org.elasticsearch.monitor.jvm.JvmInfo) Arrays(java.util.Arrays) Property(org.elasticsearch.common.settings.Setting.Property) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) TimeValue(org.elasticsearch.common.unit.TimeValue) Map(java.util.Map) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) ESTestCase(org.elasticsearch.test.ESTestCase) Tuple(org.elasticsearch.common.collect.Tuple) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 84 with TimeValue

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

the class GceInstancesServiceImpl method client.

public synchronized Compute client() {
    if (refreshInterval != null && refreshInterval.millis() != 0) {
        if (client != null && (refreshInterval.millis() < 0 || (System.currentTimeMillis() - lastRefresh) < refreshInterval.millis())) {
            if (logger.isTraceEnabled())
                logger.trace("using cache to retrieve client");
            return client;
        }
        lastRefresh = System.currentTimeMillis();
    }
    try {
        gceJsonFactory = new JacksonFactory();
        logger.info("starting GCE discovery service");
        // Forcing Google Token API URL as set in GCE SDK to
        //      http://metadata/computeMetadata/v1/instance/service-accounts/default/token
        // See https://developers.google.com/compute/docs/metadata#metadataserver
        String tokenServerEncodedUrl = GceMetadataService.GCE_HOST.get(settings) + "/computeMetadata/v1/instance/service-accounts/default/token";
        ComputeCredential credential = new ComputeCredential.Builder(getGceHttpTransport(), gceJsonFactory).setTokenServerEncodedUrl(tokenServerEncodedUrl).build();
        // hack around code messiness in GCE code
        // TODO: get this fixed
        Access.doPrivilegedIOException(credential::refreshToken);
        logger.debug("token [{}] will expire in [{}] s", credential.getAccessToken(), credential.getExpiresInSeconds());
        if (credential.getExpiresInSeconds() != null) {
            refreshInterval = TimeValue.timeValueSeconds(credential.getExpiresInSeconds() - 1);
        }
        Compute.Builder builder = new Compute.Builder(getGceHttpTransport(), gceJsonFactory, null).setApplicationName(VERSION).setRootUrl(GCE_ROOT_URL.get(settings));
        if (RETRY_SETTING.exists(settings)) {
            TimeValue maxWait = MAX_WAIT_SETTING.get(settings);
            RetryHttpInitializerWrapper retryHttpInitializerWrapper;
            if (maxWait.getMillis() > 0) {
                retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, maxWait);
            } else {
                retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential);
            }
            builder.setHttpRequestInitializer(retryHttpInitializerWrapper);
        } else {
            builder.setHttpRequestInitializer(credential);
        }
        this.client = builder.build();
    } catch (Exception e) {
        logger.warn("unable to start GCE discovery service", e);
        throw new IllegalArgumentException("unable to start GCE discovery service", e);
    }
    return this.client;
}
Also used : RetryHttpInitializerWrapper(org.elasticsearch.discovery.gce.RetryHttpInitializerWrapper) ComputeCredential(com.google.api.client.googleapis.compute.ComputeCredential) Compute(com.google.api.services.compute.Compute) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) TimeValue(org.elasticsearch.common.unit.TimeValue) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 85 with TimeValue

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

the class AsyncBulkByScrollActionTests method bulkRetryTestCase.

/**
     * Execute a bulk retry test case. The total number of failures is random and the number of retries attempted is set to
     * testRequest.getMaxRetries and controled by the failWithRejection parameter.
     */
private void bulkRetryTestCase(boolean failWithRejection) throws Exception {
    int totalFailures = randomIntBetween(1, testRequest.getMaxRetries());
    int size = randomIntBetween(1, 100);
    testRequest.setMaxRetries(totalFailures - (failWithRejection ? 1 : 0));
    client.bulksToReject = client.bulksAttempts.get() + totalFailures;
    /*
         * When we get a successful bulk response we usually start the next scroll request but lets just intercept that so we don't have to
         * deal with it. We just wait for it to happen.
         */
    CountDownLatch successLatch = new CountDownLatch(1);
    DummyAsyncBulkByScrollAction action = new DummyActionWithoutBackoff() {

        @Override
        void startNextScroll(TimeValue lastBatchStartTime, int lastBatchSize) {
            successLatch.countDown();
        }
    };
    BulkRequest request = new BulkRequest();
    for (int i = 0; i < size + 1; i++) {
        request.add(new IndexRequest("index", "type", "id" + i));
    }
    action.sendBulkRequest(timeValueNanos(System.nanoTime()), request);
    if (failWithRejection) {
        BulkByScrollResponse response = listener.get();
        assertThat(response.getBulkFailures(), hasSize(1));
        assertEquals(response.getBulkFailures().get(0).getStatus(), RestStatus.TOO_MANY_REQUESTS);
        assertThat(response.getSearchFailures(), empty());
        assertNull(response.getReasonCancelled());
    } else {
        assertTrue(successLatch.await(10, TimeUnit.SECONDS));
    }
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) CountDownLatch(java.util.concurrent.CountDownLatch) IndexRequest(org.elasticsearch.action.index.IndexRequest) 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