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 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 ClassesModifier and TypeClassDescriptionstatic class
protected static enum
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
static final String
static final int
static final int
static final int
static final String
static final String
static final boolean
protected RequestProcessor
static final String
static final String
static final String
protected String
static final String
Starting size of read and write ByteArrayOutputBuffers.static final int
protected DataTreeBean
protected ZooKeeperServerBean
protected JvmPauseMonitor
protected int
Socket listen backlog.protected static final org.slf4j.Logger
protected int
value of -1 indicates unset, use defaultprotected int
value of -1 indicates unset, use defaultstatic final Exception
protected boolean
static final String
protected ServerCnxnFactory
protected ServerCnxnFactory
protected SessionTracker
static final String
static final String
protected ZooKeeperServer.State
protected static int
protected int
static final String
static final String
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a ZooKeeperServer instance.ZooKeeperServer
(File snapDir, 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, int minSessionTimeout, int maxSessionTimeout, int clientPortListenBacklog, ZKDatabase zkDb, String initialConfig) Keeping this constructor for backward compatibilityZooKeeperServer
(FileTxnSnapLog txnLogFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, int clientPortListenBacklog, ZKDatabase zkDb, String initialConfig, boolean reconfigEnabled) * Creates a ZooKeeperServer instance.ZooKeeperServer
(FileTxnSnapLog txnLogFactory, int tickTime, String initialConfig) creates a zookeeperserver instance.ZooKeeperServer
(JvmPauseMonitor jvmPauseMonitor, FileTxnSnapLog txnLogFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, int clientPortListenBacklog, ZKDatabase zkDb, String initialConfig) Adds JvmPauseMonitor and callsZooKeeperServer(FileTxnSnapLog, int, int, int, int, ZKDatabase, String)
-
Method Summary
Modifier and TypeMethodDescriptionboolean
authWriteRequest
(Request request) Check Write Requests for Potential Access Restrictionsvoid
Grant or deny authorization to an operation on a node as a function of:protected boolean
checkPasswd
(long sessionId, byte[] passwd) void
checkQuota
(String path, byte[] lastData, byte[] data, int type) check a path whether exceeded the quota.boolean
checkRequestSizeWhenReceivingMessage
(int length) void
closeSession
(long sessionId) void
closeSession
(ServerCnxn cnxn, RequestHeader requestHeader) protected RequestThrottler
protected void
void
void
dumpConf
(PrintWriter pwriter) void
dumpEphemerals
(PrintWriter pwriter) void
dumpMonitorValues
(BiConsumer<String, Object> response) Hook into admin server, useful to expose additional data that do not represent metrics.void
void
expire
(long sessionId) void
expire
(SessionTracker.Session session) void
finishSessionInit
(ServerCnxn cnxn, boolean valid) int
int
getConf()
double
long
int
int
int
int
int
int
long
return the last processed id from the datatreelong
int
Maximum number of connections allowed from particular host (ip)int
int
int
return the total number of client connections that are alive to this serverint
long
return the outstanding requests in the queue, which haven't been processed yetint
long
static int
static long
getState()
static int
int
long
Returns the elapsed sync of time of transaction log in milliseconds.get the zookeeper database for this serverlong
getZxid()
void
static boolean
static boolean
static boolean
boolean
boolean
boolean
static boolean
protected void
killSession
(long sessionId, long zxid) void
loadData()
Restore sessions and datavoid
processConnectRequest
(ServerCnxn cnxn, ConnectRequest request) void
processPacket
(ServerCnxn cnxn, RequestHeader h, RequestRecord request) processTxn
(Request request) processTxn
(TxnHeader hdr, Record txn) protected void
protected void
void
reopenSession
(ServerCnxn cnxn, long sessionId, byte[] passwd, int sessionTimeout) void
requestFinished
(Request request) long
restoreFromSnapshot
(InputStream inputStream) Restores database from a snapshot.protected void
revalidateSession
(ServerCnxn cnxn, long sessionId, int sessionTimeout) void
setClientPortListenBacklog
(int backlog) static void
setCloseSessionTxnEnabled
(boolean enabled) void
setCreateSessionTrackerServerId
(int newId) Change the server ID used bycreateSessionTracker()
.static void
setDigestEnabled
(boolean digestEnabled) static void
setEnableEagerACLCheck
(boolean enabled) void
setLargeRequestMaxBytes
(int bytes) void
setLargeRequestThreshold
(int threshold) protected void
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
set the owner of this session as ownervoid
setResponseCachingEnabled
(boolean isEnabled) void
static void
setSerializeLastProcessedZxidEnabled
(boolean serializeLastZxidEnabled) void
setServerCnxnFactory
(ServerCnxnFactory factory) protected void
setState
(ZooKeeperServer.State state) Sets the state of ZooKeeper server.static void
setThrottledOpWaitTime
(int time) void
setTickTime
(int tickTime) void
setTxnLogFactory
(FileTxnSnapLog txnLog) protected void
void
setZKDatabase
(ZKDatabase zkDb) set the zkdatabase for this zookeeper servervoid
setZxid
(long zxid) boolean
boolean
shouldThrottle
(long outStandingCount) final void
shutdown()
final void
shutdown
(boolean fullyShutDown) Shut down the server instanceprotected void
void
protected void
protected void
protected void
void
startup()
void
submitRequest
(Request si) void
takeSnapshot
(boolean syncSnap) takeSnapshot
(boolean syncSnap, boolean isSevere, boolean fastForwardFromEdits) Takes a snapshot on the server.void
truncateLog
(long zxid) truncate the log to get in sync with others if in a quorumprotected void
protected void
protected void
validateSession
(ServerCnxn cnxn, long sessionId) Validate if a particular session can be reestablished.
-
Field Details
-
LOG
protected static final org.slf4j.Logger LOG -
GLOBAL_OUTSTANDING_LIMIT
- See Also:
-
ENABLE_EAGER_ACL_CHECK
- See Also:
-
SKIP_ACL
- See Also:
-
ENFORCE_QUOTA
- See Also:
-
enforceQuota
public static final boolean enforceQuota -
SASL_SUPER_USER
- See Also:
-
ALLOW_SASL_FAILED_CLIENTS
- See Also:
-
ZOOKEEPER_DIGEST_ENABLED
- See Also:
-
ZOOKEEPER_SERIALIZE_LAST_PROCESSED_ZXID_ENABLED
- See Also:
-
CLOSE_SESSION_TXN_ENABLED
- See Also:
-
jmxServerBean
-
jmxDataTreeBean
-
DEFAULT_TICK_TIME
public static final int DEFAULT_TICK_TIME- See Also:
-
tickTime
protected int tickTime -
DEFAULT_THROTTLED_OP_WAIT_TIME
public static final int DEFAULT_THROTTLED_OP_WAIT_TIME- See Also:
-
throttledOpWaitTime
protected static volatile int throttledOpWaitTime -
minSessionTimeout
protected int minSessionTimeoutvalue of -1 indicates unset, use default -
maxSessionTimeout
protected int maxSessionTimeoutvalue of -1 indicates unset, use default -
listenBacklog
protected int listenBacklogSocket listen backlog. Value of -1 indicates unset -
sessionTracker
-
ok
-
firstProcessor
-
jvmPauseMonitor
-
state
-
initialConfig
-
reconfigEnabled
protected boolean reconfigEnabled -
serverCnxnFactory
-
secureServerCnxnFactory
-
INT_BUFFER_STARTING_SIZE_BYTES
Starting size of read and write ByteArrayOutputBuffers. Default is 32 bytes. Flag not used for small transfers like connectResponses.- See Also:
-
DEFAULT_STARTING_BUFFER_SIZE
public static final int DEFAULT_STARTING_BUFFER_SIZE- See Also:
-
intBufferStartingSizeBytes
public static final int intBufferStartingSizeBytes -
GET_DATA_RESPONSE_CACHE_SIZE
- See Also:
-
GET_CHILDREN_RESPONSE_CACHE_SIZE
- See Also:
-
SNAP_COUNT
- See Also:
-
-
Constructor Details
-
ZooKeeperServer
public ZooKeeperServer()Creates a ZooKeeperServer instance. Nothing is setup, use the setX methods to prepare the instance (eg datadir, datalogdir, ticktime, builder, etc...) -
ZooKeeperServer
public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, int clientPortListenBacklog, ZKDatabase zkDb, String initialConfig) Keeping this constructor for backward compatibility -
ZooKeeperServer
public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, int clientPortListenBacklog, ZKDatabase zkDb, String initialConfig, boolean reconfigEnabled) * Creates a ZooKeeperServer instance. It sets everything up, but doesn't actually start listening for clients until run() is invoked. -
ZooKeeperServer
public ZooKeeperServer(JvmPauseMonitor jvmPauseMonitor, FileTxnSnapLog txnLogFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, int clientPortListenBacklog, ZKDatabase zkDb, String initialConfig) Adds JvmPauseMonitor and callsZooKeeperServer(FileTxnSnapLog, int, int, int, int, ZKDatabase, String)
-
ZooKeeperServer
creates a zookeeperserver instance.- Parameters:
txnLogFactory
- the file transaction snapshot logging classtickTime
- the ticktime for the server- Throws:
IOException
-
ZooKeeperServer
This constructor is for backward compatibility with the existing unit test code. It defaults to FileLogProvider persistence provider.- Throws:
IOException
-
ZooKeeperServer
Default constructor, relies on the config for its argument values- Throws:
IOException
-
-
Method Details
-
isEnableEagerACLCheck
public static boolean isEnableEagerACLCheck() -
setEnableEagerACLCheck
public static void setEnableEagerACLCheck(boolean enabled) -
isCloseSessionTxnEnabled
public static boolean isCloseSessionTxnEnabled() -
setCloseSessionTxnEnabled
public static void setCloseSessionTxnEnabled(boolean enabled) -
getInitialConfig
-
serverStats
-
getRequestPathMetricsCollector
-
connThrottle
-
dumpConf
-
getConf
-
getZKDatabase
get the zookeeper database for this server- Returns:
- the zookeeper database for this server
-
setZKDatabase
set the zkdatabase for this zookeeper server- Parameters:
zkDb
-
-
loadData
Restore sessions and data- Throws:
IOException
InterruptedException
-
takeSnapshot
- Throws:
IOException
-
takeSnapshot
- Throws:
IOException
-
takeSnapshot
public File takeSnapshot(boolean syncSnap, boolean isSevere, boolean fastForwardFromEdits) throws IOException Takes a snapshot on the server.- Parameters:
syncSnap
- syncSnap sync the snapshot immediately after writeisSevere
- if true system exist, otherwise throw IOExceptionfastForwardFromEdits
- whether fast forward database to the latest recorded transactions- Returns:
- file snapshot file object
- Throws:
IOException
-
restoreFromSnapshot
Restores database from a snapshot. It is used by the restore admin server command.- Parameters:
inputStream
- input stream of snapshot- Throws:
IOException
-
shouldForceWriteInitialSnapshotAfterLeaderElection
public boolean shouldForceWriteInitialSnapshotAfterLeaderElection() -
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
-
setZxid
public void setZxid(long zxid) -
closeSession
public void closeSession(long sessionId) -
killSession
protected void killSession(long sessionId, long zxid) -
expire
- Specified by:
expire
in interfaceSessionTracker.SessionExpirer
-
expire
public void expire(long sessionId) -
registerJMX
protected void registerJMX() -
startdata
- Throws:
IOException
InterruptedException
-
startup
public void startup() -
startJvmPauseMonitor
protected void startJvmPauseMonitor() -
startRequestThrottler
protected void startRequestThrottler() -
createRequestThrottler
-
setupRequestProcessors
protected void setupRequestProcessors() -
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
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.
- During maintenance (e.g. restore) the server sets the state to MAINTENANCE
- Parameters:
state
- new server state.
-
isRunning
public boolean isRunning()- Returns:
- true if the server is running, false otherwise.
-
shutdown
public final void shutdown() -
shutdown
public final void shutdown(boolean fullyShutDown) Shut down the server instance- Parameters:
fullyShutDown
- true when no other server will use the same database to replace this one
-
shutdownComponents
protected void shutdownComponents() -
unregisterJMX
protected void unregisterJMX() -
incInProcess
public void incInProcess() -
decInProcess
public void decInProcess() -
getInProcess
public int getInProcess() -
getInflight
public int getInflight() -
checkPasswd
protected boolean checkPasswd(long sessionId, byte[] passwd) -
setOwner
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 IOException - Throws:
IOException
-
reopenSession
public void reopenSession(ServerCnxn cnxn, long sessionId, byte[] passwd, int sessionTimeout) throws IOException - Throws:
IOException
-
finishSessionInit
-
closeSession
-
getServerId
public long getServerId()- Specified by:
getServerId
in interfaceSessionTracker.SessionExpirer
-
setLocalSessionFlag
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
-
enqueueRequest
-
submitRequestNow
-
getSnapCount
public static int getSnapCount() -
getGlobalOutstandingLimit
public int getGlobalOutstandingLimit() -
getSnapSizeInBytes
public static long getSnapSizeInBytes() -
setServerCnxnFactory
-
getServerCnxnFactory
-
getSecureServerCnxnFactory
-
setSecureServerCnxnFactory
-
getLastProcessedZxid
public long getLastProcessedZxid()return the last processed id from the datatree- Specified by:
getLastProcessedZxid
in interfaceServerStats.Provider
-
getOutstandingRequests
public long getOutstandingRequests()return the outstanding requests in the queue, which haven't 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
truncate 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:
IOException
-
getTickTime
public int getTickTime() -
setTickTime
public void setTickTime(int tickTime) -
getThrottledOpWaitTime
public static int getThrottledOpWaitTime() -
setThrottledOpWaitTime
public static void setThrottledOpWaitTime(int time) -
getMinSessionTimeout
public int getMinSessionTimeout() -
setMinSessionTimeout
public void setMinSessionTimeout(int min) -
getMaxSessionTimeout
public int getMaxSessionTimeout() -
setMaxSessionTimeout
public void setMaxSessionTimeout(int max) -
getClientPortListenBacklog
public int getClientPortListenBacklog() -
setClientPortListenBacklog
public void setClientPortListenBacklog(int backlog) -
getClientPort
public int getClientPort() -
getSecureClientPort
public int getSecureClientPort() -
getMaxClientCnxnsPerHost
public int getMaxClientCnxnsPerHost()Maximum number of connections allowed from particular host (ip) -
setTxnLogFactory
-
getTxnLogFactory
-
getTxnLogElapsedSyncTime
public long getTxnLogElapsedSyncTime()Returns the elapsed sync of time of transaction log in milliseconds. -
getState
- Specified by:
getState
in interfaceServerStats.Provider
-
dumpEphemerals
-
getEphemerals
-
getConnectionDropChance
public double getConnectionDropChance() -
processConnectRequest
public void processConnectRequest(ServerCnxn cnxn, ConnectRequest request) throws IOException, ClientCnxnLimitException - Throws:
IOException
ClientCnxnLimitException
-
validateSession
Validate if a particular session can be reestablished.- Parameters:
cnxn
-sessionId
-- Throws:
IOException
-
shouldThrottle
public boolean shouldThrottle(long outStandingCount) -
getLargeRequestMaxBytes
public int getLargeRequestMaxBytes() -
setLargeRequestMaxBytes
public void setLargeRequestMaxBytes(int bytes) -
getLargeRequestThreshold
public int getLargeRequestThreshold() -
setLargeRequestThreshold
public void setLargeRequestThreshold(int threshold) -
getLargeRequestBytes
public int getLargeRequestBytes() -
checkRequestSizeWhenReceivingMessage
- Throws:
IOException
-
requestFinished
-
processPacket
public void processPacket(ServerCnxn cnxn, RequestHeader h, RequestRecord request) throws IOException - Throws:
IOException
-
processTxn
-
processTxn
-
getSessionExpiryMap
-
isResponseCachingEnabled
public boolean isResponseCachingEnabled() -
setResponseCachingEnabled
public void setResponseCachingEnabled(boolean isEnabled) -
getReadResponseCache
-
getGetChildrenResponseCache
-
registerMetrics
protected void registerMetrics() -
unregisterMetrics
protected void unregisterMetrics() -
dumpMonitorValues
Hook into admin server, useful to expose additional data that do not represent metrics.- Parameters:
response
- a sink which collects the data.
-
checkACL
public void checkACL(ServerCnxn cnxn, List<ACL> acl, int perm, List<Id> ids, String path, List<ACL> setAcls) throws KeeperException.NoAuthException Grant or deny authorization to an operation on a node as a function of:- Parameters:
cnxn
- : the server connection or null for admin server commandsacl
- : set of ACLs for the nodeperm
- : the permission that the client is requestingids
- : the credentials supplied by the clientpath
- : the ZNode pathsetAcls
- : for set ACL operations, the list of ACLs being set. Otherwise null.- Throws:
KeeperException.NoAuthException
-
checkQuota
public void checkQuota(String path, byte[] lastData, byte[] data, int type) throws KeeperException.QuotaExceededException check a path whether exceeded the quota.- Parameters:
path
- the path of the node, used for the quota prefix checklastData
- the current node data,null
for nonedata
- the data to be set, ornull
for nonetype
- currently, create and setData need to check quota- Throws:
KeeperException.QuotaExceededException
-
isDigestEnabled
public static boolean isDigestEnabled() -
setDigestEnabled
public static void setDigestEnabled(boolean digestEnabled) -
isSerializeLastProcessedZxidEnabled
public static boolean isSerializeLastProcessedZxidEnabled() -
setSerializeLastProcessedZxidEnabled
public static void setSerializeLastProcessedZxidEnabled(boolean serializeLastZxidEnabled) -
authWriteRequest
Check Write Requests for Potential Access Restrictions Before a request is being proposed to the quorum, lets check it against local ACLs. Non-write requests (read, session, etc.) are passed along. Invalid requests are sent a response. While we are at it, if the request will set an ACL: make sure it's a valid one.- Parameters:
request
-- Returns:
- true if request is permitted, false if not.
- Throws:
IOException
-
getOutstandingHandshakeNum
public int getOutstandingHandshakeNum() -
isReconfigEnabled
public boolean isReconfigEnabled() -
getZkShutdownHandler
-