Package org.apache.zookeeper.server
Class ZKDatabase
java.lang.Object
org.apache.zookeeper.server.ZKDatabase
This class maintains the in memory database of zookeeper
server states that includes the sessions, datatree and the
committed logs. It is booted up after reading the logs
and snapshots from the disk.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
int
protected Queue<Leader.Proposal>
protected DataTree
make sure on a clear you take care of all these members.static final int
static final double
protected ReentrantReadWriteLock
protected long
protected long
protected ConcurrentHashMap<Long,
Integer> protected FileTxnSnapLog
static final String
Default value is to use snapshot if txnlog size exceeds 1/3 the size of snapshot -
Constructor Summary
ConstructorsConstructorDescriptionZKDatabase
(FileTxnSnapLog snapLog) the filetxnsnaplog that this zk database maps to. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCommittedProposal
(Request request) maintains a list of last committedLog or so committed requests.void
Add a watchboolean
append to the underlying transaction loglong
void
clear()
clear the zkdatabase.void
close()
close this database.void
commit()
commit to the underlying transaction logboolean
compareDigest
(TxnHeader header, Record txn, TxnDigest digest) boolean
containsWatcher
(String path, Watcher.WatcherType type, Watcher watcher) Check whether the given watcher exists in datatreevoid
deserialize a snapshot from an input archivevoid
dumpEphemerals
(PrintWriter pwriter) write a text dump of all the ephemerals in the datatreelong
Fast forward the database adding transactions from the committed log into memory.get acl for a pathint
get the acl size of the datatreeint
getAllChildrenNumber
(String path) getChildren
(String path, Stat stat, Watcher watcher) get children list for this pathbyte[]
get data and stat for a paththe datatree for this zkdatabaselong
get the last processed zxid from a datatreegetEphemerals
(long sessionId) the paths for ephemeral session idGet the lock that controls the committedLog.long
the committed log for this zk databaselong
the minimum committed transaction log available in memoryget the datanode for this pathint
the node count of the datatreegetProposalsFromTxnLog
(long startZxid, long sizeLimit) Get proposals from txnlog.long
return the sessions in the datatreeget sessions with timeoutsint
Get the number of txn since last snapshotlong
Get the size of txn since last snapshotvoid
boolean
checks to see if the zk database has been initialized or not.boolean
isSpecialPath
(String path) check if the path is special or notboolean
void
killSession
(long sessionId, long zxid) kill a given session in the datatreelong
load the database from the disk onto memory and also add the transactions to the committedlog in memory.processTxn
(TxnHeader hdr, Record txn, TxnDigest digest) the process txn on the data and perform digest comparision.void
removeCnxn
(ServerCnxn cnxn) remove a cnxn from the datatreeboolean
removeWatch
(String path, Watcher.WatcherType type, Watcher watcher) Remove watch from the datatreevoid
Reset the number of txn since last rollLogvoid
rollLog()
roll the underlying logvoid
serialize the snapshotvoid
setlastProcessedZxid
(long zxid) the last processed zxid in the datatreevoid
setSnapshotSizeFactor
(double snapshotSizeFactor) Use for unit testing, so we can turn this feature on/offvoid
setWatches
(long relativeZxid, List<String> dataWatches, List<String> existWatches, List<String> childWatches, List<String> persistentWatches, List<String> persistentRecursiveWatches, Watcher watcher) set watches on the datatreestatNode
(String path, ServerCnxn serverCnxn) stat the pathboolean
truncateLog
(long zxid) Truncate the ZKDatabase to the specified zxid
-
Field Details
-
dataTree
make sure on a clear you take care of all these members. -
sessionsWithTimeouts
-
snapLog
-
minCommittedLog
protected long minCommittedLog -
maxCommittedLog
protected long maxCommittedLog -
SNAPSHOT_SIZE_FACTOR
Default value is to use snapshot if txnlog size exceeds 1/3 the size of snapshot- See Also:
-
DEFAULT_SNAPSHOT_SIZE_FACTOR
public static final double DEFAULT_SNAPSHOT_SIZE_FACTOR- See Also:
-
COMMIT_LOG_COUNT
- See Also:
-
DEFAULT_COMMIT_LOG_COUNT
public static final int DEFAULT_COMMIT_LOG_COUNT- See Also:
-
commitLogCount
public int commitLogCount -
committedLog
-
logLock
-
-
Constructor Details
-
ZKDatabase
the filetxnsnaplog that this zk database maps to. There is a one to one relationship between a filetxnsnaplog and zkdatabase.- Parameters:
snapLog
- the FileTxnSnapLog mapping this zkdatabase
-
-
Method Details
-
isInitialized
public boolean isInitialized()checks to see if the zk database has been initialized or not.- Returns:
- true if zk database is initialized and false if not
-
clear
public void clear()clear the zkdatabase. Note to developers - be careful to see that the clear method does clear out all the data structures in zkdatabase. -
getDataTree
the datatree for this zkdatabase- Returns:
- the datatree for this zkdatabase
-
getmaxCommittedLog
public long getmaxCommittedLog()the committed log for this zk database- Returns:
- the committed log for this zkdatabase
-
getminCommittedLog
public long getminCommittedLog()the minimum committed transaction log available in memory- Returns:
- the minimum committed transaction log available in memory
-
getLogLock
Get the lock that controls the committedLog. If you want to get the pointer to the committedLog, you need to use this lock to acquire a read lock before calling getCommittedLog()- Returns:
- the lock that controls the committed log
-
getCommittedLog
-
getDataTreeLastProcessedZxid
public long getDataTreeLastProcessedZxid()get the last processed zxid from a datatree- Returns:
- the last processed zxid of a datatree
-
getSessions
return the sessions in the datatree- Returns:
- the data tree sessions
-
getSessionCount
public long getSessionCount()- Returns:
- number of (global) sessions
-
getSessionWithTimeOuts
get sessions with timeouts- Returns:
- the hashmap of sessions with timeouts
-
loadDataBase
load the database from the disk onto memory and also add the transactions to the committedlog in memory.- Returns:
- the last valid zxid on disk
- Throws:
IOException
-
fastForwardDataBase
Fast forward the database adding transactions from the committed log into memory.- Returns:
- the last valid zxid.
- Throws:
IOException
-
addCommittedProposal
maintains a list of last committedLog or so committed requests. This is used for fast follower synchronization.- Parameters:
request
- committed request
-
isTxnLogSyncEnabled
public boolean isTxnLogSyncEnabled() -
calculateTxnLogSizeLimit
public long calculateTxnLogSizeLimit() -
getProposalsFromTxnLog
Get proposals from txnlog. Only packet part of proposal is populated.- Parameters:
startZxid
- the starting zxid of the proposalsizeLimit
- maximum on-disk size of txnlog to fetch 0 is unlimited, negative value means disable.- Returns:
- list of proposal (request part of each proposal is null)
-
aclForNode
-
removeCnxn
remove a cnxn from the datatree- Parameters:
cnxn
- the cnxn to remove from the datatree
-
killSession
public void killSession(long sessionId, long zxid) kill a given session in the datatree- Parameters:
sessionId
- the session id to be killedzxid
- the zxid of kill session transaction
-
dumpEphemerals
write a text dump of all the ephemerals in the datatree- Parameters:
pwriter
- the output to write to
-
getEphemerals
-
getNodeCount
public int getNodeCount()the node count of the datatree- Returns:
- the node count of datatree
-
getEphemerals
the paths for ephemeral session id- Parameters:
sessionId
- the session id for which paths match to- Returns:
- the paths for a session id
-
setlastProcessedZxid
public void setlastProcessedZxid(long zxid) the last processed zxid in the datatree- Parameters:
zxid
- the last processed zxid in the datatree
-
processTxn
the process txn on the data and perform digest comparision.- Parameters:
hdr
- the txnheader for the txntxn
- the transaction that needs to be processeddigest
- the expected digest. A null value would skip the check- Returns:
- the result of processing the transaction on this datatree/zkdatabase
-
statNode
stat the path- Parameters:
path
- the path for which stat is to be doneserverCnxn
- the servercnxn attached to this request- Returns:
- the stat of this node
- Throws:
KeeperException.NoNodeException
-
getNode
get the datanode for this path- Parameters:
path
- the path to lookup- Returns:
- the datanode for getting the path
-
getData
public byte[] getData(String path, Stat stat, Watcher watcher) throws KeeperException.NoNodeException get data and stat for a path- Parameters:
path
- the path being queriedstat
- the stat for this pathwatcher
- the watcher function- Throws:
KeeperException.NoNodeException
-
setWatches
public void setWatches(long relativeZxid, List<String> dataWatches, List<String> existWatches, List<String> childWatches, List<String> persistentWatches, List<String> persistentRecursiveWatches, Watcher watcher) set watches on the datatree- Parameters:
relativeZxid
- the relative zxid that client has seendataWatches
- the data watches the client wants to resetexistWatches
- the exists watches the client wants to resetchildWatches
- the child watches the client wants to resetpersistentWatches
- the persistent watches the client wants to resetpersistentRecursiveWatches
- the persistent recursive watches the client wants to resetwatcher
- the watcher function
-
addWatch
Add a watch- Parameters:
basePath
- watch basewatcher
- the watchermode
- a mode from ZooDefs.AddWatchModes
-
getACL
get acl for a path- Parameters:
path
- the path to query for aclstat
- the stat for the node- Returns:
- the acl list for this path
- Throws:
KeeperException.NoNodeException
-
getChildren
public List<String> getChildren(String path, Stat stat, Watcher watcher) throws KeeperException.NoNodeException get children list for this path- Parameters:
path
- the path of the nodestat
- the stat of the nodewatcher
- the watcher function for this path- Returns:
- the list of children for this path
- Throws:
KeeperException.NoNodeException
-
getAllChildrenNumber
- Throws:
KeeperException.NoNodeException
-
isSpecialPath
check if the path is special or not- Parameters:
path
- the input path- Returns:
- true if path is special and false if not
-
getAclSize
public int getAclSize()get the acl size of the datatree- Returns:
- the acl size of the datatree
-
truncateLog
Truncate the ZKDatabase to the specified zxid- Parameters:
zxid
- the zxid to truncate zk database to- Returns:
- true if the truncate is successful and false if not
- Throws:
IOException
-
deserializeSnapshot
deserialize a snapshot from an input archive- Parameters:
ia
- the input archive you want to deserialize from- Throws:
IOException
-
serializeSnapshot
serialize the snapshot- Parameters:
oa
- the output archive to which the snapshot needs to be serialized- Throws:
IOException
InterruptedException
-
append
append to the underlying transaction log- Parameters:
si
- the request to append- Returns:
- true if the append was succesfull and false if not
- Throws:
IOException
-
rollLog
roll the underlying log- Throws:
IOException
-
commit
commit to the underlying transaction log- Throws:
IOException
-
close
close this database. free the resources- Throws:
IOException
-
initConfigInZKDatabase
-
setSnapshotSizeFactor
public void setSnapshotSizeFactor(double snapshotSizeFactor) Use for unit testing, so we can turn this feature on/off- Parameters:
snapshotSizeFactor
- Set to minus value to turn this off.
-
containsWatcher
Check whether the given watcher exists in datatree- Parameters:
path
- node to check watcher existencetype
- type of watcherwatcher
- watcher function
-
removeWatch
Remove watch from the datatree- Parameters:
path
- node to remove watches fromtype
- type of watcher to removewatcher
- watcher function to remove
-
createDataTree
-
resetTxnCount
public void resetTxnCount()Reset the number of txn since last rollLog -
getTxnCount
public int getTxnCount()Get the number of txn since last snapshot -
getTxnSize
public long getTxnSize()Get the size of txn since last snapshot -
compareDigest
-