Search in sources :

Example 16 with BinaryOutputArchive

use of org.apache.jute.BinaryOutputArchive 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.Test)

Example 17 with BinaryOutputArchive

use of org.apache.jute.BinaryOutputArchive 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 18 with BinaryOutputArchive

use of org.apache.jute.BinaryOutputArchive 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;
    Assert.assertEquals(count, dserTree.getNodeCount());
    LOG.info("Deserialized " + count + " nodes in " + durationms + " ms (" + pernodeus + "us/node), depth=" + depth + " width=" + width + " datalen=" + len);
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 19 with BinaryOutputArchive

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

the class LoadFromLogTest method doOp.

/*
     * Does create/delete depending on the type and verifies
     * if cversion before the operation is 1 less than cversion afer.
     */
private void doOp(FileTxnSnapLog logFile, int type, String path, DataTree dt, DataNode parent, int cversion) throws Exception {
    int lastSlash = path.lastIndexOf('/');
    String parentName = path.substring(0, lastSlash);
    int prevCversion = parent.stat.getCversion();
    long prevPzxid = parent.stat.getPzxid();
    List<String> child = dt.getChildren(parentName, null, null);
    StringBuilder childStr = new StringBuilder();
    for (String s : child) {
        childStr.append(s).append(" ");
    }
    LOG.info("Children: " + childStr + " for " + parentName);
    LOG.info("(cverions, pzxid): " + prevCversion + ", " + prevPzxid);
    Record txn = null;
    TxnHeader txnHeader = null;
    if (type == OpCode.delete) {
        txn = new DeleteTxn(path);
        txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, Time.currentElapsedTime(), OpCode.delete);
    } else if (type == OpCode.create) {
        txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, Time.currentElapsedTime(), OpCode.create);
        txn = new CreateTxn(path, new byte[0], null, false, cversion);
    } else if (type == OpCode.multi) {
        txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, Time.currentElapsedTime(), OpCode.create);
        txn = new CreateTxn(path, new byte[0], null, false, cversion);
        List<Txn> txnList = new ArrayList<Txn>();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
        txn.serialize(boa, "request");
        ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
        Txn txact = new Txn(OpCode.create, bb.array());
        txnList.add(txact);
        txn = new MultiTxn(txnList);
        txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, Time.currentElapsedTime(), OpCode.multi);
    }
    logFile.processTransaction(txnHeader, dt, null, txn);
    int newCversion = parent.stat.getCversion();
    long newPzxid = parent.stat.getPzxid();
    child = dt.getChildren(parentName, null, null);
    childStr = new StringBuilder();
    for (String s : child) {
        childStr.append(s).append(" ");
    }
    LOG.info("Children: " + childStr + " for " + parentName);
    LOG.info("(cverions, pzxid): " + newCversion + ", " + newPzxid);
    Assert.assertTrue(type + " <cversion, pzxid> verification failed. Expected: <" + (prevCversion + 1) + ", " + (prevPzxid + 1) + ">, found: <" + newCversion + ", " + newPzxid + ">", (newCversion == prevCversion + 1 && newPzxid == prevPzxid + 1));
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) MultiTxn(org.apache.zookeeper.txn.MultiTxn) ArrayList(java.util.ArrayList) CreateTxn(org.apache.zookeeper.txn.CreateTxn) Txn(org.apache.zookeeper.txn.Txn) MultiTxn(org.apache.zookeeper.txn.MultiTxn) DeleteTxn(org.apache.zookeeper.txn.DeleteTxn) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) DeleteTxn(org.apache.zookeeper.txn.DeleteTxn) CreateTxn(org.apache.zookeeper.txn.CreateTxn) Record(org.apache.jute.Record) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 20 with BinaryOutputArchive

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

the class NettyServerCnxn method sendResponse.

@Override
public void sendResponse(ReplyHeader h, Record r, String tag) throws IOException {
    if (!channel.isOpen()) {
        return;
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // Make space for length
    BinaryOutputArchive bos = BinaryOutputArchive.getArchive(baos);
    try {
        baos.write(fourBytes);
        bos.writeRecord(h, "header");
        if (r != null) {
            bos.writeRecord(r, tag);
        }
        baos.close();
    } catch (IOException e) {
        LOG.error("Error serializing response");
    }
    byte[] b = baos.toByteArray();
    ByteBuffer bb = ByteBuffer.wrap(b);
    bb.putInt(b.length - 4).rewind();
    sendBuffer(bb);
    if (h.getXid() > 0) {
        // zks cannot be null otherwise we would not have gotten here!
        if (!zkServer.shouldThrottle(outstandingCount.decrementAndGet())) {
            enableRecv();
        }
    }
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 ByteBuffer (java.nio.ByteBuffer)11 IOException (java.io.IOException)8 Test (org.junit.Test)7 BinaryInputArchive (org.apache.jute.BinaryInputArchive)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 CreateRequest (org.apache.zookeeper.proto.CreateRequest)5 Record (org.apache.jute.Record)4 Id (org.apache.zookeeper.data.Id)4 TxnHeader (org.apache.zookeeper.txn.TxnHeader)4 ZooKeeper (org.apache.zookeeper.ZooKeeper)3 ACL (org.apache.zookeeper.data.ACL)3 SetDataRequest (org.apache.zookeeper.proto.SetDataRequest)3 Request (org.apache.zookeeper.server.Request)3 CreateTxn (org.apache.zookeeper.txn.CreateTxn)3 Socket (java.net.Socket)2 ArrayList (java.util.ArrayList)2 KeeperException (org.apache.zookeeper.KeeperException)2 Stat (org.apache.zookeeper.data.Stat)2