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
,ReadOnlyZooKeeperServer
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.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 ServerCnxnFactory
secureServerCnxnFactory
protected ServerCnxnFactory
serverCnxnFactory
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)
Default constructor, relies on the config for its argument valuesZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime)
creates a zookeeperserver instance.ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, ZKDatabase zkDb)
Creates a ZooKeeperServer instance.
-
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, org.apache.zookeeper.proto.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()
ZooKeeperServerConf
getConf()
long
getDataDirSize()
java.util.Map<java.lang.Long,java.util.Set<java.lang.String>>
getEphemerals()
int
getGlobalOutstandingLimit()
int
getInProcess()
long
getLastProcessedZxid()
return the last proceesed id from the datatreelong
getLogDirSize()
int
getMaxClientCnxnsPerHost()
Maximum number of connections allowed from particular host (ip)int
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 yetint
getSecureClientPort()
ServerCnxnFactory
getSecureServerCnxnFactory()
ServerCnxnFactory
getServerCnxnFactory()
long
getServerId()
java.util.Map<java.lang.Long,java.util.Set<java.lang.Long>>
getSessionExpiryMap()
SessionTracker
getSessionTracker()
static int
getSnapCount()
java.lang.String
getState()
int
getTickTime()
long
getTxnLogElapsedSyncTime()
Returns the elapsed sync of time of transaction log in milliseconds.FileTxnSnapLog
getTxnLogFactory()
ZKDatabase
getZKDatabase()
get the zookeeper database for this serverZooKeeperServerListener
getZooKeeperServerListener()
long
getZxid()
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(Request request)
DataTree.ProcessTxnResult
processTxn(org.apache.zookeeper.txn.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
setCreateSessionTrackerServerId(int newId)
Change the server ID used bycreateSessionTracker()
.protected void
setLocalSessionFlag(Request si)
If the underlying Zookeeper server support local session, this method will set a isLocalSession to true if a request is associated with a local session.void
setMaxSessionTimeout(int max)
void
setMinSessionTimeout(int min)
void
setOwner(long id, java.lang.Object owner)
set the owner of this session as ownervoid
setSecureServerCnxnFactory(ServerCnxnFactory factory)
void
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
-
serverCnxnFactory
protected ServerCnxnFactory serverCnxnFactory
-
secureServerCnxnFactory
protected ServerCnxnFactory secureServerCnxnFactory
-
-
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, 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) throws java.io.IOException
creates a zookeeperserver instance.- Parameters:
txnLogFactory
- the file transaction snapshot logging classtickTime
- the ticktime for the server- 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) throws java.io.IOException
Default constructor, relies on the config for its argument values- Throws:
java.io.IOException
-
-
Method Detail
-
serverStats
public ServerStats serverStats()
-
dumpConf
public void dumpConf(java.io.PrintWriter pwriter)
-
getConf
public ZooKeeperServerConf getConf()
-
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()
-
getDataDirSize
public long getDataDirSize()
- Specified by:
getDataDirSize
in interfaceServerStats.Provider
-
getLogDirSize
public long getLogDirSize()
- Specified by:
getLogDirSize
in interfaceServerStats.Provider
-
getZxid
public long getZxid()
-
getSessionTracker
public SessionTracker getSessionTracker()
-
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()
-
setCreateSessionTrackerServerId
public void setCreateSessionTrackerServerId(int newId)
Change the server ID used bycreateSessionTracker()
. Must be called prior tostartup()
being called- Parameters:
newId
- ID to use
-
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()
- Returns:
- true if the server is running, false otherwise.
-
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, org.apache.zookeeper.proto.RequestHeader requestHeader)
-
getServerId
public long getServerId()
- Specified by:
getServerId
in interfaceSessionTracker.SessionExpirer
-
setLocalSessionFlag
protected void setLocalSessionFlag(Request si)
If the underlying Zookeeper server support local session, this method will set a isLocalSession to true if a request is associated with a local session.- Parameters:
si
-
-
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()
-
getSecureServerCnxnFactory
public ServerCnxnFactory getSecureServerCnxnFactory()
-
setSecureServerCnxnFactory
public void setSecureServerCnxnFactory(ServerCnxnFactory factory)
-
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
-
getNumAliveConnections
public int getNumAliveConnections()
return the total number of client connections that are alive to this server- Specified by:
getNumAliveConnections
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()
-
getSecureClientPort
public int getSecureClientPort()
-
getMaxClientCnxnsPerHost
public int getMaxClientCnxnsPerHost()
Maximum number of connections allowed from particular host (ip)
-
setTxnLogFactory
public void setTxnLogFactory(FileTxnSnapLog txnLog)
-
getTxnLogFactory
public FileTxnSnapLog getTxnLogFactory()
-
getTxnLogElapsedSyncTime
public long getTxnLogElapsedSyncTime()
Returns the elapsed sync of time of transaction log in milliseconds.
-
getState
public java.lang.String getState()
- Specified by:
getState
in interfaceServerStats.Provider
-
dumpEphemerals
public void dumpEphemerals(java.io.PrintWriter pwriter)
-
getEphemerals
public java.util.Map<java.lang.Long,java.util.Set<java.lang.String>> getEphemerals()
-
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(org.apache.zookeeper.txn.TxnHeader hdr, org.apache.jute.Record txn)
-
processTxn
public DataTree.ProcessTxnResult processTxn(Request request)
-
getSessionExpiryMap
public java.util.Map<java.lang.Long,java.util.Set<java.lang.Long>> getSessionExpiryMap()
-
-