use of org.apache.zookeeper.server.persistence.FileTxnLog in project coprhd-controller by CoprHD.
the class ZkTxnHandler method getLastValidZxid.
/**
* Get last valid logged txn id.
*/
public long getLastValidZxid() {
dataDir = new File(dataPath, version + VERSION);
String sDataDir = dataDir.getAbsolutePath();
FileTxnLog txnLog = new FileTxnLog(dataDir);
// If there is corrupted txn before the last txn log file,
// txnLog.getLastLoggedZxid() would incorrectly return the 1st txn of the last txn log file.
// We could cover this ZK bug by moving the txnlog files (useless and already snapshoped) but the last.
File[] files = txnLog.getLogFiles(dataDir.listFiles(), 0);
if (files.length > 1) {
for (int i = 0; i < files.length - 1; i++) {
File targetFile = new File(sDataDir, BACKUP_PREFIX + files[i].getName());
files[i].renameTo(targetFile);
}
}
long lastValidZxid = txnLog.getLastLoggedZxid();
String tmpstr = String.format("last valid logged zxid:%s(hex)", Long.toHexString(lastValidZxid));
log.info(tmpstr);
System.out.println(tmpstr);
return lastValidZxid;
}
use of org.apache.zookeeper.server.persistence.FileTxnLog in project coprhd-controller by CoprHD.
the class ZkTxnHandler method truncateToZxid.
/**
* Truncate to the specific txn.
*
* @param zxid The id(hex) of the txn to be truncated to
*/
public boolean truncateToZxid(String zxid) {
long lastValidZxid = getLastValidZxid();
long targetZxid = Long.parseLong(zxid, 16);
if (targetZxid < lastValidZxid) {
String errstr = String.format("It is not allowed to truncate to the txn %s(hex) which is prior to the last valid txn %s(hex)! It would lose data!", Long.toHexString(targetZxid), Long.toHexString(lastValidZxid));
log.error(errstr);
System.out.println(errstr);
return false;
}
try {
dataDir = new File(dataPath, version + VERSION);
FileTxnLog truncLog = new FileTxnLog(dataDir);
truncLog.truncate(targetZxid);
truncLog.close();
System.out.println(String.format("Successfully truncated to zxid: %s(hex)", Long.toHexString(targetZxid)));
} catch (IOException e) {
String errstr = String.format("Failed to truncated to zxid: %s(hex). Please check it manually.", Long.toHexString(targetZxid));
log.error(errstr);
System.out.println(errstr);
return false;
}
return true;
}
use of org.apache.zookeeper.server.persistence.FileTxnLog in project zookeeper by apache.
the class TruncateTest method testTruncationStreamReset.
@Test
public void testTruncationStreamReset() throws Exception {
File tmpdir = ClientBase.createTmpDir();
FileTxnSnapLog snaplog = new FileTxnSnapLog(tmpdir, tmpdir);
ZKDatabase zkdb = new ZKDatabase(snaplog);
// make sure to snapshot, so that we have something there when
// truncateLog reloads the db
snaplog.save(zkdb.getDataTree(), zkdb.getSessionWithTimeOuts(), false);
for (int i = 1; i <= 100; i++) {
append(zkdb, i);
}
zkdb.truncateLog(1);
append(zkdb, 200);
zkdb.close();
// verify that the truncation and subsequent append were processed
// correctly
FileTxnLog txnlog = new FileTxnLog(new File(tmpdir, "version-2"));
TxnIterator iter = txnlog.read(1);
TxnHeader hdr = iter.getHeader();
Record txn = iter.getTxn();
assertEquals(1, hdr.getZxid());
assertTrue(txn instanceof SetDataTxn);
iter.next();
hdr = iter.getHeader();
txn = iter.getTxn();
assertEquals(200, hdr.getZxid());
assertTrue(txn instanceof SetDataTxn);
iter.close();
ClientBase.recursiveDelete(tmpdir);
}
Aggregations