Search in sources :

Example 1 with OutgoingPacket

use of com.twitter.heron.common.network.OutgoingPacket in project heron by twitter.

the class ConnectTest method testStart.

/**
   * Test connection
   */
@Test
public void testStart() throws Exception {
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(new InetSocketAddress(HOST, serverPort));
    SocketChannel socketChannel = null;
    try {
        runStreamManagerClient();
        socketChannel = serverSocketChannel.accept();
        configure(socketChannel);
        socketChannel.configureBlocking(false);
        close(serverSocketChannel);
        // Receive request
        IncomingPacket incomingPacket = new IncomingPacket();
        while (incomingPacket.readFromChannel(socketChannel) != 0) {
            // 1ms sleep to mitigate busy looping
            SysUtils.sleep(1);
        }
        // Send back response
        // Though we do not use typeName, we need to unpack it first,
        // since the order is required
        String typeName = incomingPacket.unpackString();
        REQID rid = incomingPacket.unpackREQID();
        OutgoingPacket outgoingPacket = new OutgoingPacket(rid, UnitTestHelper.getRegisterInstanceResponse());
        outgoingPacket.writeToChannel(socketChannel);
        for (int i = 0; i < Constants.RETRY_TIMES; i++) {
            InstanceControlMsg instanceControlMsg = inControlQueue.poll();
            if (instanceControlMsg != null) {
                nioLooper.exitLoop();
                threadsPool.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());
                break;
            } else {
                SysUtils.sleep(Constants.RETRY_INTERVAL_MS);
            }
        }
    } catch (ClosedChannelException ignored) {
    } finally {
        close(socketChannel);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) IncomingPacket(com.twitter.heron.common.network.IncomingPacket) InstanceControlMsg(com.twitter.heron.instance.InstanceControlMsg) PhysicalPlanHelper(com.twitter.heron.common.utils.misc.PhysicalPlanHelper) InetSocketAddress(java.net.InetSocketAddress) REQID(com.twitter.heron.common.network.REQID) ServerSocketChannel(java.nio.channels.ServerSocketChannel) OutgoingPacket(com.twitter.heron.common.network.OutgoingPacket) Test(org.junit.Test)

Example 2 with OutgoingPacket

use of com.twitter.heron.common.network.OutgoingPacket in project heron by twitter.

the class HandleWriteTest method testHandleWrite.

/**
   * Test write into network
   */
@Test
public void testHandleWrite() throws Exception {
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(new InetSocketAddress(HOST, serverPort));
    SocketChannel socketChannel = null;
    try {
        runStreamManagerClient();
        socketChannel = serverSocketChannel.accept();
        configure(socketChannel);
        socketChannel.configureBlocking(false);
        close(serverSocketChannel);
        // Receive request
        IncomingPacket incomingPacket = new IncomingPacket();
        while (incomingPacket.readFromChannel(socketChannel) != 0) {
            // 1ms sleep to mitigate busy looping
            SysUtils.sleep(1);
        }
        // Send back response
        // Though we do not use typeName, we need to unpack it first,
        // since the order is required
        String typeName = incomingPacket.unpackString();
        REQID rid = incomingPacket.unpackREQID();
        OutgoingPacket outgoingPacket = new OutgoingPacket(rid, UnitTestHelper.getRegisterInstanceResponse());
        outgoingPacket.writeToChannel(socketChannel);
        for (int i = 0; i < Constants.RETRY_TIMES; i++) {
            InstanceControlMsg instanceControlMsg = inControlQueue.poll();
            if (instanceControlMsg != null) {
                break;
            } else {
                SysUtils.sleep(Constants.RETRY_INTERVAL_MS);
            }
        }
        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 = new IncomingPacket();
            while (incomingPacket.readFromChannel(socketChannel) != 0) {
                // 1ms sleep to mitigate busy looping
                SysUtils.sleep(1);
            }
            typeName = incomingPacket.unpackString();
            rid = 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());
        }
        nioLooper.exitLoop();
    } catch (ClosedChannelException ignored) {
    } finally {
        close(socketChannel);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) IncomingPacket(com.twitter.heron.common.network.IncomingPacket) InstanceControlMsg(com.twitter.heron.instance.InstanceControlMsg) InetSocketAddress(java.net.InetSocketAddress) StreamManager(com.twitter.heron.proto.stmgr.StreamManager) REQID(com.twitter.heron.common.network.REQID) ServerSocketChannel(java.nio.channels.ServerSocketChannel) OutgoingPacket(com.twitter.heron.common.network.OutgoingPacket) Test(org.junit.Test)

Example 3 with OutgoingPacket

use of com.twitter.heron.common.network.OutgoingPacket in project heron by twitter.

the class HandleReadTest method testHandleRead.

/**
   * Test reading from network
   */
@Test
public void testHandleRead() throws Exception {
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(new InetSocketAddress(HOST, serverPort));
    SocketChannel socketChannel = null;
    try {
        runStreamManagerClient();
        socketChannel = serverSocketChannel.accept();
        configure(socketChannel);
        socketChannel.configureBlocking(false);
        close(serverSocketChannel);
        // Receive request
        IncomingPacket incomingPacket = new IncomingPacket();
        while (incomingPacket.readFromChannel(socketChannel) != 0) {
            // 1ms sleep to mitigate busy looping
            SysUtils.sleep(1);
        }
        // Send back response
        // Though we do not use typeName, we need to unpack it first,
        // since the order is required
        String typeName = incomingPacket.unpackString();
        REQID rid = incomingPacket.unpackREQID();
        OutgoingPacket outgoingPacket = new OutgoingPacket(rid, UnitTestHelper.getRegisterInstanceResponse());
        outgoingPacket.writeToChannel(socketChannel);
        for (int i = 0; i < Constants.RETRY_TIMES; i++) {
            InstanceControlMsg instanceControlMsg = inControlQueue.poll();
            if (instanceControlMsg != null) {
                break;
            } else {
                SysUtils.sleep(Constants.RETRY_INTERVAL_MS);
            }
        }
        outgoingPacket = new OutgoingPacket(REQID.zeroREQID, constructMockMessage());
        outgoingPacket.writeToChannel(socketChannel);
        for (int i = 0; i < Constants.RETRY_TIMES; i++) {
            if (!inStreamQueue.isEmpty()) {
                break;
            }
            SysUtils.sleep(Constants.RETRY_INTERVAL_MS);
        }
        nioLooper.exitLoop();
        Assert.assertEquals(1, inStreamQueue.size());
        HeronTuples.HeronTupleSet msg = inStreamQueue.poll();
        HeronTuples.HeronTupleSet 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());
        String res = "";
        for (HeronTuples.HeronDataTuple heronDataTuple : heronDataTupleSet.getTuplesList()) {
            res += heronDataTuple.getValues(0).toStringUtf8();
            Assert.assertEquals(1, heronDataTuple.getRootsCount());
        }
        Assert.assertEquals("ABABABABAB", res);
    } catch (ClosedChannelException ignored) {
    } finally {
        close(socketChannel);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) IncomingPacket(com.twitter.heron.common.network.IncomingPacket) InstanceControlMsg(com.twitter.heron.instance.InstanceControlMsg) InetSocketAddress(java.net.InetSocketAddress) ByteString(com.google.protobuf.ByteString) HeronTuples(com.twitter.heron.proto.system.HeronTuples) REQID(com.twitter.heron.common.network.REQID) ServerSocketChannel(java.nio.channels.ServerSocketChannel) OutgoingPacket(com.twitter.heron.common.network.OutgoingPacket) Test(org.junit.Test)

Aggregations

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