Package org.apache.zookeeper.server
Class ZKDatabase
- java.lang.Object
-
- org.apache.zookeeper.server.ZKDatabase
-
public class ZKDatabase extends java.lang.ObjectThis 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
Fields Modifier and Type Field Description protected static intcommitLogBufferstatic intcommitLogCountprotected java.util.LinkedList<Leader.Proposal>committedLogprotected DataTreedataTreemake sure on a clear you take care of all these members.protected java.util.concurrent.locks.ReentrantReadWriteLocklogLockprotected longmaxCommittedLogprotected longminCommittedLogprotected java.util.concurrent.ConcurrentHashMap<java.lang.Long,java.lang.Integer>sessionsWithTimeoutsprotected FileTxnSnapLogsnapLog
-
Constructor Summary
Constructors Constructor Description ZKDatabase(FileTxnSnapLog snapLog)the filetxnsnaplog that this zk database maps to.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.List<ACL>aclForNode(DataNode n)voidaddCommittedProposal(Request request)maintains a list of last committedLog or so committed requests.booleanappend(Request si)append to the underlying transaction logvoidclear()clear the zkdatabase.voidclose()close this database.voidcommit()commit to the underlying transaction logvoiddeserializeSnapshot(org.apache.jute.InputArchive ia)deserialize a snapshot from an input archivevoiddumpEphemerals(java.io.PrintWriter pwriter)write a text dump of all the ephemerals in the datatreelongfastForwardDataBase()Fast forward the database adding transactions from the committed log into memory.java.util.List<ACL>getACL(java.lang.String path, Stat stat)get acl for a pathintgetAclSize()get the acl size of the datatreejava.util.List<java.lang.String>getChildren(java.lang.String path, Stat stat, Watcher watcher)get children list for this pathjava.util.LinkedList<Leader.Proposal>getCommittedLog()byte[]getData(java.lang.String path, Stat stat, Watcher watcher)get data and stat for a pathDataTreegetDataTree()the datatree for this zkdatabaselonggetDataTreeLastProcessedZxid()get the last processed zxid from a datatreejava.util.HashSet<java.lang.String>getEphemerals(long sessionId)the paths for ephemeral session idjava.util.concurrent.locks.ReentrantReadWriteLockgetLogLock()Get the lock that controls the committedLog.longgetmaxCommittedLog()the committed log for this zk databaselonggetminCommittedLog()the minimum committed transaction log available in memoryDataNodegetNode(java.lang.String path)get the datanode for this pathintgetNodeCount()the node count of the datatreejava.util.Collection<java.lang.Long>getSessions()return the sessions in the datatreejava.util.concurrent.ConcurrentHashMap<java.lang.Long,java.lang.Integer>getSessionWithTimeOuts()get sessions with timeoutsbooleanisInitialized()checks to see if the zk database has been initialized or not.booleanisSpecialPath(java.lang.String path)check if the path is special or notvoidkillSession(long sessionId, long zxid)kill a given session in the datatreelongloadDataBase()load the database from the disk onto memory and also add the transactions to the committedlog in memory.DataTree.ProcessTxnResultprocessTxn(TxnHeader hdr, org.apache.jute.Record txn)the process txn on the datavoidremoveCnxn(ServerCnxn cnxn)remove a cnxn from the datatreevoidrollLog()roll the underlying logvoidserializeSnapshot(org.apache.jute.OutputArchive oa)serialize the snapshotvoidsetDataTreeInit(boolean b)set the datatree initialized or notvoidsetlastProcessedZxid(long zxid)the last processed zxid in the datatreevoidsetWatches(long relativeZxid, java.util.List<java.lang.String> dataWatches, java.util.List<java.lang.String> existWatches, java.util.List<java.lang.String> childWatches, Watcher watcher)set watches on the datatreeStatstatNode(java.lang.String path, ServerCnxn serverCnxn)stat the pathbooleantruncateLog(long zxid)Truncate the ZKDatabase to the specified zxid
-
-
-
Field Detail
-
dataTree
protected DataTree dataTree
make sure on a clear you take care of all these members.
-
sessionsWithTimeouts
protected java.util.concurrent.ConcurrentHashMap<java.lang.Long,java.lang.Integer> sessionsWithTimeouts
-
snapLog
protected FileTxnSnapLog snapLog
-
minCommittedLog
protected long minCommittedLog
-
maxCommittedLog
protected long maxCommittedLog
-
commitLogCount
public static final int commitLogCount
- See Also:
- Constant Field Values
-
commitLogBuffer
protected static int commitLogBuffer
-
committedLog
protected java.util.LinkedList<Leader.Proposal> committedLog
-
logLock
protected java.util.concurrent.locks.ReentrantReadWriteLock logLock
-
-
Constructor Detail
-
ZKDatabase
public ZKDatabase(FileTxnSnapLog snapLog)
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 Detail
-
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
public DataTree 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
public java.util.concurrent.locks.ReentrantReadWriteLock 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
public java.util.LinkedList<Leader.Proposal> getCommittedLog()
-
getDataTreeLastProcessedZxid
public long getDataTreeLastProcessedZxid()
get the last processed zxid from a datatree- Returns:
- the last processed zxid of a datatree
-
setDataTreeInit
public void setDataTreeInit(boolean b)
set the datatree initialized or not- Parameters:
b- set the datatree initialized to b
-
getSessions
public java.util.Collection<java.lang.Long> getSessions()
return the sessions in the datatree- Returns:
- the data tree sessions
-
getSessionWithTimeOuts
public java.util.concurrent.ConcurrentHashMap<java.lang.Long,java.lang.Integer> getSessionWithTimeOuts()
get sessions with timeouts- Returns:
- the hashmap of sessions with timeouts
-
loadDataBase
public long loadDataBase() throws java.io.IOExceptionload 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:
java.io.IOException
-
fastForwardDataBase
public long fastForwardDataBase() throws java.io.IOExceptionFast forward the database adding transactions from the committed log into memory.- Returns:
- the last valid zxid.
- Throws:
java.io.IOException
-
addCommittedProposal
public void addCommittedProposal(Request request)
maintains a list of last committedLog or so committed requests. This is used for fast follower synchronization.- Parameters:
request- committed request
-
removeCnxn
public void removeCnxn(ServerCnxn cnxn)
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
public void dumpEphemerals(java.io.PrintWriter pwriter)
write a text dump of all the ephemerals in the datatree- Parameters:
pwriter- the output to write to
-
getNodeCount
public int getNodeCount()
the node count of the datatree- Returns:
- the node count of datatree
-
getEphemerals
public java.util.HashSet<java.lang.String> getEphemerals(long sessionId)
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
public DataTree.ProcessTxnResult processTxn(TxnHeader hdr, org.apache.jute.Record txn)
the process txn on the data- Parameters:
hdr- the txnheader for the txntxn- the transaction that needs to be processed- Returns:
- the result of processing the transaction on this datatree/zkdatabase
-
statNode
public Stat statNode(java.lang.String path, ServerCnxn serverCnxn) throws KeeperException.NoNodeException
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
public DataNode getNode(java.lang.String path)
get the datanode for this path- Parameters:
path- the path to lookup- Returns:
- the datanode for getting the path
-
getData
public byte[] getData(java.lang.String path, Stat stat, Watcher watcher) throws KeeperException.NoNodeExceptionget data and stat for a path- Parameters:
path- the path being queriedstat- the stat for this pathwatcher- the watcher function- Returns:
- Throws:
KeeperException.NoNodeException
-
setWatches
public void setWatches(long relativeZxid, java.util.List<java.lang.String> dataWatches, java.util.List<java.lang.String> existWatches, java.util.List<java.lang.String> childWatches, 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 resetwatcher- the watcher function
-
getACL
public java.util.List<ACL> getACL(java.lang.String path, Stat stat) throws KeeperException.NoNodeException
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 java.util.List<java.lang.String> getChildren(java.lang.String path, Stat stat, Watcher watcher) throws KeeperException.NoNodeExceptionget 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
-
isSpecialPath
public boolean isSpecialPath(java.lang.String path)
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
public boolean truncateLog(long zxid) throws java.io.IOExceptionTruncate 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:
java.io.IOException
-
deserializeSnapshot
public void deserializeSnapshot(org.apache.jute.InputArchive ia) throws java.io.IOExceptiondeserialize a snapshot from an input archive- Parameters:
ia- the input archive you want to deserialize from- Throws:
java.io.IOException
-
serializeSnapshot
public void serializeSnapshot(org.apache.jute.OutputArchive oa) throws java.io.IOException, java.lang.InterruptedExceptionserialize the snapshot- Parameters:
oa- the output archive to which the snapshot needs to be serialized- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
append
public boolean append(Request si) throws java.io.IOException
append to the underlying transaction log- Parameters:
si- the request to append- Returns:
- true if the append was succesfull and false if not
- Throws:
java.io.IOException
-
rollLog
public void rollLog() throws java.io.IOExceptionroll the underlying log- Throws:
java.io.IOException
-
commit
public void commit() throws java.io.IOExceptioncommit to the underlying transaction log- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOExceptionclose this database. free the resources- Throws:
java.io.IOException
-
-