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"));
}
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();
}
}
}
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));
}
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));
}
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()));
}
Aggregations