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 class
ZooKeeperServer.BasicDataTreeBuilder
static interface
ZooKeeperServer.DataTreeBuilder
The server delegates loading of the tree to an instance of the interfacestatic class
ZooKeeperServer.MissingSessionException
protected static class
ZooKeeperServer.State
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_TICK_TIME
protected RequestProcessor
firstProcessor
protected DataTreeBean
jmxDataTreeBean
protected ZooKeeperServerBean
jmxServerBean
protected static org.slf4j.Logger
LOG
protected int
maxSessionTimeout
value of -1 indicates unset, use defaultprotected int
minSessionTimeout
value of -1 indicates unset, use defaultstatic java.lang.Exception
ok
protected SessionTracker
sessionTracker
protected ZooKeeperServer.State
state
protected int
tickTime
-
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 boolean
canShutdown()
This can be used while shutting down the server to see whether the server is already shutdown or not.protected boolean
checkPasswd(long sessionId, byte[] passwd)
void
closeSession(long sessionId)
void
closeSession(ServerCnxn cnxn, RequestHeader requestHeader)
protected void
createSessionTracker()
void
decInProcess()
void
dumpConf(java.io.PrintWriter pwriter)
void
dumpEphemerals(java.io.PrintWriter pwriter)
void
expire(SessionTracker.Session session)
void
finishSessionInit(ServerCnxn cnxn, boolean valid)
int
getClientPort()
int
getGlobalOutstandingLimit()
int
getInProcess()
long
getLastProcessedZxid()
return the last proceesed id from the datatreeint
getMaxSessionTimeout()
int
getMinSessionTimeout()
int
getNumAliveConnections()
return the total number of client connections that are alive to this serverlong
getOutstandingRequests()
return the outstanding requests in the queue, which havent been processed yetServerCnxnFactory
getServerCnxnFactory()
long
getServerId()
static int
getSnapCount()
java.lang.String
getState()
int
getTickTime()
FileTxnSnapLog
getTxnLogFactory()
ZKDatabase
getZKDatabase()
get the zookeeper database for this serverZooKeeperServerListener
getZooKeeperServerListener()
long
getZxid()
This should be called from a synchronized block on this!void
incInProcess()
boolean
isRunning()
protected void
killSession(long sessionId, long zxid)
void
loadData()
Restore sessions and datavoid
processConnectRequest(ServerCnxn cnxn, java.nio.ByteBuffer incomingBuffer)
void
processPacket(ServerCnxn cnxn, java.nio.ByteBuffer incomingBuffer)
DataTree.ProcessTxnResult
processTxn(TxnHeader hdr, org.apache.jute.Record txn)
protected void
registerJMX()
void
reopenSession(ServerCnxn cnxn, long sessionId, byte[] passwd, int sessionTimeout)
protected void
revalidateSession(ServerCnxn cnxn, long sessionId, int sessionTimeout)
ServerStats
serverStats()
void
setMaxSessionTimeout(int max)
void
setMinSessionTimeout(int min)
void
setOwner(long id, java.lang.Object owner)
set the owner of this session as ownervoid
setServerCnxnFactory(ServerCnxnFactory factory)
protected void
setState(ZooKeeperServer.State state)
Sets the state of ZooKeeper server.void
setTickTime(int tickTime)
void
setTxnLogFactory(FileTxnSnapLog txnLog)
protected void
setupRequestProcessors()
void
setZKDatabase(ZKDatabase zkDb)
set the zkdatabase for this zookeeper servervoid
setZxid(long zxid)
boolean
shouldThrottle(long outStandingCount)
void
shutdown()
void
shutdown(boolean fullyShutDown)
Shut down the server instancevoid
startdata()
protected void
startSessionTracker()
void
startup()
void
submitRequest(Request si)
void
takeSnapshot()
void
truncateLog(long zxid)
trunccate the log to get in sync with others if in a quorumprotected void
unregisterJMX()
-
-
-
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.IOException
This 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.InterruptedException
Restore sessions and data- Throws:
java.io.IOException
java.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:
expire
in interfaceSessionTracker.SessionExpirer
-
registerJMX
protected void registerJMX()
-
startdata
public void startdata() throws java.io.IOException, java.lang.InterruptedException
- Throws:
java.io.IOException
java.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.
ZooKeeperServerListenerImpl
notifies 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.SessionExpiredException
set 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:
getServerId
in 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:
getLastProcessedZxid
in interfaceServerStats.Provider
-
getOutstandingRequests
public long getOutstandingRequests()
return the outstanding requests in the queue, which havent been processed yet- Specified by:
getOutstandingRequests
in interfaceServerStats.Provider
-
truncateLog
public void truncateLog(long zxid) throws java.io.IOException
trunccate 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:
getState
in 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:
getNumAliveConnections
in 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)
-
-