Class QuorumPeer
- java.lang.Object
-
- java.lang.Thread
-
- org.apache.zookeeper.server.ZooKeeperThread
-
- org.apache.zookeeper.server.quorum.QuorumPeer
-
- All Implemented Interfaces:
java.lang.Runnable,QuorumStats.Provider
public class QuorumPeer extends ZooKeeperThread implements QuorumStats.Provider
This class manages the quorum protocol. There are three states this server can be in:- Leader election - each server will elect a leader (proposing itself as a leader initially).
- Follower - the server will synchronize with the leader and replicate any transactions.
- Leader - the server will process requests and forward them to followers. A majority of followers must log the request before it can be accepted.
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
Nested Classes Modifier and Type Class Description static classQuorumPeer.LearnerTypestatic classQuorumPeer.QuorumServerstatic classQuorumPeer.ServerState
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringACCEPTED_EPOCH_FILENAMEstatic java.lang.StringCURRENT_EPOCH_FILENAMElongend_fleFollowerfollowerprotected intinitLimitThe number of ticks that the initial synchronization phase can takeLeaderleaderprotected intmaxSessionTimeoutMaximum number of milliseconds to allow for session timeout.protected intminSessionTimeoutMinimum number of milliseconds to allow for session timeout.Observerobserverprotected intquorumCnxnThreadsSizeThe maximum number of threads to allow in the connectionExecutors thread pool which will be used to initiate quorum server connections.protected java.lang.StringquorumLearnerLoginContextQuorum learner login context name in jaas-conf file to read the kerberos security details.protected booleanquorumLearnerSaslAuthRequiredIf this is false, quorum peer learner will talk to quorum peer server without authentication.protected booleanquorumListenOnAllIPsWhether or not to listen on all IPs for the two quorum ports (broadcast and fast leader election).protected java.util.Map<java.lang.Long,QuorumPeer.QuorumServer>quorumPeersThe servers that make up the clusterprotected booleanquorumSaslEnableAuthEnable/Disables quorum authentication using sasl.protected java.lang.StringquorumServerLoginContextQuorum server login context name in jaas-conf file to read the kerberos security details.protected booleanquorumServerSaslAuthRequiredIf this is false, quorum peer server will accept another quorum peer client connection even if the authentication did not succeed.protected java.lang.StringquorumServicePrincipalKerberos quorum service principal.longstart_flestatic java.lang.StringSYNC_ENABLEDThe syncEnabled can also be set via a system property.protected booleansyncEnabledEnables/Disables sync request processor.protected intsyncLimitThe number of ticks that can pass between sending a request and getting an acknowledgmentprotected java.util.concurrent.atomic.AtomicIntegertickThe current tickprotected inttickTimeThe number of milliseconds of each tickstatic java.lang.StringUPDATING_EPOCH_FILENAME-
Fields inherited from interface org.apache.zookeeper.server.quorum.QuorumStats.Provider
FOLLOWING_STATE, LEADING_STATE, LOOKING_STATE, OBSERVING_STATE, UNKNOWN_STATE
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedQuorumPeer()QuorumPeer(java.util.Map<java.lang.Long,QuorumPeer.QuorumServer> quorumPeers, java.io.File snapDir, java.io.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(java.util.Map<java.lang.Long,QuorumPeer.QuorumServer> quorumPeers, java.io.File snapDir, java.io.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(java.util.Map<java.lang.Long,QuorumPeer.QuorumServer> quorumPeers, java.io.File dataDir, java.io.File dataLogDir, int electionType, long myid, int tickTime, int initLimit, int syncLimit, boolean quorumListenOnAllIPs, ServerCnxnFactory cnxnFactory, QuorumVerifier quorumConfig)QuorumPeer(java.util.Map<java.lang.Long,QuorumPeer.QuorumServer> quorumPeers, java.io.File dataDir, java.io.File dataLogDir, int electionType, long myid, int tickTime, int initLimit, int syncLimit, ServerCnxnFactory cnxnFactory)For backward compatibility purposes, we instantiate QuorumMaj by default.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected static intcountParticipants(java.util.Map<java.lang.Long,QuorumPeer.QuorumServer> peers)Count the number of nodes in the map that could be followers.QuorumCnxManagercreateCnxnManager()protected ElectioncreateElectionAlgorithm(int electionAlgorithm)longgetAcceptedEpoch()ZooKeeperServergetActiveServer()intgetClientPort()ServerCnxnFactorygetCnxnFactory()longgetCurrentEpoch()VotegetCurrentVote()ElectiongetElectionAlg()Get an instance of LeaderElectionintgetElectionType()Gets the election typelonggetId()get the id of this quorum peer.intgetInitLimit()Get the number of ticks that the initial synchronization phase can takelonggetLastLoggedZxid()returns the highest zxid that this host has seenQuorumPeer.LearnerTypegetLearnerType()intgetMaxClientCnxnsPerHost()Maximum number of connections allowed from particular host (ip)intgetMaxSessionTimeout()maximum session timeout in millisecondsintgetMinSessionTimeout()minimum session timeout in millisecondslonggetMyid()get the id of this quorum peer.java.util.Map<java.lang.Long,QuorumPeer.QuorumServer>getObservingView()Returns only observers, no followers.QuorumPeer.ServerStategetPeerState()java.net.InetSocketAddressgetQuorumAddress()QuorumCnxManagergetQuorumCnxManager()get reference to QuorumCnxManagerbooleangetQuorumListenOnAllIPs()java.lang.String[]getQuorumPeers()Only used by QuorumStats at the momentintgetQuorumSize()QuorumVerifiergetQuorumVerifier()Return QuorumVerifier objectjava.lang.StringgetServerState()booleangetSyncEnabled()Return syncEnabled.intgetSyncLimit()Get the synclimitintgetTick()Get the current tickintgetTickTime()Get the number of milliseconds of each tickFileTxnSnapLoggetTxnFactory()java.util.Map<java.lang.Long,QuorumPeer.QuorumServer>getView()A 'view' is a node's current opinion of the membership of the entire ensemble.java.util.Map<java.lang.Long,QuorumPeer.QuorumServer>getVotingView()Observers are not contained in this view, only nodes with PeerType=PARTICIPANT.protected ZKDatabasegetZkDb()booleanhasAuthInitialized()voidinitialize()booleanisRunning()protected FollowermakeFollower(FileTxnSnapLog logFactory)protected LeadermakeLeader(FileTxnSnapLog logFactory)protected ElectionmakeLEStrategy()protected ObservermakeObserver(FileTxnSnapLog logFactory)voidrun()voidsetAcceptedEpoch(long e)voidsetClientPortAddress(java.net.InetSocketAddress addr)voidsetCnxnFactory(ServerCnxnFactory cnxnFactory)voidsetCurrentEpoch(long e)voidsetCurrentVote(Vote v)voidsetElectionType(int electionType)Sets the election typeprotected voidsetFollower(Follower newFollower)voidsetInitLimit(int initLimit)Set the number of ticks that the initial synchronization phase can takeprotected voidsetLeader(Leader newLeader)voidsetLearnerType(QuorumPeer.LearnerType p)Sets the LearnerType both in the QuorumPeer and in the peerMapvoidsetMaxSessionTimeout(int max)minimum session timeout in millisecondsvoidsetMinSessionTimeout(int min)minimum session timeout in millisecondsvoidsetMyid(long myid)set the id of this quorum peer.protected voidsetObserver(Observer newObserver)voidsetPeerState(QuorumPeer.ServerState newState)voidsetQuorumListenOnAllIPs(boolean quorumListenOnAllIPs)voidsetQuorumPeers(java.util.Map<java.lang.Long,QuorumPeer.QuorumServer> quorumPeers)voidsetQuorumVerifier(QuorumVerifier quorumConfig)voidsetRunning(boolean running)voidsetSyncEnabled(boolean syncEnabled)Set syncEnabled.voidsetSyncLimit(int syncLimit)Set the synclimitvoidsetTickTime(int tickTime)Set the number of milliseconds of each tickvoidsetTxnFactory(FileTxnSnapLog factory)voidsetZKDatabase(ZKDatabase database)set zk database for this nodevoidshutdown()voidstart()voidstartLeaderElection()voidstopLeaderElection()static QuorumPeertestingQuorumPeer()protected voidupdateElectionVote(long newEpoch)Updates leader election info to avoid inconsistencies when a new server tries to join the ensemble.booleanviewContains(java.lang.Long sid)Check if a node is in the current view.-
Methods inherited from class org.apache.zookeeper.server.ZooKeeperThread
handleException
-
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, stop, suspend, toString, yield
-
-
-
-
Field Detail
-
start_fle
public long start_fle
-
end_fle
public long end_fle
-
quorumPeers
protected java.util.Map<java.lang.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
-
syncEnabled
protected boolean syncEnabled
Enables/Disables sync request processor. This option is enabled by default and is to be used with observers.
-
tick
protected java.util.concurrent.atomic.AtomicInteger tick
The current tick
-
quorumListenOnAllIPs
protected boolean quorumListenOnAllIPs
Whether or not to listen on all IPs for the two quorum ports (broadcast and fast leader election).
-
quorumSaslEnableAuth
protected boolean quorumSaslEnableAuth
Enable/Disables quorum authentication using sasl. Defaulting to false.
-
quorumServerSaslAuthRequired
protected boolean quorumServerSaslAuthRequired
If this is false, quorum peer server will accept another quorum peer client connection even if the authentication did not succeed. This can be used while upgrading ZooKeeper server. Defaulting to false (required).
-
quorumLearnerSaslAuthRequired
protected boolean quorumLearnerSaslAuthRequired
If this is false, quorum peer learner will talk to quorum peer server without authentication. This can be used while upgrading ZooKeeper server. Defaulting to false (required).
-
quorumServicePrincipal
protected java.lang.String quorumServicePrincipal
Kerberos quorum service principal. Defaulting to 'zkquorum/localhost'.
-
quorumLearnerLoginContext
protected java.lang.String quorumLearnerLoginContext
Quorum learner login context name in jaas-conf file to read the kerberos security details. Defaulting to 'QuorumLearner'.
-
quorumServerLoginContext
protected java.lang.String quorumServerLoginContext
Quorum server login context name in jaas-conf file to read the kerberos security details. Defaulting to 'QuorumServer'.
-
quorumCnxnThreadsSize
protected int quorumCnxnThreadsSize
The maximum number of threads to allow in the connectionExecutors thread pool which will be used to initiate quorum server connections.
-
follower
public Follower follower
-
leader
public Leader leader
-
observer
public Observer observer
-
SYNC_ENABLED
public static final java.lang.String SYNC_ENABLED
The syncEnabled can also be set via a system property.- See Also:
- Constant Field Values
-
CURRENT_EPOCH_FILENAME
public static final java.lang.String CURRENT_EPOCH_FILENAME
- See Also:
- Constant Field Values
-
ACCEPTED_EPOCH_FILENAME
public static final java.lang.String ACCEPTED_EPOCH_FILENAME
- See Also:
- Constant Field Values
-
UPDATING_EPOCH_FILENAME
public static final java.lang.String UPDATING_EPOCH_FILENAME
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
QuorumPeer
protected QuorumPeer() throws javax.security.sasl.SaslException- Throws:
javax.security.sasl.SaslException
-
QuorumPeer
public QuorumPeer(java.util.Map<java.lang.Long,QuorumPeer.QuorumServer> quorumPeers, java.io.File dataDir, java.io.File dataLogDir, int electionType, long myid, int tickTime, int initLimit, int syncLimit, ServerCnxnFactory cnxnFactory) throws java.io.IOException
For backward compatibility purposes, we instantiate QuorumMaj by default.- Throws:
java.io.IOException
-
QuorumPeer
public QuorumPeer(java.util.Map<java.lang.Long,QuorumPeer.QuorumServer> quorumPeers, java.io.File dataDir, java.io.File dataLogDir, int electionType, long myid, int tickTime, int initLimit, int syncLimit, boolean quorumListenOnAllIPs, ServerCnxnFactory cnxnFactory, QuorumVerifier quorumConfig) throws java.io.IOException
- Throws:
java.io.IOException
-
QuorumPeer
public QuorumPeer(java.util.Map<java.lang.Long,QuorumPeer.QuorumServer> quorumPeers, java.io.File snapDir, java.io.File logDir, int clientPort, int electionAlg, long myid, int tickTime, int initLimit, int syncLimit) throws java.io.IOException
This constructor is only used by the existing unit test code. It defaults to FileLogProvider persistence provider.- Throws:
java.io.IOException
-
QuorumPeer
public QuorumPeer(java.util.Map<java.lang.Long,QuorumPeer.QuorumServer> quorumPeers, java.io.File snapDir, java.io.File logDir, int clientPort, int electionAlg, long myid, int tickTime, int initLimit, int syncLimit, QuorumVerifier quorumConfig) throws java.io.IOException
This constructor is only used by the existing unit test code. It defaults to FileLogProvider persistence provider.- Throws:
java.io.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:
getIdin classjava.lang.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 java.net.InetSocketAddress getQuorumAddress()
-
testingQuorumPeer
public static QuorumPeer testingQuorumPeer() throws javax.security.sasl.SaslException
- Throws:
javax.security.sasl.SaslException
-
initialize
public void initialize() throws javax.security.sasl.SaslException- Throws:
javax.security.sasl.SaslException
-
start
public void start()
- Overrides:
startin classjava.lang.Thread
-
stopLeaderElection
public void stopLeaderElection()
-
startLeaderElection
public void startLeaderElection()
-
countParticipants
protected static int countParticipants(java.util.Map<java.lang.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 java.io.IOException
- Throws:
java.io.IOException
-
makeLeader
protected Leader makeLeader(FileTxnSnapLog logFactory) throws java.io.IOException
- Throws:
java.io.IOException
-
makeObserver
protected Observer makeObserver(FileTxnSnapLog logFactory) throws java.io.IOException
- Throws:
java.io.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:
runin interfacejava.lang.Runnable- Overrides:
runin classjava.lang.Thread
-
shutdown
public void shutdown()
-
getView
public java.util.Map<java.lang.Long,QuorumPeer.QuorumServer> getView()
A 'view' is a node's current opinion of the membership of the entire ensemble.
-
getVotingView
public java.util.Map<java.lang.Long,QuorumPeer.QuorumServer> getVotingView()
Observers are not contained in this view, only nodes with PeerType=PARTICIPANT.
-
getObservingView
public java.util.Map<java.lang.Long,QuorumPeer.QuorumServer> getObservingView()
Returns only observers, no followers.
-
viewContains
public boolean viewContains(java.lang.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 java.lang.String[] getQuorumPeers()
Only used by QuorumStats at the moment- Specified by:
getQuorumPeersin interfaceQuorumStats.Provider
-
getServerState
public java.lang.String getServerState()
- Specified by:
getServerStatein interfaceQuorumStats.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
-
getSyncEnabled
public boolean getSyncEnabled()
Return syncEnabled.- Returns:
-
setSyncEnabled
public void setSyncEnabled(boolean syncEnabled)
Set syncEnabled.- Parameters:
syncEnabled-
-
getElectionType
public int getElectionType()
Gets the election type
-
setElectionType
public void setElectionType(int electionType)
Sets the election type
-
getQuorumListenOnAllIPs
public boolean getQuorumListenOnAllIPs()
-
setQuorumListenOnAllIPs
public void setQuorumListenOnAllIPs(boolean quorumListenOnAllIPs)
-
getCnxnFactory
public ServerCnxnFactory getCnxnFactory()
-
setCnxnFactory
public void setCnxnFactory(ServerCnxnFactory cnxnFactory)
-
setQuorumPeers
public void setQuorumPeers(java.util.Map<java.lang.Long,QuorumPeer.QuorumServer> quorumPeers)
-
getClientPort
public int getClientPort()
-
setClientPortAddress
public void setClientPortAddress(java.net.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-
-
getZkDb
protected ZKDatabase getZkDb()
-
setRunning
public void setRunning(boolean running)
-
isRunning
public boolean isRunning()
-
getQuorumCnxManager
public QuorumCnxManager getQuorumCnxManager()
get reference to QuorumCnxManager
-
getCurrentEpoch
public long getCurrentEpoch() throws java.io.IOException- Throws:
java.io.IOException
-
getAcceptedEpoch
public long getAcceptedEpoch() throws java.io.IOException- Throws:
java.io.IOException
-
setCurrentEpoch
public void setCurrentEpoch(long e) throws java.io.IOException- Throws:
java.io.IOException
-
setAcceptedEpoch
public void setAcceptedEpoch(long e) throws java.io.IOException- Throws:
java.io.IOException
-
updateElectionVote
protected void updateElectionVote(long newEpoch)
Updates leader election info to avoid inconsistencies when a new server tries to join the ensemble. See ZOOKEEPER-1732 for more info.
-
hasAuthInitialized
public boolean hasAuthInitialized()
-
createCnxnManager
public QuorumCnxManager createCnxnManager()
-
-