Search in sources :

Example 11 with BinaryInputArchive

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

the class DataTreeTest method testReconfigACLClearOnDeserialize.

@Test
@Timeout(value = 60)
public void testReconfigACLClearOnDeserialize() throws Exception {
    DataTree tree = new DataTree();
    // simulate the upgrading scenario, where the reconfig znode
    // doesn't exist and the acl cache is empty
    tree.deleteNode(ZooDefs.CONFIG_NODE, 1);
    tree.getReferenceCountedAclCache().aclIndex = 0;
    assertEquals(0, tree.aclCacheSize(), "expected to have 1 acl in acl cache map");
    // serialize the data with one znode with acl
    tree.createNode("/bug", new byte[20], ZooDefs.Ids.OPEN_ACL_UNSAFE, -1, 1, 1, 1);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BinaryOutputArchive oa = BinaryOutputArchive.getArchive(baos);
    tree.serialize(oa, "test");
    baos.flush();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    BinaryInputArchive ia = BinaryInputArchive.getArchive(bais);
    tree.deserialize(ia, "test");
    assertEquals(1, tree.aclCacheSize(), "expected to have 1 acl in acl cache map");
    assertEquals(ZooDefs.Ids.OPEN_ACL_UNSAFE, tree.getACL("/bug", new Stat()), "expected to have the same acl");
    // simulate the upgrading case where the config node will be created
    // again after leader election
    tree.addConfigNode();
    assertEquals(2, tree.aclCacheSize(), "expected to have 2 acl in acl cache map");
    assertEquals(ZooDefs.Ids.OPEN_ACL_UNSAFE, tree.getACL("/bug", new Stat()), "expected to have the same acl");
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) Stat(org.apache.zookeeper.data.Stat) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 12 with BinaryInputArchive

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

the class ReferenceCountedACLCacheTest method testPurgeUnused.

@Test
public void testPurgeUnused() 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);
    deserializedCache.purgeUnused();
    assertEquals(2, deserializedCache.size());
    assertEquals(aclId1, deserializedCache.convertAcls(acl1));
    assertEquals(aclId2, deserializedCache.convertAcls(acl2));
    assertFalse(acl3.equals(deserializedCache.convertAcls(acl3)));
    assertFalse(acl4.equals(deserializedCache.convertAcls(acl4)));
    assertFalse(acl5.equals(deserializedCache.convertAcls(acl5)));
}
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 13 with BinaryInputArchive

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

the class MultiTransactionRecordTest method codeDecode.

private MultiTransactionRecord codeDecode(MultiTransactionRecord request) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
    request.serialize(boa, "request");
    baos.close();
    ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
    bb.rewind();
    BinaryInputArchive bia = BinaryInputArchive.getArchive(new ByteBufferInputStream(bb));
    MultiTransactionRecord decodedRequest = new MultiTransactionRecord();
    decodedRequest.deserialize(bia, "request");
    return decodedRequest;
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) ByteBufferInputStream(org.apache.zookeeper.server.ByteBufferInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer)

Example 14 with BinaryInputArchive

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

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

the class MultiOperationRecordTest method codeDecode.

private MultiOperationRecord codeDecode(MultiOperationRecord request) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
    request.serialize(boa, "request");
    baos.close();
    ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
    bb.rewind();
    BinaryInputArchive bia = BinaryInputArchive.getArchive(new ByteBufferInputStream(bb));
    MultiOperationRecord decodedRequest = new MultiOperationRecord();
    decodedRequest.deserialize(bia, "request");
    return decodedRequest;
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) ByteBufferInputStream(org.apache.zookeeper.server.ByteBufferInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer)

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