Search in sources :

Example 6 with ChannelHandlerContext

use of org.jboss.netty.channel.ChannelHandlerContext in project neo4j by neo4j.

the class NetworkReceiverTest method testMessageReceivedOriginFix.

@Test
public void testMessageReceivedOriginFix() throws Exception {
    LogProvider logProvider = mock(LogProvider.class);
    when(logProvider.getLog(NetworkReceiver.class)).thenReturn(mock(Log.class));
    NetworkReceiver networkReceiver = new NetworkReceiver(mock(NetworkReceiver.Monitor.class), mock(NetworkReceiver.Configuration.class), logProvider);
    // This defines where message is coming from
    final InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", PORT);
    final Channel channel = mock(Channel.class);
    when(channel.getLocalAddress()).thenReturn(inetSocketAddress);
    when(channel.getRemoteAddress()).thenReturn(inetSocketAddress);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.getChannel()).thenReturn(channel);
    final Message message = Message.to(new MessageType() {

        @Override
        public String name() {
            return "test";
        }
    }, new URI("cluster://anywhere"));
    MessageEvent messageEvent = mock(MessageEvent.class);
    when(messageEvent.getRemoteAddress()).thenReturn(inetSocketAddress);
    when(messageEvent.getMessage()).thenReturn(message);
    when(messageEvent.getChannel()).thenReturn(channel);
    // the original FROM header should be ignored
    message.setHeader(Message.FROM, "cluster://someplace:1234");
    networkReceiver.new MessageReceiver().messageReceived(ctx, messageEvent);
    assertEquals("FROM header should have been changed to visible ip address: " + message.getHeader(Message.FROM), "cluster://127.0.0.1:1234", message.getHeader(Message.FROM));
}
Also used : Message(org.neo4j.cluster.com.message.Message) Log(org.neo4j.logging.Log) InetSocketAddress(java.net.InetSocketAddress) MessageEvent(org.jboss.netty.channel.MessageEvent) Channel(org.jboss.netty.channel.Channel) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) URI(java.net.URI) LogProvider(org.neo4j.logging.LogProvider) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 7 with ChannelHandlerContext

use of org.jboss.netty.channel.ChannelHandlerContext in project neo4j by neo4j.

the class ServerTest method channelCtx.

private ChannelHandlerContext channelCtx(Channel channel) {
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.getChannel()).thenReturn(channel);
    return ctx;
}
Also used : ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext)

Example 8 with ChannelHandlerContext

use of org.jboss.netty.channel.ChannelHandlerContext in project databus by linkedin.

the class TestNettyHttpDatabusBootstrapConnection method testServerBootstrapDisconnect.

@Test
public /**
   * This is a unit test for DDSDBUS-3537. There is a lag between a network channel disconnect and the
   * state change in AbstractNettyHttpConnection. This can cause a race condition in various requestXXX objects which
   * check the state of the connection using the network channel. As a result, they may attempt to reconnect while
   * AbstractNettyHttpConnection is still in CONNECTED state which causes an error for an incorrect transition
   * CONNECTED -> CONNECTING.
   *
   *  The test simulates the above condition by injecting a handler in the client pipeline which artificially holds up
   *  the channelClosed message. As a result we can inject a request while the netty channel is disconnected but the
   *  AbstractNettyHttpConnection object has not detected this yet.
   */
void testServerBootstrapDisconnect() throws Exception {
    final Logger log = Logger.getLogger("TestNettyHttpDatabusBootstrapConnection.testServerBootstrapDisconnect");
    log.info("starting");
    log.info("setup the client");
    TestingConnectionCallback callback = TestingConnectionCallback.createAndStart("testServerSourcesDisconnect");
    DummyRemoteExceptionHandler remoteExceptionHandler = new DummyRemoteExceptionHandler();
    final NettyHttpDatabusBootstrapConnection conn = (NettyHttpDatabusBootstrapConnection) CONN_FACTORY.createConnection(BOOTSTRAP_SERVER_INFO, callback, remoteExceptionHandler);
    try {
        log.info("initial setup");
        final List<String> sourceNamesList = Arrays.asList(SOURCE1_NAME);
        final Checkpoint cp = Checkpoint.createOnlineConsumptionCheckpoint(0);
        BootstrapCheckpointHandler cpHandler = new BootstrapCheckpointHandler(sourceNamesList);
        cpHandler.createInitialBootstrapCheckpoint(cp, 0L);
        final DummyDatabusBootstrapConnectionStateMessage bstCallback = new DummyDatabusBootstrapConnectionStateMessage(log);
        log.info("process a normal startSCN which should establish the connection");
        sendStartScnHappyPath(conn, cp, bstCallback, SOURCE1_NAME, 100L, log);
        Assert.assertTrue(conn.isConnected());
        //wait for the response
        TestUtil.assertWithBackoff(new ConditionCheck() {

            @Override
            public boolean check() {
                return null != bstCallback.getCheckpoint();
            }
        }, "wait for /startSCN response", 100, log);
        log.info("verify /startSCN response");
        final Checkpoint startScnCp = bstCallback.getCheckpoint();
        Assert.assertNotNull(startScnCp);
        Assert.assertEquals(100L, startScnCp.getBootstrapStartScn().longValue());
        log.info("instrument the client pipeline so that we can intercept and delay the channelClosed message");
        final Semaphore passMessage = new Semaphore(1);
        final CountDownLatch closeSent = new CountDownLatch(1);
        passMessage.acquire();
        conn._channel.getPipeline().addBefore("handler", "closeChannelDelay", new SimpleChannelHandler() {

            @Override
            public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
                closeSent.countDown();
                passMessage.acquire();
                try {
                    super.channelClosed(ctx, e);
                } finally {
                    passMessage.release();
                }
            }
        });
        final Channel serverChannel = getServerChannelForClientConn(conn);
        Thread asyncChannelClose = new Thread(new Runnable() {

            @Override
            public void run() {
                log.info("closing server channel");
                serverChannel.close();
                log.info("server channel: closed");
                closeSent.countDown();
            }
        }, "asyncChannelCloseThread");
        asyncChannelClose.setDaemon(true);
        Thread asyncBootstrapReq = new Thread(new Runnable() {

            @Override
            public void run() {
                conn.requestStream("1", null, 10000, startScnCp, bstCallback);
            }
        }, "asyncBootstrapReqThread");
        asyncBootstrapReq.setDaemon(true);
        log.info("simultaneously closing connection and sending /bootstrap request");
        bstCallback.reset();
        asyncChannelClose.start();
        Assert.assertTrue(closeSent.await(1000, TimeUnit.MILLISECONDS));
        TestUtil.assertWithBackoff(new ConditionCheck() {

            @Override
            public boolean check() {
                return !conn._channel.isConnected();
            }
        }, "waiting for disconnect on the client side", 1000, log);
        Assert.assertEquals(AbstractNettyHttpConnection.State.CONNECTED, conn.getNetworkState());
        log.info("asynchronously sending /bootstrap");
        asyncBootstrapReq.start();
        log.info("letting channelClose get through");
        TestUtil.assertWithBackoff(new ConditionCheck() {

            @Override
            public boolean check() {
                return bstCallback.isStreamRequestError();
            }
        }, "wait for streamRequestError callback", 1000, log);
        passMessage.release();
        log.info("finished");
    } finally {
        conn.close();
        callback.shutdown();
        log.info("cleaned");
    }
}
Also used : ConditionCheck(com.linkedin.databus2.test.ConditionCheck) ChunkedBodyReadableByteChannel(com.linkedin.databus.client.ChunkedBodyReadableByteChannel) Channel(org.jboss.netty.channel.Channel) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) Semaphore(java.util.concurrent.Semaphore) Logger(org.apache.log4j.Logger) CountDownLatch(java.util.concurrent.CountDownLatch) BootstrapCheckpointHandler(com.linkedin.databus.core.BootstrapCheckpointHandler) InvalidConfigException(com.linkedin.databus.core.util.InvalidConfigException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) Checkpoint(com.linkedin.databus.core.Checkpoint) ChannelStateEvent(org.jboss.netty.channel.ChannelStateEvent) SimpleChannelHandler(org.jboss.netty.channel.SimpleChannelHandler) Test(org.testng.annotations.Test)

Example 9 with ChannelHandlerContext

use of org.jboss.netty.channel.ChannelHandlerContext in project graylog2-server by Graylog2.

the class NettyTransport method getBaseChannelHandlers.

/**
     * Subclasses can override this to add additional ChannelHandlers to the pipeline to support additional features.
     * <p/>
     * Some common use cases are to add SSL/TLS, connection counters or throttling traffic shapers.
     *
     * @param input
     * @return the list of initial channelhandlers to add to the {@link org.jboss.netty.channel.ChannelPipelineFactory}
     */
protected LinkedHashMap<String, Callable<? extends ChannelHandler>> getBaseChannelHandlers(final MessageInput input) {
    LinkedHashMap<String, Callable<? extends ChannelHandler>> handlerList = Maps.newLinkedHashMap();
    handlerList.put("exception-logger", new Callable<ChannelHandler>() {

        @Override
        public ChannelHandler call() throws Exception {
            return new SimpleChannelUpstreamHandler() {

                @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
                    if ("Connection reset by peer".equals(e.getCause().getMessage())) {
                        log.trace("{} in Input [{}/{}] (channel {})", e.getCause().getMessage(), input.getName(), input.getId(), e.getChannel());
                    } else {
                        log.error("Error in Input [{}/{}] (channel {})", input.getName(), input.getId(), e.getChannel(), e.getCause());
                    }
                    super.exceptionCaught(ctx, e);
                }
            };
        }
    });
    handlerList.put("packet-meta-dumper", new Callable<ChannelHandler>() {

        @Override
        public ChannelHandler call() throws Exception {
            return new PacketInformationDumper(input);
        }
    });
    handlerList.put("traffic-counter", Callables.returning(throughputCounter));
    return handlerList;
}
Also used : ExceptionEvent(org.jboss.netty.channel.ExceptionEvent) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) SimpleChannelHandler(org.jboss.netty.channel.SimpleChannelHandler) ChannelHandler(org.jboss.netty.channel.ChannelHandler) SimpleChannelUpstreamHandler(org.jboss.netty.channel.SimpleChannelUpstreamHandler) PacketInformationDumper(org.graylog2.plugin.inputs.util.PacketInformationDumper) Callable(java.util.concurrent.Callable) MisfireException(org.graylog2.plugin.inputs.MisfireException)

Example 10 with ChannelHandlerContext

use of org.jboss.netty.channel.ChannelHandlerContext in project cdap by caskdata.

the class NettyRouter method startUp.

@Override
protected void startUp() throws ServiceBindException {
    ChannelUpstreamHandler connectionTracker = new SimpleChannelUpstreamHandler() {

        @Override
        public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
            channelGroup.add(e.getChannel());
            super.channelOpen(ctx, e);
        }
    };
    tokenValidator.startAndWait();
    timer = new HashedWheelTimer(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("router-idle-event-generator-timer").build());
    bootstrapClient(connectionTracker);
    bootstrapServer(connectionTracker);
}
Also used : SimpleChannelUpstreamHandler(org.jboss.netty.channel.SimpleChannelUpstreamHandler) ChannelUpstreamHandler(org.jboss.netty.channel.ChannelUpstreamHandler) ChannelStateEvent(org.jboss.netty.channel.ChannelStateEvent) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) HashedWheelTimer(org.jboss.netty.util.HashedWheelTimer) SimpleChannelUpstreamHandler(org.jboss.netty.channel.SimpleChannelUpstreamHandler)

Aggregations

ChannelHandlerContext (org.jboss.netty.channel.ChannelHandlerContext)16 Test (org.junit.Test)8 Channel (org.jboss.netty.channel.Channel)7 ArrayList (java.util.ArrayList)6 Configuration (org.apache.hadoop.conf.Configuration)6 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)6 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)6 HttpURLConnection (java.net.HttpURLConnection)5 URL (java.net.URL)5 DataOutputBuffer (org.apache.hadoop.io.DataOutputBuffer)5 AbstractChannel (org.jboss.netty.channel.AbstractChannel)5 SocketChannel (org.jboss.netty.channel.socket.SocketChannel)5 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)5 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)5 DataInputStream (java.io.DataInputStream)4 List (java.util.List)4 Map (java.util.Map)4 ShuffleHeader (org.apache.hadoop.mapreduce.task.reduce.ShuffleHeader)4 EOFException (java.io.EOFException)3 MessageEvent (org.jboss.netty.channel.MessageEvent)3