Search in sources :

Example 1 with OutgoingPacket

use of org.apache.heron.common.network.OutgoingPacket in project heron by twitter.

the class HandleWriteTest method testHandleWrite.

/**
 * Test write into network
 */
@Test
public void testHandleWrite() throws IOException {
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(new InetSocketAddress(HOST, getServerPort()));
    SocketChannel socketChannel = null;
    try {
        StreamManagerClient streamManagerClient = runStreamManagerClient();
        socketChannel = acceptSocketChannel(serverSocketChannel);
        // Receive request
        REQID rid = readIncomingPacket(socketChannel).unpackREQID();
        OutgoingPacket outgoingPacket = new OutgoingPacket(rid, UnitTestHelper.getRegisterInstanceResponse());
        outgoingPacket.writeToChannel(socketChannel);
        HeronServerTester.await(getInControlQueueOfferLatch());
        for (int i = 0; i < 10; i++) {
            // We randomly choose some messages writing to stream mgr
            streamManagerClient.sendMessage(UnitTestHelper.getRegisterInstanceResponse());
        }
        for (int i = 0; i < 10; i++) {
            IncomingPacket incomingPacket = readIncomingPacket(socketChannel);
            incomingPacket.unpackREQID();
            StreamManager.RegisterInstanceResponse.Builder builder = StreamManager.RegisterInstanceResponse.newBuilder();
            incomingPacket.unpackMessage(builder);
            StreamManager.RegisterInstanceResponse response = builder.build();
            Assert.assertNotNull(response);
            Assert.assertTrue(response.isInitialized());
            Assert.assertEquals(Common.StatusCode.OK, response.getStatus().getStatus());
            Assert.assertEquals(1, response.getPplan().getStmgrsCount());
            Assert.assertEquals(2, response.getPplan().getInstancesCount());
            Assert.assertEquals(1, response.getPplan().getTopology().getBoltsCount());
            Assert.assertEquals(1, response.getPplan().getTopology().getSpoutsCount());
            Assert.assertEquals(TopologyAPI.TopologyState.RUNNING, response.getPplan().getTopology().getState());
        }
        getNIOLooper().exitLoop();
    } catch (ClosedChannelException ignored) {
    } finally {
        close(socketChannel);
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) IncomingPacket(org.apache.heron.common.network.IncomingPacket) InetSocketAddress(java.net.InetSocketAddress) StreamManager(org.apache.heron.proto.stmgr.StreamManager) REQID(org.apache.heron.common.network.REQID) ServerSocketChannel(java.nio.channels.ServerSocketChannel) OutgoingPacket(org.apache.heron.common.network.OutgoingPacket) Test(org.junit.Test)

Example 2 with OutgoingPacket

use of org.apache.heron.common.network.OutgoingPacket in project heron by twitter.

the class HandleReadTest method testHandleRead.

/**
 * Test reading from network
 */
@Test
public void testHandleRead() throws IOException {
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(new InetSocketAddress(HOST, getServerPort()));
    SocketChannel socketChannel = null;
    try {
        runStreamManagerClient();
        socketChannel = acceptSocketChannel(serverSocketChannel);
        // Receive request
        REQID rid = readIncomingPacket(socketChannel).unpackREQID();
        OutgoingPacket outgoingPacket = new OutgoingPacket(rid, UnitTestHelper.getRegisterInstanceResponse());
        outgoingPacket.writeToChannel(socketChannel);
        HeronServerTester.await(getInControlQueueOfferLatch());
        outgoingPacket = new OutgoingPacket(REQID.zeroREQID, constructMockMessage());
        outgoingPacket.writeToChannel(socketChannel);
        HeronServerTester.await(getInStreamQueueOfferLatch());
        getNIOLooper().exitLoop();
        Assert.assertEquals(1, getInStreamQueue().size());
        Message msg = getInStreamQueue().poll();
        Assert.assertTrue(msg instanceof HeronTuples.HeronTupleSet);
        HeronTuples.HeronTupleSet heronTupleSet = (HeronTuples.HeronTupleSet) msg;
        Assert.assertTrue(heronTupleSet.hasData());
        Assert.assertFalse(heronTupleSet.hasControl());
        HeronTuples.HeronDataTupleSet heronDataTupleSet = heronTupleSet.getData();
        Assert.assertEquals("test-spout", heronDataTupleSet.getStream().getComponentName());
        Assert.assertEquals("default", heronDataTupleSet.getStream().getId());
        StringBuilder response = new StringBuilder();
        for (HeronTuples.HeronDataTuple heronDataTuple : heronDataTupleSet.getTuplesList()) {
            response.append(heronDataTuple.getValues(0).toStringUtf8());
            Assert.assertEquals(1, heronDataTuple.getRootsCount());
        }
        Assert.assertEquals("ABABABABAB", response.toString());
    } catch (ClosedChannelException ignored) {
    } finally {
        close(socketChannel);
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) Message(com.google.protobuf.Message) InetSocketAddress(java.net.InetSocketAddress) HeronTuples(org.apache.heron.proto.system.HeronTuples) REQID(org.apache.heron.common.network.REQID) ServerSocketChannel(java.nio.channels.ServerSocketChannel) OutgoingPacket(org.apache.heron.common.network.OutgoingPacket) Test(org.junit.Test)

Example 3 with OutgoingPacket

use of org.apache.heron.common.network.OutgoingPacket in project heron by twitter.

the class ConnectTest method testStart.

/**
 * Test connection
 */
@Test
public void testStart() throws IOException {
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(new InetSocketAddress(HOST, getServerPort()));
    SocketChannel socketChannel = null;
    try {
        runStreamManagerClient();
        socketChannel = acceptSocketChannel(serverSocketChannel);
        // Receive request
        REQID rid = readIncomingPacket(socketChannel).unpackREQID();
        OutgoingPacket outgoingPacket = new OutgoingPacket(rid, UnitTestHelper.getRegisterInstanceResponse());
        outgoingPacket.writeToChannel(socketChannel);
        HeronServerTester.await(getInControlQueueOfferLatch());
        InstanceControlMsg instanceControlMsg = getInControlQueue().poll();
        assertNotNull(instanceControlMsg);
        getNIOLooper().exitLoop();
        getThreadPool().shutdownNow();
        PhysicalPlanHelper physicalPlanHelper = instanceControlMsg.getNewPhysicalPlanHelper();
        Assert.assertEquals("test-bolt", physicalPlanHelper.getMyComponent());
        Assert.assertEquals(InetAddress.getLocalHost().getHostName(), physicalPlanHelper.getMyHostname());
        Assert.assertEquals(0, physicalPlanHelper.getMyInstanceIndex());
        Assert.assertEquals(1, physicalPlanHelper.getMyTaskId());
    } catch (ClosedChannelException ignored) {
    } finally {
        close(socketChannel);
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) InstanceControlMsg(org.apache.heron.instance.InstanceControlMsg) PhysicalPlanHelper(org.apache.heron.common.utils.misc.PhysicalPlanHelper) InetSocketAddress(java.net.InetSocketAddress) REQID(org.apache.heron.common.network.REQID) ServerSocketChannel(java.nio.channels.ServerSocketChannel) OutgoingPacket(org.apache.heron.common.network.OutgoingPacket) Test(org.junit.Test)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 ServerSocketChannel (java.nio.channels.ServerSocketChannel)3 SocketChannel (java.nio.channels.SocketChannel)3 OutgoingPacket (org.apache.heron.common.network.OutgoingPacket)3 REQID (org.apache.heron.common.network.REQID)3 Test (org.junit.Test)3 Message (com.google.protobuf.Message)1 IncomingPacket (org.apache.heron.common.network.IncomingPacket)1 PhysicalPlanHelper (org.apache.heron.common.utils.misc.PhysicalPlanHelper)1 InstanceControlMsg (org.apache.heron.instance.InstanceControlMsg)1 StreamManager (org.apache.heron.proto.stmgr.StreamManager)1 HeronTuples (org.apache.heron.proto.system.HeronTuples)1