Leader (Apache ZooKeeper - Server 3.7.0 API)
Date: 2021-03-17

org.apache.zookeeper.server.quorum

## Class Leader

- java.lang.Object
- - org.apache.zookeeper.server.quorum.LearnerMaster
  - - org.apache.zookeeper.server.quorum.Leader

-     public class Leader
      extends LearnerMaster

  This class has the control logic for the Leader.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `Leader.Proposal`
    `static class` | `Leader.XidRolloverException`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `ACKEPOCH` This message is used by the follow to ack a proposed epoch.
    `protected Set<Long>` | `connectingFollowers`
    `protected Set<Long>` | `electingFollowers`
    `protected boolean` | `electionFinished`
    `static int` | `LEADERINFO` This message is the first that a follower receives from the leader.
    `static String` | `MAX_TIME_TO_WAIT_FOR_EPOCH`
    `protected Leader.Proposal` | `newLeaderProposal`
    `static int` | `PROPOSAL` This message type is sent by a leader to propose a mutation.
    `protected boolean` | `quorumFormed`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Leader(QuorumPeer self, LeaderZooKeeperServer zk)`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addLearnerHandler(LearnerHandler learner)` Adds peer to the leader.
    `static QuorumPacket` | `buildInformAndActivePacket(long zxid, long designatedLeader, byte[] proposalData)`
    `void` | `commit(long zxid)` Create a commit packet and send it to all the members of the quorum
    `void` | `commitAndActivate(long zxid, long designatedLeader)`
    `static int` | `getAckLoggingFrequency()`
    `long` | `getAndDecrementFollowerCounter()` decrement follower count
    `int` | `getCurrentTick()` the current tick
    `long` | `getEpoch()` Returns the current epoch of the leader.
    `long` | `getEpochToPropose(long sid, long lastAcceptedEpoch)` get the first zxid of the next epoch
    `List<LearnerHandler>` | `getForwardingFollowers()` Returns a copy of the current forwarding follower snapshot
    `long` | `getLastProposed()` last proposed zxid
    `List<LearnerHandler>` | `getLearners()` Returns a copy of the current learner snapshot
    `List<LearnerHandler>` | `getNonVotingFollowers()`
    `int` | `getNumPendingSyncs()`
    `List<LearnerHandler>` | `getObservingLearners()` Returns a copy of the current observer snapshot
    `Iterable<Map<String,Object>>` | `getObservingLearnersInfo()`
    `static String` | `getPacketType(int packetType)` Get string representation of a given packet type
    `String` | `getPeerInfo(long sid)`
    `BufferStats` | `getProposalStats()`
    `QuorumAuthServer` | `getQuorumAuthServer()`
    `byte[]` | `getQuorumVerifierBytes()` identifier of current quorum verifier for new leader
    `long` | `getQuorumVerifierVersion()` version of current quorum verifier
    `int` | `getTickOfInitialAckDeadline()` next deadline tick marking observer sync (steady state)
    `int` | `getTickOfNextAckDeadline()` deadline tick marking observer sync (initial)
    `long` | `getUptime()`
    `ZKDatabase` | `getZKDatabase()` ZKDatabase
    `void` | `inform(Leader.Proposal proposal)` Create an inform packet and send it to all observers.
    `void` | `informAndActivate(Leader.Proposal proposal, long designatedLeader)` Create an inform and activate packet and send it to all observers.
    `boolean` | `isQuorumSynced(QuorumVerifier qv)` Returns true if a quorum in qv is connected and synced with the leader and false otherwise
    `void` | `processAck(long sid, long zxid, SocketAddress followerAddr)` Keep a count of acks that are received by the leader for a particular proposal
    `void` | `processSync(LearnerSyncRequest r)` Process sync requests
    `Leader.Proposal` | `propose(Request request)` create a proposal and send it out to all the members
    `void` | `registerLearnerHandlerBean(LearnerHandler learnerHandler, Socket socket)` registers the handler's bean
    `void` | `removeLearnerHandler(LearnerHandler peer)` Remove the learner from the learner list
    `void` | `reportLookingSid(long sid)`
    `void` | `resetObserverConnectionStats()`
    `void` | `revalidateSession(QuorumPacket qp, LearnerHandler learnerHandler)` handle revalidate packet
    `void` | `sendSync(LearnerSyncRequest r)` Sends a sync message to the appropriate server
    `static void` | `setAckLoggingFrequency(int frequency)`
    `void` | `setLeadingVoteSet(SyncedLearnerTracker voteSet)`
    `static void` | `setMaxTimeToWaitForEpoch(int maxTimeToWaitForEpoch)`
    `long` | `startForwarding(LearnerHandler handler, long lastSeenZxid)` lets the leader know that a follower is capable of following and is done syncing
    `void` | `submitLearnerRequest(Request si)` proxy request from learner to server
    `int` | `syncTimeout()` time allowed for sync response
    `void` | `touch(long sess, int to)` mark session as alive
    `boolean` | `tryToCommit(Leader.Proposal p, long zxid, SocketAddress followerAddr)`
    `void` | `unregisterLearnerHandlerBean(LearnerHandler learnerHandler)` unregisters the handler's bean
    `void` | `waitForEpochAck(long id, StateSummary ss)` wait for the leader of the new epoch to be confirmed by followers
    `void` | `waitForNewLeaderAck(long sid, long zxid)` Process NEWLEADER ack of a given sid and wait until the leader receives sufficient acks.
    `void` | `waitForStartup()` wait for server to start

    - ### Methods inherited from class org.apache.zookeeper.server.quorum.LearnerMaster

      `getLearnerDiffSyncThrottler, getLearnerSnapSyncThrottler, getMaxConcurrentDiffSyncs, getMaxConcurrentSnapSyncs, setMaxConcurrentDiffSyncs, setMaxConcurrentSnapSyncs`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### quorumFormed

          protected boolean quorumFormed

    - #### LEADERINFO

          public static final int LEADERINFO

      This message is the first that a follower receives from the leader. It has the protocol version and the epoch of the leader.
      See Also:
        Constant Field Values

    - #### ACKEPOCH

          public static final int ACKEPOCH

      This message is used by the follow to ack a proposed epoch.
      See Also:
        Constant Field Values

    - #### PROPOSAL

          public static final int PROPOSAL

      This message type is sent by a leader to propose a mutation.
      See Also:
        Constant Field Values

    - #### newLeaderProposal

          protected final Leader.Proposal newLeaderProposal

    - #### connectingFollowers

          protected final Set<Long> connectingFollowers

    - #### MAX_TIME_TO_WAIT_FOR_EPOCH

          public static final String MAX_TIME_TO_WAIT_FOR_EPOCH

      See Also:
        Constant Field Values

    - #### electingFollowers

          protected final Set<Long> electingFollowers

    - #### electionFinished

          protected boolean electionFinished

  - ### Constructor Detail

    - #### Leader

          public Leader(QuorumPeer self,
                        LeaderZooKeeperServer zk)
                 throws IOException

      Throws:
        `IOException`

  - ### Method Detail

    - #### setAckLoggingFrequency

          public static void setAckLoggingFrequency(int frequency)

    - #### getAckLoggingFrequency

          public static int getAckLoggingFrequency()

    - #### getProposalStats

          public BufferStats getProposalStats()

    - #### getLearners

          public List<LearnerHandler> getLearners()

      Returns a copy of the current learner snapshot

    - #### getForwardingFollowers

          public List<LearnerHandler> getForwardingFollowers()

      Returns a copy of the current forwarding follower snapshot

    - #### getNonVotingFollowers

          public List<LearnerHandler> getNonVotingFollowers()

    - #### getObservingLearners

          public List<LearnerHandler> getObservingLearners()

      Returns a copy of the current observer snapshot

    - #### getObservingLearnersInfo

          public Iterable<Map<String,Object>> getObservingLearnersInfo()

    - #### resetObserverConnectionStats

          public void resetObserverConnectionStats()

    - #### getNumPendingSyncs

          public int getNumPendingSyncs()

    - #### addLearnerHandler

          public void addLearnerHandler(LearnerHandler learner)

      Adds peer to the leader.
      Parameters:
        `learner` - instance of learner handle

    - #### removeLearnerHandler

          public void removeLearnerHandler(LearnerHandler peer)

      Remove the learner from the learner list
      Parameters:
        `peer` -

    - #### isQuorumSynced

          public boolean isQuorumSynced(QuorumVerifier qv)

      Returns true if a quorum in qv is connected and synced with the leader and false otherwise
      Parameters:
        `qv` - is a QuorumVerifier

    - #### getUptime

          public long getUptime()

    - #### tryToCommit

          public boolean tryToCommit(Leader.Proposal p,
                                     long zxid,
                                     SocketAddress followerAddr)

      Returns:
        True if committed, otherwise false.

    - #### processAck

          public void processAck(long sid,
                                 long zxid,
                                 SocketAddress followerAddr)

      Keep a count of acks that are received by the leader for a particular proposal
      Parameters:
        `sid` - is the id of the server that sent the ack
        `zxid` - is the zxid of the proposal sent out
        `followerAddr` -

    - #### commit

          public void commit(long zxid)

      Create a commit packet and send it to all the members of the quorum
      Parameters:
        `zxid` -

    - #### commitAndActivate

          public void commitAndActivate(long zxid,
                                        long designatedLeader)

    - #### inform

          public void inform(Leader.Proposal proposal)

      Create an inform packet and send it to all observers.

    - #### buildInformAndActivePacket

          public static QuorumPacket buildInformAndActivePacket(long zxid,
                                                                long designatedLeader,
                                                                byte[] proposalData)

    - #### informAndActivate

          public void informAndActivate(Leader.Proposal proposal,
                                        long designatedLeader)

      Create an inform and activate packet and send it to all observers.

    - #### getLastProposed

          public long getLastProposed()

      Description copied from class: `LearnerMaster`
      last proposed zxid
      Returns:
        last proposed zxid

    - #### getEpoch

          public long getEpoch()

      Returns the current epoch of the leader.

    - #### propose

          public Leader.Proposal propose(Request request)
                                  throws Leader.XidRolloverException

      create a proposal and send it out to all the members
      Parameters:
        `request` -
      Returns:
        the proposal that is queued to send to all the members
      Throws:
        `Leader.XidRolloverException`

    - #### processSync

          public void processSync(LearnerSyncRequest r)

      Process sync requests
      Parameters:
        `r` - the request

    - #### sendSync

          public void sendSync(LearnerSyncRequest r)

      Sends a sync message to the appropriate server

    - #### startForwarding

          public long startForwarding(LearnerHandler handler,
                                      long lastSeenZxid)

      lets the leader know that a follower is capable of following and is done syncing
      Parameters:
        `handler` - handler of the follower
        `lastSeenZxid` - zxid of learner
      Returns:
        last proposed zxid

    - #### waitForStartup

          public void waitForStartup()
                              throws InterruptedException

      Description copied from class: `LearnerMaster`
      wait for server to start
      Throws:
        `InterruptedException`

    - #### setMaxTimeToWaitForEpoch

          public static void setMaxTimeToWaitForEpoch(int maxTimeToWaitForEpoch)

    - #### setLeadingVoteSet

          public void setLeadingVoteSet(SyncedLearnerTracker voteSet)

    - #### reportLookingSid

          public void reportLookingSid(long sid)

    - #### getEpochToPropose

          public long getEpochToPropose(long sid,
                                        long lastAcceptedEpoch)
                                 throws InterruptedException,
                                        IOException

      Description copied from class: `LearnerMaster`
      get the first zxid of the next epoch
      Parameters:
        `sid` - learner id
      Returns:
        the first zxid of the next epoch
      Throws:
        `InterruptedException`
        `IOException`

    - #### getZKDatabase

          public ZKDatabase getZKDatabase()

      Description copied from class: `LearnerMaster`
      ZKDatabase
      Returns:
        ZKDatabase

    - #### waitForEpochAck

          public void waitForEpochAck(long id,
                                      StateSummary ss)
                               throws IOException,
                                      InterruptedException

      Description copied from class: `LearnerMaster`
      wait for the leader of the new epoch to be confirmed by followers
      Parameters:
        `id` - learner id
      Throws:
        `IOException`
        `InterruptedException`

    - #### waitForNewLeaderAck

          public void waitForNewLeaderAck(long sid,
                                          long zxid)
                                   throws InterruptedException

      Process NEWLEADER ack of a given sid and wait until the leader receives sufficient acks.
      Parameters:
        `sid` -
        `zxid` - zxid at learner
      Throws:
        `InterruptedException`

    - #### getPacketType

          public static String getPacketType(int packetType)

      Get string representation of a given packet type
      Parameters:
        `packetType` -
      Returns:
        string representing the packet type

    - #### getCurrentTick

          public int getCurrentTick()

      Description copied from class: `LearnerMaster`
      the current tick
      Returns:
        the current tick

    - #### syncTimeout

          public int syncTimeout()

      Description copied from class: `LearnerMaster`
      time allowed for sync response
      Returns:
        time allowed for sync response

    - #### getTickOfNextAckDeadline

          public int getTickOfNextAckDeadline()

      Description copied from class: `LearnerMaster`
      deadline tick marking observer sync (initial)
      Returns:
        deadline tick marking observer sync (initial)

    - #### getTickOfInitialAckDeadline

          public int getTickOfInitialAckDeadline()

      Description copied from class: `LearnerMaster`
      next deadline tick marking observer sync (steady state)
      Returns:
        next deadline tick marking observer sync (steady state)

    - #### getAndDecrementFollowerCounter

          public long getAndDecrementFollowerCounter()

      Description copied from class: `LearnerMaster`
      decrement follower count
      Returns:
        previous follower count

    - #### touch

          public void touch(long sess,
                            int to)

      Description copied from class: `LearnerMaster`
      mark session as alive
      Parameters:
        `sess` - session id
        `to` - timeout

    - #### submitLearnerRequest

          public void submitLearnerRequest(Request si)

      Description copied from class: `LearnerMaster`
      proxy request from learner to server
      Parameters:
        `si` - request

    - #### getQuorumVerifierVersion

          public long getQuorumVerifierVersion()

      Description copied from class: `LearnerMaster`
      version of current quorum verifier
      Returns:
        version of current quorum verifier

    - #### getPeerInfo

          public String getPeerInfo(long sid)

      Parameters:
        `sid` - server id
      Returns:
        server information in the view

    - #### getQuorumVerifierBytes

          public byte[] getQuorumVerifierBytes()

      Description copied from class: `LearnerMaster`
      identifier of current quorum verifier for new leader
      Returns:
        identifier of current quorum verifier for new leader

    - #### getQuorumAuthServer

          public QuorumAuthServer getQuorumAuthServer()

    - #### revalidateSession

          public void revalidateSession(QuorumPacket qp,
                                        LearnerHandler learnerHandler)
                                 throws IOException

      Description copied from class: `LearnerMaster`
      handle revalidate packet
      Parameters:
        `qp` - session packet
        `learnerHandler` - learner
      Throws:
        `IOException`

    - #### registerLearnerHandlerBean

          public void registerLearnerHandlerBean(LearnerHandler learnerHandler,
                                                 Socket socket)

      Description copied from class: `LearnerMaster`
      registers the handler's bean
      Parameters:
        `learnerHandler` - handler
        `socket` - connection to learner

    - #### unregisterLearnerHandlerBean

          public void unregisterLearnerHandlerBean(LearnerHandler learnerHandler)

      Description copied from class: `LearnerMaster`
      unregisters the handler's bean
      Parameters:
        `learnerHandler` - handler

Copyright © 2008–2021 The Apache Software Foundation. All rights reserved.