Search in sources :

Example 16 with TimeValue

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

the class CacheBuilderTests method testSettingExpireAfterAccess.

public void testSettingExpireAfterAccess() {
    IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> CacheBuilder.builder().setExpireAfterAccess(TimeValue.MINUS_ONE));
    assertThat(iae.getMessage(), containsString("expireAfterAccess <="));
    iae = expectThrows(IllegalArgumentException.class, () -> CacheBuilder.builder().setExpireAfterAccess(TimeValue.ZERO));
    assertThat(iae.getMessage(), containsString("expireAfterAccess <="));
    final TimeValue timeValue = TimeValue.parseTimeValue(randomPositiveTimeValue(), "");
    Cache<Object, Object> cache = CacheBuilder.builder().setExpireAfterAccess(timeValue).build();
    assertEquals(timeValue.getNanos(), cache.getExpireAfterAccessNanos());
}
Also used : TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 17 with TimeValue

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

the class ClusterServiceTests method testClusterStateApplierCanCreateAnObserver.

public void testClusterStateApplierCanCreateAnObserver() throws InterruptedException {
    AtomicReference<Throwable> error = new AtomicReference<>();
    AtomicBoolean applierCalled = new AtomicBoolean();
    clusterService.addStateApplier(event -> {
        try {
            applierCalled.set(true);
            ClusterStateObserver observer = new ClusterStateObserver(event.state(), clusterService, null, logger, threadPool.getThreadContext());
            observer.waitForNextChange(new ClusterStateObserver.Listener() {

                @Override
                public void onNewClusterState(ClusterState state) {
                }

                @Override
                public void onClusterServiceClose() {
                }

                @Override
                public void onTimeout(TimeValue timeout) {
                }
            });
        } catch (AssertionError e) {
            error.set(e);
        }
    });
    CountDownLatch latch = new CountDownLatch(1);
    clusterService.submitStateUpdateTask("test", new ClusterStateUpdateTask() {

        @Override
        public ClusterState execute(ClusterState currentState) throws Exception {
            return ClusterState.builder(currentState).build();
        }

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

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
            latch.countDown();
        }
    });
    latch.await();
    assertNull(error.get());
    assertTrue(applierCalled.get());
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 18 with TimeValue

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

the class ClusterServiceTests method testTimedOutUpdateTaskCleanedUp.

public void testTimedOutUpdateTaskCleanedUp() throws Exception {
    final CountDownLatch block = new CountDownLatch(1);
    final CountDownLatch blockCompleted = new CountDownLatch(1);
    clusterService.submitStateUpdateTask("block-task", new ClusterStateUpdateTask() {

        @Override
        public ClusterState execute(ClusterState currentState) {
            try {
                block.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            blockCompleted.countDown();
            return currentState;
        }

        @Override
        public void onFailure(String source, Exception e) {
            throw new RuntimeException(e);
        }
    });
    final CountDownLatch block2 = new CountDownLatch(1);
    clusterService.submitStateUpdateTask("test", new ClusterStateUpdateTask() {

        @Override
        public ClusterState execute(ClusterState currentState) {
            block2.countDown();
            return currentState;
        }

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

        @Override
        public void onFailure(String source, Exception e) {
            block2.countDown();
        }
    });
    block.countDown();
    block2.await();
    blockCompleted.await();
    synchronized (clusterService.updateTasksPerExecutor) {
        assertTrue("expected empty map but was " + clusterService.updateTasksPerExecutor, clusterService.updateTasksPerExecutor.isEmpty());
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 19 with TimeValue

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

the class ClusterServiceTests method testTimeoutUpdateTask.

public void testTimeoutUpdateTask() throws Exception {
    final CountDownLatch block = new CountDownLatch(1);
    clusterService.submitStateUpdateTask("test1", new ClusterStateUpdateTask() {

        @Override
        public ClusterState execute(ClusterState currentState) {
            try {
                block.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return currentState;
        }

        @Override
        public void onFailure(String source, Exception e) {
            throw new RuntimeException(e);
        }
    });
    final CountDownLatch timedOut = new CountDownLatch(1);
    final AtomicBoolean executeCalled = new AtomicBoolean();
    clusterService.submitStateUpdateTask("test2", new ClusterStateUpdateTask() {

        @Override
        public TimeValue timeout() {
            return TimeValue.timeValueMillis(2);
        }

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

        @Override
        public ClusterState execute(ClusterState currentState) {
            executeCalled.set(true);
            return currentState;
        }

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
        }
    });
    timedOut.await();
    block.countDown();
    final CountDownLatch allProcessed = new CountDownLatch(1);
    clusterService.submitStateUpdateTask("test3", new ClusterStateUpdateTask() {

        @Override
        public void onFailure(String source, Exception e) {
            throw new RuntimeException(e);
        }

        @Override
        public ClusterState execute(ClusterState currentState) {
            allProcessed.countDown();
            return currentState;
        }
    });
    // executed another task to double check that execute on the timed out update task is not called...
    allProcessed.await();
    assertThat(executeCalled.get(), equalTo(false));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 20 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)

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