Search in sources :

Example 21 with BinaryInputArchive

use of org.apache.jute.BinaryInputArchive in project zookeeper by apache.

the class DeserializationPerfTest method deserializeTree.

private static void deserializeTree(int depth, int width, int len) throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException {
    BinaryInputArchive ia;
    int count;
    {
        DataTree tree = new DataTree();
        SerializationPerfTest.createNodes(tree, "/", depth, width, tree.getNode("/").stat.getCversion(), new byte[len]);
        count = tree.getNodeCount();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BinaryOutputArchive oa = BinaryOutputArchive.getArchive(baos);
        tree.serialize(oa, "test");
        baos.flush();
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ia = BinaryInputArchive.getArchive(bais);
    }
    DataTree dserTree = new DataTree();
    System.gc();
    long start = System.nanoTime();
    dserTree.deserialize(ia, "test");
    long end = System.nanoTime();
    long durationms = (end - start) / 1000000L;
    long pernodeus = ((end - start) / 1000L) / count;
    assertEquals(count, dserTree.getNodeCount());
    LOG.info("Deserialized {} nodes in {} ms ({}us/node), depth={} width={} datalen={}", count, durationms, pernodeus, depth, width, len);
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 22 with BinaryInputArchive

use of org.apache.jute.BinaryInputArchive in project zookeeper by apache.

the class ReferenceCountedACLCacheTest method testSerializeDeserialize.

@Test
public void testSerializeDeserialize() throws IOException {
    ReferenceCountedACLCache cache = new ReferenceCountedACLCache();
    List<ACL> acl1 = createACL("one");
    List<ACL> acl2 = createACL("two");
    List<ACL> acl3 = createACL("three");
    List<ACL> acl4 = createACL("four");
    List<ACL> acl5 = createACL("five");
    Long aclId1 = convertACLsNTimes(cache, acl1, 1);
    Long aclId2 = convertACLsNTimes(cache, acl2, 2);
    Long aclId3 = convertACLsNTimes(cache, acl3, 3);
    Long aclId4 = convertACLsNTimes(cache, acl4, 4);
    Long aclId5 = convertACLsNTimes(cache, acl5, 5);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BinaryOutputArchive archive = BinaryOutputArchive.getArchive(baos);
    cache.serialize(archive);
    BinaryInputArchive inArchive = BinaryInputArchive.getArchive(new ByteArrayInputStream(baos.toByteArray()));
    ReferenceCountedACLCache deserializedCache = new ReferenceCountedACLCache();
    deserializedCache.deserialize(inArchive);
    callAddUsageNTimes(deserializedCache, aclId1, 1);
    callAddUsageNTimes(deserializedCache, aclId2, 2);
    callAddUsageNTimes(deserializedCache, aclId3, 3);
    callAddUsageNTimes(deserializedCache, aclId4, 4);
    callAddUsageNTimes(deserializedCache, aclId5, 5);
    assertCachesEqual(cache, deserializedCache);
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) ByteArrayInputStream(java.io.ByteArrayInputStream) ACL(org.apache.zookeeper.data.ACL) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 23 with BinaryInputArchive

use of org.apache.jute.BinaryInputArchive in project zookeeper by apache.

the class ReferenceCountedACLCacheTest method testNPEInDeserialize.

@Test
public void testNPEInDeserialize() throws IOException {
    ReferenceCountedACLCache serializeCache = new ReferenceCountedACLCache() {

        @Override
        public synchronized void serialize(OutputArchive oa) throws IOException {
            oa.writeInt(1, "map");
            oa.writeLong(1, "long");
            oa.startVector(null, "acls");
            oa.endVector(null, "acls");
        }
    };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BinaryOutputArchive archive = BinaryOutputArchive.getArchive(baos);
    serializeCache.serialize(archive);
    BinaryInputArchive inArchive = BinaryInputArchive.getArchive(new ByteArrayInputStream(baos.toByteArray()));
    ReferenceCountedACLCache deserializedCache = new ReferenceCountedACLCache();
    try {
        deserializedCache.deserialize(inArchive);
    } catch (NullPointerException e) {
        fail("should not throw NPE while do deserialized");
    } catch (RuntimeException e) {
    // do nothing.
    }
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 24 with BinaryInputArchive

use of org.apache.jute.BinaryInputArchive 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 25 with BinaryInputArchive

use of org.apache.jute.BinaryInputArchive 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

BinaryInputArchive (org.apache.jute.BinaryInputArchive)25 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)7 FileHeader (org.apache.zookeeper.server.persistence.FileHeader)7 Test (org.junit.jupiter.api.Test)7 IOException (java.io.IOException)6 FileInputStream (java.io.FileInputStream)5 Record (org.apache.jute.Record)5 ByteBufferInputStream (org.apache.zookeeper.server.ByteBufferInputStream)5 TxnHeader (org.apache.zookeeper.txn.TxnHeader)5 ByteBuffer (java.nio.ByteBuffer)4 EOFException (java.io.EOFException)3 File (java.io.File)3 Adler32 (java.util.zip.Adler32)3 Checksum (java.util.zip.Checksum)3 Timeout (org.junit.jupiter.api.Timeout)3 ACL (org.apache.zookeeper.data.ACL)2 ConnectRequest (org.apache.zookeeper.proto.ConnectRequest)2 ReplyHeader (org.apache.zookeeper.proto.ReplyHeader)2