Search in sources :

Example 6 with FileHeader

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

the class TxnLogSource method isTransactionFile.

public static boolean isTransactionFile(String file) throws IOException {
    RandomAccessFileReader reader = new RandomAccessFileReader(new File(file));
    BinaryInputArchive logStream = new BinaryInputArchive(reader);
    FileHeader fhdr = new FileHeader();
    fhdr.deserialize(logStream, "fileheader");
    reader.close();
    return fhdr.getMagic() == FileTxnLog.TXNLOG_MAGIC;
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) File(java.io.File) FileHeader(org.apache.zookeeper.server.persistence.FileHeader)

Example 7 with FileHeader

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

the class ZKLogFormatter method readTransactionLog.

private static void readTransactionLog(String logfilepath) throws FileNotFoundException, IOException, EOFException {
    FileInputStream fis = new FileInputStream(logfilepath);
    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 " + logfilepath);
        System.exit(2);
    }
    if (bw != null) {
        bw.write("ZooKeeper Transactional Log File with dbid " + fhdr.getDbid() + " txnlog format version " + fhdr.getVersion());
        bw.newLine();
    } else {
        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) {
            if (bw != null) {
                bw.write("EOF reached after " + count + " txns.");
                bw.newLine();
            } else {
                System.out.println("EOF reached after " + count + " txns.");
            }
            break;
        }
        if (bytes.length == 0) {
            // empty transaction
            if (bw != null) {
                bw.write("EOF reached after " + count + " txns.");
                bw.newLine();
            } else {
                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);
        if (bw != null) {
            bw.write(formatTransaction(hdr, txn));
            bw.newLine();
        } else {
            System.out.println(formatTransaction(hdr, txn));
        }
        if (logStream.readByte("EOR") != 'B') {
            LOG.error("Last transaction was partial.");
            throw new EOFException("Last transaction was partial.");
        }
        count++;
    }
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) Checksum(java.util.zip.Checksum) EOFException(java.io.EOFException) Record(org.apache.jute.Record) IOException(java.io.IOException) FileHeader(org.apache.zookeeper.server.persistence.FileHeader) FileInputStream(java.io.FileInputStream) Adler32(java.util.zip.Adler32) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 8 with FileHeader

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

the class ZKLogFormatter method readSnapshotLog.

private static void readSnapshotLog(String snapshotPath) throws Exception {
    FileInputStream fis = new FileInputStream(snapshotPath);
    BinaryInputArchive ia = BinaryInputArchive.getArchive(fis);
    Map<Long, Integer> sessions = new HashMap<Long, Integer>();
    DataTree dt = new DataTree();
    FileHeader header = new FileHeader();
    header.deserialize(ia, "fileheader");
    if (header.getMagic() != FileSnap.SNAP_MAGIC) {
        throw new IOException("mismatching magic headers " + header.getMagic() + " !=  " + FileSnap.SNAP_MAGIC);
    }
    SerializeUtils.deserializeSnapshot(dt, ia, sessions);
    if (bw != null) {
        bw.write(sessions.toString());
        bw.newLine();
    } else {
        System.out.println(sessions);
    }
    traverse(dt, 1, "/");
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) HashMap(java.util.HashMap) DataTree(org.apache.zookeeper.server.DataTree) IOException(java.io.IOException) FileHeader(org.apache.zookeeper.server.persistence.FileHeader) FileInputStream(java.io.FileInputStream)

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