java.lang.Object
org.elasticsearch.cluster.coordination.MasterHistory
All Implemented Interfaces:
ClusterStateListener

public class MasterHistory extends Object implements ClusterStateListener
This class represents a node's view of the history of which nodes have been elected master over the last 30 minutes. It is kept in memory, so when a node comes up it does not have any knowledge of previous master history before that point. This object is updated if and when the cluster state changes with a new master node.
  • Field Details

    • MAX_HISTORY_SIZE

      public static final int MAX_HISTORY_SIZE
      This is the maximum number of master nodes kept in history so that the list doesn't grow extremely large and impact performance if things become really unstable. We don't get additional any value in keeping more than this.
      See Also:
    • MAX_HISTORY_AGE_SETTING

      public static final Setting<TimeValue> MAX_HISTORY_AGE_SETTING
  • Constructor Details

  • Method Details

    • getMaxHistoryAge

      public TimeValue getMaxHistoryAge()
    • clusterChanged

      public void clusterChanged(ClusterChangedEvent event)
      Description copied from interface: ClusterStateListener
      Called when cluster state changes.

      Cluster states are applied one-by-one which means they can be a performance bottleneck. Implementations of this method should therefore be fast, so please consider forking work into the background rather than doing everything inline.

      Specified by:
      clusterChanged in interface ClusterStateListener
    • getMostRecentMaster

      @Nullable public DiscoveryNode getMostRecentMaster()
      Returns the node that has been most recently seen as the master
      Returns:
      The node that has been most recently seen as the master, which could be null if no master exists
    • getMostRecentNonNullMaster

      @Nullable public DiscoveryNode getMostRecentNonNullMaster()
      Returns the most recent non-null master seen, or null if there has been no master seen. Only 30 minutes of history is kept. If the most recent master change is more than 30 minutes old and that change was to set the master to null, then null will be returned.
      Returns:
      The most recent non-null master seen, or null if there has been no master seen.
    • hasMasterGoneNullAtLeastNTimes

      public boolean hasMasterGoneNullAtLeastNTimes(int n)
      Returns true if for the life of this MasterHistory (30 minutes) non-null masters have transitioned to null n times.
      Parameters:
      n - The number of times a non-null master must have switched to null
      Returns:
      True if non-null masters have transitioned to null n or more times.
    • hasMasterGoneNullAtLeastNTimes

      public static boolean hasMasterGoneNullAtLeastNTimes(List<DiscoveryNode> masters, int n)
      Returns true if for the List of master nodes passed in, non-null masters have transitioned to null n times. So for example: node1 -> null is 1 transition to null node1 -> null -> null is 1 transition to null null -> node1 -> null is 1 transition to null node1 -> null -> node1 is 1 transition to null node1 -> null -> node1 -> null is 2 transitions to null node1 -> null -> node2 -> null is 2 transitions to null
      Parameters:
      masters - The List of masters to use
      n - The number of times a non-null master must have switched to null
      Returns:
      True if non-null masters have transitioned to null n or more timesin the given list of masters.
    • getNumberOfMasterIdentityChanges

      public static int getNumberOfMasterIdentityChanges(List<DiscoveryNode> masterHistory)
      An identity change is when we get notified of a change to a non-null master that is different from the previous non-null master. Note that a master changes to null on (virtually) every identity change. So for example: node1 -> node2 is 1 identity change node1 -> node2 -> node1 is 2 identity changes node1 -> node2 -> node2 is 1 identity change (transitions from a node to itself do not count) node1 -> null -> node1 is 0 identity changes (transitions from a node to itself, even with null in the middle, do not count) node1 -> null -> node2 is 1 identity change
      Parameters:
      masterHistory - The list of nodes that have been master
      Returns:
      The number of master identity changes as defined above
    • hasSeenMasterInLastNSeconds

      public boolean hasSeenMasterInLastNSeconds(int nSeconds)
      Returns true if a non-null master existed at any point in the last nSeconds seconds. Note that this could be a master whose start time was more than nSeconds ago, as long as either it is still master or the next master took over less than nSeconds ago.
      Parameters:
      nSeconds - The number of seconds to look back
      Returns:
      true if the current master is non-null or if a non-null master was seen in the last nSeconds seconds
    • getRawNodes

      public List<DiscoveryNode> getRawNodes()
      This method returns an immutable view of this master history, typically for sending over the wire to another node. The returned List is ordered by when the master was seen, with the earliest-seen masters being first. The List can contain null values. Times are intentionally not included because they cannot be compared across machines. This list contains nodes even if they are not currently in the cluster.
      Returns:
      An immutable view of this master history
    • getNodes

      public List<DiscoveryNode> getNodes()