Search in sources :

Example 41 with TimeValue

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

the class GatewayServiceTests method testDefaultRecoverAfterTime.

public void testDefaultRecoverAfterTime() throws IOException {
    // check that the default is not set
    GatewayService service = createService(Settings.builder());
    assertNull(service.recoverAfterTime());
    // ensure default is set when setting expected_nodes
    service = createService(Settings.builder().put("gateway.expected_nodes", 1));
    assertThat(service.recoverAfterTime(), Matchers.equalTo(GatewayService.DEFAULT_RECOVER_AFTER_TIME_IF_EXPECTED_NODES_IS_SET));
    // ensure default is set when setting expected_data_nodes
    service = createService(Settings.builder().put("gateway.expected_data_nodes", 1));
    assertThat(service.recoverAfterTime(), Matchers.equalTo(GatewayService.DEFAULT_RECOVER_AFTER_TIME_IF_EXPECTED_NODES_IS_SET));
    // ensure default is set when setting expected_master_nodes
    service = createService(Settings.builder().put("gateway.expected_master_nodes", 1));
    assertThat(service.recoverAfterTime(), Matchers.equalTo(GatewayService.DEFAULT_RECOVER_AFTER_TIME_IF_EXPECTED_NODES_IS_SET));
    // ensure settings override default
    TimeValue timeValue = TimeValue.timeValueHours(3);
    // ensure default is set when setting expected_nodes
    service = createService(Settings.builder().put("gateway.expected_nodes", 1).put("gateway.recover_after_time", timeValue.toString()));
    assertThat(service.recoverAfterTime().millis(), Matchers.equalTo(timeValue.millis()));
}
Also used : TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 42 with TimeValue

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

the class CorruptedTranslogIT method testCorruptTranslogFiles.

public void testCorruptTranslogFiles() throws Exception {
    internalCluster().startNodes(1, Settings.EMPTY);
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).put("index.refresh_interval", "-1").put(MockEngineSupport.DISABLE_FLUSH_ON_CLOSE.getKey(), // never flush - always recover from translog
    true)));
    // Index some documents
    int numDocs = scaledRandomIntBetween(100, 1000);
    IndexRequestBuilder[] builders = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < builders.length; i++) {
        builders[i] = client().prepareIndex("test", "type").setSource("foo", "bar");
    }
    disableTranslogFlush("test");
    // this one
    indexRandom(false, false, false, Arrays.asList(builders));
    // Corrupt the translog file(s)
    corruptRandomTranslogFiles();
    // Restart the single node
    internalCluster().fullRestart();
    client().admin().cluster().prepareHealth().setWaitForYellowStatus().setTimeout(new TimeValue(1000, TimeUnit.MILLISECONDS)).setWaitForEvents(Priority.LANGUID).get();
    try {
        client().prepareSearch("test").setQuery(matchAllQuery()).get();
        fail("all shards should be failed due to a corrupted translog");
    } catch (SearchPhaseExecutionException e) {
    // Good, all shards should be failed because there is only a
    // single shard and its translog is corrupt
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 43 with TimeValue

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

the class JvmGcMonitorServiceTests method testSlowGcLogging.

public void testSlowGcLogging() {
    final Logger logger = mock(Logger.class);
    when(logger.isWarnEnabled()).thenReturn(true);
    when(logger.isInfoEnabled()).thenReturn(true);
    when(logger.isDebugEnabled()).thenReturn(true);
    final JvmGcMonitorService.JvmMonitor.Threshold threshold = randomFrom(JvmGcMonitorService.JvmMonitor.Threshold.values());
    final String name = randomAsciiOfLength(16);
    final long seq = randomIntBetween(1, 1 << 30);
    final int elapsedValue = randomIntBetween(1, 1 << 10);
    final long totalCollectionCount = randomIntBetween(1, 16);
    final long currentCollectionCount = randomIntBetween(1, 16);
    final TimeValue totalCollectionTime = TimeValue.timeValueMillis(randomIntBetween(1, elapsedValue));
    final TimeValue currentCollectionTime = TimeValue.timeValueMillis(randomIntBetween(1, elapsedValue));
    final ByteSizeValue lastHeapUsed = new ByteSizeValue(randomIntBetween(1, 1 << 10));
    JvmStats lastJvmStats = mock(JvmStats.class);
    JvmStats.Mem lastMem = mock(JvmStats.Mem.class);
    when(lastMem.getHeapUsed()).thenReturn(lastHeapUsed);
    when(lastJvmStats.getMem()).thenReturn(lastMem);
    when(lastJvmStats.toString()).thenReturn("last");
    final ByteSizeValue currentHeapUsed = new ByteSizeValue(randomIntBetween(1, 1 << 10));
    JvmStats currentJvmStats = mock(JvmStats.class);
    JvmStats.Mem currentMem = mock(JvmStats.Mem.class);
    when(currentMem.getHeapUsed()).thenReturn(currentHeapUsed);
    when(currentJvmStats.getMem()).thenReturn(currentMem);
    when(currentJvmStats.toString()).thenReturn("current");
    JvmStats.GarbageCollector gc = mock(JvmStats.GarbageCollector.class);
    when(gc.getName()).thenReturn(name);
    when(gc.getCollectionCount()).thenReturn(totalCollectionCount);
    when(gc.getCollectionTime()).thenReturn(totalCollectionTime);
    final ByteSizeValue maxHeapUsed = new ByteSizeValue(Math.max(lastHeapUsed.getBytes(), currentHeapUsed.getBytes()) + 1 << 10);
    JvmGcMonitorService.JvmMonitor.SlowGcEvent slowGcEvent = new JvmGcMonitorService.JvmMonitor.SlowGcEvent(gc, currentCollectionCount, currentCollectionTime, elapsedValue, lastJvmStats, currentJvmStats, maxHeapUsed);
    JvmGcMonitorService.logSlowGc(logger, threshold, seq, slowGcEvent, (l, c) -> l.toString() + ", " + c.toString());
    switch(threshold) {
        case WARN:
            verify(logger).isWarnEnabled();
            verify(logger).warn("[gc][{}][{}][{}] duration [{}], collections [{}]/[{}], total [{}]/[{}], memory [{}]->[{}]/[{}], all_pools {}", name, seq, totalCollectionCount, currentCollectionTime, currentCollectionCount, TimeValue.timeValueMillis(elapsedValue), currentCollectionTime, totalCollectionTime, lastHeapUsed, currentHeapUsed, maxHeapUsed, "last, current");
            break;
        case INFO:
            verify(logger).isInfoEnabled();
            verify(logger).info("[gc][{}][{}][{}] duration [{}], collections [{}]/[{}], total [{}]/[{}], memory [{}]->[{}]/[{}], all_pools {}", name, seq, totalCollectionCount, currentCollectionTime, currentCollectionCount, TimeValue.timeValueMillis(elapsedValue), currentCollectionTime, totalCollectionTime, lastHeapUsed, currentHeapUsed, maxHeapUsed, "last, current");
            break;
        case DEBUG:
            verify(logger).isDebugEnabled();
            verify(logger).debug("[gc][{}][{}][{}] duration [{}], collections [{}]/[{}], total [{}]/[{}], memory [{}]->[{}]/[{}], all_pools {}", name, seq, totalCollectionCount, currentCollectionTime, currentCollectionCount, TimeValue.timeValueMillis(elapsedValue), currentCollectionTime, totalCollectionTime, lastHeapUsed, currentHeapUsed, maxHeapUsed, "last, current");
            break;
    }
    verifyNoMoreInteractions(logger);
}
Also used : ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) Logger(org.apache.logging.log4j.Logger) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 44 with TimeValue

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

the class ActiveShardsObserver method waitForActiveShards.

/**
     * Waits on the specified number of active shards to be started before executing the
     *
     * @param indexName the index to wait for active shards on
     * @param activeShardCount the number of active shards to wait on before returning
     * @param timeout the timeout value
     * @param onResult a function that is executed in response to the requisite shards becoming active or a timeout (whichever comes first)
     * @param onFailure a function that is executed in response to an error occurring during waiting for the active shards
     */
public void waitForActiveShards(final String indexName, final ActiveShardCount activeShardCount, final TimeValue timeout, final Consumer<Boolean> onResult, final Consumer<Exception> onFailure) {
    // wait for the configured number of active shards to be allocated before executing the result consumer
    if (activeShardCount == ActiveShardCount.NONE) {
        // not waiting, so just run whatever we were to run when the waiting is
        onResult.accept(true);
        return;
    }
    final ClusterState state = clusterService.state();
    final ClusterStateObserver observer = new ClusterStateObserver(state, clusterService, null, logger, threadPool.getThreadContext());
    if (activeShardCount.enoughShardsActive(state, indexName)) {
        onResult.accept(true);
    } else {
        final Predicate<ClusterState> shardsAllocatedPredicate = newState -> activeShardCount.enoughShardsActive(newState, indexName);
        final ClusterStateObserver.Listener observerListener = new ClusterStateObserver.Listener() {

            @Override
            public void onNewClusterState(ClusterState state) {
                onResult.accept(true);
            }

            @Override
            public void onClusterServiceClose() {
                logger.debug("[{}] cluster service closed while waiting for enough shards to be started.", indexName);
                onFailure.accept(new NodeClosedException(clusterService.localNode()));
            }

            @Override
            public void onTimeout(TimeValue timeout) {
                onResult.accept(false);
            }
        };
        observer.waitForNextChange(observerListener, shardsAllocatedPredicate, timeout);
    }
}
Also used : Consumer(java.util.function.Consumer) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) AbstractComponent(org.elasticsearch.common.component.AbstractComponent) Predicate(java.util.function.Predicate) TimeValue(org.elasticsearch.common.unit.TimeValue) ClusterService(org.elasticsearch.cluster.service.ClusterService) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) NodeClosedException(org.elasticsearch.node.NodeClosedException) ActionListener(org.elasticsearch.action.ActionListener) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) ActionListener(org.elasticsearch.action.ActionListener) NodeClosedException(org.elasticsearch.node.NodeClosedException) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 45 with TimeValue

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

the class MasterNodeRequest method readFrom.

@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    masterNodeTimeout = 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