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);
}
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;
}
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);
}
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));
}
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();
}
}
}
Aggregations