Search in sources :

Example 1 with AckClusterStatePublishResponseHandler

use of org.elasticsearch.discovery.AckClusterStatePublishResponseHandler in project elasticsearch by elastic.

the class PublishClusterStateAction method publish.

/**
     * publishes a cluster change event to other nodes. if at least minMasterNodes acknowledge the change it is committed and will
     * be processed by the master and the other nodes.
     * <p>
     * The method is guaranteed to throw a {@link org.elasticsearch.discovery.Discovery.FailedToCommitClusterStateException}
     * if the change is not committed and should be rejected.
     * Any other exception signals the something wrong happened but the change is committed.
     */
public void publish(final ClusterChangedEvent clusterChangedEvent, final int minMasterNodes, final Discovery.AckListener ackListener) throws Discovery.FailedToCommitClusterStateException {
    final DiscoveryNodes nodes;
    final SendingController sendingController;
    final Set<DiscoveryNode> nodesToPublishTo;
    final Map<Version, BytesReference> serializedStates;
    final Map<Version, BytesReference> serializedDiffs;
    final boolean sendFullVersion;
    try {
        nodes = clusterChangedEvent.state().nodes();
        nodesToPublishTo = new HashSet<>(nodes.getSize());
        DiscoveryNode localNode = nodes.getLocalNode();
        final int totalMasterNodes = nodes.getMasterNodes().size();
        for (final DiscoveryNode node : nodes) {
            if (node.equals(localNode) == false) {
                nodesToPublishTo.add(node);
            }
        }
        sendFullVersion = !discoverySettings.getPublishDiff() || clusterChangedEvent.previousState() == null;
        serializedStates = new HashMap<>();
        serializedDiffs = new HashMap<>();
        // we build these early as a best effort not to commit in the case of error.
        // sadly this is not water tight as it may that a failed diff based publishing to a node
        // will cause a full serialization based on an older version, which may fail after the
        // change has been committed.
        buildDiffAndSerializeStates(clusterChangedEvent.state(), clusterChangedEvent.previousState(), nodesToPublishTo, sendFullVersion, serializedStates, serializedDiffs);
        final BlockingClusterStatePublishResponseHandler publishResponseHandler = new AckClusterStatePublishResponseHandler(nodesToPublishTo, ackListener);
        sendingController = new SendingController(clusterChangedEvent.state(), minMasterNodes, totalMasterNodes, publishResponseHandler);
    } catch (Exception e) {
        throw new Discovery.FailedToCommitClusterStateException("unexpected error while preparing to publish", e);
    }
    try {
        innerPublish(clusterChangedEvent, nodesToPublishTo, sendingController, sendFullVersion, serializedStates, serializedDiffs);
    } catch (Discovery.FailedToCommitClusterStateException t) {
        throw t;
    } catch (Exception e) {
        // try to fail committing, in cause it's still on going
        if (sendingController.markAsFailed("unexpected error", e)) {
            // signal the change should be rejected
            throw new Discovery.FailedToCommitClusterStateException("unexpected error", e);
        } else {
            throw e;
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Discovery(org.elasticsearch.discovery.Discovery) ElasticsearchException(org.elasticsearch.ElasticsearchException) IncompatibleClusterStateVersionException(org.elasticsearch.cluster.IncompatibleClusterStateVersionException) IOException(java.io.IOException) TransportException(org.elasticsearch.transport.TransportException) Version(org.elasticsearch.Version) BlockingClusterStatePublishResponseHandler(org.elasticsearch.discovery.BlockingClusterStatePublishResponseHandler) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) AckClusterStatePublishResponseHandler(org.elasticsearch.discovery.AckClusterStatePublishResponseHandler)

Aggregations

IOException (java.io.IOException)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 Version (org.elasticsearch.Version)1 IncompatibleClusterStateVersionException (org.elasticsearch.cluster.IncompatibleClusterStateVersionException)1 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)1 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 AckClusterStatePublishResponseHandler (org.elasticsearch.discovery.AckClusterStatePublishResponseHandler)1 BlockingClusterStatePublishResponseHandler (org.elasticsearch.discovery.BlockingClusterStatePublishResponseHandler)1 Discovery (org.elasticsearch.discovery.Discovery)1 TransportException (org.elasticsearch.transport.TransportException)1