Search in sources :

Example 6 with ExtendedReadTimeoutHandler

use of com.linkedin.databus2.core.container.ExtendedReadTimeoutHandler in project databus by linkedin.

the class SimpleClientPipelineFactory method testServerSimpleRequestResponse.

@Test
public /**
   * Simulates the follow communication with no induced timeouts
   *
   * <table>
   * <th><td>client</td><td>server</td></th>
   * <tr><td>send "hello"</td><td></td></tr>
   * <tr><td></td><td>start read timeout</td></tr>
   * <tr><td>send "eom"</td><td></td></tr>
   * <tr><td>start read timeout</td><td></td></tr>
   * <tr><td></td><td>stop read timeout</td></tr>
   * <tr><td></td>send "hi there"<td></td></tr>
   * <tr><td></td>send "eom"<td></td></tr>
   * <tr><td>stop read timeout</td><td></td></tr>
   * </table>
   */
void testServerSimpleRequestResponse() {
    SimpleTestServerConnection srvConn = new SimpleTestServerConnection(_eventFactory.getByteOrder());
    srvConn.setPipelineFactory(new SimpleServerPipelineFactory());
    boolean serverStarted = srvConn.startSynchronously(1, CONNECT_TIMEOUT_MS);
    Assert.assertTrue(serverStarted, "server started");
    SimpleTestClientConnection clientConn = new SimpleTestClientConnection(_eventFactory.getByteOrder());
    clientConn.setPipelineFactory(new SimpleClientPipelineFactory());
    boolean clientConnected = clientConn.startSynchronously(1, CONNECT_TIMEOUT_MS);
    Assert.assertTrue(clientConnected, "client connected");
    //hook in to key places in the server pipeline
    ChannelPipeline lastSrvConnPipeline = srvConn.getLastConnChannel().getPipeline();
    ExtendedReadTimeoutHandler srvTimeoutHandler = (ExtendedReadTimeoutHandler) lastSrvConnPipeline.get(ExtendedReadTimeoutHandler.class.getSimpleName());
    SimpleTestMessageReader srvMsgReader = (SimpleTestMessageReader) lastSrvConnPipeline.get(SimpleTestMessageReader.class.getSimpleName());
    ExceptionListenerTestHandler srvExceptionListener = (ExceptionListenerTestHandler) lastSrvConnPipeline.get(ExceptionListenerTestHandler.class.getSimpleName());
    //hook in to key places in the client pipeline
    ChannelPipeline clientPipeline = clientConn.getChannel().getPipeline();
    ExtendedReadTimeoutHandler clientTimeoutHandler = (ExtendedReadTimeoutHandler) clientPipeline.get(ExtendedReadTimeoutHandler.class.getSimpleName());
    SimpleTestMessageReader clientMsgReader = (SimpleTestMessageReader) clientPipeline.get(SimpleTestMessageReader.class.getSimpleName());
    ExceptionListenerTestHandler clientExceptionListener = (ExceptionListenerTestHandler) clientPipeline.get(ExceptionListenerTestHandler.class.getSimpleName());
    //send a request
    ChannelBuffer msg = ChannelBuffers.wrappedBuffer("hello".getBytes(Charset.defaultCharset()));
    clientConn.getChannel().write(msg);
    //wait for the request to propagate
    try {
        Thread.sleep(10);
    } catch (InterruptedException ie) {
    }
    ;
    Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout");
    Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout");
    Assert.assertEquals(srvMsgReader.getMsg(), "hello", "message read");
    //start server read timeout
    srvTimeoutHandler.start(lastSrvConnPipeline.getContext(srvTimeoutHandler));
    ChannelBuffer msg2 = ChannelBuffers.wrappedBuffer("eom".getBytes(Charset.defaultCharset()));
    clientConn.getChannel().write(msg2);
    //start the client timeout handler
    clientTimeoutHandler.start(clientPipeline.getContext(clientTimeoutHandler));
    Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout");
    Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout");
    Assert.assertEquals(srvMsgReader.getMsg(), "eom", "message read");
    //stop server read timeout
    srvTimeoutHandler.stop();
    ChannelBuffer resp = ChannelBuffers.wrappedBuffer("hi there".getBytes(Charset.defaultCharset()));
    lastSrvConnPipeline.getChannel().write(resp);
    //wait for the response to propagate
    try {
        Thread.sleep(10);
    } catch (InterruptedException ie) {
    }
    ;
    Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout");
    Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout");
    Assert.assertEquals(clientMsgReader.getMsg(), "hi there", "response read");
    ChannelBuffer resp2 = ChannelBuffers.wrappedBuffer("eom".getBytes(Charset.defaultCharset()));
    lastSrvConnPipeline.getChannel().write(resp2);
    //wait for the response to propagate
    try {
        Thread.sleep(10);
    } catch (InterruptedException ie) {
    }
    ;
    Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout");
    Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout");
    Assert.assertEquals(clientMsgReader.getMsg(), "eom", "response read");
    //stop client read timeout
    clientTimeoutHandler.stop();
    clientConn.stop();
    srvConn.stop();
}
Also used : SimpleTestClientConnection(com.linkedin.databus2.test.container.SimpleTestClientConnection) SimpleTestMessageReader(com.linkedin.databus2.test.container.SimpleTestMessageReader) SimpleTestServerConnection(com.linkedin.databus2.test.container.SimpleTestServerConnection) ExceptionListenerTestHandler(com.linkedin.databus2.test.container.ExceptionListenerTestHandler) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Test(org.testng.annotations.Test)

Example 7 with ExtendedReadTimeoutHandler

use of com.linkedin.databus2.core.container.ExtendedReadTimeoutHandler in project databus by linkedin.

the class SimpleClientPipelineFactory method testServerComplexRequestResponse.

@Test
public /**
   * Simulates the follow communication with no induced timeouts
   *
   * <table>
   * <th><td>client</td><td>server</td></th>
   * <tr><td>send "hello1"</td><td></td></tr>
   * <tr><td></td><td>start read timeout</td></tr>
   * <tr><td>send "hello2"</td><td></td></tr>
   * <tr><td>:</td><td></td></tr>
   * <tr><td>:</td><td></td></tr>
   * <tr><td>send "hello50"</td><td></td></tr>
   * <tr><td>send "eom"</td><td></td></tr>
   * <tr><td>start read timeout</td><td></td></tr>
   * <tr><td></td><td>stop read timeout</td></tr>
   * <tr><td></td>send "hi there1"<td></td></tr>
   * <tr><td></td>send ":"<td></td></tr>
   * <tr><td></td>send "hi there100"<td></td></tr>
   * <tr><td></td>send "eom"<td></td></tr>
   * <tr><td>stop read timeout</td><td></td></tr>
   * </table>
   */
void testServerComplexRequestResponse() {
    SimpleTestServerConnection srvConn = new SimpleTestServerConnection(_eventFactory.getByteOrder());
    srvConn.setPipelineFactory(new SimpleServerPipelineFactory());
    boolean serverStarted = srvConn.startSynchronously(2, CONNECT_TIMEOUT_MS);
    Assert.assertTrue(serverStarted, "server started");
    SimpleTestClientConnection clientConn = new SimpleTestClientConnection(_eventFactory.getByteOrder());
    clientConn.setPipelineFactory(new SimpleClientPipelineFactory());
    boolean clientConnected = clientConn.startSynchronously(2, CONNECT_TIMEOUT_MS);
    Assert.assertTrue(clientConnected, "client connected");
    //hook in to key places in the server pipeline
    ChannelPipeline lastSrvConnPipeline = srvConn.getLastConnChannel().getPipeline();
    ExtendedReadTimeoutHandler srvTimeoutHandler = (ExtendedReadTimeoutHandler) lastSrvConnPipeline.get(ExtendedReadTimeoutHandler.class.getSimpleName());
    SimpleTestMessageReader srvMsgReader = (SimpleTestMessageReader) lastSrvConnPipeline.get(SimpleTestMessageReader.class.getSimpleName());
    ExceptionListenerTestHandler srvExceptionListener = (ExceptionListenerTestHandler) lastSrvConnPipeline.get(ExceptionListenerTestHandler.class.getSimpleName());
    //hook in to key places in the client pipeline
    ChannelPipeline clientPipeline = clientConn.getChannel().getPipeline();
    ExtendedReadTimeoutHandler clientTimeoutHandler = (ExtendedReadTimeoutHandler) clientPipeline.get(ExtendedReadTimeoutHandler.class.getSimpleName());
    SimpleTestMessageReader clientMsgReader = (SimpleTestMessageReader) clientPipeline.get(SimpleTestMessageReader.class.getSimpleName());
    ExceptionListenerTestHandler clientExceptionListener = (ExceptionListenerTestHandler) clientPipeline.get(ExceptionListenerTestHandler.class.getSimpleName());
    for (int i = 0; i < 50; ++i) {
        //send a request
        ChannelBuffer msg = ChannelBuffers.wrappedBuffer(("hello" + i).getBytes(Charset.defaultCharset()));
        clientConn.getChannel().write(msg);
        //wait for the request to propagate
        try {
            Thread.sleep(10);
        } catch (InterruptedException ie) {
        }
        ;
        Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout: " + i);
        Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout: " + i);
        Assert.assertEquals(srvMsgReader.getMsg(), "hello" + i, "message read: " + i);
        //start server read timeout after the first message
        if (0 == i)
            srvTimeoutHandler.start(lastSrvConnPipeline.getContext(srvTimeoutHandler));
    }
    ChannelBuffer msg2 = ChannelBuffers.wrappedBuffer("eom".getBytes(Charset.defaultCharset()));
    clientConn.getChannel().write(msg2);
    //start the client timeout handler
    clientTimeoutHandler.start(clientPipeline.getContext(clientTimeoutHandler));
    Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout");
    Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout");
    Assert.assertEquals(srvMsgReader.getMsg(), "eom", "message read");
    //stop server read timeout
    srvTimeoutHandler.stop();
    for (int i = 0; i < 100; ++i) {
        String responseString = "hi there " + i;
        ChannelBuffer resp = ChannelBuffers.wrappedBuffer(responseString.getBytes(Charset.defaultCharset()));
        lastSrvConnPipeline.getChannel().write(resp);
        //wait for the response to propagate
        try {
            Thread.sleep(10);
        } catch (InterruptedException ie) {
        }
        ;
        Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout: " + i);
        Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout: " + i);
        Assert.assertEquals(clientMsgReader.getMsg(), responseString, "response read: " + i);
    }
    ChannelBuffer resp2 = ChannelBuffers.wrappedBuffer("eom".getBytes(Charset.defaultCharset()));
    lastSrvConnPipeline.getChannel().write(resp2);
    //wait for the response to propagate
    try {
        Thread.sleep(10);
    } catch (InterruptedException ie) {
    }
    ;
    Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout");
    Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout");
    Assert.assertEquals(clientMsgReader.getMsg(), "eom", "response read");
    //stop client read timeout
    clientTimeoutHandler.stop();
    clientConn.stop();
    srvConn.stop();
}
Also used : SimpleTestClientConnection(com.linkedin.databus2.test.container.SimpleTestClientConnection) SimpleTestMessageReader(com.linkedin.databus2.test.container.SimpleTestMessageReader) SimpleTestServerConnection(com.linkedin.databus2.test.container.SimpleTestServerConnection) ExceptionListenerTestHandler(com.linkedin.databus2.test.container.ExceptionListenerTestHandler) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Test(org.testng.annotations.Test)

Example 8 with ExtendedReadTimeoutHandler

use of com.linkedin.databus2.core.container.ExtendedReadTimeoutHandler in project databus by linkedin.

the class TcpServerPipelineFactory method getPipeline.

@Override
public ChannelPipeline getPipeline() throws Exception {
    // Create a default pipeline implementation.
    ChannelPipeline pipeline = pipeline();
    pipeline.addLast("auto group register ", new ConnectionChannelRegistrationHandler(_serverContainer.getTcpChannelGroup()));
    if (Logger.getRootLogger().isTraceEnabled()) {
        pipeline.addLast("netty server traffic", new LoggingHandler("netty server traffic", InternalLogLevel.DEBUG, true));
    }
    pipeline.addLast("outbound statistics collector", new OutboundContainerStatisticsCollectingHandler(_serverContainer.getContainerStatsCollector()));
    ExtendedWriteTimeoutHandler writeTimeoutHandler = new ExtendedWriteTimeoutHandler("server container " + _serverContainer.getContainerStaticConfig().getId(), _serverContainer.getNetworkTimeoutTimer(), _serverContainer.getContainerStaticConfig().getWriteTimeoutMs(), true);
    ExtendedReadTimeoutHandler readTimeoutHandler = new ExtendedReadTimeoutHandler("server container " + _serverContainer.getContainerStaticConfig().getId(), _serverContainer.getNetworkTimeoutTimer(), _serverContainer.getContainerStaticConfig().getReadTimeoutMs(), true);
    pipeline.addLast("read timeout", readTimeoutHandler);
    //add a handler to deal with write timeouts
    pipeline.addLast("server container write timeout handler", writeTimeoutHandler);
    pipeline.addLast("decoder", new SimpleBinaryDatabusRequestDecoder(_serverContainer.getCommandsRegistry(), readTimeoutHandler, _byteOrder));
    pipeline.addLast("encoder", new SimpleBinaryDatabusResponseEncoder());
    // Fix for DDSDBUS-1000
    //    pipeline.addLast("executionHandler", _serverContainer.getNettyExecHandler());
    //Dummy handler that will be automatically replaced depending on the current command being
    //executed
    pipeline.addLast(SimpleBinaryDatabusRequestDecoder.REQUEST_EXEC_HANDLER_NAME, new LoggingHandler("netty server traffic", InternalLogLevel.DEBUG, true));
    return pipeline;
}
Also used : LoggingHandler(org.jboss.netty.handler.logging.LoggingHandler) ExtendedWriteTimeoutHandler(com.linkedin.databus2.core.container.ExtendedWriteTimeoutHandler) ExtendedReadTimeoutHandler(com.linkedin.databus2.core.container.ExtendedReadTimeoutHandler) SimpleBinaryDatabusRequestDecoder(com.linkedin.databus2.core.container.request.SimpleBinaryDatabusRequestDecoder) SimpleBinaryDatabusResponseEncoder(com.linkedin.databus2.core.container.request.SimpleBinaryDatabusResponseEncoder) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline)

Aggregations

ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)8 ExceptionListenerTestHandler (com.linkedin.databus2.test.container.ExceptionListenerTestHandler)5 SimpleTestMessageReader (com.linkedin.databus2.test.container.SimpleTestMessageReader)5 SimpleTestClientConnection (com.linkedin.databus2.test.container.SimpleTestClientConnection)4 SimpleTestServerConnection (com.linkedin.databus2.test.container.SimpleTestServerConnection)4 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)4 Test (org.testng.annotations.Test)4 ExtendedReadTimeoutHandler (com.linkedin.databus2.core.container.ExtendedReadTimeoutHandler)3 ExtendedWriteTimeoutHandler (com.linkedin.databus2.core.container.ExtendedWriteTimeoutHandler)3 LoggingHandler (org.jboss.netty.handler.logging.LoggingHandler)3 HttpRequestLoggingHandler (com.linkedin.databus2.core.container.HttpRequestLoggingHandler)2 ConnectionChannelRegistrationHandler (com.linkedin.databus2.core.container.netty.ConnectionChannelRegistrationHandler)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 ReadTimeoutException (org.jboss.netty.handler.timeout.ReadTimeoutException)2 DatabusRequestExecutionHandler (com.linkedin.databus2.core.container.netty.DatabusRequestExecutionHandler)1 HttpRequestHandler (com.linkedin.databus2.core.container.netty.HttpRequestHandler)1 InboundContainerStatisticsCollectingHandler (com.linkedin.databus2.core.container.netty.InboundContainerStatisticsCollectingHandler)1 OutboundContainerStatisticsCollectingHandler (com.linkedin.databus2.core.container.netty.OutboundContainerStatisticsCollectingHandler)1 SimpleBinaryDatabusRequestDecoder (com.linkedin.databus2.core.container.request.SimpleBinaryDatabusRequestDecoder)1 SimpleBinaryDatabusResponseEncoder (com.linkedin.databus2.core.container.request.SimpleBinaryDatabusResponseEncoder)1