Search in sources :

Example 1 with Listener

use of org.opensearch.cluster.ClusterStateObserver.Listener in project OpenSearch by opensearch-project.

the class TransportClearVotingConfigExclusionsAction method masterOperation.

@Override
protected void masterOperation(ClearVotingConfigExclusionsRequest request, ClusterState initialState, ActionListener<ClearVotingConfigExclusionsResponse> listener) throws Exception {
    final long startTimeMillis = threadPool.relativeTimeInMillis();
    final Predicate<ClusterState> allExclusionsRemoved = newState -> {
        for (VotingConfigExclusion tombstone : initialState.getVotingConfigExclusions()) {
            // NB checking for the existence of any node with this persistent ID, because persistent IDs are how votes are counted.
            if (newState.nodes().nodeExists(tombstone.getNodeId())) {
                return false;
            }
        }
        return true;
    };
    if (request.getWaitForRemoval() && allExclusionsRemoved.test(initialState) == false) {
        final ClusterStateObserver clusterStateObserver = new ClusterStateObserver(initialState, clusterService, request.getTimeout(), logger, threadPool.getThreadContext());
        clusterStateObserver.waitForNextChange(new Listener() {

            @Override
            public void onNewClusterState(ClusterState state) {
                submitClearVotingConfigExclusionsTask(request, startTimeMillis, listener);
            }

            @Override
            public void onClusterServiceClose() {
                listener.onFailure(new OpenSearchException("cluster service closed while waiting for removal of nodes " + initialState.getVotingConfigExclusions()));
            }

            @Override
            public void onTimeout(TimeValue timeout) {
                listener.onFailure(new OpenSearchTimeoutException("timed out waiting for removal of nodes; if nodes should not be removed, set waitForRemoval to false. " + initialState.getVotingConfigExclusions()));
            }
        }, allExclusionsRemoved);
    } else {
        submitClearVotingConfigExclusionsTask(request, startTimeMillis, listener);
    }
}
Also used : VotingConfigExclusion(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion) Metadata(org.opensearch.cluster.metadata.Metadata) ThreadPool(org.opensearch.threadpool.ThreadPool) Priority(org.opensearch.common.Priority) TransportMasterNodeAction(org.opensearch.action.support.master.TransportMasterNodeAction) OpenSearchException(org.opensearch.OpenSearchException) CoordinationMetadata(org.opensearch.cluster.coordination.CoordinationMetadata) ClusterState(org.opensearch.cluster.ClusterState) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) ClusterStateObserver(org.opensearch.cluster.ClusterStateObserver) StreamInput(org.opensearch.common.io.stream.StreamInput) TimeValue(org.opensearch.common.unit.TimeValue) Predicate(java.util.function.Predicate) ClusterBlockLevel(org.opensearch.cluster.block.ClusterBlockLevel) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) Listener(org.opensearch.cluster.ClusterStateObserver.Listener) TransportService(org.opensearch.transport.TransportService) ActionFilters(org.opensearch.action.support.ActionFilters) Logger(org.apache.logging.log4j.Logger) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) ClusterService(org.opensearch.cluster.service.ClusterService) LogManager(org.apache.logging.log4j.LogManager) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Names(org.opensearch.threadpool.ThreadPool.Names) VotingConfigExclusion(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion) ClusterState(org.opensearch.cluster.ClusterState) ClusterStateObserver(org.opensearch.cluster.ClusterStateObserver) ActionListener(org.opensearch.action.ActionListener) Listener(org.opensearch.cluster.ClusterStateObserver.Listener) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) OpenSearchException(org.opensearch.OpenSearchException) TimeValue(org.opensearch.common.unit.TimeValue)

Example 2 with Listener

use of org.opensearch.cluster.ClusterStateObserver.Listener in project OpenSearch by opensearch-project.

the class TransportAddVotingConfigExclusionsAction method masterOperation.

@Override
protected void masterOperation(AddVotingConfigExclusionsRequest request, ClusterState state, ActionListener<AddVotingConfigExclusionsResponse> listener) throws Exception {
    resolveVotingConfigExclusionsAndCheckMaximum(request, state, maxVotingConfigExclusions);
    // throws IAE if no nodes matched or maximum exceeded
    clusterService.submitStateUpdateTask("add-voting-config-exclusions", new ClusterStateUpdateTask(Priority.URGENT) {

        private Set<VotingConfigExclusion> resolvedExclusions;

        @Override
        public ClusterState execute(ClusterState currentState) {
            assert resolvedExclusions == null : resolvedExclusions;
            final int finalMaxVotingConfigExclusions = TransportAddVotingConfigExclusionsAction.this.maxVotingConfigExclusions;
            resolvedExclusions = resolveVotingConfigExclusionsAndCheckMaximum(request, currentState, finalMaxVotingConfigExclusions);
            final CoordinationMetadata.Builder builder = CoordinationMetadata.builder(currentState.coordinationMetadata());
            resolvedExclusions.forEach(builder::addVotingConfigExclusion);
            final Metadata newMetadata = Metadata.builder(currentState.metadata()).coordinationMetadata(builder.build()).build();
            final ClusterState newState = ClusterState.builder(currentState).metadata(newMetadata).build();
            assert newState.getVotingConfigExclusions().size() <= finalMaxVotingConfigExclusions;
            return newState;
        }

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

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
            final ClusterStateObserver observer = new ClusterStateObserver(clusterService, request.getTimeout(), logger, threadPool.getThreadContext());
            final Set<String> excludedNodeIds = resolvedExclusions.stream().map(VotingConfigExclusion::getNodeId).collect(Collectors.toSet());
            final Predicate<ClusterState> allNodesRemoved = clusterState -> {
                final Set<String> votingConfigNodeIds = clusterState.getLastCommittedConfiguration().getNodeIds();
                return excludedNodeIds.stream().noneMatch(votingConfigNodeIds::contains);
            };
            final Listener clusterStateListener = new Listener() {

                @Override
                public void onNewClusterState(ClusterState state) {
                    listener.onResponse(new AddVotingConfigExclusionsResponse());
                }

                @Override
                public void onClusterServiceClose() {
                    listener.onFailure(new OpenSearchException("cluster service closed while waiting for voting config exclusions " + resolvedExclusions + " to take effect"));
                }

                @Override
                public void onTimeout(TimeValue timeout) {
                    listener.onFailure(new OpenSearchTimeoutException("timed out waiting for voting config exclusions " + resolvedExclusions + " to take effect"));
                }
            };
            if (allNodesRemoved.test(newState)) {
                clusterStateListener.onNewClusterState(newState);
            } else {
                observer.waitForNextChange(clusterStateListener, allNodesRemoved);
            }
        }
    });
}
Also used : VotingConfigExclusion(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion) ClusterState(org.opensearch.cluster.ClusterState) ClusterStateObserver(org.opensearch.cluster.ClusterStateObserver) Set(java.util.Set) ActionListener(org.opensearch.action.ActionListener) Listener(org.opensearch.cluster.ClusterStateObserver.Listener) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) Metadata(org.opensearch.cluster.metadata.Metadata) CoordinationMetadata(org.opensearch.cluster.coordination.CoordinationMetadata) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) OpenSearchException(org.opensearch.OpenSearchException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) Predicate(java.util.function.Predicate) OpenSearchException(org.opensearch.OpenSearchException) TimeValue(org.opensearch.common.unit.TimeValue)

Aggregations

IOException (java.io.IOException)2 Predicate (java.util.function.Predicate)2 OpenSearchException (org.opensearch.OpenSearchException)2 OpenSearchTimeoutException (org.opensearch.OpenSearchTimeoutException)2 ActionListener (org.opensearch.action.ActionListener)2 ClusterState (org.opensearch.cluster.ClusterState)2 ClusterStateObserver (org.opensearch.cluster.ClusterStateObserver)2 Listener (org.opensearch.cluster.ClusterStateObserver.Listener)2 ClusterStateUpdateTask (org.opensearch.cluster.ClusterStateUpdateTask)2 ClusterBlockException (org.opensearch.cluster.block.ClusterBlockException)2 CoordinationMetadata (org.opensearch.cluster.coordination.CoordinationMetadata)2 VotingConfigExclusion (org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion)2 Metadata (org.opensearch.cluster.metadata.Metadata)2 TimeValue (org.opensearch.common.unit.TimeValue)2 Set (java.util.Set)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 ActionFilters (org.opensearch.action.support.ActionFilters)1 TransportMasterNodeAction (org.opensearch.action.support.master.TransportMasterNodeAction)1 ClusterBlockLevel (org.opensearch.cluster.block.ClusterBlockLevel)1