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 class QuorumPeer.LearnerType
           
static class QuorumPeer.QuorumServer
           
static class QuorumPeer.ServerState
           
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
static String ACCEPTED_EPOCH_FILENAME
           
static String CURRENT_EPOCH_FILENAME
           
 long end_fle
           
 Follower follower
           
protected  int initLimit
          The number of ticks that the initial synchronization phase can take
 Leader leader
           
protected  int maxSessionTimeout
          Maximum number of milliseconds to allow for session timeout.
protected  int minSessionTimeout
          Minimum number of milliseconds to allow for session timeout.
 Observer observer
           
protected  Map<Long,QuorumPeer.QuorumServer> quorumPeers
          The servers that make up the cluster
 long start_fle
           
protected  int syncLimit
          The number of ticks that can pass between sending a request and getting an acknowledgment
protected  int tick
          The current tick
protected  int tickTime
          The number of milliseconds of each tick
 
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, OBSERVING_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, ServerCnxnFactory 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, ServerCnxnFactory cnxnFactory, QuorumVerifier quorumConfig)
           
 
Method Summary
protected static int countParticipants(Map<Long,QuorumPeer.QuorumServer> peers)
          Count the number of nodes in the map that could be followers.
protected  Election createElectionAlgorithm(int electionAlgorithm)
           
 long getAcceptedEpoch()
           
 ZooKeeperServer getActiveServer()
           
 int getClientPort()
           
 ServerCnxnFactory getCnxnFactory()
           
 long getCurrentEpoch()
           
 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
 QuorumPeer.LearnerType getLearnerType()
           
 int getMaxClientCnxnsPerHost()
          Maximum number of connections allowed from particular host (ip)
 int getMaxSessionTimeout()
          maximum session timeout in milliseconds
 int getMinSessionTimeout()
          minimum session timeout in milliseconds
 long getMyid()
          get the id of this quorum peer.
 Map<Long,QuorumPeer.QuorumServer> getObservingView()
          Returns only observers, no followers.
 QuorumPeer.ServerState getPeerState()
           
 InetSocketAddress getQuorumAddress()
           
 QuorumCnxManager getQuorumCnxManager()
          get reference to QuorumCnxManager
 String[] getQuorumPeers()
          Only used by QuorumStats at the moment
 int getQuorumSize()
           
 QuorumVerifier getQuorumVerifier()
          Return QuorumVerifier object
 String getServerState()
           
 int getSyncLimit()
          Get the synclimit
 int getTick()
          Get the current tick
 int getTickTime()
          Get the number of milliseconds of each tick
 FileTxnSnapLog getTxnFactory()
           
 Map<Long,QuorumPeer.QuorumServer> getView()
          A 'view' is a node's current opinion of the membership of the entire ensemble.
 Map<Long,QuorumPeer.QuorumServer> getVotingView()
          Observers are not contained in this view, only nodes with PeerType=PARTICIPANT.
 boolean isRunning()
           
protected  Follower makeFollower(FileTxnSnapLog logFactory)
           
protected  Leader makeLeader(FileTxnSnapLog logFactory)
           
protected  Election makeLEStrategy()
           
protected  Observer makeObserver(FileTxnSnapLog logFactory)
           
 void run()
           
 void setAcceptedEpoch(long e)
           
 void setClientPortAddress(InetSocketAddress addr)
           
 void setCnxnFactory(ServerCnxnFactory cnxnFactory)
           
 void setCurrentEpoch(long e)
           
 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 setLearnerType(QuorumPeer.LearnerType p)
          Sets the LearnerType both in the QuorumPeer and in the peerMap
 void setMaxSessionTimeout(int max)
          minimum session timeout in milliseconds
 void setMinSessionTimeout(int min)
          minimum session timeout in milliseconds
 void setMyid(long myid)
          set the id of this quorum peer.
protected  void setObserver(Observer newObserver)
           
 void setPeerState(QuorumPeer.ServerState newState)
           
 void setQuorumPeers(Map<Long,QuorumPeer.QuorumServer> quorumPeers)
           
 void setQuorumVerifier(QuorumVerifier quorumConfig)
           
 void setRunning(boolean running)
           
 void setSyncLimit(int syncLimit)
          Set the synclimit
 void setTickTime(int tickTime)
          Set the number of milliseconds of each tick
 void setTxnFactory(FileTxnSnapLog factory)
           
 void setZKDatabase(ZKDatabase database)
          set zk database for this node
 void shutdown()
           
 void start()
           
 void startLeaderElection()
           
 void stopLeaderElection()
           
 boolean viewContains(Long sid)
          Check if a node is in the current view.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, 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
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

start_fle

public long start_fle

end_fle

public long end_fle

quorumPeers

protected Map<Long,QuorumPeer.QuorumServer> quorumPeers
The servers that make up the cluster


tickTime

protected int tickTime
The number of milliseconds of each tick


minSessionTimeout

protected int minSessionTimeout
Minimum number of milliseconds to allow for session timeout. A value of -1 indicates unset, use default.


maxSessionTimeout

protected int maxSessionTimeout
Maximum number of milliseconds to allow for session timeout. A value of -1 indicates unset, use default.


initLimit

protected int initLimit
The number of ticks that the initial synchronization phase can take


syncLimit

protected int syncLimit
The number of ticks that can pass between sending a request and getting an acknowledgment


tick

protected int tick
The current tick


follower

public Follower follower

leader

public Leader leader

observer

public Observer observer

CURRENT_EPOCH_FILENAME

public static final String CURRENT_EPOCH_FILENAME
See Also:
Constant Field Values

ACCEPTED_EPOCH_FILENAME

public static final String ACCEPTED_EPOCH_FILENAME
See Also:
Constant Field Values
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,
                  ServerCnxnFactory 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,
                  ServerCnxnFactory 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

getLearnerType

public QuorumPeer.LearnerType getLearnerType()

setLearnerType

public void setLearnerType(QuorumPeer.LearnerType p)
Sets the LearnerType both in the QuorumPeer and in the peerMap


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()

countParticipants

protected static int countParticipants(Map<Long,QuorumPeer.QuorumServer> peers)
Count the number of nodes in the map that could be followers.

Parameters:
peers -
Returns:
The number of followers in the map

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

makeObserver

protected Observer makeObserver(FileTxnSnapLog logFactory)
                         throws IOException
Throws:
IOException

createElectionAlgorithm

protected Election createElectionAlgorithm(int electionAlgorithm)

makeLEStrategy

protected Election makeLEStrategy()

setLeader

protected void setLeader(Leader newLeader)

setFollower

protected void setFollower(Follower newFollower)

setObserver

protected void setObserver(Observer newObserver)

getActiveServer

public ZooKeeperServer getActiveServer()

run

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

shutdown

public void shutdown()

getView

public Map<Long,QuorumPeer.QuorumServer> getView()
A 'view' is a node's current opinion of the membership of the entire ensemble.


getVotingView

public Map<Long,QuorumPeer.QuorumServer> getVotingView()
Observers are not contained in this view, only nodes with PeerType=PARTICIPANT.


getObservingView

public Map<Long,QuorumPeer.QuorumServer> getObservingView()
Returns only observers, no followers.


viewContains

public boolean viewContains(Long sid)
Check if a node is in the current view. With static membership, the result of this check will never change; only when dynamic membership is introduced will this be more useful.


getQuorumPeers

public String[] getQuorumPeers()
Only used by QuorumStats at the moment

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


getMaxClientCnxnsPerHost

public int getMaxClientCnxnsPerHost()
Maximum number of connections allowed from particular host (ip)


getMinSessionTimeout

public int getMinSessionTimeout()
minimum session timeout in milliseconds


setMinSessionTimeout

public void setMinSessionTimeout(int min)
minimum session timeout in milliseconds


getMaxSessionTimeout

public int getMaxSessionTimeout()
maximum session timeout in milliseconds


setMaxSessionTimeout

public void setMaxSessionTimeout(int max)
minimum session timeout in milliseconds


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 synclimit


setSyncLimit

public void setSyncLimit(int syncLimit)
Set the synclimit


getElectionType

public int getElectionType()
Gets the election type


setElectionType

public void setElectionType(int electionType)
Sets the election type


getCnxnFactory

public ServerCnxnFactory getCnxnFactory()

setCnxnFactory

public void setCnxnFactory(ServerCnxnFactory cnxnFactory)

setQuorumPeers

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

getClientPort

public int getClientPort()

setClientPortAddress

public void setClientPortAddress(InetSocketAddress addr)

setTxnFactory

public void setTxnFactory(FileTxnSnapLog factory)

getTxnFactory

public FileTxnSnapLog getTxnFactory()

setZKDatabase

public void setZKDatabase(ZKDatabase database)
set zk database for this node

Parameters:
database -

setRunning

public void setRunning(boolean running)

isRunning

public boolean isRunning()

getQuorumCnxManager

public QuorumCnxManager getQuorumCnxManager()
get reference to QuorumCnxManager


getCurrentEpoch

public long getCurrentEpoch()
                     throws IOException
Throws:
IOException

getAcceptedEpoch

public long getAcceptedEpoch()
                      throws IOException
Throws:
IOException

setCurrentEpoch

public void setCurrentEpoch(long e)
                     throws IOException
Throws:
IOException

setAcceptedEpoch

public void setAcceptedEpoch(long e)
                      throws IOException
Throws:
IOException


Copyright © 2011 The Apache Software Foundation