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