Search in sources :

Example 1 with FileTxnLog

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);
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) CreateTxn(org.apache.zookeeper.txn.CreateTxn) FileTxnLog(org.apache.zookeeper.server.persistence.FileTxnLog) Record(org.apache.jute.Record) File(java.io.File) FileHeader(org.apache.zookeeper.server.persistence.FileHeader) FileInputStream(java.io.FileInputStream) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.Test)

Example 2 with FileTxnLog

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);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) FileTxnIterator(org.apache.zookeeper.server.persistence.FileTxnLog.FileTxnIterator) FileTxnLog(org.apache.zookeeper.server.persistence.FileTxnLog) File(java.io.File) TxnIterator(org.apache.zookeeper.server.persistence.TxnLog.TxnIterator) FileTxnIterator(org.apache.zookeeper.server.persistence.FileTxnLog.FileTxnIterator) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.jupiter.api.Test)

Example 3 with FileTxnLog

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());
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) FileTxnLog(org.apache.zookeeper.server.persistence.FileTxnLog) File(java.io.File) TxnIterator(org.apache.zookeeper.server.persistence.TxnLog.TxnIterator) FileTxnIterator(org.apache.zookeeper.server.persistence.FileTxnLog.FileTxnIterator) Test(org.junit.jupiter.api.Test)

Example 4 with FileTxnLog

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);
}
Also used : FileTxnLog(org.apache.zookeeper.server.persistence.FileTxnLog) File(java.io.File) FileTxnLog(org.apache.zookeeper.server.persistence.FileTxnLog) TxnLog(org.apache.zookeeper.server.persistence.TxnLog)

Example 5 with FileTxnLog

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;
}
Also used : FileTxnLog(org.apache.zookeeper.server.persistence.FileTxnLog) TxnIterator(org.apache.zookeeper.server.persistence.TxnLog.TxnIterator) File(java.io.File) TxnDigest(org.apache.zookeeper.txn.TxnDigest)

Aggregations

File (java.io.File)13 FileTxnLog (org.apache.zookeeper.server.persistence.FileTxnLog)13 Test (org.junit.jupiter.api.Test)8 TxnIterator (org.apache.zookeeper.server.persistence.TxnLog.TxnIterator)5 TxnHeader (org.apache.zookeeper.txn.TxnHeader)5 FileInputStream (java.io.FileInputStream)3 Record (org.apache.jute.Record)3 ZooKeeper (org.apache.zookeeper.ZooKeeper)3 IOException (java.io.IOException)2 BinaryInputArchive (org.apache.jute.BinaryInputArchive)2 FileHeader (org.apache.zookeeper.server.persistence.FileHeader)2 FileTxnIterator (org.apache.zookeeper.server.persistence.FileTxnLog.FileTxnIterator)2 CreateTxn (org.apache.zookeeper.txn.CreateTxn)2 FileOutputStream (java.io.FileOutputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)1 FileSnap (org.apache.zookeeper.server.persistence.FileSnap)1 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)1 TxnLog (org.apache.zookeeper.server.persistence.TxnLog)1 DeleteTxn (org.apache.zookeeper.txn.DeleteTxn)1