Class Learner
java.lang.Object
org.apache.zookeeper.server.quorum.Learner
This class is the superclass of two of the three main actors in a ZK
ensemble: Followers and Observers. Both Followers and Observers share
a good deal of code which is moved into Peer to avoid duplication.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected BufferedOutputStreamstatic final booleanprotected MultipleAddressesprotected InputArchiveprotected OutputArchiveprotected intthe protocol version of the leaderstatic final Stringstatic final Stringprotected static final org.slf4j.Loggerprotected final MessageTrackerprotected Socketprotected AtomicBoolean -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidconnectToLeader(MultipleAddresses multiAddr, String hostname) Establish a connection with the LearnerMaster found by findLearnerMaster.protected SocketCreating a simple or and SSL socket.protected QuorumPeer.QuorumServerReturns the address of the node we think is the leader.protected static booleanintSocket getterprotected longnanoTime()Overridable helper method to return the System.nanoTime().protected voidping(QuorumPacket qp) protected longregisterWithLeader(int pktType) Once connected to the leader or learner master, perform the handshake protocol to establish a following / observing connection.protected voidprotected static voidsetAsyncSending(boolean newMode) voidshutdown()Shutdown the Peerprotected voidsockConnect(Socket sock, InetSocketAddress addr, int timeout) Overridable helper method to simply call sock.connect().protected voidStart thread that will forward any packet in the queue to the leaderprotected voidsyncWithLeader(long newLeaderZxid) Finally, synchronize our history with the Leader (if Follower) or the LearnerMaster (if Observer).
-
Field Details
-
bufferedOutput
-
sock
-
leaderAddr
-
sockBeingClosed
-
leaderIs
-
leaderOs
-
leaderProtocolVersion
protected int leaderProtocolVersionthe protocol version of the leader -
messageTracker
-
LOG
protected static final org.slf4j.Logger LOG -
LEARNER_ASYNC_SENDING
- See Also:
-
LEARNER_CLOSE_SOCKET_ASYNC
- See Also:
-
closeSocketAsync
public static final boolean closeSocketAsync
-
-
Constructor Details
-
Learner
public Learner()
-
-
Method Details
-
getSocket
Socket getter -
getPendingRevalidationsCount
public int getPendingRevalidationsCount() -
setAsyncSending
protected static void setAsyncSending(boolean newMode) -
getAsyncSending
protected static boolean getAsyncSending() -
startSendingThread
protected void startSendingThread()Start thread that will forward any packet in the queue to the leader -
findLeader
Returns the address of the node we think is the leader. -
nanoTime
protected long nanoTime()Overridable helper method to return the System.nanoTime(). This method behaves identical to System.nanoTime(). -
sockConnect
Overridable helper method to simply call sock.connect(). This can be overriden in tests to fake connection success/failure for connectToLeader.- Throws:
IOException
-
connectToLeader
Establish a connection with the LearnerMaster found by findLearnerMaster. Followers only connect to Leaders, Observers can connect to any active LearnerMaster. Retries until either initLimit time has elapsed or 5 tries have happened.- Parameters:
multiAddr- - the address of the Peer to connect to.- Throws:
IOException- - if the socket connection fails on the 5th attempt if there is an authentication failure while connecting to leader
-
createSocket
Creating a simple or and SSL socket. This can be overridden in tests to fake already connected sockets for connectToLeader.- Throws:
X509ExceptionIOException
-
registerWithLeader
Once connected to the leader or learner master, perform the handshake protocol to establish a following / observing connection.- Parameters:
pktType-- Returns:
- the zxid the Leader sends for synchronization purposes.
- Throws:
IOException
-
syncWithLeader
Finally, synchronize our history with the Leader (if Follower) or the LearnerMaster (if Observer).- Parameters:
newLeaderZxid-- Throws:
IOExceptionInterruptedExceptionException
-
revalidate
- Throws:
IOException
-
ping
- Throws:
IOException
-
shutdown
public void shutdown()Shutdown the Peer
-