Class FileTxnSnapLog
- java.lang.Object
-
- org.apache.zookeeper.server.persistence.FileTxnSnapLog
-
public class FileTxnSnapLog extends Object
This is a helper class above the implementations of txnlog and snapshot classes
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FileTxnSnapLog.DatadirException
static class
FileTxnSnapLog.LogDirContentCheckException
static interface
FileTxnSnapLog.PlayBackListener
This listener helps the external apis calling restore to gather information while the data is being restored.static class
FileTxnSnapLog.SnapDirContentCheckException
-
Field Summary
Fields Modifier and Type Field Description static String
version
static int
VERSION
static String
ZOOKEEPER_DATADIR_AUTOCREATE
static String
ZOOKEEPER_DATADIR_AUTOCREATE_DEFAULT
static String
ZOOKEEPER_SNAPSHOT_TRUST_EMPTY
-
Constructor Summary
Constructors Constructor Description FileTxnSnapLog(File dataDir, File snapDir)
the constructor which takes the datadir and snapdir.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
append(Request si)
append the request to the transaction logsvoid
close()
close the transaction log filesvoid
commit()
commit the transaction of logslong
fastForwardFromEdits(DataTree dt, Map<Long,Integer> sessions, FileTxnSnapLog.PlayBackListener listener)
This function will fast forward the server database to have the latest transactions in it.File
findMostRecentSnapshot()
the most recent snapshot in the snapshot directoryList<File>
findNRecentSnapshots(int n)
the n most recent snapshotsList<File>
findNValidSnapshots(int n)
the n recent valid snapshotsFile
getDataDir()
get the datadir used by this filetxn snap loglong
getLastLoggedZxid()
the last logged zxid on the transaction logsSnapshotInfo
getLastSnapshotInfo()
get information of the last saved/restored snapshotFile
getSnapDir()
get the snap dir used by this filetxn snap logFile[]
getSnapshotLogs(long zxid)
get the snapshot logs which may contain transactions newer than the given zxid.long
getTotalLogSize()
long
getTxnLogElapsedSyncTime()
void
processTransaction(TxnHeader hdr, DataTree dt, Map<Long,Integer> sessions, Record txn)
process the transaction on the datatreeTxnLog.TxnIterator
readTxnLog(long zxid)
Get TxnIterator for iterating through txnlog starting at a given zxidTxnLog.TxnIterator
readTxnLog(long zxid, boolean fastForward)
Get TxnIterator for iterating through txnlog starting at a given zxidlong
restore(DataTree dt, Map<Long,Integer> sessions, FileTxnSnapLog.PlayBackListener listener)
this function restores the server database after reading from the snapshots and transaction logsvoid
rollLog()
roll the transaction logsvoid
save(DataTree dataTree, ConcurrentHashMap<Long,Integer> sessionsWithTimeouts, boolean syncSnap)
save the datatree and the sessions into a snapshotvoid
setServerStats(ServerStats serverStats)
void
setTotalLogSize(long size)
boolean
shouldForceWriteInitialSnapshotAfterLeaderElection()
whether to force the write of an initial snapshot after a leader election, to address ZOOKEEPER-3781 after upgrading from Zookeeper 3.4.x.boolean
truncateLog(long zxid)
truncate the transaction logs the zxid specified
-
-
-
Field Detail
-
VERSION
public static final int VERSION
- See Also:
- Constant Field Values
-
version
public static final String version
- See Also:
- Constant Field Values
-
ZOOKEEPER_DATADIR_AUTOCREATE
public static final String ZOOKEEPER_DATADIR_AUTOCREATE
- See Also:
- Constant Field Values
-
ZOOKEEPER_DATADIR_AUTOCREATE_DEFAULT
public static final String ZOOKEEPER_DATADIR_AUTOCREATE_DEFAULT
- See Also:
- Constant Field Values
-
ZOOKEEPER_SNAPSHOT_TRUST_EMPTY
public static final String ZOOKEEPER_SNAPSHOT_TRUST_EMPTY
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
FileTxnSnapLog
public FileTxnSnapLog(File dataDir, File snapDir) throws IOException
the constructor which takes the datadir and snapdir.- Parameters:
dataDir
- the transaction directorysnapDir
- the snapshot directory- Throws:
IOException
-
-
Method Detail
-
setServerStats
public void setServerStats(ServerStats serverStats)
-
getDataDir
public File getDataDir()
get the datadir used by this filetxn snap log- Returns:
- the data dir
-
getSnapDir
public File getSnapDir()
get the snap dir used by this filetxn snap log- Returns:
- the snap dir
-
getLastSnapshotInfo
public SnapshotInfo getLastSnapshotInfo()
get information of the last saved/restored snapshot- Returns:
- info of last snapshot
-
shouldForceWriteInitialSnapshotAfterLeaderElection
public boolean shouldForceWriteInitialSnapshotAfterLeaderElection()
whether to force the write of an initial snapshot after a leader election, to address ZOOKEEPER-3781 after upgrading from Zookeeper 3.4.x.- Returns:
- true if an initial snapshot should be written even if not otherwise required, false otherwise.
-
restore
public long restore(DataTree dt, Map<Long,Integer> sessions, FileTxnSnapLog.PlayBackListener listener) throws IOException
this function restores the server database after reading from the snapshots and transaction logs- Parameters:
dt
- the datatree to be restoredsessions
- the sessions to be restoredlistener
- the playback listener to run on the database restoration- Returns:
- the highest zxid restored
- Throws:
IOException
-
fastForwardFromEdits
public long fastForwardFromEdits(DataTree dt, Map<Long,Integer> sessions, FileTxnSnapLog.PlayBackListener listener) throws IOException
This function will fast forward the server database to have the latest transactions in it. This is the same as restore, but only reads from the transaction logs and not restores from a snapshot.- Parameters:
dt
- the datatree to write transactions to.sessions
- the sessions to be restored.listener
- the playback listener to run on the database transactions.- Returns:
- the highest zxid restored.
- Throws:
IOException
-
readTxnLog
public TxnLog.TxnIterator readTxnLog(long zxid) throws IOException
Get TxnIterator for iterating through txnlog starting at a given zxid- Parameters:
zxid
- starting zxid- Returns:
- TxnIterator
- Throws:
IOException
-
readTxnLog
public TxnLog.TxnIterator readTxnLog(long zxid, boolean fastForward) throws IOException
Get TxnIterator for iterating through txnlog starting at a given zxid- Parameters:
zxid
- starting zxidfastForward
- true if the iterator should be fast forwarded to point to the txn of a given zxid, else the iterator will point to the starting txn of a txnlog that may contain txn of a given zxid- Returns:
- TxnIterator
- Throws:
IOException
-
processTransaction
public void processTransaction(TxnHeader hdr, DataTree dt, Map<Long,Integer> sessions, Record txn) throws KeeperException.NoNodeException
process the transaction on the datatree- Parameters:
hdr
- the hdr of the transactiondt
- the datatree to apply transaction tosessions
- the sessions to be restoredtxn
- the transaction to be applied- Throws:
KeeperException.NoNodeException
-
getLastLoggedZxid
public long getLastLoggedZxid()
the last logged zxid on the transaction logs- Returns:
- the last logged zxid
-
save
public void save(DataTree dataTree, ConcurrentHashMap<Long,Integer> sessionsWithTimeouts, boolean syncSnap) throws IOException
save the datatree and the sessions into a snapshot- Parameters:
dataTree
- the datatree to be serialized onto disksessionsWithTimeouts
- the session timeouts to be serialized onto disksyncSnap
- sync the snapshot immediately after write- Throws:
IOException
-
truncateLog
public boolean truncateLog(long zxid)
truncate the transaction logs the zxid specified- Parameters:
zxid
- the zxid to truncate the logs to- Returns:
- true if able to truncate the log, false if not
- Throws:
IOException
-
findMostRecentSnapshot
public File findMostRecentSnapshot() throws IOException
the most recent snapshot in the snapshot directory- Returns:
- the file that contains the most recent snapshot
- Throws:
IOException
-
findNRecentSnapshots
public List<File> findNRecentSnapshots(int n) throws IOException
the n most recent snapshots- Parameters:
n
- the number of recent snapshots- Returns:
- the list of n most recent snapshots, with the most recent in front
- Throws:
IOException
-
findNValidSnapshots
public List<File> findNValidSnapshots(int n) throws IOException
the n recent valid snapshots- Parameters:
n
- the number of recent valid snapshots- Returns:
- the list of n recent valid snapshots, with the most recent in front
- Throws:
IOException
-
getSnapshotLogs
public File[] getSnapshotLogs(long zxid)
get the snapshot logs which may contain transactions newer than the given zxid. This includes logs with starting zxid greater than given zxid, as well as the newest transaction log with starting zxid less than given zxid. The latter log file may contain transactions beyond given zxid.- Parameters:
zxid
- the zxid that contains logs greater than zxid- Returns:
- the snapshot logs which may contain transactions newer than the given zxid
-
append
public boolean append(Request si) throws IOException
append the request to the transaction logs- Parameters:
si
- the request to be appended- Returns:
- true iff something appended, otw false
- Throws:
IOException
-
commit
public void commit() throws IOException
commit the transaction of logs- Throws:
IOException
-
getTxnLogElapsedSyncTime
public long getTxnLogElapsedSyncTime()
- Returns:
- elapsed sync time of transaction log commit in milliseconds
-
rollLog
public void rollLog() throws IOException
roll the transaction logs- Throws:
IOException
-
close
public void close() throws IOException
close the transaction log files- Throws:
IOException
-
setTotalLogSize
public void setTotalLogSize(long size)
-
getTotalLogSize
public long getTotalLogSize()
-
-