use of org.apache.jute.BinaryInputArchive in project zookeeper by apache.
the class ControllableConnection method readRequest.
@Override
protected void readRequest() throws IOException {
if (controller.shouldFailNextRequest()) {
ByteBuffer buffer = incomingBuffer.slice();
BinaryInputArchive bia = BinaryInputArchive.getArchive(new ByteBufferInputStream(buffer));
RequestHeader h = new RequestHeader();
h.deserialize(bia, "header");
super.sendResponse(new ReplyHeader(h.getXid(), 0, KeeperException.Code.APIERROR.intValue()), null, null);
} else {
controller.delayRequestIfNeeded();
super.readRequest();
}
}
use of org.apache.jute.BinaryInputArchive in project zookeeper by apache.
the class SaslQuorumAuthServer method receive.
private byte[] receive(DataInputStream din) throws IOException {
QuorumAuthPacket authPacket = new QuorumAuthPacket();
BinaryInputArchive bia = BinaryInputArchive.getArchive(din);
authPacket.deserialize(bia, QuorumAuth.QUORUM_AUTH_MESSAGE_TAG);
return authPacket.getToken();
}
use of org.apache.jute.BinaryInputArchive in project zookeeper by apache.
the class LogChopper method chop.
public static boolean chop(InputStream is, OutputStream os, long zxid) throws IOException {
BinaryInputArchive logStream = BinaryInputArchive.getArchive(is);
BinaryOutputArchive choppedStream = BinaryOutputArchive.getArchive(os);
FileHeader fhdr = new FileHeader();
fhdr.deserialize(logStream, "fileheader");
if (fhdr.getMagic() != FileTxnLog.TXNLOG_MAGIC) {
System.err.println("Invalid magic number in txn log file");
return false;
}
System.out.println("ZooKeeper Transactional Log File with dbid " + fhdr.getDbid() + " txnlog format version " + fhdr.getVersion());
fhdr.serialize(choppedStream, "fileheader");
int count = 0;
boolean hasZxid = false;
long previousZxid = -1;
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.");
// returning false because nothing was chopped
return false;
}
if (bytes.length == 0) {
// Since we preallocate, we define EOF to be an
// empty transaction
System.out.println("EOF reached after " + count + " txns.");
// returning false because nothing was chopped
return false;
}
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());
}
TxnLogEntry entry = SerializeUtils.deserializeTxn(bytes);
TxnHeader hdr = entry.getHeader();
Record txn = entry.getTxn();
if (logStream.readByte("EOR") != 'B') {
System.out.println("Last transaction was partial.");
throw new EOFException("Last transaction was partial.");
}
final long txnZxid = hdr.getZxid();
if (txnZxid == zxid) {
hasZxid = true;
}
// logging the gap to make the inconsistency investigation easier
if (previousZxid != -1 && txnZxid != previousZxid + 1) {
long txnEpoch = ZxidUtils.getEpochFromZxid(txnZxid);
long txnCounter = ZxidUtils.getCounterFromZxid(txnZxid);
long previousEpoch = ZxidUtils.getEpochFromZxid(previousZxid);
if (txnEpoch == previousEpoch) {
System.out.println(String.format("There is intra-epoch gap between %x and %x", previousZxid, txnZxid));
} else if (txnCounter != 1) {
System.out.println(String.format("There is inter-epoch gap between %x and %x", previousZxid, txnZxid));
}
}
previousZxid = txnZxid;
if (txnZxid > zxid) {
if (count == 0 || !hasZxid) {
System.out.println(String.format("This log does not contain zxid %x", zxid));
return false;
}
System.out.println(String.format("Chopping at %x new log has %d records", zxid, count));
return true;
}
choppedStream.writeLong(crcValue, "crcvalue");
choppedStream.writeBuffer(bytes, "txnEntry");
choppedStream.writeByte((byte) 'B', "EOR");
count++;
}
}
use of org.apache.jute.BinaryInputArchive in project zookeeper by apache.
the class DataTreeTest method testPathTrieClearOnDeserialize.
@Test
@Timeout(value = 60)
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.quotaPath("/bug"), null, null, -1, 1, 1, 1);
dserTree.createNode(Quotas.limitPath("/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
assertEquals("/", pTrie.findMaxPrefix("/bug"), "/bug is still in pTrie");
}
use of org.apache.jute.BinaryInputArchive in project zookeeper by apache.
the class DataTreeTest method testDeserializeDoesntLockACLCacheWhileReading.
/* ZOOKEEPER-3531 - similarly for aclCache.deserialize, we should not hold lock either
*/
@Test
@Timeout(value = 60)
public void testDeserializeDoesntLockACLCacheWhileReading() throws Exception {
DataTree tree = new DataTree();
tree.createNode("/marker", new byte[] { 42 }, null, -1, 1, 1, 1);
final AtomicBoolean ranTestCase = new AtomicBoolean();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baos);
BinaryOutputArchive oa = new BinaryOutputArchive(out);
tree.serialize(oa, "test");
DataTree tree2 = new DataTree();
DataInputStream in = new DataInputStream(new ByteArrayInputStream(baos.toByteArray()));
BinaryInputArchive ia = new BinaryInputArchive(in) {
@Override
public long readLong(String tag) throws IOException {
final Semaphore semaphore = new Semaphore(0);
new Thread(new Runnable() {
@Override
public void run() {
synchronized (tree2.getReferenceCountedAclCache()) {
// When we lock ACLCache, allow readLong to continue
semaphore.release();
}
}
}).start();
try {
boolean acquired = semaphore.tryAcquire(30, TimeUnit.SECONDS);
// This is the real assertion - could another thread lock
// the ACLCache
assertTrue(acquired, "Couldn't acquire a lock on the ACLCache while we were calling tree.deserialize");
} catch (InterruptedException e1) {
throw new RuntimeException(e1);
}
ranTestCase.set(true);
return super.readLong(tag);
}
};
tree2.deserialize(ia, "test");
// Let's make sure that we hit the code that ran the real assertion above
assertTrue(ranTestCase.get(), "Didn't find the expected node");
}
Aggregations