Search in sources :

Example 61 with TimeValue

use of io.crate.common.unit.TimeValue in project crate by crate.

the class DeterministicTaskQueueTests method testThreadPoolSchedulesFutureTasks.

public void testThreadPoolSchedulesFutureTasks() {
    final DeterministicTaskQueue taskQueue = newTaskQueue();
    advanceToRandomTime(taskQueue);
    final long startTime = taskQueue.getCurrentTimeMillis();
    final List<String> strings = new ArrayList<>(5);
    final ThreadPool threadPool = taskQueue.getThreadPool();
    final long delayMillis = randomLongBetween(1, 100);
    threadPool.schedule(() -> strings.add("deferred"), TimeValue.timeValueMillis(delayMillis), GENERIC);
    assertFalse(taskQueue.hasRunnableTasks());
    assertTrue(taskQueue.hasDeferredTasks());
    threadPool.schedule(() -> strings.add("runnable"), TimeValue.ZERO, GENERIC);
    assertTrue(taskQueue.hasRunnableTasks());
    threadPool.schedule(() -> strings.add("also runnable"), TimeValue.MINUS_ONE, GENERIC);
    taskQueue.runAllTasks();
    assertThat(taskQueue.getCurrentTimeMillis(), is(startTime + delayMillis));
    assertThat(strings, containsInAnyOrder("runnable", "also runnable", "deferred"));
    final long delayMillis1 = randomLongBetween(2, 100);
    final long delayMillis2 = randomLongBetween(1, delayMillis1 - 1);
    threadPool.schedule(() -> strings.add("further deferred"), TimeValue.timeValueMillis(delayMillis1), GENERIC);
    threadPool.schedule(() -> strings.add("not quite so deferred"), TimeValue.timeValueMillis(delayMillis2), GENERIC);
    assertFalse(taskQueue.hasRunnableTasks());
    assertTrue(taskQueue.hasDeferredTasks());
    taskQueue.runAllTasks();
    assertThat(taskQueue.getCurrentTimeMillis(), is(startTime + delayMillis + delayMillis1));
    final TimeValue cancelledDelay = TimeValue.timeValueMillis(randomLongBetween(1, 100));
    final Scheduler.Cancellable cancelledBeforeExecution = threadPool.schedule(() -> strings.add("cancelled before execution"), cancelledDelay, "");
    cancelledBeforeExecution.cancel();
    taskQueue.runAllTasks();
    assertThat(strings, containsInAnyOrder("runnable", "also runnable", "deferred", "not quite so deferred", "further deferred"));
}
Also used : Scheduler(org.elasticsearch.threadpool.Scheduler) ArrayList(java.util.ArrayList) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TimeValue(io.crate.common.unit.TimeValue)

Example 62 with TimeValue

use of io.crate.common.unit.TimeValue in project crate by crate.

the class MasterServiceTests method testAcking.

public void testAcking() throws InterruptedException {
    final DiscoveryNode node1 = new DiscoveryNode("node1", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    final DiscoveryNode node2 = new DiscoveryNode("node2", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    final DiscoveryNode node3 = new DiscoveryNode("node3", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    try (MasterService masterService = new MasterService(Settings.builder().put(ClusterName.CLUSTER_NAME_SETTING.getKey(), MasterServiceTests.class.getSimpleName()).put(Node.NODE_NAME_SETTING.getKey(), "test_node").build(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), threadPool)) {
        final ClusterState initialClusterState = ClusterState.builder(new ClusterName(MasterServiceTests.class.getSimpleName())).nodes(DiscoveryNodes.builder().add(node1).add(node2).add(node3).localNodeId(node1.getId()).masterNodeId(node1.getId())).blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK).build();
        final AtomicReference<ClusterStatePublisher> publisherRef = new AtomicReference<>();
        masterService.setClusterStatePublisher((e, pl, al) -> publisherRef.get().publish(e, pl, al));
        masterService.setClusterStateSupplier(() -> initialClusterState);
        masterService.start();
        // check that we don't time out before even committing the cluster state
        {
            final CountDownLatch latch = new CountDownLatch(1);
            publisherRef.set((clusterChangedEvent, publishListener, ackListener) -> publishListener.onFailure(new FailedToCommitClusterStateException("mock exception")));
            masterService.submitStateUpdateTask("test2", new AckedClusterStateUpdateTask<Void>(null, null) {

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

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

                @Override
                public TimeValue timeout() {
                    return null;
                }

                @Override
                public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                    fail();
                }

                @Override
                protected Void newResponse(boolean acknowledged) {
                    fail();
                    return null;
                }

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

                @Override
                public void onAckTimeout() {
                    fail();
                }
            });
            latch.await();
        }
        // check that we timeout if commit took too long
        {
            final CountDownLatch latch = new CountDownLatch(2);
            final TimeValue ackTimeout = TimeValue.timeValueMillis(randomInt(100));
            publisherRef.set((clusterChangedEvent, publishListener, ackListener) -> {
                publishListener.onResponse(null);
                ackListener.onCommit(TimeValue.timeValueMillis(ackTimeout.millis() + randomInt(100)));
                ackListener.onNodeAck(node1, null);
                ackListener.onNodeAck(node2, null);
                ackListener.onNodeAck(node3, null);
            });
            masterService.submitStateUpdateTask("test2", new AckedClusterStateUpdateTask<Void>(null, null) {

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

                @Override
                public TimeValue ackTimeout() {
                    return ackTimeout;
                }

                @Override
                public TimeValue timeout() {
                    return null;
                }

                @Override
                public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                    latch.countDown();
                }

                @Override
                protected Void newResponse(boolean acknowledged) {
                    fail();
                    return null;
                }

                @Override
                public void onFailure(String source, Exception e) {
                    fail();
                }

                @Override
                public void onAckTimeout() {
                    latch.countDown();
                }
            });
            latch.await();
        }
    }
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) Level(org.apache.logging.log4j.Level) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) Matchers.hasKey(org.hamcrest.Matchers.hasKey) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) Settings(org.elasticsearch.common.settings.Settings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ClusterName(org.elasticsearch.cluster.ClusterName) FailedToCommitClusterStateException(org.elasticsearch.cluster.coordination.FailedToCommitClusterStateException) MockLogAppender(org.elasticsearch.test.MockLogAppender) AfterClass(org.junit.AfterClass) CyclicBarrier(java.util.concurrent.CyclicBarrier) Priority(org.elasticsearch.common.Priority) TestLogging(org.elasticsearch.test.junit.annotations.TestLogging) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TimeValue(io.crate.common.unit.TimeValue) Matchers.anyOf(org.hamcrest.Matchers.anyOf) Matchers.containsString(org.hamcrest.Matchers.containsString) AckedClusterStateUpdateTask(org.elasticsearch.cluster.AckedClusterStateUpdateTask) Tuple(io.crate.common.collections.Tuple) BeforeClass(org.junit.BeforeClass) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) BaseFuture(org.elasticsearch.common.util.concurrent.BaseFuture) HashSet(java.util.HashSet) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ClusterStateTaskListener(org.elasticsearch.cluster.ClusterStateTaskListener) Node(org.elasticsearch.node.Node) ESTestCase(org.elasticsearch.test.ESTestCase) Before(org.junit.Before) Loggers(org.elasticsearch.common.logging.Loggers) Collections.emptyMap(java.util.Collections.emptyMap) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Collections.emptySet(java.util.Collections.emptySet) Semaphore(java.util.concurrent.Semaphore) ClusterStateTaskConfig(org.elasticsearch.cluster.ClusterStateTaskConfig) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ClusterStateTaskExecutor(org.elasticsearch.cluster.ClusterStateTaskExecutor) TimeUnit(java.util.concurrent.TimeUnit) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) ClusterStatePublisher(org.elasticsearch.cluster.coordination.ClusterStatePublisher) LocalClusterUpdateTask(org.elasticsearch.cluster.LocalClusterUpdateTask) LogManager(org.apache.logging.log4j.LogManager) FailedToCommitClusterStateException(org.elasticsearch.cluster.coordination.FailedToCommitClusterStateException) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) ElasticsearchException(org.elasticsearch.ElasticsearchException) FailedToCommitClusterStateException(org.elasticsearch.cluster.coordination.FailedToCommitClusterStateException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) AckedClusterStateUpdateTask(org.elasticsearch.cluster.AckedClusterStateUpdateTask) ClusterName(org.elasticsearch.cluster.ClusterName) ClusterStatePublisher(org.elasticsearch.cluster.coordination.ClusterStatePublisher) TimeValue(io.crate.common.unit.TimeValue)

Example 63 with TimeValue

use of io.crate.common.unit.TimeValue in project crate by crate.

the class GatewayIndexStateIT method testJustMasterNode.

/**
 * Creating a table without any data node will take very long as internally at CrateDB, a table creation
 * is waiting for all shards to acknowledge until it times out if no data node is available.
 * So this will run under the @Slow annotation.
 */
@Slow
@Test
public void testJustMasterNode() throws Exception {
    logger.info("--> cleaning nodes");
    logger.info("--> starting 1 master node non data");
    internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), false).build());
    logger.info("--> create an index");
    execute("create table test (id int) with (number_of_replicas = 0, \"write.wait_for_active_shards\" = 0)", null, new TimeValue(90, TimeUnit.SECONDS));
    var tableName = getFqn("test");
    logger.info("--> restarting master node");
    internalCluster().fullRestart(new RestartCallback() {

        @Override
        public Settings onNodeStopped(String nodeName) {
            return Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), false).build();
        }
    });
    logger.info("--> waiting for test index to be created");
    ClusterHealthResponse health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setIndices(tableName).execute().actionGet(REQUEST_TIMEOUT);
    assertThat(health.isTimedOut(), equalTo(false));
    logger.info("--> verify we have an index");
    ClusterStateResponse clusterStateResponse = client().admin().cluster().prepareState().setIndices(tableName).execute().actionGet(REQUEST_TIMEOUT);
    assertThat(clusterStateResponse.getState().metadata().hasIndex(tableName), equalTo(true));
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) RestartCallback(org.elasticsearch.test.InternalTestCluster.RestartCallback) TimeValue(io.crate.common.unit.TimeValue) Settings(org.elasticsearch.common.settings.Settings) Test(org.junit.Test)

Example 64 with TimeValue

use of io.crate.common.unit.TimeValue in project crate by crate.

the class SettingTests method testTimeValue.

@Test
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 : Matchers.hasToString(org.hamcrest.Matchers.hasToString) ByteSizeUnit(org.elasticsearch.common.unit.ByteSizeUnit) Arrays(java.util.Arrays) Tuple(io.crate.common.collections.Tuple) Property(org.elasticsearch.common.settings.Setting.Property) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ESTestCase(org.elasticsearch.test.ESTestCase) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) JvmInfo(org.elasticsearch.monitor.jvm.JvmInfo) Iterator(java.util.Iterator) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Version(org.elasticsearch.Version) Stream(java.util.stream.Stream) DataTypes(io.crate.types.DataTypes) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TimeValue(io.crate.common.unit.TimeValue) Matchers.is(org.hamcrest.Matchers.is) SettingUpdater(org.elasticsearch.common.settings.AbstractScopedSettings.SettingUpdater) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) TimeValue(io.crate.common.unit.TimeValue) Test(org.junit.Test)

Example 65 with TimeValue

use of io.crate.common.unit.TimeValue in project crate by crate.

the class SettingTests method testDefault.

@Test
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(), DataTypes.STRING, 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(), DataTypes.STRING, 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(), DataTypes.STRING, 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.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) TimeValue(io.crate.common.unit.TimeValue) Test(org.junit.Test)

Aggregations

TimeValue (io.crate.common.unit.TimeValue)75 Test (org.junit.Test)23 ClusterState (org.elasticsearch.cluster.ClusterState)20 IOException (java.io.IOException)17 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)12 ActionListener (org.elasticsearch.action.ActionListener)12 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)11 ArrayList (java.util.ArrayList)10 ThreadPool (org.elasticsearch.threadpool.ThreadPool)10 ElasticsearchException (org.elasticsearch.ElasticsearchException)9 Settings (org.elasticsearch.common.settings.Settings)9 Logger (org.apache.logging.log4j.Logger)8 ClusterStateUpdateTask (org.elasticsearch.cluster.ClusterStateUpdateTask)8 ClusterService (org.elasticsearch.cluster.service.ClusterService)8 List (java.util.List)7 LogManager (org.apache.logging.log4j.LogManager)7 Version (org.elasticsearch.Version)7 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)6 ClusterStateObserver (org.elasticsearch.cluster.ClusterStateObserver)6 StreamInput (org.elasticsearch.common.io.stream.StreamInput)6