Search in sources :

Example 1 with LocalClusterUpdateTask

use of org.opensearch.cluster.LocalClusterUpdateTask in project OpenSearch by opensearch-project.

the class MasterServiceTests method testMasterAwareExecution.

public void testMasterAwareExecution() throws Exception {
    final MasterService nonMaster = createMasterService(false);
    final boolean[] taskFailed = { false };
    final CountDownLatch latch1 = new CountDownLatch(1);
    nonMaster.submitStateUpdateTask("test", new ClusterStateUpdateTask() {

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

        @Override
        public void onFailure(String source, Exception e) {
            taskFailed[0] = true;
            latch1.countDown();
        }
    });
    latch1.await();
    assertTrue("cluster state update task was executed on a non-master", taskFailed[0]);
    final CountDownLatch latch2 = new CountDownLatch(1);
    nonMaster.submitStateUpdateTask("test", new LocalClusterUpdateTask() {

        @Override
        public ClusterTasksResult<LocalClusterUpdateTask> execute(ClusterState currentState) {
            taskFailed[0] = false;
            latch2.countDown();
            return unchanged();
        }

        @Override
        public void onFailure(String source, Exception e) {
            taskFailed[0] = true;
            latch2.countDown();
        }
    });
    latch2.await();
    assertFalse("non-master cluster state update task was not executed", taskFailed[0]);
    nonMaster.close();
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) LocalClusterUpdateTask(org.opensearch.cluster.LocalClusterUpdateTask) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) AckedClusterStateUpdateTask(org.opensearch.cluster.AckedClusterStateUpdateTask) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) OpenSearchException(org.opensearch.OpenSearchException) FailedToCommitClusterStateException(org.opensearch.cluster.coordination.FailedToCommitClusterStateException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException)

Example 2 with LocalClusterUpdateTask

use of org.opensearch.cluster.LocalClusterUpdateTask in project OpenSearch by opensearch-project.

the class TransportClusterHealthAction method waitForEventsAndExecuteHealth.

private void waitForEventsAndExecuteHealth(final ClusterHealthRequest request, final ActionListener<ClusterHealthResponse> listener, final int waitCount, final long endTimeRelativeMillis) {
    assert request.waitForEvents() != null;
    if (request.local()) {
        clusterService.submitStateUpdateTask("cluster_health (wait_for_events [" + request.waitForEvents() + "])", new LocalClusterUpdateTask(request.waitForEvents()) {

            @Override
            public ClusterTasksResult<LocalClusterUpdateTask> execute(ClusterState currentState) {
                return unchanged();
            }

            @Override
            public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                final long timeoutInMillis = Math.max(0, endTimeRelativeMillis - threadPool.relativeTimeInMillis());
                final TimeValue newTimeout = TimeValue.timeValueMillis(timeoutInMillis);
                request.timeout(newTimeout);
                executeHealth(request, clusterService.state(), listener, waitCount, observedState -> waitForEventsAndExecuteHealth(request, listener, waitCount, endTimeRelativeMillis));
            }

            @Override
            public void onFailure(String source, Exception e) {
                logger.error(() -> new ParameterizedMessage("unexpected failure during [{}]", source), e);
                listener.onFailure(e);
            }
        });
    } else {
        final TimeValue taskTimeout = TimeValue.timeValueMillis(Math.max(0, endTimeRelativeMillis - threadPool.relativeTimeInMillis()));
        clusterService.submitStateUpdateTask("cluster_health (wait_for_events [" + request.waitForEvents() + "])", new ClusterStateUpdateTask(request.waitForEvents()) {

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

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

            @Override
            public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                final long timeoutInMillis = Math.max(0, endTimeRelativeMillis - threadPool.relativeTimeInMillis());
                final TimeValue newTimeout = TimeValue.timeValueMillis(timeoutInMillis);
                request.timeout(newTimeout);
                // we must use the state from the applier service, because if the state-not-recovered block is in place then the
                // applier service has a different view of the cluster state from the one supplied here
                final ClusterState appliedState = clusterService.state();
                assert newState.stateUUID().equals(appliedState.stateUUID()) : newState.stateUUID() + " vs " + appliedState.stateUUID();
                executeHealth(request, appliedState, listener, waitCount, observedState -> waitForEventsAndExecuteHealth(request, listener, waitCount, endTimeRelativeMillis));
            }

            @Override
            public void onNoLongerMaster(String source) {
                logger.trace("stopped being master while waiting for events with priority [{}]. retrying.", request.waitForEvents());
                // TransportMasterNodeAction implements the retry logic, which is triggered by passing a NotMasterException
                listener.onFailure(new NotMasterException("no longer master. source: [" + source + "]"));
            }

            @Override
            public void onFailure(String source, Exception e) {
                if (e instanceof ProcessClusterEventTimeoutException) {
                    listener.onResponse(getResponse(request, clusterService.state(), waitCount, TimeoutState.TIMED_OUT));
                } else {
                    logger.error(() -> new ParameterizedMessage("unexpected failure during [{}]", source), e);
                    listener.onFailure(e);
                }
            }
        });
    }
}
Also used : ThreadPool(org.opensearch.threadpool.ThreadPool) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) IndicesOptions(org.opensearch.action.support.IndicesOptions) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Strings(org.opensearch.common.Strings) ClusterState(org.opensearch.cluster.ClusterState) NotMasterException(org.opensearch.cluster.NotMasterException) NodeClosedException(org.opensearch.node.NodeClosedException) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) ClusterStateObserver(org.opensearch.cluster.ClusterStateObserver) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) ClusterHealthStatus(org.opensearch.cluster.health.ClusterHealthStatus) ProcessClusterEventTimeoutException(org.opensearch.cluster.metadata.ProcessClusterEventTimeoutException) StreamInput(org.opensearch.common.io.stream.StreamInput) TimeValue(org.opensearch.common.unit.TimeValue) CollectionUtils(org.opensearch.common.util.CollectionUtils) Predicate(java.util.function.Predicate) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) Task(org.opensearch.tasks.Task) TransportService(org.opensearch.transport.TransportService) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) LocalClusterUpdateTask(org.opensearch.cluster.LocalClusterUpdateTask) Consumer(java.util.function.Consumer) ActionFilters(org.opensearch.action.support.ActionFilters) Logger(org.apache.logging.log4j.Logger) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) ClusterService(org.opensearch.cluster.service.ClusterService) LogManager(org.apache.logging.log4j.LogManager) TransportMasterNodeReadAction(org.opensearch.action.support.master.TransportMasterNodeReadAction) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) ClusterState(org.opensearch.cluster.ClusterState) LocalClusterUpdateTask(org.opensearch.cluster.LocalClusterUpdateTask) ProcessClusterEventTimeoutException(org.opensearch.cluster.metadata.ProcessClusterEventTimeoutException) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) NotMasterException(org.opensearch.cluster.NotMasterException) TimeValue(org.opensearch.common.unit.TimeValue) NotMasterException(org.opensearch.cluster.NotMasterException) NodeClosedException(org.opensearch.node.NodeClosedException) ProcessClusterEventTimeoutException(org.opensearch.cluster.metadata.ProcessClusterEventTimeoutException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IOException(java.io.IOException)

Aggregations

ClusterState (org.opensearch.cluster.ClusterState)2 ClusterStateUpdateTask (org.opensearch.cluster.ClusterStateUpdateTask)2 LocalClusterUpdateTask (org.opensearch.cluster.LocalClusterUpdateTask)2 IOException (java.io.IOException)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Consumer (java.util.function.Consumer)1 Predicate (java.util.function.Predicate)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 OpenSearchException (org.opensearch.OpenSearchException)1 ActionListener (org.opensearch.action.ActionListener)1 ActionFilters (org.opensearch.action.support.ActionFilters)1 ActiveShardCount (org.opensearch.action.support.ActiveShardCount)1 IndicesOptions (org.opensearch.action.support.IndicesOptions)1 TransportMasterNodeReadAction (org.opensearch.action.support.master.TransportMasterNodeReadAction)1 AckedClusterStateUpdateTask (org.opensearch.cluster.AckedClusterStateUpdateTask)1 ClusterStateObserver (org.opensearch.cluster.ClusterStateObserver)1