Search in sources :

Example 16 with ClusterStateObserver

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

the class TransportAddVotingConfigExclusionsAction method masterOperation.

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

        private Set<VotingConfigExclusion> resolvedExclusions;

        @Override
        public ClusterState execute(ClusterState currentState) {
            assert resolvedExclusions == null : resolvedExclusions;
            resolvedExclusions = resolveVotingConfigExclusionsAndCheckMaximum(request, currentState);
            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() <= MAXIMUM_VOTING_CONFIG_EXCLUSIONS_SETTING.get(currentState.metadata().settings());
            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);
            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 ElasticsearchException("cluster service closed while waiting for voting config exclusions " + resolvedExclusions + " to take effect"));
                }

                @Override
                public void onTimeout(TimeValue timeout) {
                    listener.onFailure(new ElasticsearchTimeoutException("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.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) Set(java.util.Set) Listener(org.elasticsearch.cluster.ClusterStateObserver.Listener) ActionListener(org.elasticsearch.action.ActionListener) CoordinationMetadata(org.elasticsearch.cluster.coordination.CoordinationMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) Predicate(java.util.function.Predicate) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) TimeValue(io.crate.common.unit.TimeValue)

Example 17 with ClusterStateObserver

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

the class ShardStateAction method sendShardAction.

private void sendShardAction(final String actionName, final ClusterState currentState, final TransportRequest request, final ActionListener<Void> listener) {
    ClusterStateObserver observer = new ClusterStateObserver(currentState, clusterService, null, LOGGER);
    DiscoveryNode masterNode = currentState.nodes().getMasterNode();
    Predicate<ClusterState> changePredicate = MasterNodeChangePredicate.build(currentState);
    if (masterNode == null) {
        LOGGER.warn("no master known for action [{}] for shard entry [{}]", actionName, request);
        waitForNewMasterAndRetry(actionName, observer, request, listener, changePredicate);
    } else {
        LOGGER.debug("sending [{}] to [{}] for shard entry [{}]", actionName, masterNode.getId(), request);
        transportService.sendRequest(masterNode, actionName, request, new EmptyTransportResponseHandler(ThreadPool.Names.SAME) {

            @Override
            public void handleResponse(TransportResponse.Empty response) {
                listener.onResponse(null);
            }

            @Override
            public void handleException(TransportException exp) {
                if (isMasterChannelException(exp)) {
                    waitForNewMasterAndRetry(actionName, observer, request, listener, changePredicate);
                } else {
                    LOGGER.warn(new ParameterizedMessage("unexpected failure while sending request [{}] to [{}] for shard entry [{}]", actionName, masterNode, request), exp);
                    listener.onFailure(exp instanceof RemoteTransportException ? (Exception) (exp.getCause() instanceof Exception ? exp.getCause() : new ElasticsearchException(exp.getCause())) : exp);
                }
            }
        });
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ElasticsearchException(org.elasticsearch.ElasticsearchException) TransportResponse(org.elasticsearch.transport.TransportResponse) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) TransportException(org.elasticsearch.transport.TransportException) ElasticsearchException(org.elasticsearch.ElasticsearchException) NodeClosedException(org.elasticsearch.node.NodeClosedException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) FailedToCommitClusterStateException(org.elasticsearch.cluster.coordination.FailedToCommitClusterStateException) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) TransportException(org.elasticsearch.transport.TransportException) NotMasterException(org.elasticsearch.cluster.NotMasterException) IOException(java.io.IOException) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) EmptyTransportResponseHandler(org.elasticsearch.transport.EmptyTransportResponseHandler)

Aggregations

ClusterState (org.elasticsearch.cluster.ClusterState)17 ClusterStateObserver (org.elasticsearch.cluster.ClusterStateObserver)17 IOException (java.io.IOException)9 TimeValue (io.crate.common.unit.TimeValue)8 ClusterService (org.elasticsearch.cluster.service.ClusterService)8 ThreadPool (org.elasticsearch.threadpool.ThreadPool)8 Predicate (java.util.function.Predicate)7 ActionListener (org.elasticsearch.action.ActionListener)7 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)6 ClusterStateUpdateTask (org.elasticsearch.cluster.ClusterStateUpdateTask)6 NodeClosedException (org.elasticsearch.node.NodeClosedException)6 TransportService (org.elasticsearch.transport.TransportService)6 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)5 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)5 IndexNameExpressionResolver (org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)5 Metadata (org.elasticsearch.cluster.metadata.Metadata)5 Settings (org.elasticsearch.common.settings.Settings)5 TimeValue (org.elasticsearch.common.unit.TimeValue)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 Consumer (java.util.function.Consumer)4