use of org.apache.jute.BinaryOutputArchive in project zookeeper by apache.
the class ZooKeeperServer method finishSessionInit.
public void finishSessionInit(ServerCnxn cnxn, boolean valid) {
// register with JMX
try {
if (valid) {
if (serverCnxnFactory != null && serverCnxnFactory.cnxns.contains(cnxn)) {
serverCnxnFactory.registerConnection(cnxn);
} else if (secureServerCnxnFactory != null && secureServerCnxnFactory.cnxns.contains(cnxn)) {
secureServerCnxnFactory.registerConnection(cnxn);
}
}
} catch (Exception e) {
LOG.warn("Failed to register with JMX", e);
}
try {
ConnectResponse rsp = new ConnectResponse(0, valid ? cnxn.getSessionTimeout() : // send 0 if session is no
0, // send 0 if session is no
valid ? cnxn.getSessionId() : 0, // longer valid
valid ? generatePasswd(cnxn.getSessionId()) : new byte[16]);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BinaryOutputArchive bos = BinaryOutputArchive.getArchive(baos);
bos.writeInt(-1, "len");
rsp.serialize(bos, "connect");
if (!cnxn.isOldClient) {
bos.writeBool(this instanceof ReadOnlyZooKeeperServer, "readOnly");
}
baos.close();
ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
bb.putInt(bb.remaining() - 4).rewind();
cnxn.sendBuffer(bb);
if (valid) {
LOG.info("Established session 0x" + Long.toHexString(cnxn.getSessionId()) + " with negotiated timeout " + cnxn.getSessionTimeout() + " for client " + cnxn.getRemoteSocketAddress());
cnxn.enableRecv();
} else {
LOG.info("Invalid session 0x" + Long.toHexString(cnxn.getSessionId()) + " for client " + cnxn.getRemoteSocketAddress() + ", probably expired");
cnxn.sendBuffer(ServerCnxnFactory.closeConn);
}
} catch (Exception e) {
LOG.warn("Exception while establishing session, closing", e);
cnxn.close();
}
}
use of org.apache.jute.BinaryOutputArchive in project zookeeper by apache.
the class DataTreeTest method testPathTrieClearOnDeserialize.
@Test(timeout = 60000)
public void testPathTrieClearOnDeserialize() throws Exception {
//Create a DataTree with quota nodes so PathTrie get updated
DataTree dserTree = new DataTree();
dserTree.createNode("/bug", new byte[20], null, -1, 1, 1, 1);
dserTree.createNode(Quotas.quotaZookeeper + "/bug", null, null, -1, 1, 1, 1);
dserTree.createNode(Quotas.quotaPath("/bug"), new byte[20], null, -1, 1, 1, 1);
dserTree.createNode(Quotas.statPath("/bug"), new byte[20], null, -1, 1, 1, 1);
//deserialize a DataTree; this should clear the old /bug nodes and pathTrie
DataTree tree = new DataTree();
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);
dserTree.deserialize(ia, "test");
Field pfield = DataTree.class.getDeclaredField("pTrie");
pfield.setAccessible(true);
PathTrie pTrie = (PathTrie) pfield.get(dserTree);
//Check that the node path is removed from pTrie
Assert.assertEquals("/bug is still in pTrie", "", pTrie.findMaxPrefix("/bug"));
}
use of org.apache.jute.BinaryOutputArchive in project zookeeper by apache.
the class DataTreeTest method testSerializeDoesntLockDataNodeWhileWriting.
/*
* ZOOKEEPER-2201 - OutputArchive.writeRecord can block for long periods of
* time, we must call it outside of the node lock.
* We call tree.serialize, which calls our modified writeRecord method that
* blocks until it can verify that a separate thread can lock the DataNode
* currently being written, i.e. that DataTree.serializeNode does not hold
* the DataNode lock while calling OutputArchive.writeRecord.
*/
@Test(timeout = 60000)
public void testSerializeDoesntLockDataNodeWhileWriting() throws Exception {
DataTree tree = new DataTree();
tree.createNode("/marker", new byte[] { 42 }, null, -1, 1, 1, 1);
final DataNode markerNode = tree.getNode("/marker");
final AtomicBoolean ranTestCase = new AtomicBoolean();
DataOutputStream out = new DataOutputStream(new ByteArrayOutputStream());
BinaryOutputArchive oa = new BinaryOutputArchive(out) {
@Override
public void writeRecord(Record r, String tag) throws IOException {
// which adds default ACL to config node.
if (r instanceof DataNode) {
DataNode node = (DataNode) r;
if (node.data.length == 1 && node.data[0] == 42) {
final Semaphore semaphore = new Semaphore(0);
new Thread(new Runnable() {
@Override
public void run() {
synchronized (markerNode) {
//When we lock markerNode, allow writeRecord to continue
semaphore.release();
}
}
}).start();
try {
boolean acquired = semaphore.tryAcquire(30, TimeUnit.SECONDS);
//This is the real assertion - could another thread lock
//the DataNode we're currently writing
Assert.assertTrue("Couldn't acquire a lock on the DataNode while we were calling tree.serialize", acquired);
} catch (InterruptedException e1) {
throw new RuntimeException(e1);
}
ranTestCase.set(true);
}
}
super.writeRecord(r, tag);
}
};
tree.serialize(oa, "test");
//Let's make sure that we hit the code that ran the real assertion above
Assert.assertTrue("Didn't find the expected node", ranTestCase.get());
}
use of org.apache.jute.BinaryOutputArchive 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)));
}
use of org.apache.jute.BinaryOutputArchive in project zookeeper by apache.
the class SerializationPerfTest method serializeTree.
private static void serializeTree(int depth, int width, int len) throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException {
DataTree tree = new DataTree();
createNodes(tree, "/", depth, width, tree.getNode("/").stat.getCversion(), new byte[len]);
int count = tree.getNodeCount();
BinaryOutputArchive oa = BinaryOutputArchive.getArchive(new NullOutputStream());
System.gc();
long start = System.nanoTime();
tree.serialize(oa, "test");
long end = System.nanoTime();
long durationms = (end - start) / 1000000L;
long pernodeus = ((end - start) / 1000L) / count;
LOG.info("Serialized " + count + " nodes in " + durationms + " ms (" + pernodeus + "us/node), depth=" + depth + " width=" + width + " datalen=" + len);
}
Aggregations