Search in sources :

Example 1 with AngelThreadFactory

use of com.tencent.angel.common.AngelThreadFactory in project angel by Tencent.

the class MatrixTransportClient method init.

private void init() {
    Configuration conf = PSAgentContext.get().getConf();
    rpcContext.init(conf, PSAgentContext.get().getLocationManager().getPsIds());
    // Init response handler
    registeHandler();
    // Init network parameters
    int nettyWorkerNum = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_CLIENT_EVENTGROUP_THREADNUM, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_CLIENT_EVENTGROUP_THREADNUM);
    int sendBuffSize = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_CLIENT_SNDBUF, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_CLIENT_SNDBUF);
    int recvBuffSize = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_CLIENT_RCVBUF, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_CLIENT_RCVBUF);
    final int maxMessageSize = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_MAX_MESSAGE_SIZE, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_MAX_MESSAGE_SIZE);
    requestThreadPool = Executors.newFixedThreadPool(conf.getInt(AngelConf.ANGEL_MATRIXTRANSFER_CLIENT_REQUESTER_POOL_SIZE, AngelConf.DEFAULT_ANGEL_MATRIXTRANSFER_CLIENT_REQUESTER_POOL_SIZE), new AngelThreadFactory("RPCRequest"));
    responseThreadPool = Executors.newFixedThreadPool(conf.getInt(AngelConf.ANGEL_MATRIXTRANSFER_CLIENT_RESPONSER_POOL_SIZE, AngelConf.DEFAULT_ANGEL_MATRIXTRANSFER_CLIENT_RESPONSER_POOL_SIZE), new AngelThreadFactory("RPCResponser"));
    ChannelPoolParam poolParam = new ChannelPoolParam();
    poolParam.maxActive = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_CLIENT_MAX_CONN_PERSERVER, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_CLIENT_MAX_CONN_PERSERVER);
    poolParam.minActive = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_CLIENT_MIN_CONN_PERSERVER, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_CLIENT_MIN_CONN_PERSERVER);
    poolParam.maxIdleTimeMs = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_CLIENT_MAX_CONN_IDLETIME_MS, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_CLIENT_MAX_CONN_IDLETIME_MS);
    poolParam.getChannelTimeoutMs = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_CLIENT_GET_CONN_TIMEOUT_MS, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_CLIENT_GET_CONN_TIMEOUT_MS);
    int ioRatio = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_CLIENT_IORATIO, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_CLIENT_IORATIO);
    String channelType = conf.get(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_CLIENT_CHANNEL_TYPE, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_CLIENT_CHANNEL_TYPE);
    hbThreadPool = Executors.newFixedThreadPool(8, new AngelThreadFactory("Heartbeat"));
    bootstrap = new Bootstrap();
    channelManager = new ChannelManager2(bootstrap, poolParam);
    channelManager.initAndStart();
    // Use Epoll for linux
    Class channelClass;
    String os = System.getProperty("os.name");
    if (os != null && os.toLowerCase().startsWith("linux") && channelType.equals("epoll")) {
        LOG.info("Use epoll channel");
        channelClass = EpollSocketChannel.class;
        eventGroup = new EpollEventLoopGroup(nettyWorkerNum);
        ((EpollEventLoopGroup) eventGroup).setIoRatio(ioRatio);
    } else {
        LOG.info("Use nio channel");
        channelClass = NioSocketChannel.class;
        eventGroup = new NioEventLoopGroup(nettyWorkerNum);
        ((NioEventLoopGroup) eventGroup).setIoRatio(ioRatio);
    }
    MatrixTransportClient client = this;
    bootstrap.group(eventGroup).channel(channelClass).option(ChannelOption.SO_SNDBUF, sendBuffSize).option(ChannelOption.SO_RCVBUF, recvBuffSize).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeLine = ch.pipeline();
            pipeLine.addLast(new LengthFieldBasedFrameDecoder(maxMessageSize, 0, 4, 0, 4));
            pipeLine.addLast(new LengthFieldPrepender(4));
            pipeLine.addLast(new MatrixTransportClientHandler(client, dispatchMessageQueue, rpcContext));
        }
    });
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) Configuration(org.apache.hadoop.conf.Configuration) AngelThreadFactory(com.tencent.angel.common.AngelThreadFactory) ChannelPoolParam(com.tencent.angel.common.transport.ChannelPoolParam) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) ServiceException(com.google.protobuf.ServiceException) AngelException(com.tencent.angel.exception.AngelException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ChannelPipeline(io.netty.channel.ChannelPipeline) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelManager2(com.tencent.angel.common.transport.ChannelManager2) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

ServiceException (com.google.protobuf.ServiceException)1 AngelThreadFactory (com.tencent.angel.common.AngelThreadFactory)1 ChannelManager2 (com.tencent.angel.common.transport.ChannelManager2)1 ChannelPoolParam (com.tencent.angel.common.transport.ChannelPoolParam)1 AngelException (com.tencent.angel.exception.AngelException)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)1 EpollSocketChannel (io.netty.channel.epoll.EpollSocketChannel)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)1 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Configuration (org.apache.hadoop.conf.Configuration)1