Class ZkShardTerms

  • All Implemented Interfaces:
    AutoCloseable

    public class ZkShardTerms
    extends Object
    implements AutoCloseable
    Class used for interact with a ZK term node. Each ZK term node relates to a shard of a collection and have this format (in json)

    { "replicaNodeName1" : 1, "replicaNodeName2" : 2, .. }

    The values correspond to replicas are called terms. Only replicas with highest term value are considered up to date and be able to become leader and serve queries.

    Terms can only updated in two strict ways:

    • A replica sets its term equals to leader's term
    • The leader increase its term and some other replicas by 1
    This class should not be reused after Watcher.Event.KeeperState.Expired event
    • Method Detail

      • ensureTermsIsHigher

        public void ensureTermsIsHigher​(String leader,
                                        Set<String> replicasNeedingRecovery)
        Ensure that terms are higher than some replica's terms. If the current leader is attempting to give up leadership and included in replicasNeedingRecovery, then other replicas that are in sync will have higher terms, while the leader will stay where it is.
        Parameters:
        leader - coreNodeName of leader
        replicasNeedingRecovery - set of replicas in which their terms should be lower than leader's term
      • getShardTerms

        public ShardTerms getShardTerms()
      • canBecomeLeader

        public boolean canBecomeLeader​(String coreNodeName)
        Can this replica become leader?
        Parameters:
        coreNodeName - of the replica
        Returns:
        true if this replica can become leader, false if otherwise
      • skipSendingUpdatesTo

        public boolean skipSendingUpdatesTo​(String coreNodeName)
        Should leader skip sending updates to this replica?
        Parameters:
        coreNodeName - of the replica
        Returns:
        true if this replica has term equals to leader's term, false if otherwise
      • registered

        public boolean registered​(String coreNodeName)
        Did this replica registered its term? This is a sign to check f
        Parameters:
        coreNodeName - of the replica
        Returns:
        true if this replica registered its term, false if otherwise
      • setTermEqualsToLeader

        public void setTermEqualsToLeader​(String coreNodeName)
        Set a replica's term equals to leader's term, and remove recovering flag of a replica. This call should only be used by CollectionParams.CollectionAction.FORCELEADER
        Parameters:
        coreNodeName - of the replica
      • setTermToZero

        public void setTermToZero​(String coreNodeName)
        Set a replica's term to 0. If the term does not exist, create it.
        Parameters:
        coreNodeName - of the replica
      • startRecovering

        public void startRecovering​(String coreNodeName)
        Mark coreNodeName as recovering
      • doneRecovering

        public void doneRecovering​(String coreNodeName)
        Mark coreNodeName as finished recovering
      • isRecovering

        public boolean isRecovering​(String name)
      • ensureHighestTermsAreNotZero

        public void ensureHighestTermsAreNotZero()
        When first updates come in, all replicas have some data now, so we must switch from term 0 (registered) to 1 (have some data)
      • getHighestTerm

        public long getHighestTerm()
      • getTerm

        public long getTerm​(String coreNodeName)
      • refreshTerms

        public void refreshTerms()
        Fetch latest terms from ZK