Search in sources :

Example 1 with InputArchive

use of org.apache.jute.InputArchive in project zookeeper by apache.

the class SnapshotFormatter method run.

public void run(String snapshotFileName) throws IOException {
    InputStream is = new CheckedInputStream(new BufferedInputStream(new FileInputStream(snapshotFileName)), new Adler32());
    InputArchive ia = BinaryInputArchive.getArchive(is);
    FileSnap fileSnap = new FileSnap(null);
    DataTree dataTree = new DataTree();
    Map<Long, Integer> sessions = new HashMap<Long, Integer>();
    fileSnap.deserialize(dataTree, sessions, ia);
    printDetails(dataTree, sessions);
}
Also used : FileSnap(org.apache.zookeeper.server.persistence.FileSnap) BufferedInputStream(java.io.BufferedInputStream) HashMap(java.util.HashMap) CheckedInputStream(java.util.zip.CheckedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) CheckedInputStream(java.util.zip.CheckedInputStream) FileInputStream(java.io.FileInputStream) Adler32(java.util.zip.Adler32)

Example 2 with InputArchive

use of org.apache.jute.InputArchive in project zookeeper by apache.

the class Zab1_0Test method testLeaderConversation.

public void testLeaderConversation(LeaderConversation conversation) throws Exception {
    Socket[] pair = getSocketPair();
    Socket leaderSocket = pair[0];
    Socket followerSocket = pair[1];
    File tmpDir = File.createTempFile("test", "dir", testData);
    tmpDir.delete();
    tmpDir.mkdir();
    LeadThread leadThread = null;
    Leader leader = null;
    try {
        QuorumPeer peer = createQuorumPeer(tmpDir);
        leader = createLeader(tmpDir, peer);
        peer.leader = leader;
        leadThread = new LeadThread(leader);
        leadThread.start();
        while (leader.cnxAcceptor == null || !leader.cnxAcceptor.isAlive()) {
            Thread.sleep(20);
        }
        LearnerHandler lh = new LearnerHandler(leaderSocket, leader);
        lh.start();
        leaderSocket.setSoTimeout(4000);
        InputArchive ia = BinaryInputArchive.getArchive(followerSocket.getInputStream());
        OutputArchive oa = BinaryOutputArchive.getArchive(followerSocket.getOutputStream());
        conversation.converseWithLeader(ia, oa, leader);
    } finally {
        if (leader != null) {
            leader.shutdown("end of test");
        }
        if (leadThread != null) {
            leadThread.interrupt();
            leadThread.join();
        }
        TestUtils.deleteFileRecursively(tmpDir);
    }
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) File(java.io.File) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 3 with InputArchive

use of org.apache.jute.InputArchive in project zookeeper by apache.

the class Zab1_0Test method testFollowerConversation.

public void testFollowerConversation(FollowerConversation conversation) throws Exception {
    File tmpDir = File.createTempFile("test", "dir", testData);
    tmpDir.delete();
    tmpDir.mkdir();
    Thread followerThread = null;
    ConversableFollower follower = null;
    QuorumPeer peer = null;
    try {
        peer = createQuorumPeer(tmpDir);
        follower = createFollower(tmpDir, peer);
        peer.follower = follower;
        ServerSocket ss = new ServerSocket(0, 50, InetAddress.getByName("127.0.0.1"));
        follower.setLeaderSocketAddress((InetSocketAddress) ss.getLocalSocketAddress());
        final Follower followerForThread = follower;
        followerThread = new Thread() {

            public void run() {
                try {
                    followerForThread.followLeader();
                } catch (InterruptedException e) {
                    LOG.info("Follower thread interrupted", e);
                } catch (Exception e) {
                    LOG.warn("Unexpected exception in follower thread", e);
                }
            }
        };
        followerThread.start();
        Socket leaderSocket = ss.accept();
        InputArchive ia = BinaryInputArchive.getArchive(leaderSocket.getInputStream());
        OutputArchive oa = BinaryOutputArchive.getArchive(leaderSocket.getOutputStream());
        conversation.converseWithFollower(ia, oa, follower);
    } finally {
        if (follower != null) {
            follower.shutdown();
        }
        if (followerThread != null) {
            followerThread.interrupt();
            followerThread.join();
        }
        if (peer != null) {
            peer.shutdown();
        }
        TestUtils.deleteFileRecursively(tmpDir);
    }
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) ServerSocket(java.net.ServerSocket) File(java.io.File) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 4 with InputArchive

use of org.apache.jute.InputArchive in project exhibitor by soabase.

the class ZooKeeperLogParser method parse.

public void parse(LogEntryReceiver receiver) throws Exception {
    if (!validHeader) {
        throw new Exception("Invalid magic number for");
    }
    while (true) {
        long crcValue;
        byte[] bytes;
        try {
            crcValue = logStream.readLong("crcvalue");
            bytes = logStream.readBuffer("txnEntry");
        } catch (EOFException e) {
            break;
        }
        if (bytes.length == 0) {
            // empty transaction
            break;
        }
        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());
        }
        InputArchive iab = BinaryInputArchive.getArchive(new ByteArrayInputStream(bytes));
        TxnHeader hdr = new TxnHeader();
        Record record = useOldDeserializeMethod ? (Record) deserializeTxnMethod.invoke(null, iab, hdr) : (Record) deserializeTxnMethod.invoke(null, bytes, hdr);
        if (logStream.readByte("EOR") != 'B') {
            // partial transaction
            break;
        }
        receiver.receiveEntry(hdr, record);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Checksum(java.util.zip.Checksum) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) EOFException(java.io.EOFException) Record(org.apache.jute.Record) IOException(java.io.IOException) IOException(java.io.IOException) EOFException(java.io.EOFException) Adler32(java.util.zip.Adler32) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 5 with InputArchive

use of org.apache.jute.InputArchive in project zookeeper by apache.

the class SerializeUtils method deserializeTxn.

public static Record deserializeTxn(byte[] txnBytes, TxnHeader hdr) throws IOException {
    final ByteArrayInputStream bais = new ByteArrayInputStream(txnBytes);
    InputArchive ia = BinaryInputArchive.getArchive(bais);
    hdr.deserialize(ia, "hdr");
    bais.mark(bais.available());
    Record txn = null;
    switch(hdr.getType()) {
        case OpCode.createSession:
            // This isn't really an error txn; it just has the same
            // format. The error represents the timeout
            txn = new CreateSessionTxn();
            break;
        case OpCode.closeSession:
            return null;
        case OpCode.create:
        case OpCode.create2:
            txn = new CreateTxn();
            break;
        case OpCode.createTTL:
            txn = new CreateTTLTxn();
            break;
        case OpCode.createContainer:
            txn = new CreateContainerTxn();
            break;
        case OpCode.delete:
        case OpCode.deleteContainer:
            txn = new DeleteTxn();
            break;
        case OpCode.reconfig:
        case OpCode.setData:
            txn = new SetDataTxn();
            break;
        case OpCode.setACL:
            txn = new SetACLTxn();
            break;
        case OpCode.error:
            txn = new ErrorTxn();
            break;
        case OpCode.multi:
            txn = new MultiTxn();
            break;
        default:
            throw new IOException("Unsupported Txn with type=%d" + hdr.getType());
    }
    if (txn != null) {
        try {
            txn.deserialize(ia, "txn");
        } catch (EOFException e) {
            // perhaps this is a V0 Create
            if (hdr.getType() == OpCode.create) {
                CreateTxn create = (CreateTxn) txn;
                bais.reset();
                CreateTxnV0 createv0 = new CreateTxnV0();
                createv0.deserialize(ia, "txn");
                // cool now make it V1. a -1 parentCVersion will
                // trigger fixup processing in processTxn
                create.setPath(createv0.getPath());
                create.setData(createv0.getData());
                create.setAcl(createv0.getAcl());
                create.setEphemeral(createv0.getEphemeral());
                create.setParentCVersion(-1);
            } else {
                throw e;
            }
        }
    }
    return txn;
}
Also used : CreateContainerTxn(org.apache.zookeeper.txn.CreateContainerTxn) MultiTxn(org.apache.zookeeper.txn.MultiTxn) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) SetDataTxn(org.apache.zookeeper.txn.SetDataTxn) IOException(java.io.IOException) CreateTxnV0(org.apache.zookeeper.txn.CreateTxnV0) CreateTTLTxn(org.apache.zookeeper.txn.CreateTTLTxn) DeleteTxn(org.apache.zookeeper.txn.DeleteTxn) CreateSessionTxn(org.apache.zookeeper.txn.CreateSessionTxn) CreateTxn(org.apache.zookeeper.txn.CreateTxn) ErrorTxn(org.apache.zookeeper.txn.ErrorTxn) ByteArrayInputStream(java.io.ByteArrayInputStream) EOFException(java.io.EOFException) Record(org.apache.jute.Record) SetACLTxn(org.apache.zookeeper.txn.SetACLTxn)

Aggregations

BinaryInputArchive (org.apache.jute.BinaryInputArchive)10 InputArchive (org.apache.jute.InputArchive)10 IOException (java.io.IOException)7 File (java.io.File)5 BufferedInputStream (java.io.BufferedInputStream)4 EOFException (java.io.EOFException)4 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 ServerSocket (java.net.ServerSocket)4 Socket (java.net.Socket)4 Adler32 (java.util.zip.Adler32)4 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)4 OutputArchive (org.apache.jute.OutputArchive)4 CheckedInputStream (java.util.zip.CheckedInputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 Record (org.apache.jute.Record)2 CreateTxn (org.apache.zookeeper.txn.CreateTxn)2 TxnHeader (org.apache.zookeeper.txn.TxnHeader)2 FilterInputStream (java.io.FilterInputStream)1