Search in sources :

Example 11 with ChannelHandlerContext

use of org.jboss.netty.channel.ChannelHandlerContext in project canal by alibaba.

the class ClientAuthenticationHandler method messageReceived.

public void messageReceived(final ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
    final Packet packet = Packet.parseFrom(buffer.readBytes(buffer.readableBytes()).array());
    switch(packet.getVersion()) {
        case SUPPORTED_VERSION:
        default:
            final ClientAuth clientAuth = ClientAuth.parseFrom(packet.getBody());
            // 如果存在订阅信息
            if (StringUtils.isNotEmpty(clientAuth.getDestination()) && StringUtils.isNotEmpty(clientAuth.getClientId())) {
                ClientIdentity clientIdentity = new ClientIdentity(clientAuth.getDestination(), Short.valueOf(clientAuth.getClientId()), clientAuth.getFilter());
                try {
                    MDC.put("destination", clientIdentity.getDestination());
                    embeddedServer.subscribe(clientIdentity);
                    // 设置状态数据
                    ctx.setAttachment(clientIdentity);
                    // 尝试启动,如果已经启动,忽略
                    if (!embeddedServer.isStart(clientIdentity.getDestination())) {
                        ServerRunningMonitor runningMonitor = ServerRunningMonitors.getRunningMonitor(clientIdentity.getDestination());
                        if (!runningMonitor.isStart()) {
                            runningMonitor.start();
                        }
                    }
                } finally {
                    MDC.remove("destination");
                }
            }
            NettyUtils.ack(ctx.getChannel(), new ChannelFutureListener() {

                public void operationComplete(ChannelFuture future) throws Exception {
                    logger.info("remove unused channel handlers after authentication is done successfully.");
                    ctx.getPipeline().remove(HandshakeInitializationHandler.class.getName());
                    ctx.getPipeline().remove(ClientAuthenticationHandler.class.getName());
                    int readTimeout = defaultSubscriptorDisconnectIdleTimeout;
                    int writeTimeout = defaultSubscriptorDisconnectIdleTimeout;
                    if (clientAuth.getNetReadTimeout() > 0) {
                        readTimeout = clientAuth.getNetReadTimeout();
                    }
                    if (clientAuth.getNetWriteTimeout() > 0) {
                        writeTimeout = clientAuth.getNetWriteTimeout();
                    }
                    IdleStateHandler idleStateHandler = new IdleStateHandler(NettyUtils.hashedWheelTimer, readTimeout, writeTimeout, 0);
                    ctx.getPipeline().addBefore(SessionHandler.class.getName(), IdleStateHandler.class.getName(), idleStateHandler);
                    IdleStateAwareChannelHandler idleStateAwareChannelHandler = new IdleStateAwareChannelHandler() {

                        public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception {
                            logger.warn("channel:{} idle timeout exceeds, close channel to save server resources...", ctx.getChannel());
                            ctx.getChannel().close();
                        }
                    };
                    ctx.getPipeline().addBefore(SessionHandler.class.getName(), IdleStateAwareChannelHandler.class.getName(), idleStateAwareChannelHandler);
                }
            });
            break;
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) IdleStateEvent(org.jboss.netty.handler.timeout.IdleStateEvent) Packet(com.alibaba.otter.canal.protocol.CanalPacket.Packet) ClientIdentity(com.alibaba.otter.canal.protocol.ClientIdentity) IdleStateHandler(org.jboss.netty.handler.timeout.IdleStateHandler) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) ServerRunningMonitor(com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor) ClientAuth(com.alibaba.otter.canal.protocol.CanalPacket.ClientAuth) IdleStateAwareChannelHandler(org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 12 with ChannelHandlerContext

use of org.jboss.netty.channel.ChannelHandlerContext in project MSEC by Tencent.

the class RequestProcessor method call.

public RpcResponse call() throws Exception {
    setThreadContext("session", rpcContext);
    RpcRequest rpcRequest = rpcContext.getRequest();
    ChannelHandlerContext channelHandlerContext = rpcContext.getChannelContext();
    RpcResponse rpcResponse = new RpcResponse();
    rpcResponse.setSerializeMode(rpcRequest.getSerializeMode());
    rpcResponse.setSeq(rpcRequest.getSeq());
    rpcResponse.setResultObj(null);
    rpcResponse.setError(null);
    if (rpcRequest.getException() != null) {
        //already exceptions for the request
        //invalid request
        rpcResponse.setErrno(-1);
        rpcResponse.setError(rpcRequest.getException());
        channelHandlerContext.getChannel().write(rpcResponse);
        return null;
    }
    if (rpcRequest.getHttpCgiName() != null && rpcRequest.getHttpCgiName().compareToIgnoreCase("/list") == 0) {
        processListCgi(rpcRequest, rpcResponse);
        channelHandlerContext.getChannel().write(rpcResponse);
        return null;
    }
    //Map<String, String> headers = new HashMap<String, String>();
    //headers.put("Coloring", "1");
    //AccessLog.doLog(AccessLog.LOG_LEVEL_DEBUG, headers, "system rpc log. request: " + rpcRequest.getParameter());
    String serviceMethodName = rpcRequest.getServiceName() + "/" + rpcRequest.getMethodName();
    log.info("Rpc Call: " + rpcContext.getClientAddr() + " ---> " + rpcContext.getLocalAddr() + " " + serviceMethodName + " request: " + rpcRequest.getParameter());
    //AccessLog.doLog(AccessLog.LOG_LEVEL_INFO, "Rpc Call: " + rpcContext.getClientAddr() + " ---> " +
    //        rpcContext.getLocalAddr() + " " + serviceMethodName + " request: " + rpcRequest.getParameter());
    ServiceFactory.ServiceMethodEntry serviceMethodEntry = ServiceFactory.getServiceMethodEntry(rpcRequest.getServiceName(), rpcRequest.getMethodName());
    if (serviceMethodEntry != null) {
        Object invokeResultObj = null;
        try {
            long timeBegin = System.currentTimeMillis();
            invokeResultObj = serviceMethodEntry.invoke(rpcRequest.getParameter());
            long timeEnd = System.currentTimeMillis();
            log.error("Request processor time cost: " + (timeEnd - timeBegin));
            rpcResponse.setErrno(0);
            rpcResponse.setResultObj(invokeResultObj);
            AccessMonitor.add("frm.rpc " + rpcRequest.getMethodName() + " succ count");
            log.info("RPC invoke discrete method succeeded: " + invokeResultObj);
        //AccessLog.doLog(AccessLog.LOG_LEVEL_INFO, "Rpc Call timecost: " + (timeEnd - timeBegin) + "ms response: " + invokeResultObj);
        } catch (InvocationTargetException ex) {
            rpcResponse.setErrno(-1000);
            rpcResponse.setError(ex);
            log.error("Invoke method failed. " + ex.getMessage());
            AccessLog.doLog(AccessLog.LOG_LEVEL_ERROR, "Rpc Call exception: " + ex.getMessage());
            AccessMonitor.add("frm.rpc " + rpcRequest.getMethodName() + " failed count");
        } catch (IllegalAccessException ex) {
            rpcResponse.setErrno(-1000);
            rpcResponse.setError(ex);
            log.error("Invoke method failed. " + ex.getMessage());
            AccessLog.doLog(AccessLog.LOG_LEVEL_ERROR, "Rpc Call exception: " + ex.getMessage());
            AccessMonitor.add("frm.rpc " + rpcRequest.getMethodName() + " failed count");
        }
    } else {
        log.error("No service method registered: " + rpcRequest.getServiceName() + rpcRequest.getMethodName());
        AccessLog.doLog(AccessLog.LOG_LEVEL_ERROR, "Rpc Call exception: No service method registered: " + rpcRequest.getServiceName() + rpcRequest.getMethodName());
        AccessMonitor.add("frm.rpc " + rpcRequest.getMethodName() + " not registered");
        AccessMonitor.add("frm.rpc " + rpcRequest.getMethodName() + " failed count");
        rpcResponse.setErrno(-1);
        rpcResponse.setError(new Exception("No service method registered: " + rpcRequest.getServiceName() + rpcRequest.getMethodName()));
    }
    channelHandlerContext.getChannel().write(rpcResponse);
    return null;
}
Also used : ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 13 with ChannelHandlerContext

use of org.jboss.netty.channel.ChannelHandlerContext in project bagheera by mozilla-metrics.

the class DummyProducer method testMessage.

// Ensure that one invocation of the message results in the specified number of deletes and creates
// and that two invocations results in double those numbers.
private void testMessage(String id, boolean doDelete, int expectedDeletes, int expectedCreates, String... deletes) throws Exception {
    SubmissionHandler handler = new SubmissionHandler(validator, producer, channelGroup, manager);
    ChannelHandlerContext context = Mockito.mock(ChannelHandlerContext.class);
    MessageEvent messageEvent = Mockito.mock(MessageEvent.class);
    Mockito.when(messageEvent.getChannel()).thenReturn(mockChannel);
    BagheeraHttpRequest mockMessage = getMockMessage(HttpMethod.POST, id, doDelete, deletes);
    Mockito.when(messageEvent.getMessage()).thenReturn(mockMessage);
    testTwoInvocations(expectedDeletes, expectedCreates, handler, context, messageEvent);
    producer.reset();
}
Also used : MessageEvent(org.jboss.netty.channel.MessageEvent) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext)

Example 14 with ChannelHandlerContext

use of org.jboss.netty.channel.ChannelHandlerContext in project bagheera by mozilla-metrics.

the class DummyProducer method testDelete.

@Test
public void testDelete() throws Exception {
    // Test an actual delete (as opposed to a POST message with an X-Obsolete-Doc header).
    SubmissionHandler handler = new SubmissionHandler(validator, producer, channelGroup, manager);
    ChannelHandlerContext context = Mockito.mock(ChannelHandlerContext.class);
    MessageEvent messageEvent = Mockito.mock(MessageEvent.class);
    Mockito.when(messageEvent.getChannel()).thenReturn(mockChannel);
    BagheeraHttpRequest mockMessage = getMockMessage(HttpMethod.DELETE, "delete-id", false);
    Mockito.when(messageEvent.getMessage()).thenReturn(mockMessage);
    testTwoInvocations(1, 0, handler, context, messageEvent);
    producer.reset();
}
Also used : MessageEvent(org.jboss.netty.channel.MessageEvent) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) Test(org.junit.Test)

Example 15 with ChannelHandlerContext

use of org.jboss.netty.channel.ChannelHandlerContext in project hadoop by apache.

the class TestShuffleHandler method testMaxConnections.

/**
   * Validate the limit on number of shuffle connections.
   * 
   * @throws Exception exception
   */
@Test(timeout = 10000)
public void testMaxConnections() throws Exception {
    Configuration conf = new Configuration();
    conf.setInt(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY, 0);
    conf.setInt(ShuffleHandler.MAX_SHUFFLE_CONNECTIONS, 3);
    ShuffleHandler shuffleHandler = new ShuffleHandler() {

        @Override
        protected Shuffle getShuffle(Configuration conf) {
            // replace the shuffle handler with one stubbed for testing
            return new Shuffle(conf) {

                @Override
                protected MapOutputInfo getMapOutputInfo(String mapId, int reduce, String jobId, String user) throws IOException {
                    // Do nothing.
                    return null;
                }

                @Override
                protected void populateHeaders(List<String> mapIds, String jobId, String user, int reduce, HttpRequest request, HttpResponse response, boolean keepAliveParam, Map<String, MapOutputInfo> infoMap) throws IOException {
                // Do nothing.
                }

                @Override
                protected void verifyRequest(String appid, ChannelHandlerContext ctx, HttpRequest request, HttpResponse response, URL requestUri) throws IOException {
                // Do nothing.
                }

                @Override
                protected ChannelFuture sendMapOutput(ChannelHandlerContext ctx, Channel ch, String user, String mapId, int reduce, MapOutputInfo info) throws IOException {
                    // send a shuffle header and a lot of data down the channel
                    // to trigger a broken pipe
                    ShuffleHeader header = new ShuffleHeader("dummy_header", 5678, 5678, 1);
                    DataOutputBuffer dob = new DataOutputBuffer();
                    header.write(dob);
                    ch.write(wrappedBuffer(dob.getData(), 0, dob.getLength()));
                    dob = new DataOutputBuffer();
                    for (int i = 0; i < 100000; ++i) {
                        header.write(dob);
                    }
                    return ch.write(wrappedBuffer(dob.getData(), 0, dob.getLength()));
                }
            };
        }
    };
    shuffleHandler.init(conf);
    shuffleHandler.start();
    // setup connections
    int connAttempts = 3;
    HttpURLConnection[] conns = new HttpURLConnection[connAttempts];
    for (int i = 0; i < connAttempts; i++) {
        String URLstring = "http://127.0.0.1:" + shuffleHandler.getConfig().get(ShuffleHandler.SHUFFLE_PORT_CONFIG_KEY) + "/mapOutput?job=job_12345_1&reduce=1&map=attempt_12345_1_m_" + i + "_0";
        URL url = new URL(URLstring);
        conns[i] = (HttpURLConnection) url.openConnection();
        conns[i].setRequestProperty(ShuffleHeader.HTTP_HEADER_NAME, ShuffleHeader.DEFAULT_HTTP_HEADER_NAME);
        conns[i].setRequestProperty(ShuffleHeader.HTTP_HEADER_VERSION, ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION);
    }
    // Try to open numerous connections
    for (int i = 0; i < connAttempts; i++) {
        conns[i].connect();
    }
    //Ensure first connections are okay
    conns[0].getInputStream();
    int rc = conns[0].getResponseCode();
    Assert.assertEquals(HttpURLConnection.HTTP_OK, rc);
    conns[1].getInputStream();
    rc = conns[1].getResponseCode();
    Assert.assertEquals(HttpURLConnection.HTTP_OK, rc);
    // This connection should be closed because it to above the limit
    try {
        rc = conns[2].getResponseCode();
        Assert.assertEquals("Expected a too-many-requests response code", ShuffleHandler.TOO_MANY_REQ_STATUS.getCode(), rc);
        long backoff = Long.valueOf(conns[2].getHeaderField(ShuffleHandler.RETRY_AFTER_HEADER));
        Assert.assertTrue("The backoff value cannot be negative.", backoff > 0);
        conns[2].getInputStream();
        Assert.fail("Expected an IOException");
    } catch (IOException ioe) {
        LOG.info("Expected - connection should not be open");
    } catch (NumberFormatException ne) {
        Assert.fail("Expected a numerical value for RETRY_AFTER header field");
    } catch (Exception e) {
        Assert.fail("Expected a IOException");
    }
    shuffleHandler.stop();
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) SocketChannel(org.jboss.netty.channel.socket.SocketChannel) Channel(org.jboss.netty.channel.Channel) AbstractChannel(org.jboss.netty.channel.AbstractChannel) ShuffleHeader(org.apache.hadoop.mapreduce.task.reduce.ShuffleHeader) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) IOException(java.io.IOException) URL(java.net.URL) EOFException(java.io.EOFException) IOException(java.io.IOException) ServiceStateException(org.apache.hadoop.service.ServiceStateException) HttpURLConnection(java.net.HttpURLConnection) DataOutputBuffer(org.apache.hadoop.io.DataOutputBuffer) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) Test(org.junit.Test)

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