use of org.apache.zookeeper.server.persistence.FileTxnLog.FileTxnIterator 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);
}
Aggregations