/** * Note, this test can only work as long as we have a single thread executor executing the state * update tasks! */ @Test public void testPrioritizedTasks() throws Exception { Settings settings = settingsBuilder().put("discovery.type", "local").build(); internalCluster().startNode(settings); ClusterService clusterService = internalCluster().getInstance(ClusterService.class); BlockingTask block = new BlockingTask(); clusterService.submitStateUpdateTask("test", Priority.IMMEDIATE, block); int taskCount = randomIntBetween(5, 20); Priority[] priorities = Priority.values(); // will hold all the tasks in the order in which they were executed List<PrioritiezedTask> tasks = new ArrayList<>(taskCount); CountDownLatch latch = new CountDownLatch(taskCount); for (int i = 0; i < taskCount; i++) { Priority priority = priorities[randomIntBetween(0, priorities.length - 1)]; clusterService.submitStateUpdateTask( "test", priority, new PrioritiezedTask(priority, latch, tasks)); } block.release(); latch.await(); Priority prevPriority = null; for (PrioritiezedTask task : tasks) { if (prevPriority == null) { prevPriority = task.priority; } else { assertThat(task.priority.sameOrAfter(prevPriority), is(true)); } } }
/** * Note, this test can only work as long as we have a single thread executor executing the state * update tasks! */ public void testPrioritizedTasks() throws Exception { BlockingTask block = new BlockingTask(Priority.IMMEDIATE); clusterService.submitStateUpdateTask("test", block); int taskCount = randomIntBetween(5, 20); // will hold all the tasks in the order in which they were executed List<PrioritizedTask> tasks = new ArrayList<>(taskCount); CountDownLatch latch = new CountDownLatch(taskCount); for (int i = 0; i < taskCount; i++) { Priority priority = randomFrom(Priority.values()); clusterService.submitStateUpdateTask("test", new PrioritizedTask(priority, latch, tasks)); } block.close(); latch.await(); Priority prevPriority = null; for (PrioritizedTask task : tasks) { if (prevPriority == null) { prevPriority = task.priority(); } else { assertThat(task.priority().sameOrAfter(prevPriority), is(true)); } } }
public void interrupt() { Selector activeSelector = currentSelector; if (activeSelector != null) { activeSelector.wakeup(); } BlockingIO.Condition iowait = blockingIO; if (iowait != null) { iowait.cancel(); } BlockingTask task = currentBlockingTask; if (task != null) { task.wakeup(); } }
public void executeBlockingTask(BlockingTask task) throws InterruptedException { enterSleep(); try { currentBlockingTask = task; pollThreadEvents(); task.run(); } finally { exitSleep(); currentBlockingTask = null; pollThreadEvents(); } }