rocketmq remoting 源码阅读笔记

rocketmq网络部分的整体的架构


remoting 模块是 mq 的基础通信模块,理解通信层的原理对理解模块间的交互很有帮助。RocketMQ Remoting 模块底层基于 Netty 网络库驱动,因此需要先了解一些基本的Netty原理。

Netty

Netty 使用 Reactor 模式,将监听线程、IO 线程、业务逻辑线程隔离开来。对每个连接,都对应一个 ChannelPipeline。ChannelPipeline 的默认实现 DefaultChannelPipeline 中用一个双向链表储存着若干 ChannelHandlerContext,每个ChannelHandlerContext 又对应着一个 ChannelHandler。链表的头部是一个 ChannelOutboundHandler:

class HeadContext extends AbstractChannelHandlerContext implements ChannelOutboundHandler {
  ...
}

尾部是一个 ChannelInboundHandler:

class TailContext extends AbstractChannelHandlerContext implements ChannelInboundHandler {
          ...
}

这里的 Inbound 是指这个 Handler 处理 外界触发 的事件,典型的就是对端发送了数据过来; Outbound 是指事件是 自己触发 的,比如向对端发送数据。同时,一个 inbound 的事件将在 ChannelPipeline 的 ChannelHandlerContext 链表中从头到尾传播;而一个 outbound 的事件将会在 ChannelPipeline 的 ChannelHandlerContext 链表中从尾向头传播。这样,就能将数据解码、数据处理、数据编码等操作分散到不同的 ChannelHandler 中去了。

另外,RocketMQ 的协议格式如下,开头4字节表示整个消息长度,随后4字节表示头部数据的长度,最后就是消息体的长度:

<4 byte length> <4 byte header length> <N byte header data> <N byte body data>

最后,我们再来看一下 RocketMQ remoting 部分的 UML 图,了解一下其大概由哪些部分组成:

RocketMQ remoting

上图这些类中,最重要的是 NettyRemotingClient 和 NettyRemotingServer,它们的一些公共方法就被封装在 NettyRemotingAbstract 中。

RemotingServer


有了上面的基本认识,就可以开始着手分析 RemotingServer 的源码了。

启动

public void start() {
       ...
       ServerBootstrap childHandler =
                this.serverBootstrap.group(this.eventLoopGroupBoss, this.eventLoopGroupSelector)
                        .channel(NioServerSocketChannel.class)
                        .option(ChannelOption.SO_BACKLOG, 1024)
                        .option(ChannelOption.SO_REUSEADDR, true)
                        .option(ChannelOption.SO_KEEPALIVE, false)
                        .childOption(ChannelOption.TCP_NODELAY, true)
                        .option(ChannelOption.SO_SNDBUF, nettyServerConfig.getServerSocketSndBufSize())
                        .option(ChannelOption.SO_RCVBUF, nettyServerConfig.getServerSocketRcvBufSize())
                        .localAddress(new InetSocketAddress(this.nettyServerConfig.getListenPort()))
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            public void initChannel(SocketChannel ch) throws Exception {
                                ch.pipeline().addLast(
                                        defaultEventExecutorGroup,
                                        new NettyEncoder(),
                                        new NettyDecoder(),
                                        new IdleStateHandler(0, 0, nettyServerConfig.getServerChannelMaxIdleTimeSeconds()),
                                        new NettyConnetManageHandler(),
                                        new NettyServerHandler());
                            }
                        });
  ...
        try {
            ChannelFuture sync = this.serverBootstrap.bind().sync();
            InetSocketAddress addr = (InetSocketAddress) sync.channel().localAddress();
            this.port = addr.getPort();
        } catch (InterruptedException e1) {
            throw new RuntimeException("this.serverBootstrap.bind().sync() InterruptedException", e1);
        }
  ...
}

可以看到,在 NettyRemotingServer 的 start() 方法中,启动了 netty,使用成员变量 eventLoopGroupBoss 接受连接,使用 eventLoopGroupSelector 处理 IO,并且使用 defaultEventExecutorGroup 来处理 ChannelHandler 中的业务逻辑。nettyServerConfig 用来封装对 Netty 的配置信息,包括 SendBufSize、RcvBufSize 等。最重要的是,添加了 NettyEncoderNettyDecoderIdleStateHandlerNettyConnetManageHandlerNettyServerHandler 几个ChannelHandler。

随后,如果 channelEventListener 不为 null, 则启动一个专门的线程监听 Channel 的各种事件。

      if (this.channelEventListener != null) {
            this.nettyEventExecuter.start();
      }

这个类主要是循环的从一个 LinkedBlockingQueue 中读取事件,而后调用 channelEventListener 的不同方法处理事件:

 class NettyEventExecuter extends ServiceThread {
        //使用一个 LinkedBlockingQueue 来存储待处理的 NettyEvent
        private final LinkedBlockingQueue<NettyEvent> eventQueue = new LinkedBlockingQueue<NettyEvent>();
        private final int maxSize = 10000;

        //添加待处理事件,如果队列大小没用超过限制,则将事件入队
        public void putNettyEvent(final NettyEvent event) {
            if (this.eventQueue.size() <= maxSize) {
                this.eventQueue.add(event);
            } else {
                PLOG.warn("event queue size[{}] enough, so drop this event {}", this.eventQueue.size(), event.toString());
            }
        }

        @Override
        public void run() {
            PLOG.info(this.getServiceName() + " service started");

            final ChannelEventListener listener = NettyRemotingAbstract.this.getChannelEventListener();
            //循环读取事件,并处理
            while (!this.isStopped()) {
                try {
                    NettyEvent event = this.eventQueue.poll(3000, TimeUnit.MILLISECONDS);
                    if (event != null && listener != null) {
                        switch (event.getType()) {
                            case IDLE:
                                listener.onChannelIdle(event.getRemoteAddr(), event.getChannel());
                                break;
                            case CLOSE:
                                listener.onChannelClose(event.getRemoteAddr(), event.getChannel());
                                break;
                            case CONNECT:
                                listener.onChannelConnect(event.getRemoteAddr(), event.getChannel());
                                break;
                            case EXCEPTION:
                                listener.onChannelException(event.getRemoteAddr(), event.getChannel());
                                break;
                            default:
                                break;
                        }
                    }
                } catch (Exception e) {
                    PLOG.warn(this.getServiceName() + " service has exception. ", e);
                }
            }

            PLOG.info(this.getServiceName() + " service end");
        }
  ...
}

随后,则启动一个定时器,每隔一段时间查看 responseTable 是否有超时未回应的请求,并完成一些清理工作,responseTable 的作用将在后文说明发送请求过程时说明:

       this.timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                try {
                    NettyRemotingServer.this.scanResponseTable();
                } catch (Exception e) {
                    log.error("scanResponseTable exception", e);
                }
            }
        }, 1000 * 3, 1000);

至此,NettyRemotingServer 的启动过程就结束了。

ChannelHandler

在启动时,向 ChannelPipeline 中添加了以下ChannelHandler,我们分别来解释其作用。

NettyEncoder

对发送请求按照上文提到的格式进行编码,没用什么特殊的:

    @Override
    public void encode(ChannelHandlerContext ctx, RemotingCommand remotingCommand, ByteBuf out)
        throws Exception {
        try {
            ByteBuffer header = remotingCommand.encodeHeader();
            out.writeBytes(header);
            byte[] body = remotingCommand.getBody();
            if (body != null) {
                out.writeBytes(body);
            }
        } catch (Exception e) {
            log.error("encode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e);
            if (remotingCommand != null) {
                log.error(remotingCommand.toString());
            }
            RemotingUtil.closeChannel(ctx.channel());
        }
    }

NettyDecoder

与 NettyEncoder 相反,这是一个 Inbound ChannelHandler,对接收到的数据进行解码,注意由于 RocketMQ 的协议的头部是定长的,所以它继承了 LengthFieldBasedFrameDecoder:

 @Override
    public Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
        ByteBuf frame = null;
        try {
            frame = (ByteBuf) super.decode(ctx, in);
            if (null == frame) {
                return null;
            }

            ByteBuffer byteBuffer = frame.nioBuffer();

            return RemotingCommand.decode(byteBuffer);
        } catch (Exception e) {
            log.error("decode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e);
            RemotingUtil.closeChannel(ctx.channel());
        } finally {
            if (null != frame) {
                frame.release();
            }
        }

        return null;
    }

IdleStateHandler

这个 Handler 是用来进行 keepalive 的,当一段时间没有发送或接收到数据时,则触发 IdleStateEvent。

  protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
        ctx.fireUserEventTriggered(evt);
  }

NettyConnetManageHandler

负责处理各种连接事件,尤其是 IdleState,将其交给 channelEventListener 处理。

IdleStateEvent evnet = (IdleStateEvent) evt;
if ( evnet.state().equals( IdleState.ALL_IDLE ) )
{
    final String remoteAddress = RemotingHelper.parseChannelRemoteAddr( ctx.channel() );
    log.warn( "NETTY SERVER PIPELINE: IDLE exception [{}]", remoteAddress );
    RemotingUtil.closeChannel( ctx.channel() );
    if ( NettyRemotingServer.this.channelEventListener != null )
    {
        NettyRemotingServer.this
        .putNettyEvent( new NettyEvent( NettyEventType.IDLE, remoteAddress.toString(), ctx.channel() ) );
    }
}

NettyServerHandler

调用 NettyRemotingAbstract 的 processMessageReceived 方法处理请求。

class NettyServerHandler extends SimpleChannelInboundHandler<RemotingCommand> {
    @Override
    protected void channelRead0( ChannelHandlerContext ctx, RemotingCommand msg ) throws Exception
    {
        processMessageReceived( ctx, msg );
    }
}

public void processMessageReceived( ChannelHandlerContext ctx, RemotingCommand msg ) throws Exception
{
    final RemotingCommand cmd = msg;
    if ( cmd != null )
    {
        switch ( cmd.getType() )
        {
        case REQUEST_COMMAND:
            processRequestCommand( ctx, cmd );
            break;
        case RESPONSE_COMMAND:
            processResponseCommand( ctx, cmd );
            break;
        default:
            break;
        }
    }
}

在这里,请求可以分为两类,一类是处理别的服务发来的请求;另外一类是处理自己发给别的服务的请求的处理结果。所有的请求其实都是异步的,只是将请求相关的 ResponseFuture记在一个 ConcurrentHashMap 中,map 的 key 为与请求相关的一个整数。

protected final ConcurrentHashMap<Integer /* opaque */, ResponseFuture> responseTable =
        new ConcurrentHashMap<Integer, ResponseFuture>(256);

另外需要注意的时,对不同类型的请求(由 RemotingCommand 的 code 字段标识),会提前注册对应的 NettyRequestProcessor 以及 ExecutorService,对请求的处理将放在注册好的线程池中进行:

 public void processRequestCommand(final ChannelHandlerContext ctx, final RemotingCommand cmd) {
        final Pair<NettyRequestProcessor, ExecutorService> matched = this.processorTable.get(cmd.getCode());
        final Pair<NettyRequestProcessor, ExecutorService> pair = null == matched ? this.defaultRequestProcessor : matched;
        final int opaque = cmd.getOpaque();
        ...
}

对于 ResponseRequest 的处理则较为简单,只是将其从 responseTable 中删掉,然后再调用 ResponseFuture 的 putResponse 方法设置返回结果,或是调用 responseFuture 中预设的回掉方法。

  public void processResponseCommand(ChannelHandlerContext ctx, RemotingCommand cmd) {
        final int opaque = cmd.getOpaque();
        final ResponseFuture responseFuture = responseTable.get(opaque);
        if (responseFuture != null) {
            responseFuture.setResponseCommand(cmd);

            responseFuture.release();

            responseTable.remove(opaque);

            if (responseFuture.getInvokeCallback() != null) {
                executeInvokeCallback(responseFuture);
            } else {
                responseFuture.putResponse(cmd);
            }
        } else {
            PLOG.warn("receive response, but not matched any request, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
            PLOG.warn(cmd.toString());
        }
    }

向其他服务发起请求

请求有三种,分别是异步请求、同步请求以及单向请求,分别调用了 NettyRemotingAbstract 的对应方法。从上文的分析我们可以看到,异步请求其实是使用 opaque 字段标识了一次请求,然后生成一个占位符 ResponseFuture 并存储起来。接收方在处理完请求后,发送一个相同 opaque 值的回应请求,从而通过 opaque 找到对应的 ResponseFuture,返回结果或是运行预设的回调函数。同步请求其实也是一个异步请求,只不过通过 CountdownLatch 使调用者发生阻塞。单向请求最简单,只发送,不关注请求结果。

下面以 invokeAsync 为例分析整个过程:

//调用 NettyRemotingAbstract 的 invokeAsyncImpl 方法
@Override
public void invokeAsync(Channel channel, RemotingCommand request, long timeoutMillis, InvokeCallback invokeCallback)
            throws InterruptedException, RemotingTooMuchRequestException, RemotingTimeoutException, RemotingSendRequestException {
        this.invokeAsyncImpl(channel, request, timeoutMillis, invokeCallback);
}

public void invokeAsyncImpl( final Channel channel, final RemotingCommand request, final long timeoutMillis,
             final InvokeCallback invokeCallback )
throws InterruptedException, RemotingTooMuchRequestException, RemotingTimeoutException, RemotingSendRequestException
{
    final int   opaque      = request.getOpaque();
    boolean     acquired    = this.semaphoreAsync.tryAcquire( timeoutMillis, TimeUnit.MILLISECONDS );
    if ( acquired )
    {
        final SemaphoreReleaseOnlyOnce once = new SemaphoreReleaseOnlyOnce( this.semaphoreAsync );

        final ResponseFuture responseFuture = new ResponseFuture( opaque, timeoutMillis, invokeCallback, once );
        this.responseTable.put( opaque, responseFuture );
        try {
            channel.writeAndFlush( request ).addListener( new ChannelFutureListener()
                                 {
                                     @Override
                                     public void operationComplete( ChannelFuture f ) throws Exception {
                                         if ( f.isSuccess() )
                                         {
                                             responseFuture.setSendRequestOK( true );
                                             return;
                                         } else {
                                             responseFuture.setSendRequestOK( false );
                                         }

                                         responseFuture.putResponse( null );
                                         responseTable.remove( opaque );
                                         try {
                                             executeInvokeCallback( responseFuture );
                                         } catch ( Throwable e ) {
                                             PLOG.warn( "excute callback in writeAndFlush addListener, and callback throw", e );
                                         } finally {
                                             responseFuture.release();
                                         }

                                         PLOG.warn( "send a request command to channel <{}> failed.", RemotingHelper.parseChannelRemoteAddr( channel ) );
                                          }
                                      } );
        } catch ( Exception e ) {
            responseFuture.release();
            PLOG.warn( "send a request command to channel <" + RemotingHelper.parseChannelRemoteAddr( channel ) + "> Exception", e );
            throw new RemotingSendRequestException( RemotingHelper.parseChannelRemoteAddr( channel ), e );
        }
    } else {
        String info =
            String.format( "invokeAsyncImpl tryAcquire semaphore timeout, %dms, waiting thread nums: %d semaphoreAsyncValue: %d",   /*  */
                       timeoutMillis,                                                                                           /*  */
                       this.semaphoreAsync.getQueueLength(),                                                                    /*  */
                       this.semaphoreAsync.availablePermits()                                                                   /*  */
                       );
        PLOG.warn( info );
        throw new RemotingTooMuchRequestException( info );
    }
}

在请求时如果失败成功则直接返回,如果失败则从 responseTable 删除本次请求,并调用 responseFuture.putResponse( null ),然后执行失败回调 executeInvokeCallback( responseFuture )。而后,就是等待对方发来的 Response Request 了,上文已经有过分析,这里不再赘述。

下面,看看同步消息。在发送请求后,调用了 ResponseFuture 的 waitResponse 方法。这个方法调用了 CountDownLatch 的 await 方法。请求处理成功或失败后则会调用 ResponseFuture 的 putResponse 方法,设置处理结果并打开 CountDownLatch,从而实现了同步调用。

   public RemotingCommand invokeSyncImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis)
        throws InterruptedException, RemotingSendRequestException, RemotingTimeoutException {
        final int opaque = request.getOpaque();

        try {
            final ResponseFuture responseFuture = new ResponseFuture(opaque, timeoutMillis, null, null);
            this.responseTable.put(opaque, responseFuture);
            final SocketAddress addr = channel.remoteAddress();
            channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture f) throws Exception {
                    if (f.isSuccess()) {
                        responseFuture.setSendRequestOK(true);
                        return;
                    } else {
                        responseFuture.setSendRequestOK(false);
                    }

                    responseTable.remove(opaque);
                    responseFuture.setCause(f.cause());
                    responseFuture.putResponse(null);
                    PLOG.warn("send a request command to channel <" + addr + "> failed.");
                }
            });

            RemotingCommand responseCommand = responseFuture.waitResponse(timeoutMillis);
            if (null == responseCommand) {
                if (responseFuture.isSendRequestOK()) {
                    throw new RemotingTimeoutException(RemotingHelper.parseSocketAddressAddr(addr), timeoutMillis,
                        responseFuture.getCause());
                } else {
                    throw new RemotingSendRequestException(RemotingHelper.parseSocketAddressAddr(addr), responseFuture.getCause());
                }
            }

            return responseCommand;
        } finally {
            this.responseTable.remove(opaque);
        }
    }

   public RemotingCommand waitResponse(final long timeoutMillis) throws InterruptedException {
        this.countDownLatch.await(timeoutMillis, TimeUnit.MILLISECONDS);
        return this.responseCommand;
   }

   public void putResponse(final RemotingCommand responseCommand) {
        this.responseCommand = responseCommand;
        this.countDownLatch.countDown();
   }

NettyRemotingClient 的思路与 NettyRemotingServer 类似,这里不再进行分析。

以上。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,961评论 5 473
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,444评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,009评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,082评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,101评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,271评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,738评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,395评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,539评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,434评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,481评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,160评论 3 317
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,749评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,816评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,038评论 1 256
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,548评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,140评论 2 341

推荐阅读更多精彩内容

  • Channel 与 ChannelPipeline在Netty中每个Channel都有且仅有一个ChannelPi...
    水欣阅读 2,145评论 0 1
  • 作者: 一字马胡 转载标志 【2017-11-03】 更新日志 ChannelHandler Netty线程模型...
    一字马胡阅读 12,904评论 1 24
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,569评论 18 139
  • 2011年9月30日 你后来跟我说,那次换座位是你故意的,坐到我后边是想吸引我的注意,而我居然傻傻的以为,我们的后...
    99709a08cec2阅读 161评论 0 0
  • 从昨天开始,参加省内阅卷工作,见到了当年的老师,当年的师兄师姐,回到文学院。心情特别轻快,很舒服,我喜欢大学的...
    心如美玉阅读 189评论 0 0