org.apache.zookeeper.server.quorum
Class QuorumPeer

java.lang.Object
  extended by java.lang.Thread
      extended by org.apache.zookeeper.server.quorum.QuorumPeer
All Implemented Interfaces:
Runnable, QuorumStats.Provider

public class QuorumPeer
extends Thread
implements QuorumStats.Provider

This class manages the quorum protocol. There are three states this server can be in:

  1. Leader election - each server will elect a leader (proposing itself as a leader initially).
  2. Follower - the server will synchronize with the leader and replicate any transactions.
  3. Leader - the server will process requests and forward them to followers. A majority of followers must log the request before it can be accepted.
This class will setup a datagram socket that will always respond with its view of the current leader. The response will take the form of:
 int xid;

 long myid;

 long leader_id;

 long leader_zxid;
 
The request for the current leader will consist solely of an xid: int xid;


Nested Class Summary
static interface QuorumPeer.Factory
          Create an instance of a quorum peer
static class QuorumPeer.QuorumServer
           
static class QuorumPeer.ServerState
           
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
 Follower follower
           
 Leader leader
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Fields inherited from interface org.apache.zookeeper.server.quorum.QuorumStats.Provider
FOLLOWING_STATE, LEADING_STATE, LOOKING_STATE, UNKNOWN_STATE
 
Constructor Summary
QuorumPeer()
           
QuorumPeer(Map<Long,QuorumPeer.QuorumServer> quorumPeers, File snapDir, File logDir, int clientPort, int electionAlg, long myid, int tickTime, int initLimit, int syncLimit)
          This constructor is only used by the existing unit test code.
QuorumPeer(Map<Long,QuorumPeer.QuorumServer> quorumPeers, File snapDir, File logDir, int clientPort, int electionAlg, long myid, int tickTime, int initLimit, int syncLimit, QuorumVerifier quorumConfig)
          This constructor is only used by the existing unit test code.
QuorumPeer(Map<Long,QuorumPeer.QuorumServer> quorumPeers, File dataDir, File dataLogDir, int electionType, long myid, int tickTime, int initLimit, int syncLimit, NIOServerCnxn.Factory cnxnFactory)
          For backward compatibility purposes, we instantiate QuorumMaj by default.
QuorumPeer(Map<Long,QuorumPeer.QuorumServer> quorumPeers, File dataDir, File dataLogDir, int electionType, long myid, int tickTime, int initLimit, int syncLimit, NIOServerCnxn.Factory cnxnFactory, QuorumVerifier quorumConfig)
           
 
Method Summary
 ZooKeeperServer getActiveServer()
           
 int getClientPort()
           
 NIOServerCnxn.Factory getCnxnFactory()
           
 Vote getCurrentVote()
           
 Election getElectionAlg()
          Get an instance of LeaderElection
 int getElectionType()
          Gets the election type
 long getId()
          get the id of this quorum peer.
 int getInitLimit()
          Get the number of ticks that the initial synchronization phase can take
 long getLastLoggedZxid()
          returns the highest zxid that this host has seen
 long getMyid()
          get the id of this quorum peer.
 QuorumPeer.ServerState getPeerState()
           
 InetSocketAddress getQuorumAddress()
           
 String[] getQuorumPeers()
           
 int getQuorumSize()
           
 QuorumVerifier getQuorumVerifier()
          Return QuorumVerifier object
 String getServerState()
           
 int getSyncLimit()
          Get the number of ticks that can pass between sending a request and getting an acknowledgement
 int getTick()
          Get the current tick
 int getTickTime()
          Get the number of milliseconds of each tick
 FileTxnSnapLog getTxnFactory()
           
protected  Follower makeFollower(FileTxnSnapLog logFactory)
           
protected  Leader makeLeader(FileTxnSnapLog logFactory)
           
protected  Election makeLEStrategy()
           
 void run()
           
 void setClientPort(int clientPort)
           
 void setCnxnFactory(NIOServerCnxn.Factory cnxnFactory)
           
 void setCurrentVote(Vote v)
           
 void setElectionType(int electionType)
          Sets the election type
protected  void setFollower(Follower newFollower)
           
 void setInitLimit(int initLimit)
          Set the number of ticks that the initial synchronization phase can take
protected  void setLeader(Leader newLeader)
           
 void setMyid(long myid)
          set the id of this quorum peer.
 void setPeerState(QuorumPeer.ServerState newState)
           
 void setQuorumPeers(Map<Long,QuorumPeer.QuorumServer> quorumPeers)
           
 void setQuorumVerifier(QuorumVerifier quorumConfig)
           
 void setSyncLimit(int syncLimit)
          Set the number of ticks that can pass between sending a request and getting an acknowledgement
 void setTickTime(int tickTime)
          Set the number of milliseconds of each tick
 void setTxnFactory(FileTxnSnapLog factory)
           
 void shutdown()
           
 void start()
           
 void startLeaderElection()
           
 void stopLeaderElection()
           
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

follower

public Follower follower

leader

public Leader leader
Constructor Detail

QuorumPeer

public QuorumPeer()

QuorumPeer

public QuorumPeer(Map<Long,QuorumPeer.QuorumServer> quorumPeers,
                  File dataDir,
                  File dataLogDir,
                  int electionType,
                  long myid,
                  int tickTime,
                  int initLimit,
                  int syncLimit,
                  NIOServerCnxn.Factory cnxnFactory)
           throws IOException
For backward compatibility purposes, we instantiate QuorumMaj by default.

Throws:
IOException

QuorumPeer

public QuorumPeer(Map<Long,QuorumPeer.QuorumServer> quorumPeers,
                  File dataDir,
                  File dataLogDir,
                  int electionType,
                  long myid,
                  int tickTime,
                  int initLimit,
                  int syncLimit,
                  NIOServerCnxn.Factory cnxnFactory,
                  QuorumVerifier quorumConfig)
           throws IOException
Throws:
IOException

QuorumPeer

public QuorumPeer(Map<Long,QuorumPeer.QuorumServer> quorumPeers,
                  File snapDir,
                  File logDir,
                  int clientPort,
                  int electionAlg,
                  long myid,
                  int tickTime,
                  int initLimit,
                  int syncLimit)
           throws IOException
This constructor is only used by the existing unit test code. It defaults to FileLogProvider persistence provider.

Throws:
IOException

QuorumPeer

public QuorumPeer(Map<Long,QuorumPeer.QuorumServer> quorumPeers,
                  File snapDir,
                  File logDir,
                  int clientPort,
                  int electionAlg,
                  long myid,
                  int tickTime,
                  int initLimit,
                  int syncLimit,
                  QuorumVerifier quorumConfig)
           throws IOException
This constructor is only used by the existing unit test code. It defaults to FileLogProvider persistence provider.

Throws:
IOException
Method Detail

getQuorumSize

public int getQuorumSize()

getId

public long getId()
get the id of this quorum peer.

Overrides:
getId in class Thread

getCurrentVote

public Vote getCurrentVote()

setCurrentVote

public void setCurrentVote(Vote v)

setPeerState

public void setPeerState(QuorumPeer.ServerState newState)

getPeerState

public QuorumPeer.ServerState getPeerState()

getQuorumAddress

public InetSocketAddress getQuorumAddress()

start

public void start()
Overrides:
start in class Thread

stopLeaderElection

public void stopLeaderElection()

startLeaderElection

public void startLeaderElection()

getLastLoggedZxid

public long getLastLoggedZxid()
returns the highest zxid that this host has seen

Returns:
the highest zxid for this host

makeFollower

protected Follower makeFollower(FileTxnSnapLog logFactory)
                         throws IOException
Throws:
IOException

makeLeader

protected Leader makeLeader(FileTxnSnapLog logFactory)
                     throws IOException
Throws:
IOException

makeLEStrategy

protected Election makeLEStrategy()

setLeader

protected void setLeader(Leader newLeader)

setFollower

protected void setFollower(Follower newFollower)

getActiveServer

public ZooKeeperServer getActiveServer()

run

public void run()
Specified by:
run in interface Runnable
Overrides:
run in class Thread

shutdown

public void shutdown()

getQuorumPeers

public String[] getQuorumPeers()
Specified by:
getQuorumPeers in interface QuorumStats.Provider

getServerState

public String getServerState()
Specified by:
getServerState in interface QuorumStats.Provider

getMyid

public long getMyid()
get the id of this quorum peer.


setMyid

public void setMyid(long myid)
set the id of this quorum peer.


getTickTime

public int getTickTime()
Get the number of milliseconds of each tick


setTickTime

public void setTickTime(int tickTime)
Set the number of milliseconds of each tick


getInitLimit

public int getInitLimit()
Get the number of ticks that the initial synchronization phase can take


setInitLimit

public void setInitLimit(int initLimit)
Set the number of ticks that the initial synchronization phase can take


getTick

public int getTick()
Get the current tick


getQuorumVerifier

public QuorumVerifier getQuorumVerifier()
Return QuorumVerifier object


setQuorumVerifier

public void setQuorumVerifier(QuorumVerifier quorumConfig)

getElectionAlg

public Election getElectionAlg()
Get an instance of LeaderElection


getSyncLimit

public int getSyncLimit()
Get the number of ticks that can pass between sending a request and getting an acknowledgement


setSyncLimit

public void setSyncLimit(int syncLimit)
Set the number of ticks that can pass between sending a request and getting an acknowledgement


getElectionType

public int getElectionType()
Gets the election type


setElectionType

public void setElectionType(int electionType)
Sets the election type


getCnxnFactory

public NIOServerCnxn.Factory getCnxnFactory()

setCnxnFactory

public void setCnxnFactory(NIOServerCnxn.Factory cnxnFactory)

setQuorumPeers

public void setQuorumPeers(Map<Long,QuorumPeer.QuorumServer> quorumPeers)

getClientPort

public int getClientPort()

setClientPort

public void setClientPort(int clientPort)

setTxnFactory

public void setTxnFactory(FileTxnSnapLog factory)

getTxnFactory

public FileTxnSnapLog getTxnFactory()