Search in sources :

Example 11 with FileTxnLog

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

Example 12 with FileTxnLog

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

Example 13 with FileTxnLog

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

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