Class Leader
- java.lang.Object
-
- org.apache.zookeeper.server.quorum.Leader
-
public class Leader extends java.lang.Object
This class has the control logic for the Leader.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Leader.Proposal
static class
Leader.XidRolloverException
-
Field Summary
Fields Modifier and Type Field Description static int
ACKEPOCH
This message is used by the follow to ack a proposed epoch.protected java.util.Set<java.lang.Long>
connectingFollowers
protected java.util.Set<java.lang.Long>
electingFollowers
protected boolean
electionFinished
static int
LEADERINFO
This message is the first that a follower receives from the leader.protected Leader.Proposal
newLeaderProposal
static int
PROPOSAL
This message type is sent by a leader to propose a mutation.protected boolean
quorumFormed
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
commit(long zxid)
Create a commit packet and send it to all the members of the quorumvoid
commitAndActivate(long zxid, long designatedLeader)
LearnerSnapshotThrottler
createLearnerSnapshotThrottler(int maxConcurrentSnapshots, long maxConcurrentSnapshotTimeout)
long
getEpoch()
Returns the current epoch of the leader.long
getEpochToPropose(long sid, long lastAcceptedEpoch)
java.util.List<LearnerHandler>
getForwardingFollowers()
Returns a copy of the current forwarding follower snapshotjava.util.List<LearnerHandler>
getLearners()
Returns a copy of the current learner snapshotLearnerSnapshotThrottler
getLearnerSnapshotThrottler()
int
getNumPendingSyncs()
java.util.List<LearnerHandler>
getObservingLearners()
Returns a copy of the current observer snapshotstatic java.lang.String
getPacketType(int packetType)
Get string representation of a given packet typeBufferStats
getProposalStats()
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&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 otherwisevoid
processAck(long sid, long zxid, java.net.SocketAddress followerAddr)
Keep a count of acks that are received by the leader for a particular proposalvoid
processSync(LearnerSyncRequest r)
Process sync requestsLeader.Proposal
propose(Request request)
create a proposal and send it out to all the membersvoid
sendSync(LearnerSyncRequest r)
Sends a sync message to the appropriate serverlong
startForwarding(LearnerHandler handler, long lastSeenZxid)
lets the leader know that a follower is capable of following and is done syncingboolean
tryToCommit(Leader.Proposal p, long zxid, java.net.SocketAddress followerAddr)
void
waitForEpochAck(long id, StateSummary ss)
void
waitForNewLeaderAck(long sid, long zxid)
Process NEWLEADER ack of a given sid and wait until the leader receives sufficient acks.
-
-
-
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 java.util.Set<java.lang.Long> connectingFollowers
-
electingFollowers
protected final java.util.Set<java.lang.Long> electingFollowers
-
electionFinished
protected boolean electionFinished
-
-
Method Detail
-
getProposalStats
public BufferStats getProposalStats()
-
createLearnerSnapshotThrottler
public LearnerSnapshotThrottler createLearnerSnapshotThrottler(int maxConcurrentSnapshots, long maxConcurrentSnapshotTimeout)
-
getLearners
public java.util.List<LearnerHandler> getLearners()
Returns a copy of the current learner snapshot
-
getForwardingFollowers
public java.util.List<LearnerHandler> getForwardingFollowers()
Returns a copy of the current forwarding follower snapshot
-
getObservingLearners
public java.util.List<LearnerHandler> getObservingLearners()
Returns a copy of the current observer snapshot
-
getNumPendingSyncs
public int getNumPendingSyncs()
-
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
- , a QuorumVerifier
-
tryToCommit
public boolean tryToCommit(Leader.Proposal p, long zxid, java.net.SocketAddress followerAddr)
- Returns:
- True if committed, otherwise false.
-
processAck
public void processAck(long sid, long zxid, java.net.SocketAddress followerAddr)
Keep a count of acks that are received by the leader for a particular proposal- Parameters:
zxid
- , the zxid of the proposal sent outsid
- , the id of the server that sent the ackfollowerAddr
-
-
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.
-
informAndActivate
public void informAndActivate(Leader.Proposal proposal, long designatedLeader)
Create an inform&activate packet and send it to all observers.
-
getEpoch
public long getEpoch()
Returns the current epoch of the leader.- Returns:
-
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
-
getLearnerSnapshotThrottler
public LearnerSnapshotThrottler getLearnerSnapshotThrottler()
-
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- Returns:
- last proposed zxid
- Throws:
java.lang.InterruptedException
-
getEpochToPropose
public long getEpochToPropose(long sid, long lastAcceptedEpoch) throws java.lang.InterruptedException, java.io.IOException
- Throws:
java.lang.InterruptedException
java.io.IOException
-
waitForEpochAck
public void waitForEpochAck(long id, StateSummary ss) throws java.io.IOException, java.lang.InterruptedException
- Throws:
java.io.IOException
java.lang.InterruptedException
-
waitForNewLeaderAck
public void waitForNewLeaderAck(long sid, long zxid) throws java.lang.InterruptedException
Process NEWLEADER ack of a given sid and wait until the leader receives sufficient acks.- Parameters:
sid
-- Throws:
java.lang.InterruptedException
-
getPacketType
public static java.lang.String getPacketType(int packetType)
Get string representation of a given packet type- Parameters:
packetType
-- Returns:
- string representing the packet type
-
-