Search in sources :

Example 6 with ClusterStateTaskListener

use of org.elasticsearch.cluster.ClusterStateTaskListener in project elasticsearch by elastic.

the class ClusterServiceTests method testClusterStateUpdateTasksAreExecutedInOrder.

// test that for a single thread, tasks are executed in the order
// that they are submitted
public void testClusterStateUpdateTasksAreExecutedInOrder() throws BrokenBarrierException, InterruptedException {
    class TaskExecutor implements ClusterStateTaskExecutor<Integer> {

        List<Integer> tasks = new ArrayList<>();

        @Override
        public ClusterTasksResult<Integer> execute(ClusterState currentState, List<Integer> tasks) throws Exception {
            this.tasks.addAll(tasks);
            return ClusterTasksResult.<Integer>builder().successes(tasks).build(ClusterState.builder(currentState).build());
        }
    }
    int numberOfThreads = randomIntBetween(2, 8);
    TaskExecutor[] executors = new TaskExecutor[numberOfThreads];
    for (int i = 0; i < numberOfThreads; i++) {
        executors[i] = new TaskExecutor();
    }
    int tasksSubmittedPerThread = randomIntBetween(2, 1024);
    CopyOnWriteArrayList<Tuple<String, Throwable>> failures = new CopyOnWriteArrayList<>();
    CountDownLatch updateLatch = new CountDownLatch(numberOfThreads * tasksSubmittedPerThread);
    ClusterStateTaskListener listener = new ClusterStateTaskListener() {

        @Override
        public void onFailure(String source, Exception e) {
            logger.error((Supplier<?>) () -> new ParameterizedMessage("unexpected failure: [{}]", source), e);
            failures.add(new Tuple<>(source, e));
            updateLatch.countDown();
        }

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
            updateLatch.countDown();
        }
    };
    CyclicBarrier barrier = new CyclicBarrier(1 + numberOfThreads);
    for (int i = 0; i < numberOfThreads; i++) {
        final int index = i;
        Thread thread = new Thread(() -> {
            try {
                barrier.await();
                for (int j = 0; j < tasksSubmittedPerThread; j++) {
                    clusterService.submitStateUpdateTask("[" + index + "][" + j + "]", j, ClusterStateTaskConfig.build(randomFrom(Priority.values())), executors[index], listener);
                }
                barrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                throw new AssertionError(e);
            }
        });
        thread.start();
    }
    // wait for all threads to be ready
    barrier.await();
    // wait for all threads to finish
    barrier.await();
    updateLatch.await();
    assertThat(failures, empty());
    for (int i = 0; i < numberOfThreads; i++) {
        assertEquals(tasksSubmittedPerThread, executors[i].tasks.size());
        for (int j = 0; j < tasksSubmittedPerThread; j++) {
            assertNotNull(executors[i].tasks.get(j));
            assertEquals("cluster state update task executed out of order", j, (int) executors[i].tasks.get(j));
        }
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClusterStateTaskExecutor(org.elasticsearch.cluster.ClusterStateTaskExecutor) ClusterStateTaskExecutor(org.elasticsearch.cluster.ClusterStateTaskExecutor) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Tuple(org.elasticsearch.common.collect.Tuple) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ClusterStateTaskListener(org.elasticsearch.cluster.ClusterStateTaskListener)

Example 7 with ClusterStateTaskListener

use of org.elasticsearch.cluster.ClusterStateTaskListener in project elasticsearch by elastic.

the class ClusterServiceTests method testBlockingCallInClusterStateTaskListenerFails.

public void testBlockingCallInClusterStateTaskListenerFails() throws InterruptedException {
    assumeTrue("assertions must be enabled for this test to work", BaseFuture.class.desiredAssertionStatus());
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<AssertionError> assertionRef = new AtomicReference<>();
    clusterService.submitStateUpdateTask("testBlockingCallInClusterStateTaskListenerFails", new Object(), ClusterStateTaskConfig.build(Priority.NORMAL), new ClusterStateTaskExecutor<Object>() {

        @Override
        public ClusterTasksResult<Object> execute(ClusterState currentState, List<Object> tasks) throws Exception {
            ClusterState newClusterState = ClusterState.builder(currentState).build();
            return ClusterTasksResult.builder().successes(tasks).build(newClusterState);
        }
    }, new ClusterStateTaskListener() {

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
            BaseFuture<Void> future = new BaseFuture<Void>() {
            };
            try {
                if (randomBoolean()) {
                    future.get(1L, TimeUnit.SECONDS);
                } else {
                    future.get();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            } catch (AssertionError e) {
                assertionRef.set(e);
                latch.countDown();
            }
        }

        @Override
        public void onFailure(String source, Exception e) {
        }
    });
    latch.await();
    assertNotNull(assertionRef.get());
    assertThat(assertionRef.get().getMessage(), containsString("not be the cluster state update thread. Reason: [Blocking operation]"));
}
Also used : BaseFuture(org.elasticsearch.common.util.concurrent.BaseFuture) ClusterState(org.elasticsearch.cluster.ClusterState) 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) ClusterStateTaskListener(org.elasticsearch.cluster.ClusterStateTaskListener)

Example 8 with ClusterStateTaskListener

use of org.elasticsearch.cluster.ClusterStateTaskListener in project elasticsearch by elastic.

the class ClusterServiceTests method testSingleBatchSubmission.

public void testSingleBatchSubmission() throws InterruptedException {
    Map<Integer, ClusterStateTaskListener> tasks = new HashMap<>();
    final int numOfTasks = randomInt(10);
    final CountDownLatch latch = new CountDownLatch(numOfTasks);
    for (int i = 0; i < numOfTasks; i++) {
        while (null != tasks.put(randomInt(1024), new ClusterStateTaskListener() {

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

            @Override
            public void onFailure(String source, Exception e) {
                fail(ExceptionsHelper.detailedMessage(e));
            }
        })) ;
    }
    clusterService.submitStateUpdateTasks("test", tasks, ClusterStateTaskConfig.build(Priority.LANGUID), (currentState, taskList) -> {
        assertThat(taskList.size(), equalTo(tasks.size()));
        assertThat(taskList.stream().collect(Collectors.toSet()), equalTo(tasks.keySet()));
        return ClusterStateTaskExecutor.ClusterTasksResult.<Integer>builder().successes(taskList).build(currentState);
    });
    latch.await();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClusterState(org.elasticsearch.cluster.ClusterState) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ClusterStateTaskListener(org.elasticsearch.cluster.ClusterStateTaskListener)

Aggregations

ClusterState (org.elasticsearch.cluster.ClusterState)8 ClusterStateTaskListener (org.elasticsearch.cluster.ClusterStateTaskListener)8 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 Matchers.containsString (org.hamcrest.Matchers.containsString)7 Matchers.hasToString (org.hamcrest.Matchers.hasToString)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)5 ClusterChangedEvent (org.elasticsearch.cluster.ClusterChangedEvent)5 ClusterStateTaskExecutor (org.elasticsearch.cluster.ClusterStateTaskExecutor)5 Collections (java.util.Collections)4 Map (java.util.Map)4 CyclicBarrier (java.util.concurrent.CyclicBarrier)4