Search in sources :

Example 6 with BinaryInputArchive

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();
    }
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) ByteBufferInputStream(org.apache.zookeeper.server.ByteBufferInputStream) RequestHeader(org.apache.zookeeper.proto.RequestHeader) ByteBuffer(java.nio.ByteBuffer)

Example 7 with BinaryInputArchive

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();
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) QuorumAuthPacket(org.apache.zookeeper.server.quorum.QuorumAuthPacket)

Example 8 with BinaryInputArchive

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++;
    }
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) IOException(java.io.IOException) Adler32(java.util.zip.Adler32) BinaryInputArchive(org.apache.jute.BinaryInputArchive) Checksum(java.util.zip.Checksum) TxnLogEntry(org.apache.zookeeper.server.TxnLogEntry) EOFException(java.io.EOFException) Record(org.apache.jute.Record) FileHeader(org.apache.zookeeper.server.persistence.FileHeader) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 9 with BinaryInputArchive

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");
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) Field(java.lang.reflect.Field) PathTrie(org.apache.zookeeper.common.PathTrie) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 10 with BinaryInputArchive

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");
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Semaphore(java.util.concurrent.Semaphore) DataInputStream(java.io.DataInputStream) BinaryInputArchive(org.apache.jute.BinaryInputArchive) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

BinaryInputArchive (org.apache.jute.BinaryInputArchive)25 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)7 FileHeader (org.apache.zookeeper.server.persistence.FileHeader)7 Test (org.junit.jupiter.api.Test)7 IOException (java.io.IOException)6 FileInputStream (java.io.FileInputStream)5 Record (org.apache.jute.Record)5 ByteBufferInputStream (org.apache.zookeeper.server.ByteBufferInputStream)5 TxnHeader (org.apache.zookeeper.txn.TxnHeader)5 ByteBuffer (java.nio.ByteBuffer)4 EOFException (java.io.EOFException)3 File (java.io.File)3 Adler32 (java.util.zip.Adler32)3 Checksum (java.util.zip.Checksum)3 Timeout (org.junit.jupiter.api.Timeout)3 ACL (org.apache.zookeeper.data.ACL)2 ConnectRequest (org.apache.zookeeper.proto.ConnectRequest)2 ReplyHeader (org.apache.zookeeper.proto.ReplyHeader)2