Search in sources :

Example 1 with FileHeader

use of org.apache.zookeeper.server.persistence.FileHeader 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 FileHeader

use of org.apache.zookeeper.server.persistence.FileHeader in project zookeeper by apache.

the class LogChopper method chop.

public static boolean chop(InputStream is, OutputStream os, long zxid) throws IOException {
    BinaryInputArchive logStream = BinaryInputArchive.getArchive(is);
    BinaryOutputArchive choppedStream = BinaryOutputArchive.getArchive(os);
    FileHeader fhdr = new FileHeader();
    fhdr.deserialize(logStream, "fileheader");
    if (fhdr.getMagic() != FileTxnLog.TXNLOG_MAGIC) {
        System.err.println("Invalid magic number in txn log file");
        return false;
    }
    System.out.println("ZooKeeper Transactional Log File with dbid " + fhdr.getDbid() + " txnlog format version " + fhdr.getVersion());
    fhdr.serialize(choppedStream, "fileheader");
    int count = 0;
    boolean hasZxid = false;
    long previousZxid = -1;
    while (true) {
        long crcValue;
        byte[] bytes;
        try {
            crcValue = logStream.readLong("crcvalue");
            bytes = logStream.readBuffer("txnEntry");
        } catch (EOFException e) {
            System.out.println("EOF reached after " + count + " txns.");
            // returning false because nothing was chopped
            return false;
        }
        if (bytes.length == 0) {
            // Since we preallocate, we define EOF to be an
            // empty transaction
            System.out.println("EOF reached after " + count + " txns.");
            // returning false because nothing was chopped
            return false;
        }
        Checksum crc = new Adler32();
        crc.update(bytes, 0, bytes.length);
        if (crcValue != crc.getValue()) {
            throw new IOException("CRC doesn't match " + crcValue + " vs " + crc.getValue());
        }
        TxnLogEntry entry = SerializeUtils.deserializeTxn(bytes);
        TxnHeader hdr = entry.getHeader();
        Record txn = entry.getTxn();
        if (logStream.readByte("EOR") != 'B') {
            System.out.println("Last transaction was partial.");
            throw new EOFException("Last transaction was partial.");
        }
        final long txnZxid = hdr.getZxid();
        if (txnZxid == zxid) {
            hasZxid = true;
        }
        // logging the gap to make the inconsistency investigation easier
        if (previousZxid != -1 && txnZxid != previousZxid + 1) {
            long txnEpoch = ZxidUtils.getEpochFromZxid(txnZxid);
            long txnCounter = ZxidUtils.getCounterFromZxid(txnZxid);
            long previousEpoch = ZxidUtils.getEpochFromZxid(previousZxid);
            if (txnEpoch == previousEpoch) {
                System.out.println(String.format("There is intra-epoch gap between %x and %x", previousZxid, txnZxid));
            } else if (txnCounter != 1) {
                System.out.println(String.format("There is inter-epoch gap between %x and %x", previousZxid, txnZxid));
            }
        }
        previousZxid = txnZxid;
        if (txnZxid > zxid) {
            if (count == 0 || !hasZxid) {
                System.out.println(String.format("This log does not contain zxid %x", zxid));
                return false;
            }
            System.out.println(String.format("Chopping at %x new log has %d records", zxid, count));
            return true;
        }
        choppedStream.writeLong(crcValue, "crcvalue");
        choppedStream.writeBuffer(bytes, "txnEntry");
        choppedStream.writeByte((byte) 'B', "EOR");
        count++;
    }
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) IOException(java.io.IOException) Adler32(java.util.zip.Adler32) BinaryInputArchive(org.apache.jute.BinaryInputArchive) Checksum(java.util.zip.Checksum) TxnLogEntry(org.apache.zookeeper.server.TxnLogEntry) EOFException(java.io.EOFException) Record(org.apache.jute.Record) FileHeader(org.apache.zookeeper.server.persistence.FileHeader) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 3 with FileHeader

use of org.apache.zookeeper.server.persistence.FileHeader in project zookeeper by apache.

the class PurgeTxnTest method makeValidSnapshot.

private void makeValidSnapshot(File snapFile) throws IOException {
    SnapStream.setStreamMode(SnapStream.StreamMode.CHECKED);
    CheckedOutputStream os = SnapStream.getOutputStream(snapFile, true);
    OutputArchive oa = BinaryOutputArchive.getArchive(os);
    FileHeader header = new FileHeader(FileSnap.SNAP_MAGIC, 2, 1);
    header.serialize(oa, "fileheader");
    SnapStream.sealStream(os, oa);
    os.flush();
    os.close();
    assertTrue(SnapStream.isValidSnapshot(snapFile));
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) CheckedOutputStream(java.util.zip.CheckedOutputStream) FileHeader(org.apache.zookeeper.server.persistence.FileHeader)

Example 4 with FileHeader

use of org.apache.zookeeper.server.persistence.FileHeader in project zookeeper by apache.

the class LogFormatter method main.

/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("USAGE: LogFormatter log_file");
        System.exit(2);
    }
    FileInputStream fis = new FileInputStream(args[0]);
    BinaryInputArchive logStream = BinaryInputArchive.getArchive(fis);
    FileHeader fhdr = new FileHeader();
    fhdr.deserialize(logStream, "fileheader");
    if (fhdr.getMagic() != FileTxnLog.TXNLOG_MAGIC) {
        System.err.println("Invalid magic number for " + args[0]);
        System.exit(2);
    }
    System.out.println("ZooKeeper Transactional Log File with dbid " + fhdr.getDbid() + " txnlog format version " + fhdr.getVersion());
    int count = 0;
    while (true) {
        long crcValue;
        byte[] bytes;
        try {
            crcValue = logStream.readLong("crcvalue");
            bytes = logStream.readBuffer("txnEntry");
        } catch (EOFException e) {
            System.out.println("EOF reached after " + count + " txns.");
            return;
        }
        if (bytes.length == 0) {
            // Since we preallocate, we define EOF to be an
            // empty transaction
            System.out.println("EOF reached after " + count + " txns.");
            return;
        }
        Checksum crc = new Adler32();
        crc.update(bytes, 0, bytes.length);
        if (crcValue != crc.getValue()) {
            throw new IOException("CRC doesn't match " + crcValue + " vs " + crc.getValue());
        }
        TxnHeader hdr = new TxnHeader();
        Record txn = SerializeUtils.deserializeTxn(bytes, hdr);
        System.out.println(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(new Date(hdr.getTime())) + " session 0x" + Long.toHexString(hdr.getClientId()) + " cxid 0x" + Long.toHexString(hdr.getCxid()) + " zxid 0x" + Long.toHexString(hdr.getZxid()) + " " + TraceFormatter.op2String(hdr.getType()) + " " + txn);
        if (logStream.readByte("EOR") != 'B') {
            LOG.error("Last transaction was partial.");
            throw new EOFException("Last transaction was partial.");
        }
        count++;
    }
}
Also used : IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Adler32(java.util.zip.Adler32) Date(java.util.Date) BinaryInputArchive(org.apache.jute.BinaryInputArchive) Checksum(java.util.zip.Checksum) EOFException(java.io.EOFException) Record(org.apache.jute.Record) FileHeader(org.apache.zookeeper.server.persistence.FileHeader) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 5 with FileHeader

use of org.apache.zookeeper.server.persistence.FileHeader in project zookeeper by apache.

the class LoadFromLogNoServerTest 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(), ZooDefs.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 : {} Expected : {}", header.getMagic(), FileTxnLog.TXNLOG_MAGIC);
    assertTrue(header.getMagic() == FileTxnLog.TXNLOG_MAGIC, "Missing magic number ");
}
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.jupiter.api.Test)

Aggregations

FileHeader (org.apache.zookeeper.server.persistence.FileHeader)8 BinaryInputArchive (org.apache.jute.BinaryInputArchive)7 FileInputStream (java.io.FileInputStream)5 Record (org.apache.jute.Record)5 TxnHeader (org.apache.zookeeper.txn.TxnHeader)5 IOException (java.io.IOException)4 EOFException (java.io.EOFException)3 File (java.io.File)3 Adler32 (java.util.zip.Adler32)3 Checksum (java.util.zip.Checksum)3 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)2 FileTxnLog (org.apache.zookeeper.server.persistence.FileTxnLog)2 CreateTxn (org.apache.zookeeper.txn.CreateTxn)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 CheckedOutputStream (java.util.zip.CheckedOutputStream)1 OutputArchive (org.apache.jute.OutputArchive)1 DataTree (org.apache.zookeeper.server.DataTree)1 TxnLogEntry (org.apache.zookeeper.server.TxnLogEntry)1 Test (org.junit.Test)1