Class Learner

  • Direct Known Subclasses:
    Follower, Observer

    public class Learner
    extends java.lang.Object
    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

      Fields 
      Modifier and Type Field Description
      protected java.io.BufferedOutputStream bufferedOutput  
      protected org.apache.jute.InputArchive leaderIs  
      protected org.apache.jute.OutputArchive leaderOs  
      protected int leaderProtocolVersion
      the protocol version of the leader
      protected static org.slf4j.Logger LOG  
      protected java.net.Socket sock  
    • Constructor Summary

      Constructors 
      Constructor Description
      Learner()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void connectToLeader​(java.net.InetSocketAddress addr, java.lang.String hostname)
      Establish a connection with the Leader found by findLeader.
      protected QuorumPeer.QuorumServer findLeader()
      Returns the address of the node we think is the leader.
      int getPendingRevalidationsCount()  
      java.net.Socket getSocket()
      Socket getter
      protected long nanoTime()
      Overridable helper method to return the System.nanoTime().
      protected void ping​(org.apache.zookeeper.server.quorum.QuorumPacket qp)  
      protected long registerWithLeader​(int pktType)
      Once connected to the leader, perform the handshake protocol to establish a following / observing connection.
      protected void revalidate​(org.apache.zookeeper.server.quorum.QuorumPacket qp)  
      void shutdown()
      Shutdown the Peer
      protected void sockConnect​(java.net.Socket sock, java.net.InetSocketAddress addr, int timeout)
      Overridable helper method to simply call sock.connect().
      protected void syncWithLeader​(long newLeaderZxid)
      Finally, synchronize our history with the Leader.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • bufferedOutput

        protected java.io.BufferedOutputStream bufferedOutput
      • sock

        protected java.net.Socket sock
      • leaderIs

        protected org.apache.jute.InputArchive leaderIs
      • leaderOs

        protected org.apache.jute.OutputArchive leaderOs
      • leaderProtocolVersion

        protected int leaderProtocolVersion
        the protocol version of the leader
      • LOG

        protected static final org.slf4j.Logger LOG
    • Constructor Detail

      • Learner

        public Learner()
    • Method Detail

      • getSocket

        public java.net.Socket getSocket()
        Socket getter
        Returns:
      • getPendingRevalidationsCount

        public int getPendingRevalidationsCount()
      • findLeader

        protected QuorumPeer.QuorumServer 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

        protected void sockConnect​(java.net.Socket sock,
                                   java.net.InetSocketAddress addr,
                                   int timeout)
                            throws java.io.IOException
        Overridable helper method to simply call sock.connect(). This can be overriden in tests to fake connection success/failure for connectToLeader.
        Throws:
        java.io.IOException
      • connectToLeader

        protected void connectToLeader​(java.net.InetSocketAddress addr,
                                       java.lang.String hostname)
                                throws java.io.IOException,
                                       java.lang.InterruptedException,
                                       X509Exception
        Establish a connection with the Leader found by findLeader. Retries until either initLimit time has elapsed or 5 tries have happened.
        Parameters:
        addr - - the address of the Leader to connect to.
        Throws:
        java.io.IOException - - if the socket connection fails on the 5th attempt
      • if there is an authentication failure while connecting to leader
      • java.net.ConnectException
        java.lang.InterruptedException
        X509Exception
      • registerWithLeader

        protected long registerWithLeader​(int pktType)
                                   throws java.io.IOException
        Once connected to the leader, perform the handshake protocol to establish a following / observing connection.
        Parameters:
        pktType -
        Returns:
        the zxid the Leader sends for synchronization purposes.
        Throws:
        java.io.IOException
      • syncWithLeader

        protected void syncWithLeader​(long newLeaderZxid)
                               throws java.lang.Exception
        Finally, synchronize our history with the Leader.
        Parameters:
        newLeaderZxid -
        Throws:
        java.io.IOException
        java.lang.InterruptedException
        java.lang.Exception
      • revalidate

        protected void revalidate​(org.apache.zookeeper.server.quorum.QuorumPacket qp)
                           throws java.io.IOException
        Throws:
        java.io.IOException
      • ping

        protected void ping​(org.apache.zookeeper.server.quorum.QuorumPacket qp)
                     throws java.io.IOException
        Throws:
        java.io.IOException
      • shutdown

        public void shutdown()
        Shutdown the Peer