Search in sources :

Example 11 with ClusterStateUpdateTask

use of org.elasticsearch.cluster.ClusterStateUpdateTask 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 12 with ClusterStateUpdateTask

use of org.elasticsearch.cluster.ClusterStateUpdateTask 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 13 with ClusterStateUpdateTask

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

the class ClusterServiceTests method testClusterStateUpdateLogging.

// To ensure that we log cluster state events on TRACE level
@TestLogging("org.elasticsearch.cluster.service:TRACE")
public void testClusterStateUpdateLogging() throws Exception {
    MockLogAppender mockAppender = new MockLogAppender();
    mockAppender.start();
    mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test1", "org.elasticsearch.cluster.service.ClusterServiceTests$TimedClusterService", Level.DEBUG, "*processing [test1]: took [1s] no change in cluster_state"));
    mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test2", "org.elasticsearch.cluster.service.ClusterServiceTests$TimedClusterService", Level.TRACE, "*failed to execute cluster state update in [2s]*"));
    mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test3", "org.elasticsearch.cluster.service.ClusterServiceTests$TimedClusterService", Level.DEBUG, "*processing [test3]: took [3s] done applying updated cluster_state (version: *, uuid: *)"));
    Logger clusterLogger = Loggers.getLogger("org.elasticsearch.cluster.service");
    Loggers.addAppender(clusterLogger, mockAppender);
    try {
        final CountDownLatch latch = new CountDownLatch(4);
        clusterService.currentTimeOverride = System.nanoTime();
        clusterService.submitStateUpdateTask("test1", new ClusterStateUpdateTask() {

            @Override
            public ClusterState execute(ClusterState currentState) throws Exception {
                clusterService.currentTimeOverride += TimeValue.timeValueSeconds(1).nanos();
                return currentState;
            }

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

            @Override
            public void onFailure(String source, Exception e) {
                fail();
            }
        });
        clusterService.submitStateUpdateTask("test2", new ClusterStateUpdateTask() {

            @Override
            public ClusterState execute(ClusterState currentState) {
                clusterService.currentTimeOverride += TimeValue.timeValueSeconds(2).nanos();
                throw new IllegalArgumentException("Testing handling of exceptions in the cluster state task");
            }

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

            @Override
            public void onFailure(String source, Exception e) {
                latch.countDown();
            }
        });
        clusterService.submitStateUpdateTask("test3", new ClusterStateUpdateTask() {

            @Override
            public ClusterState execute(ClusterState currentState) {
                clusterService.currentTimeOverride += TimeValue.timeValueSeconds(3).nanos();
                return ClusterState.builder(currentState).incrementVersion().build();
            }

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

            @Override
            public void onFailure(String source, Exception e) {
                fail();
            }
        });
        // Additional update task to make sure all previous logging made it to the loggerName
        // We don't check logging for this on since there is no guarantee that it will occur before our check
        clusterService.submitStateUpdateTask("test4", new ClusterStateUpdateTask() {

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

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

            @Override
            public void onFailure(String source, Exception e) {
                fail();
            }
        });
        latch.await();
    } finally {
        Loggers.removeAppender(clusterLogger, mockAppender);
        mockAppender.stop();
    }
    mockAppender.assertAllExpectationsMatched();
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) MockLogAppender(org.elasticsearch.test.MockLogAppender) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) Logger(org.apache.logging.log4j.Logger) CountDownLatch(java.util.concurrent.CountDownLatch) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) TestLogging(org.elasticsearch.test.junit.annotations.TestLogging)

Example 14 with ClusterStateUpdateTask

use of org.elasticsearch.cluster.ClusterStateUpdateTask 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 15 with ClusterStateUpdateTask

use of org.elasticsearch.cluster.ClusterStateUpdateTask in project crate by crate.

the class MetadataIndexTemplateService method putTemplate.

public void putTemplate(final PutRequest request, final PutListener listener) {
    Settings.Builder updatedSettingsBuilder = Settings.builder();
    updatedSettingsBuilder.put(request.settings).normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX);
    request.settings(updatedSettingsBuilder.build());
    if (request.name == null) {
        listener.onFailure(new IllegalArgumentException("index_template must provide a name"));
        return;
    }
    if (request.indexPatterns == null) {
        listener.onFailure(new IllegalArgumentException("index_template must provide a template"));
        return;
    }
    try {
        validate(request);
    } catch (Exception e) {
        listener.onFailure(e);
        return;
    }
    final IndexTemplateMetadata.Builder templateBuilder = IndexTemplateMetadata.builder(request.name);
    clusterService.submitStateUpdateTask("create-index-template [" + request.name + "], cause [" + request.cause + "]", new ClusterStateUpdateTask(Priority.URGENT) {

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

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

        @Override
        public ClusterState execute(ClusterState currentState) throws Exception {
            if (request.create && currentState.metadata().templates().containsKey(request.name)) {
                throw new IllegalArgumentException("index_template [" + request.name + "] already exists");
            }
            validateAndAddTemplate(request, templateBuilder, indicesService, xContentRegistry);
            for (Alias alias : request.aliases) {
                AliasMetadata aliasMetadata = AliasMetadata.builder(alias.name()).filter(alias.filter()).indexRouting(alias.indexRouting()).searchRouting(alias.searchRouting()).build();
                templateBuilder.putAlias(aliasMetadata);
            }
            IndexTemplateMetadata template = templateBuilder.build();
            Metadata.Builder builder = Metadata.builder(currentState.metadata()).put(template);
            LOGGER.info("adding template [{}] for index patterns {}", request.name, request.indexPatterns);
            return ClusterState.builder(currentState).metadata(builder).build();
        }

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
            listener.onResponse(new PutResponse(true, templateBuilder.build()));
        }
    });
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) ValidationException(org.elasticsearch.common.ValidationException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) InvalidIndexTemplateException(org.elasticsearch.indices.InvalidIndexTemplateException) IndexTemplateMissingException(org.elasticsearch.indices.IndexTemplateMissingException) Alias(org.elasticsearch.action.admin.indices.alias.Alias) Settings(org.elasticsearch.common.settings.Settings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) TimeValue(io.crate.common.unit.TimeValue)

Aggregations

ClusterState (org.elasticsearch.cluster.ClusterState)45 ClusterStateUpdateTask (org.elasticsearch.cluster.ClusterStateUpdateTask)45 CountDownLatch (java.util.concurrent.CountDownLatch)21 Matchers.containsString (org.hamcrest.Matchers.containsString)15 ArrayList (java.util.ArrayList)13 ClusterService (org.elasticsearch.cluster.service.ClusterService)13 IOException (java.io.IOException)12 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)12 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)12 List (java.util.List)11 Logger (org.apache.logging.log4j.Logger)9 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 SnapshotDeletionsInProgress (org.elasticsearch.cluster.SnapshotDeletionsInProgress)8 SnapshotsInProgress (org.elasticsearch.cluster.SnapshotsInProgress)8 FailedToCommitClusterStateException (org.elasticsearch.cluster.coordination.FailedToCommitClusterStateException)8 Priority (org.elasticsearch.common.Priority)8 Settings (org.elasticsearch.common.settings.Settings)8 Set (java.util.Set)7 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)7 RepositoryMissingException (org.elasticsearch.repositories.RepositoryMissingException)7