Search in sources :

Example 1 with FileHeader

use of org.apache.zookeeper_voltpatches.server.persistence.FileHeader in project voltdb by VoltDB.

the class FileSnap method serialize.

/**
     * serialize the datatree and session into the file snapshot
     * @param dt the datatree to be serialized
     * @param sessions the sessions to be serialized
     * @param snapShot the file to store snapshot into
     */
@Override
public synchronized void serialize(DataTree dt, Map<Long, Long> sessions, File snapShot) throws IOException {
    if (!close) {
        OutputStream sessOS = new BufferedOutputStream(new FileOutputStream(snapShot));
        CheckedOutputStream crcOut = new CheckedOutputStream(sessOS, new Adler32());
        //CheckedOutputStream cout = new CheckedOutputStream()
        OutputArchive oa = BinaryOutputArchive.getArchive(crcOut);
        FileHeader header = new FileHeader(SNAP_MAGIC, VERSION, dbId);
        serialize(dt, sessions, oa, header);
        long val = crcOut.getChecksum().getValue();
        oa.writeLong(val, "val");
        oa.writeString("/", "path");
        sessOS.flush();
        crcOut.close();
        sessOS.close();
    }
}
Also used : BinaryOutputArchive(org.apache.jute_voltpatches.BinaryOutputArchive) OutputArchive(org.apache.jute_voltpatches.OutputArchive) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) CheckedOutputStream(java.util.zip.CheckedOutputStream) FileOutputStream(java.io.FileOutputStream) CheckedOutputStream(java.util.zip.CheckedOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileHeader(org.apache.zookeeper_voltpatches.server.persistence.FileHeader) Adler32(java.util.zip.Adler32)

Example 2 with FileHeader

use of org.apache.zookeeper_voltpatches.server.persistence.FileHeader in project voltdb by VoltDB.

the class FileHeader method compareTo.

public int compareTo(Object peer_) throws ClassCastException {
    if (!(peer_ instanceof FileHeader)) {
        throw new ClassCastException("Comparing different types of records.");
    }
    FileHeader peer = (FileHeader) peer_;
    int ret = 0;
    ret = (magic == peer.magic) ? 0 : ((magic < peer.magic) ? -1 : 1);
    if (ret != 0)
        return ret;
    ret = (version == peer.version) ? 0 : ((version < peer.version) ? -1 : 1);
    if (ret != 0)
        return ret;
    ret = (dbid == peer.dbid) ? 0 : ((dbid < peer.dbid) ? -1 : 1);
    if (ret != 0)
        return ret;
    return ret;
}
Also used : FileHeader(org.apache.zookeeper_voltpatches.server.persistence.FileHeader)

Example 3 with FileHeader

use of org.apache.zookeeper_voltpatches.server.persistence.FileHeader in project voltdb by VoltDB.

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());
        }
        InputArchive iab = BinaryInputArchive.getArchive(new ByteArrayInputStream(bytes));
        TxnHeader hdr = new TxnHeader();
        SerializeUtils.deserializeTxn(iab, 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()));
        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_voltpatches.BinaryInputArchive) InputArchive(org.apache.jute_voltpatches.InputArchive) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Adler32(java.util.zip.Adler32) Date(java.util.Date) BinaryInputArchive(org.apache.jute_voltpatches.BinaryInputArchive) ByteArrayInputStream(java.io.ByteArrayInputStream) Checksum(java.util.zip.Checksum) EOFException(java.io.EOFException) FileHeader(org.apache.zookeeper_voltpatches.server.persistence.FileHeader) TxnHeader(org.apache.zookeeper_voltpatches.txn.TxnHeader)

Example 4 with FileHeader

use of org.apache.zookeeper_voltpatches.server.persistence.FileHeader in project voltdb by VoltDB.

the class FileTxnLog method readHeader.

/**
     * read the header of the transaction file
     * @param file the transaction file to read
     * @return header that was read fomr the file
     * @throws IOException
     */
private static FileHeader readHeader(File file) throws IOException {
    InputStream is = null;
    try {
        is = new BufferedInputStream(new FileInputStream(file));
        InputArchive ia = BinaryInputArchive.getArchive(is);
        FileHeader hdr = new FileHeader();
        hdr.deserialize(ia, "fileheader");
        return hdr;
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
            LOG.warn("Ignoring exception during close", e);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InputArchive(org.apache.jute_voltpatches.InputArchive) BinaryInputArchive(org.apache.jute_voltpatches.BinaryInputArchive) IOException(java.io.IOException) FileHeader(org.apache.zookeeper_voltpatches.server.persistence.FileHeader) FileInputStream(java.io.FileInputStream)

Example 5 with FileHeader

use of org.apache.zookeeper_voltpatches.server.persistence.FileHeader in project voltdb by VoltDB.

the class FileTxnLog method append.

/**
     * append an entry to the transaction log
     * @param hdr the header of the transaction
     * @param txn the transaction part of the entry
     * returns true iff something appended, otw false
     */
public synchronized boolean append(TxnHeader hdr, Record txn) throws IOException {
    if (hdr != null) {
        if (hdr.getZxid() <= lastZxidSeen) {
            LOG.warn("Current zxid " + hdr.getZxid() + " is <= " + lastZxidSeen + " for " + hdr.getType());
        }
        if (logStream == null) {
            if (LOG.isInfoEnabled()) {
                LOG.info("Creating new log file: log." + Long.toHexString(hdr.getZxid()));
            }
            logFileWrite = new File(logDir, ("log." + Long.toHexString(hdr.getZxid())));
            fos = new FileOutputStream(logFileWrite);
            logStream = new BufferedOutputStream(fos);
            oa = BinaryOutputArchive.getArchive(logStream);
            FileHeader fhdr = new FileHeader(TXNLOG_MAGIC, VERSION, dbId);
            fhdr.serialize(oa, "fileheader");
            currentSize = fos.getChannel().position();
            streamsToFlush.add(fos);
        }
        padFile(fos);
        byte[] buf = Util.marshallTxnEntry(hdr, txn);
        if (buf == null || buf.length == 0) {
            throw new IOException("Faulty serialization for header " + "and txn");
        }
        Checksum crc = makeChecksumAlgorithm();
        crc.update(buf, 0, buf.length);
        oa.writeLong(crc.getValue(), "txnEntryCRC");
        Util.writeTxnBytes(oa, buf);
        return true;
    }
    return false;
}
Also used : Checksum(java.util.zip.Checksum) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) FileHeader(org.apache.zookeeper_voltpatches.server.persistence.FileHeader)

Aggregations

FileHeader (org.apache.zookeeper_voltpatches.server.persistence.FileHeader)8 IOException (java.io.IOException)5 BufferedOutputStream (java.io.BufferedOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 Adler32 (java.util.zip.Adler32)2 Checksum (java.util.zip.Checksum)2 BinaryInputArchive (org.apache.jute_voltpatches.BinaryInputArchive)2 InputArchive (org.apache.jute_voltpatches.InputArchive)2 BufferedInputStream (java.io.BufferedInputStream)1 EOFException (java.io.EOFException)1 File (java.io.File)1 FilterInputStream (java.io.FilterInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 Date (java.util.Date)1 CheckedOutputStream (java.util.zip.CheckedOutputStream)1 BinaryOutputArchive (org.apache.jute_voltpatches.BinaryOutputArchive)1