Search in sources :

Example 6 with OutputArchive

use of org.apache.jute.OutputArchive 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, new BufferedInputStream(leaderSocket.getInputStream()), 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) MockLeader(org.apache.zookeeper.server.quorum.ZabUtils.MockLeader) ZabUtils.createMockLeader(org.apache.zookeeper.server.quorum.ZabUtils.createMockLeader) ZabUtils.createLeader(org.apache.zookeeper.server.quorum.ZabUtils.createLeader) BufferedInputStream(java.io.BufferedInputStream) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) ZabUtils.createQuorumPeer(org.apache.zookeeper.server.quorum.ZabUtils.createQuorumPeer) File(java.io.File) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 7 with OutputArchive

use of org.apache.jute.OutputArchive 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"));
        QuorumServer leaderQS = new QuorumServer(1, (InetSocketAddress) ss.getLocalSocketAddress());
        follower.setLeaderQuorumServer(leaderQS);
        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) QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) ZabUtils.createQuorumPeer(org.apache.zookeeper.server.quorum.ZabUtils.createQuorumPeer) ServerSocket(java.net.ServerSocket) File(java.io.File) EOFException(java.io.EOFException) IOException(java.io.IOException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 8 with OutputArchive

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

the class SerializeUtilsTest method testSerializeRequestWithoutTxn.

@Test
public void testSerializeRequestWithoutTxn() throws IOException {
    // Arrange
    TxnHeader header = mock(TxnHeader.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            OutputArchive oa = (OutputArchive) args[0];
            oa.writeString("header", "test");
            return null;
        }
    }).when(header).serialize(any(OutputArchive.class), anyString());
    Request request = new Request(1, 2, 3, header, null, 4);
    // Act
    byte[] data = SerializeUtils.serializeRequest(request);
    // Assert
    assertNotNull(data);
    verify(header).serialize(any(OutputArchive.class), eq("hdr"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
    boa.writeString("header", "test");
    baos.close();
    assertArrayEquals(baos.toByteArray(), data);
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Request(org.apache.zookeeper.server.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.jupiter.api.Test)

Example 9 with OutputArchive

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

the class Util method marshallTxnEntry.

/**
 * Serializes transaction header and transaction data into a byte buffer.
 *
 * @param hdr transaction header
 * @param txn transaction data
 * @return serialized transaction record
 * @throws IOException
 */
public static byte[] marshallTxnEntry(TxnHeader hdr, Record txn) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputArchive boa = BinaryOutputArchive.getArchive(baos);
    hdr.serialize(boa, "hdr");
    if (txn != null) {
        txn.serialize(boa, "txn");
    }
    return baos.toByteArray();
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 10 with OutputArchive

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

the class Zab1_0Test method testObserverConversation.

public void testObserverConversation(ObserverConversation conversation) throws Exception {
    File tmpDir = File.createTempFile("test", "dir", testData);
    tmpDir.delete();
    tmpDir.mkdir();
    Thread observerThread = null;
    ConversableObserver observer = null;
    QuorumPeer peer = null;
    try {
        peer = createQuorumPeer(tmpDir);
        peer.setSyncEnabled(true);
        observer = createObserver(tmpDir, peer);
        peer.observer = observer;
        ServerSocket ss = new ServerSocket(0, 50, InetAddress.getByName("127.0.0.1"));
        QuorumServer leaderQS = new QuorumServer(1, (InetSocketAddress) ss.getLocalSocketAddress());
        observer.setLeaderQuorumServer(leaderQS);
        final Observer observerForThread = observer;
        observerThread = new Thread() {

            public void run() {
                try {
                    observerForThread.observeLeader();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        observerThread.start();
        Socket leaderSocket = ss.accept();
        InputArchive ia = BinaryInputArchive.getArchive(leaderSocket.getInputStream());
        OutputArchive oa = BinaryOutputArchive.getArchive(leaderSocket.getOutputStream());
        conversation.converseWithObserver(ia, oa, observer);
    } finally {
        if (observer != null) {
            observer.shutdown();
        }
        if (observerThread != null) {
            observerThread.interrupt();
            observerThread.join();
        }
        if (peer != null) {
            peer.shutdown();
        }
        TestUtils.deleteFileRecursively(tmpDir);
    }
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) ZabUtils.createQuorumPeer(org.apache.zookeeper.server.quorum.ZabUtils.createQuorumPeer) ServerSocket(java.net.ServerSocket) File(java.io.File) EOFException(java.io.EOFException) IOException(java.io.IOException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Aggregations

OutputArchive (org.apache.jute.OutputArchive)16 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)14 BinaryInputArchive (org.apache.jute.BinaryInputArchive)7 File (java.io.File)6 InputArchive (org.apache.jute.InputArchive)6 TxnHeader (org.apache.zookeeper.txn.TxnHeader)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ServerSocket (java.net.ServerSocket)4 Socket (java.net.Socket)4 CheckedOutputStream (java.util.zip.CheckedOutputStream)4 ZabUtils.createQuorumPeer (org.apache.zookeeper.server.quorum.ZabUtils.createQuorumPeer)4 Test (org.junit.jupiter.api.Test)4 Mockito.doAnswer (org.mockito.Mockito.doAnswer)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Answer (org.mockito.stubbing.Answer)4 IOException (java.io.IOException)3 Record (org.apache.jute.Record)3 Request (org.apache.zookeeper.server.Request)3 BufferedInputStream (java.io.BufferedInputStream)2 EOFException (java.io.EOFException)2