Elasticsearch's ElectMasterService: A Deep Dive

Elasticsearch’s ElectMasterService: A Deep Dive

In this article, we will delve into the inner workings of Elasticsearch’s ElectMasterService, a critical component of the ZenDiscovery module. The ElectMasterService is responsible for electing a new master node in a cluster, ensuring high availability and fault tolerance.

Sequence of Events

The sequence of events in the ElectMasterService can be broken down into the following steps:

  1. Discovery: The ZenDiscovery module discovers potential master nodes in the cluster.
  2. Master Election: The ElectMasterService evaluates the candidates and elects a new master node.
  3. Cluster Formation: The newly elected master node forms a cluster with the other nodes in the cluster.

ElectMasterService Class

The ElectMasterService class is responsible for electing a new master node. It is configured to read the discovery.zen.minimum_master_nodes variable, which determines the minimum number of master nodes required for a cluster to be considered stable.

public class ElectMasterService {
    private static final Logger logger = LogManager.getLogger(ElectMasterService.class);

    public static final Setting<Integer> DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING =
        Setting.intSetting("discovery.zen.minimum_master_nodes", -1, Property.Dynamic, Property.NodeScope, Property.Deprecated);

    private volatile int minimumMasterNodes;

    // ...
}

MasterCandidate Class

The MasterCandidate class represents a potential master node in the cluster. It has two main properties: node and clusterStateVersion. The node property represents the DiscoveryNode object, while the clusterStateVersion property represents the cluster state version.

public static class MasterCandidate {
    public static final long UNRECOVERED_CLUSTER_VERSION = -1;

    final DiscoveryNode node;
    final long clusterStateVersion;

    public MasterCandidate(DiscoveryNode node, long clusterStateVersion) {
        Objects.requireNonNull(node);
        assert clusterStateVersion >= -1 : "got: " + clusterStateVersion;
        assert node.isMasterNode();
        this.node = node;
        this.clusterStateVersion = clusterStateVersion;
    }

    // ...
}

ElectMaster Method

The electMaster method is responsible for electing a new master node. It first determines whether there are sufficient candidates, then sorts them, and finally returns the first candidate as the master.

public MasterCandidate electMaster(Collection<MasterCandidate> candidates) {
    assert hasEnoughCandidates(candidates);
    List<MasterCandidate> sortedCandidates = new ArrayList<>(candidates);
    sortedCandidates.sort(MasterCandidate::compare);
    return sortedCandidates.get(0);
}

ZenDiscovery.findMaster Method

The ZenDiscovery.findMaster method is responsible for finding a master node in the cluster. It uses the ElectMasterService to elect a new master node.

private DiscoveryNode findMaster() {
    // ...
    List<ElectMasterService.MasterCandidate> masterCandidates = new ArrayList<>();
    for (ZenPing.PingResponse pingResponse : pingResponses) {
        if (pingResponse.node().isMasterNode()) {
            masterCandidates.add(new ElectMasterService.MasterCandidate(pingResponse.node(), pingResponse.getClusterStateVersion()));
        }
    }
    if (activeMasters.isEmpty()) {
        if (electMaster.hasEnoughCandidates(masterCandidates)) {
            final ElectMasterService.MasterCandidate winner = electMaster.electMaster(masterCandidates);
            logger.trace("Candidate {} won election", winner);
            return winner.getNode();
        } else {
            // ...
        }
    } else {
        // ...
    }
}

Conclusion

In conclusion, the ElectMasterService is a critical component of the ZenDiscovery module in Elasticsearch. It is responsible for electing a new master node in a cluster, ensuring high availability and fault tolerance. The ElectMasterService class uses the MasterCandidate class to represent potential master nodes, and the electMaster method to elect a new master node. The ZenDiscovery.findMaster method uses the ElectMasterService to find a master node in the cluster.