Package org.apache.zookeeper.server
Class ZooKeeperServer
- java.lang.Object
-
- org.apache.zookeeper.server.ZooKeeperServer
-
- All Implemented Interfaces:
ServerStats.Provider,SessionTracker.SessionExpirer
- Direct Known Subclasses:
QuorumZooKeeperServer
public class ZooKeeperServer extends java.lang.Object implements SessionTracker.SessionExpirer, ServerStats.Provider
This class implements a simple standalone ZooKeeperServer. It sets up the following chain of RequestProcessors to process requests: PrepRequestProcessor -> SyncRequestProcessor -> FinalRequestProcessor
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classZooKeeperServer.BasicDataTreeBuilderstatic interfaceZooKeeperServer.DataTreeBuilderThe server delegates loading of the tree to an instance of the interfacestatic classZooKeeperServer.MissingSessionExceptionprotected static classZooKeeperServer.State
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_TICK_TIMEprotected RequestProcessorfirstProcessorprotected DataTreeBeanjmxDataTreeBeanprotected ZooKeeperServerBeanjmxServerBeanprotected static org.slf4j.LoggerLOGprotected intmaxSessionTimeoutvalue of -1 indicates unset, use defaultprotected intminSessionTimeoutvalue of -1 indicates unset, use defaultstatic java.lang.Exceptionokprotected SessionTrackersessionTrackerprotected ZooKeeperServer.Statestateprotected inttickTime
-
Constructor Summary
Constructors Constructor Description ZooKeeperServer()Creates a ZooKeeperServer instance.ZooKeeperServer(java.io.File snapDir, java.io.File logDir, int tickTime)This constructor is for backward compatibility with the existing unit test code.ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, ZooKeeperServer.DataTreeBuilder treeBuilder, ZKDatabase zkDb)Creates a ZooKeeperServer instance.ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, ZooKeeperServer.DataTreeBuilder treeBuilder)creates a zookeeperserver instance.ZooKeeperServer(FileTxnSnapLog txnLogFactory, ZooKeeperServer.DataTreeBuilder treeBuilder)Default constructor, relies on the config for its agrument values
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleancanShutdown()This can be used while shutting down the server to see whether the server is already shutdown or not.protected booleancheckPasswd(long sessionId, byte[] passwd)voidcloseSession(long sessionId)voidcloseSession(ServerCnxn cnxn, RequestHeader requestHeader)protected voidcreateSessionTracker()voiddecInProcess()voiddumpConf(java.io.PrintWriter pwriter)voiddumpEphemerals(java.io.PrintWriter pwriter)voidexpire(SessionTracker.Session session)voidfinishSessionInit(ServerCnxn cnxn, boolean valid)intgetClientPort()intgetGlobalOutstandingLimit()intgetInProcess()longgetLastProcessedZxid()return the last proceesed id from the datatreeintgetMaxSessionTimeout()intgetMinSessionTimeout()intgetNumAliveConnections()return the total number of client connections that are alive to this serverlonggetOutstandingRequests()return the outstanding requests in the queue, which havent been processed yetServerCnxnFactorygetServerCnxnFactory()longgetServerId()static intgetSnapCount()java.lang.StringgetState()intgetTickTime()FileTxnSnapLoggetTxnLogFactory()ZKDatabasegetZKDatabase()get the zookeeper database for this serverZooKeeperServerListenergetZooKeeperServerListener()longgetZxid()This should be called from a synchronized block on this!voidincInProcess()booleanisRunning()protected voidkillSession(long sessionId, long zxid)voidloadData()Restore sessions and datavoidprocessConnectRequest(ServerCnxn cnxn, java.nio.ByteBuffer incomingBuffer)voidprocessPacket(ServerCnxn cnxn, java.nio.ByteBuffer incomingBuffer)DataTree.ProcessTxnResultprocessTxn(TxnHeader hdr, org.apache.jute.Record txn)protected voidregisterJMX()voidreopenSession(ServerCnxn cnxn, long sessionId, byte[] passwd, int sessionTimeout)protected voidrevalidateSession(ServerCnxn cnxn, long sessionId, int sessionTimeout)ServerStatsserverStats()voidsetMaxSessionTimeout(int max)voidsetMinSessionTimeout(int min)voidsetOwner(long id, java.lang.Object owner)set the owner of this session as ownervoidsetServerCnxnFactory(ServerCnxnFactory factory)protected voidsetState(ZooKeeperServer.State state)Sets the state of ZooKeeper server.voidsetTickTime(int tickTime)voidsetTxnLogFactory(FileTxnSnapLog txnLog)protected voidsetupRequestProcessors()voidsetZKDatabase(ZKDatabase zkDb)set the zkdatabase for this zookeeper servervoidsetZxid(long zxid)booleanshouldThrottle(long outStandingCount)voidshutdown()voidshutdown(boolean fullyShutDown)Shut down the server instancevoidstartdata()protected voidstartSessionTracker()voidstartup()voidsubmitRequest(Request si)voidtakeSnapshot()voidtruncateLog(long zxid)trunccate the log to get in sync with others if in a quorumprotected voidunregisterJMX()
-
-
-
Field Detail
-
LOG
protected static final org.slf4j.Logger LOG
-
jmxServerBean
protected ZooKeeperServerBean jmxServerBean
-
jmxDataTreeBean
protected DataTreeBean jmxDataTreeBean
-
DEFAULT_TICK_TIME
public static final int DEFAULT_TICK_TIME
- See Also:
- Constant Field Values
-
tickTime
protected int tickTime
-
minSessionTimeout
protected int minSessionTimeout
value of -1 indicates unset, use default
-
maxSessionTimeout
protected int maxSessionTimeout
value of -1 indicates unset, use default
-
sessionTracker
protected SessionTracker sessionTracker
-
ok
public static final java.lang.Exception ok
-
firstProcessor
protected RequestProcessor firstProcessor
-
state
protected volatile ZooKeeperServer.State state
-
-
Constructor Detail
-
ZooKeeperServer
public ZooKeeperServer()
Creates a ZooKeeperServer instance. Nothing is setup, use the setX methods to prepare the instance (eg datadir, datalogdir, ticktime, builder, etc...)- Throws:
java.io.IOException
-
ZooKeeperServer
public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, ZooKeeperServer.DataTreeBuilder treeBuilder, ZKDatabase zkDb)
Creates a ZooKeeperServer instance. It sets everything up, but doesn't actually start listening for clients until run() is invoked.- Parameters:
dataDir- the directory to put the data
-
ZooKeeperServer
public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, ZooKeeperServer.DataTreeBuilder treeBuilder) throws java.io.IOException
creates a zookeeperserver instance.- Parameters:
txnLogFactory- the file transaction snapshot logging classtickTime- the ticktime for the servertreeBuilder- the datatree builder- Throws:
java.io.IOException
-
ZooKeeperServer
public ZooKeeperServer(java.io.File snapDir, java.io.File logDir, int tickTime) throws java.io.IOExceptionThis constructor is for backward compatibility with the existing unit test code. It defaults to FileLogProvider persistence provider.- Throws:
java.io.IOException
-
ZooKeeperServer
public ZooKeeperServer(FileTxnSnapLog txnLogFactory, ZooKeeperServer.DataTreeBuilder treeBuilder) throws java.io.IOException
Default constructor, relies on the config for its agrument values- Throws:
java.io.IOException
-
-
Method Detail
-
serverStats
public ServerStats serverStats()
-
dumpConf
public void dumpConf(java.io.PrintWriter pwriter)
-
getZKDatabase
public ZKDatabase getZKDatabase()
get the zookeeper database for this server- Returns:
- the zookeeper database for this server
-
setZKDatabase
public void setZKDatabase(ZKDatabase zkDb)
set the zkdatabase for this zookeeper server- Parameters:
zkDb-
-
loadData
public void loadData() throws java.io.IOException, java.lang.InterruptedExceptionRestore sessions and data- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
takeSnapshot
public void takeSnapshot()
-
getZxid
public long getZxid()
This should be called from a synchronized block on this!
-
setZxid
public void setZxid(long zxid)
-
closeSession
public void closeSession(long sessionId)
-
killSession
protected void killSession(long sessionId, long zxid)
-
expire
public void expire(SessionTracker.Session session)
- Specified by:
expirein interfaceSessionTracker.SessionExpirer
-
registerJMX
protected void registerJMX()
-
startdata
public void startdata() throws java.io.IOException, java.lang.InterruptedException- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
startup
public void startup()
-
setupRequestProcessors
protected void setupRequestProcessors()
-
getZooKeeperServerListener
public ZooKeeperServerListener getZooKeeperServerListener()
-
createSessionTracker
protected void createSessionTracker()
-
startSessionTracker
protected void startSessionTracker()
-
setState
protected void setState(ZooKeeperServer.State state)
Sets the state of ZooKeeper server. After changing the state, it notifies the server state change to a registered shutdown handler, if any.The following are the server state transitions:
- During startup the server will be in the INITIAL state.
- After successfully starting, the server sets the state to RUNNING.
- The server transitions to the ERROR state if it hits an internal error.
ZooKeeperServerListenerImplnotifies any critical resource error events, e.g., SyncRequestProcessor not being able to write a txn to disk.- During shutdown the server sets the state to SHUTDOWN, which corresponds to the server not running.
- Parameters:
state- new server state.
-
canShutdown
protected boolean canShutdown()
This can be used while shutting down the server to see whether the server is already shutdown or not.- Returns:
- true if the server is running or server hits an error, false otherwise.
-
isRunning
public boolean isRunning()
-
shutdown
public void shutdown()
-
shutdown
public void shutdown(boolean fullyShutDown)
Shut down the server instance- Parameters:
fullyShutDown- true if another server using the same database will not replace this one in the same process
-
unregisterJMX
protected void unregisterJMX()
-
incInProcess
public void incInProcess()
-
decInProcess
public void decInProcess()
-
getInProcess
public int getInProcess()
-
checkPasswd
protected boolean checkPasswd(long sessionId, byte[] passwd)
-
setOwner
public void setOwner(long id, java.lang.Object owner) throws KeeperException.SessionExpiredExceptionset the owner of this session as owner- Parameters:
id- the session idowner- the owner of the session- Throws:
KeeperException.SessionExpiredException
-
revalidateSession
protected void revalidateSession(ServerCnxn cnxn, long sessionId, int sessionTimeout) throws java.io.IOException
- Throws:
java.io.IOException
-
reopenSession
public void reopenSession(ServerCnxn cnxn, long sessionId, byte[] passwd, int sessionTimeout) throws java.io.IOException
- Throws:
java.io.IOException
-
finishSessionInit
public void finishSessionInit(ServerCnxn cnxn, boolean valid)
-
closeSession
public void closeSession(ServerCnxn cnxn, RequestHeader requestHeader)
-
getServerId
public long getServerId()
- Specified by:
getServerIdin interfaceSessionTracker.SessionExpirer
-
submitRequest
public void submitRequest(Request si)
-
getSnapCount
public static int getSnapCount()
-
getGlobalOutstandingLimit
public int getGlobalOutstandingLimit()
-
setServerCnxnFactory
public void setServerCnxnFactory(ServerCnxnFactory factory)
-
getServerCnxnFactory
public ServerCnxnFactory getServerCnxnFactory()
-
getLastProcessedZxid
public long getLastProcessedZxid()
return the last proceesed id from the datatree- Specified by:
getLastProcessedZxidin interfaceServerStats.Provider
-
getOutstandingRequests
public long getOutstandingRequests()
return the outstanding requests in the queue, which havent been processed yet- Specified by:
getOutstandingRequestsin interfaceServerStats.Provider
-
truncateLog
public void truncateLog(long zxid) throws java.io.IOExceptiontrunccate the log to get in sync with others if in a quorum- Parameters:
zxid- the zxid that it needs to get in sync with others- Throws:
java.io.IOException
-
getTickTime
public int getTickTime()
-
setTickTime
public void setTickTime(int tickTime)
-
getMinSessionTimeout
public int getMinSessionTimeout()
-
setMinSessionTimeout
public void setMinSessionTimeout(int min)
-
getMaxSessionTimeout
public int getMaxSessionTimeout()
-
setMaxSessionTimeout
public void setMaxSessionTimeout(int max)
-
getClientPort
public int getClientPort()
-
setTxnLogFactory
public void setTxnLogFactory(FileTxnSnapLog txnLog)
-
getTxnLogFactory
public FileTxnSnapLog getTxnLogFactory()
-
getState
public java.lang.String getState()
- Specified by:
getStatein interfaceServerStats.Provider
-
dumpEphemerals
public void dumpEphemerals(java.io.PrintWriter pwriter)
-
getNumAliveConnections
public int getNumAliveConnections()
return the total number of client connections that are alive to this server- Specified by:
getNumAliveConnectionsin interfaceServerStats.Provider
-
processConnectRequest
public void processConnectRequest(ServerCnxn cnxn, java.nio.ByteBuffer incomingBuffer) throws java.io.IOException
- Throws:
java.io.IOException
-
shouldThrottle
public boolean shouldThrottle(long outStandingCount)
-
processPacket
public void processPacket(ServerCnxn cnxn, java.nio.ByteBuffer incomingBuffer) throws java.io.IOException
- Throws:
java.io.IOException
-
processTxn
public DataTree.ProcessTxnResult processTxn(TxnHeader hdr, org.apache.jute.Record txn)
-
-