use of org.apache.zookeeper.server.persistence.FileTxnLog in project zookeeper by apache.
the class LoadFromLogTest method testPad.
/**
* Simulates ZOOKEEPER-1069 and verifies that flush() before padLogFile
* fixes it.
*/
@Test
public void testPad() throws Exception {
File tmpDir = ClientBase.createTmpDir();
FileTxnLog txnLog = new FileTxnLog(tmpDir);
TxnHeader txnHeader = new TxnHeader(0xabcd, 0x123, 0x123, Time.currentElapsedTime(), OpCode.create);
Record txn = new CreateTxn("/Test", new byte[0], null, false, 1);
txnLog.append(txnHeader, txn);
FileInputStream in = new FileInputStream(tmpDir.getPath() + "/log." + Long.toHexString(txnHeader.getZxid()));
BinaryInputArchive ia = BinaryInputArchive.getArchive(in);
FileHeader header = new FileHeader();
header.deserialize(ia, "fileheader");
LOG.info("Received magic : " + header.getMagic() + " Expected : " + FileTxnLog.TXNLOG_MAGIC);
Assert.assertTrue("Missing magic number ", header.getMagic() == FileTxnLog.TXNLOG_MAGIC);
}
use of org.apache.zookeeper.server.persistence.FileTxnLog in project zookeeper by apache.
the class LoadFromLogTest method testLoad.
/**
* test that all transactions from the Log are loaded, and only once
* @throws Exception an exception might be thrown here
*/
@Test
public void testLoad() throws Exception {
// generate some transactions that will get logged
ZooKeeper zk = createZKClient(hostPort);
try {
for (int i = 0; i < NUM_MESSAGES; i++) {
zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
} finally {
zk.close();
}
stopServer();
// now verify that the FileTxnLog reads every transaction only once
File logDir = new File(tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION);
FileTxnLog txnLog = new FileTxnLog(logDir);
TxnIterator itr = txnLog.read(0);
// Check that storage space return some value
FileTxnIterator fileItr = (FileTxnIterator) itr;
long storageSize = fileItr.getStorageSize();
LOG.info("Txnlog size: {} bytes", storageSize);
assertTrue((storageSize > 0), "Storage size is greater than zero ");
long expectedZxid = 0;
long lastZxid = 0;
TxnHeader hdr;
do {
hdr = itr.getHeader();
expectedZxid++;
assertTrue(lastZxid != hdr.getZxid(), "not the same transaction. lastZxid=" + lastZxid + ", zxid=" + hdr.getZxid());
assertTrue((hdr.getZxid() == expectedZxid), "excepting next transaction. expected=" + expectedZxid + ", retrieved=" + hdr.getZxid());
lastZxid = hdr.getZxid();
} while (itr.next());
assertTrue((expectedZxid == TOTAL_TRANSACTIONS), "processed all transactions. " + expectedZxid + " == " + TOTAL_TRANSACTIONS);
}
use of org.apache.zookeeper.server.persistence.FileTxnLog in project zookeeper by apache.
the class LoadFromLogTest method testLoadFailure.
/**
* test that we fail to load txnlog of a request zxid that is older
* than what exist on disk
* @throws Exception an exception might be thrown here
*/
@Test
public void testLoadFailure() throws Exception {
// generate some transactions that will get logged
ZooKeeper zk = createZKClient(hostPort);
try {
for (int i = 0; i < NUM_MESSAGES; i++) {
zk.create("/data-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
}
} finally {
zk.close();
}
stopServer();
File logDir = new File(tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION);
File[] logFiles = FileTxnLog.getLogFiles(logDir.listFiles(), 0);
// Verify that we have at least NUM_MESSAGES / SNAPCOUNT txnlog
assertTrue(logFiles.length > NUM_MESSAGES / 100);
// Delete the first log file, so we will fail to read it back from disk
assertTrue(logFiles[0].delete(), "delete the first log file");
// Find zxid for the second log
long secondStartZxid = Util.getZxidFromName(logFiles[1].getName(), "log");
FileTxnLog txnLog = new FileTxnLog(logDir);
TxnIterator itr = txnLog.read(1, false);
// Oldest log is already remove, so this should point to the start of
// of zxid on the second log
assertEquals(secondStartZxid, itr.getHeader().getZxid());
itr = txnLog.read(secondStartZxid, false);
assertEquals(secondStartZxid, itr.getHeader().getZxid());
assertTrue(itr.next());
// Trying to get a second txn on second txnlog give us the
// the start of second log, since the first one is removed
long nextZxid = itr.getHeader().getZxid();
itr = txnLog.read(nextZxid, false);
assertEquals(secondStartZxid, itr.getHeader().getZxid());
// Trying to get a first txn on the third give us the
// the start of second log, since the first one is removed
long thirdStartZxid = Util.getZxidFromName(logFiles[2].getName(), "log");
itr = txnLog.read(thirdStartZxid, false);
assertEquals(secondStartZxid, itr.getHeader().getZxid());
assertTrue(itr.next());
nextZxid = itr.getHeader().getZxid();
itr = txnLog.read(nextZxid, false);
assertEquals(secondStartZxid, itr.getHeader().getZxid());
}
use of org.apache.zookeeper.server.persistence.FileTxnLog in project zookeeper by apache.
the class LogChopperTest method getFirstLastZxid.
Pair<Long, Long> getFirstLastZxid(File logFile) throws IOException {
File tmp = createTmpDir();
Files.copy(logFile.toPath(), new File(tmp, "log.0").toPath());
FileTxnLog txnLog = new FileTxnLog(tmp);
TxnLog.TxnIterator it = txnLog.read(0);
long firstZxid = it.getHeader().getZxid();
long lastZxid = firstZxid;
while (it.next()) {
lastZxid = it.getHeader().getZxid();
}
txnLog.close();
rmr(tmp);
return new Pair<Long, Long>(firstZxid, lastZxid);
}
use of org.apache.zookeeper.server.persistence.FileTxnLog in project zookeeper by apache.
the class TxnLogDigestTest method getLastTxnLogDigest.
private TxnDigest getLastTxnLogDigest() throws IOException {
TxnIterator itr = new FileTxnLog(new File(tmpDir, "version-2")).read(1);
TxnDigest lastDigest = null;
while (itr.next()) {
lastDigest = itr.getDigest();
}
return lastDigest;
}
Aggregations