继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

分布式消息队列Apache RocketMQ源码剖析-Producer分析

慕神8447489
关注TA
已关注
手记 1273
粉丝 174
获赞 956

正文

首先我们看一下Producer的继承结构:


webp

image.png

MQAdmin主要包含一些管理性的接口,比如创建topic、查询某个特定消息以方便排查问题,ClientConfig主要定义了一些基本的配置,比如持久化consumer端消费offset的间隔时间(offset就是consumer端当前消费到的位置,offset的持久化机制也决定了是exactly once 还是根据时间戳等消费),然后再来看DefaultMQProducer,我们发现它将具体的实现都代理给了DefaultMQProducerImpl去做,这个类主要包含了不同的send方法,同步、异步、oneway(发出去就不管了,比如可以用于日志同步)。

source-java
    SendResult send(final Message msg) throws MQClientException, RemotingException, MQBrokerException,
        InterruptedException;    SendResult send(final Message msg, final long timeout) throws MQClientException,
        RemotingException, MQBrokerException, InterruptedException;    void send(final Message msg, final SendCallback sendCallback) throws MQClientException,
        RemotingException, InterruptedException;    void send(final Message msg, final SendCallback sendCallback, final long timeout)
        throws MQClientException, RemotingException, InterruptedException;    void sendOneway(final Message msg) throws MQClientException, RemotingException,
        InterruptedException;

通过这几个接口我们也能了解到其功能,下面来分析一下其具体实现。首先我们看一下它的主要字段以及其含义:

source-java    //高并发下推荐替换为TheradLocalRandom,Random在高并发下会有竞争造成更高的开销
    private final Random random = new Random();    private final DefaultMQProducer defaultMQProducer;    //记录topic的路由信息
    private final ConcurrentMap<String/* topic */, TopicPublishInfo> topicPublishInfoTable =        new ConcurrentHashMap<String, TopicPublishInfo>();    private final ArrayList<SendMessageHook> sendMessageHookList = new ArrayList<SendMessageHook>();    private final RPCHook rpcHook;    protected BlockingQueue<Runnable> checkRequestQueue;    protected ExecutorService checkExecutor;    //Producer当前的状态
    private ServiceState serviceState = ServiceState.CREATE_JUST;    private MQClientInstance mQClientFactory;    private ArrayList<CheckForbiddenHook> checkForbiddenHookList = new ArrayList<CheckForbiddenHook>();    //压缩等级
    private int zipCompressLevel = Integer.parseInt(System.getProperty(MixAll.MESSAGE_COMPRESS_LEVEL, "5"));

至于Producer的启动流程,可以参考之前的博客,这里主要来看一下核心的发送流程:

source-java    public SendResult send(
        Message msg) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {        return send(msg, this.defaultMQProducer.getSendMsgTimeout());
    }    public SendResult send(Message msg,        long timeout) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {        //同步、异步、oneway默认都会调用sendDefaultImpl这个方法
        return this.sendDefaultImpl(msg, CommunicationMode.SYNC, null, timeout);
    }    private SendResult sendDefaultImpl(
        Message msg, //消息体        final CommunicationMode communicationMode,/发送方式:同步/异步/Oneway        final SendCallback sendCallback,//回调        final long timeout//超时时间
    ) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {        this.makeSureStateOK();        //校验消息,比如topic名字不能超过256字节,body不能大于设置的最大值等
        Validators.checkMessage(msg, this.defaultMQProducer);        //Random在高并发下会有竞争造成更高的开销,可替换成ThreadLocalRandom
        final long invokeID = random.nextLong();        long beginTimestampFirst = System.currentTimeMillis();        long beginTimestampPrev = beginTimestampFirst;        long endTimestamp = beginTimestampFirst;        /*
            拉取topic路由信息
         */
        TopicPublishInfo topicPublishInfo = this.tryToFindTopicPublishInfo(msg.getTopic());        if (topicPublishInfo != null && topicPublishInfo.ok()) {
            MessageQueue mq = null;
            Exception exception = null;
            SendResult sendResult = null;            /*
                同步模式下可能会造成消息重发
             */
            int timesTotal = communicationMode == CommunicationMode.SYNC ? 1 + this.defaultMQProducer.getRetryTimesWhenSendFailed() : 1;            int times = 0;            //同步模式下会默认有两次重试
            String[] brokersSent = new String[timesTotal];            for (; times < timesTotal; times++) {                /*
                    记录上一个发送失败的broker,重试时排除掉这个有问题的broker
                     但是这里只记录了上一个,第三次重试还是会有机会轮询到第一个
                 */
                String lastBrokerName = null == mq ? null : mq.getBrokerName();                 //选择一个要发送到topic队列,默认是轮询的方式,从而达到负载均衡
                MessageQueue tmpmq = this.selectOneMessageQueue(topicPublishInfo, lastBrokerName);                if (tmpmq != null) {
                    mq = tmpmq;
                    brokersSent[times] = mq.getBrokerName();                    try {
                        beginTimestampPrev = System.currentTimeMillis();                        //核心的发送方法是
                        sendResult = this.sendKernelImpl(msg, mq, communicationMode, sendCallback, topicPublishInfo, timeout);
                        endTimestamp = System.currentTimeMillis();                        this.updateFaultItem(mq.getBrokerName(), endTimestamp - beginTimestampPrev, false);                        switch (communicationMode) {                            //根据不用的发送方式,进行结果的处理,异步的直接回调,返回值可以为null
                            case ASYNC:                                return null;                            case ONEWAY:                                return null;                            case SYNC:                                if (sendResult.getSendStatus() != SendStatus.SEND_OK) {                                    if (this.defaultMQProducer.isRetryAnotherBrokerWhenNotStoreOK()) {                                        continue;
                                    }
                                }                                return sendResult;                            default:                                break;
                        }
                    } catch (RemotingException e) {
                        endTimestamp = System.currentTimeMillis();                        this.updateFaultItem(mq.getBrokerName(), endTimestamp - beginTimestampPrev, true);
                        log.warn(String.format("sendKernelImpl exception, resend at once, InvokeID: %s, RT: %sms, Broker: %s", invokeID, endTimestamp - beginTimestampPrev, mq), e);
                        log.warn(msg.toString());
                        exception = e;                        continue;
                    } catch (MQClientException e) {
                        endTimestamp = System.currentTimeMillis();                        this.updateFaultItem(mq.getBrokerName(), endTimestamp - beginTimestampPrev, true);
                        log.warn(String.format("sendKernelImpl exception, resend at once, InvokeID: %s, RT: %sms, Broker: %s", invokeID, endTimestamp - beginTimestampPrev, mq), e);
                        log.warn(msg.toString());
                        exception = e;                        continue;
                    } catch (MQBrokerException e) {
                        endTimestamp = System.currentTimeMillis();                        this.updateFaultItem(mq.getBrokerName(), endTimestamp - beginTimestampPrev, true);
                        log.warn(String.format("sendKernelImpl exception, resend at once, InvokeID: %s, RT: %sms, Broker: %s", invokeID, endTimestamp - beginTimestampPrev, mq), e);
                        log.warn(msg.toString());
                        exception = e;                        switch (e.getResponseCode()) {                            case ResponseCode.TOPIC_NOT_EXIST:                            case ResponseCode.SERVICE_NOT_AVAILABLE:                            case ResponseCode.SYSTEM_ERROR:                            case ResponseCode.NO_PERMISSION:                            case ResponseCode.NO_BUYER_ID:                            case ResponseCode.NOT_IN_CURRENT_UNIT:                                continue;                            default:                                if (sendResult != null) {                                    return sendResult;
                                }                                throw e;
                        }
                    } catch (InterruptedException e) {
                        endTimestamp = System.currentTimeMillis();                        this.updateFaultItem(mq.getBrokerName(), endTimestamp - beginTimestampPrev, false);
                        log.warn(String.format("sendKernelImpl exception, throw exception, InvokeID: %s, RT: %sms, Broker: %s", invokeID, endTimestamp - beginTimestampPrev, mq), e);
                        log.warn(msg.toString());

                        log.warn("sendKernelImpl exception", e);
                        log.warn(msg.toString());                        throw e;
                    }
                } else {                    break;
                }
            }            if (sendResult != null) {                return sendResult;
            }

            String info = String.format("Send [%d] times, still failed, cost [%d]ms, Topic: %s, BrokersSent: %s",
                times,
                System.currentTimeMillis() - beginTimestampFirst,
                msg.getTopic(),
                Arrays.toString(brokersSent));            //有些异常会打出github相关的讨论issue,不过alibaba group下的issue已经删除了,
           //这个逻辑可以干掉
            info += FAQUrl.suggestTodo(FAQUrl.SEND_MSG_FAILED);

            MQClientException mqClientException = new MQClientException(info, exception);            //异常处理,有点繁琐
            if (exception instanceof MQBrokerException) {
                mqClientException.setResponseCode(((MQBrokerException) exception).getResponseCode());
            } else if (exception instanceof RemotingConnectException) {
                mqClientException.setResponseCode(ClientErrorCode.CONNECT_BROKER_EXCEPTION);
            } else if (exception instanceof RemotingTimeoutException) {
                mqClientException.setResponseCode(ClientErrorCode.ACCESS_BROKER_TIMEOUT);
            } else if (exception instanceof MQClientException) {
                mqClientException.setResponseCode(ClientErrorCode.BROKER_NOT_EXIST_EXCEPTION);
            }            throw mqClientException;
        }

        List<String> nsList = this.getmQClientFactory().getMQClientAPIImpl().getNameServerAddressList();        if (null == nsList || nsList.isEmpty()) {            throw new MQClientException(                "No name server address, please set it." + FAQUrl.suggestTodo(FAQUrl.NAME_SERVER_ADDR_NOT_EXIST_URL), null).setResponseCode(ClientErrorCode.NO_NAME_SERVER_EXCEPTION);
        }        throw new MQClientException("No route info of this topic, " + msg.getTopic() + FAQUrl.suggestTodo(FAQUrl.NO_TOPIC_ROUTE_INFO),            null).setResponseCode(ClientErrorCode.NOT_FOUND_TOPIC_EXCEPTION);
    }    private SendResult sendKernelImpl(final Message msg,        final MessageQueue mq,        final CommunicationMode communicationMode,        final SendCallback sendCallback,        final TopicPublishInfo topicPublishInfo,        final long timeout) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
        String brokerAddr = this.mQClientFactory.findBrokerAddressInPublish(mq.getBrokerName());        if (null == brokerAddr) {            /*
                找不到则尝试去抓取,因此第一次发送消息可能会有比较大延时,可以考虑再应用启动时预加载路由信息
             */
            tryToFindTopicPublishInfo(mq.getTopic());
            brokerAddr = this.mQClientFactory.findBrokerAddressInPublish(mq.getBrokerName());
        }

        SendMessageContext context = null;        if (brokerAddr != null) {//获取broker地址
            brokerAddr = MixAll.brokerVIPChannel(this.defaultMQProducer.isSendMessageWithVIPChannel(), brokerAddr);            byte[] prevBody = msg.getBody();            try {                //for MessageBatch,ID has been set in the generating process
                if (!(msg instanceof MessageBatch)) {
                    MessageClientIDSetter.setUniqID(msg);
                }                int sysFlag = 0;                if (this.tryToCompressMessage(msg)) {
                    sysFlag |= MessageSysFlag.COMPRESSED_FLAG;
                }                final String tranMsg = msg.getProperty(MessageConst.PROPERTY_TRANSACTION_PREPARED);                if (tranMsg != null && Boolean.parseBoolean(tranMsg)) {
                    sysFlag |= MessageSysFlag.TRANSACTION_PREPARED_TYPE;
                }                if (hasCheckForbiddenHook()) {
                    CheckForbiddenContext checkForbiddenContext = new CheckForbiddenContext();
                    checkForbiddenContext.setNameSrvAddr(this.defaultMQProducer.getNamesrvAddr());
                    checkForbiddenContext.setGroup(this.defaultMQProducer.getProducerGroup());
                    checkForbiddenContext.setCommunicationMode(communicationMode);
                    checkForbiddenContext.setBrokerAddr(brokerAddr);
                    checkForbiddenContext.setMessage(msg);
                    checkForbiddenContext.setMq(mq);
                    checkForbiddenContext.setUnitMode(this.isUnitMode());                    this.executeCheckForbiddenHook(checkForbiddenContext);
                }                if (this.hasSendMessageHook()) {
                    context = new SendMessageContext();
                    context.setProducer(this);
                    context.setProducerGroup(this.defaultMQProducer.getProducerGroup());
                    context.setCommunicationMode(communicationMode);
                    context.setBornHost(this.defaultMQProducer.getClientIP());
                    context.setBrokerAddr(brokerAddr);
                    context.setMessage(msg);
                    context.setMq(mq);
                    String isTrans = msg.getProperty(MessageConst.PROPERTY_TRANSACTION_PREPARED);                    if (isTrans != null && isTrans.equals("true")) {
                        context.setMsgType(MessageType.Trans_Msg_Half);
                    }                    if (msg.getProperty("__STARTDELIVERTIME") != null || msg.getProperty(MessageConst.PROPERTY_DELAY_TIME_LEVEL) != null) {
                        context.setMsgType(MessageType.Delay_Msg);
                    }                    this.executeSendMessageHookBefore(context);
                }                /*
                    构建消息头
                 */
                SendMessageRequestHeader requestHeader = new SendMessageRequestHeader();                /*
                    producerGroup在非事物模式下木有啥用
                 */
                requestHeader.setProducerGroup(this.defaultMQProducer.getProducerGroup());
                requestHeader.setTopic(msg.getTopic());
                requestHeader.setDefaultTopic(this.defaultMQProducer.getCreateTopicKey());                /*
                    默认每个topic分配4个queue
                 */
                requestHeader.setDefaultTopicQueueNums(this.defaultMQProducer.getDefaultTopicQueueNums());                /*
                    写到哪个queue
                 */
                requestHeader.setQueueId(mq.getQueueId());
                requestHeader.setSysFlag(sysFlag);
                requestHeader.setBornTimestamp(System.currentTimeMillis());
                requestHeader.setFlag(msg.getFlag());
                requestHeader.setProperties(MessageDecoder.messageProperties2String(msg.getProperties()));
                requestHeader.setReconsumeTimes(0);
                requestHeader.setUnitMode(this.isUnitMode());
                requestHeader.setBatch(msg instanceof MessageBatch);                /*
                    重试消息
                 */
                if (requestHeader.getTopic().startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
                    String reconsumeTimes = MessageAccessor.getReconsumeTime(msg);                    if (reconsumeTimes != null) {
                        requestHeader.setReconsumeTimes(Integer.valueOf(reconsumeTimes));
                        MessageAccessor.clearProperty(msg, MessageConst.PROPERTY_RECONSUME_TIME);
                    }

                    String maxReconsumeTimes = MessageAccessor.getMaxReconsumeTimes(msg);                    if (maxReconsumeTimes != null) {
                        requestHeader.setMaxReconsumeTimes(Integer.valueOf(maxReconsumeTimes));
                        MessageAccessor.clearProperty(msg, MessageConst.PROPERTY_MAX_RECONSUME_TIMES);
                    }
                }

                SendResult sendResult = null;                switch (communicationMode) {                    case ASYNC:
                        sendResult = this.mQClientFactory.getMQClientAPIImpl().sendMessage(
                            brokerAddr,
                            mq.getBrokerName(),
                            msg,
                            requestHeader,
                            timeout,
                            communicationMode,
                            sendCallback,
                            topicPublishInfo,                            this.mQClientFactory,                            this.defaultMQProducer.getRetryTimesWhenSendAsyncFailed(),
                            context,                            this);                        break;                    case ONEWAY:                    case SYNC:
                        sendResult = this.mQClientFactory.getMQClientAPIImpl().sendMessage(
                            brokerAddr,
                            mq.getBrokerName(),
                            msg,
                            requestHeader,
                            timeout,
                            communicationMode,
                            context,                            this);                        break;                    default:                        assert false;                        break;
                }                if (this.hasSendMessageHook()) {
                    context.setSendResult(sendResult);                    this.executeSendMessageHookAfter(context);
                }                return sendResult;
            } catch (RemotingException e) {                /*
                    这几个异常处理方式都一样,可以考虑合为一个
                 */
                if (this.hasSendMessageHook()) {
                    context.setException(e);                    this.executeSendMessageHookAfter(context);
                }                throw e;
            } catch (MQBrokerException e) {                if (this.hasSendMessageHook()) {
                    context.setException(e);                    this.executeSendMessageHookAfter(context);
                }                throw e;
            } catch (InterruptedException e) {                if (this.hasSendMessageHook()) {
                    context.setException(e);                    this.executeSendMessageHookAfter(context);
                }                throw e;
            } finally {
                msg.setBody(prevBody);
            }
        }        throw new MQClientException("The broker[" + mq.getBrokerName() + "] not exist", null);
    }    private boolean tryToCompressMessage(final Message msg) {        if (msg instanceof MessageBatch) {            //batch dose not support compressing right now
            return false;
        }        byte[] body = msg.getBody();        if (body != null) {            /*
                默认大于4K压缩,不支持其他压缩方式
             */
            if (body.length >= this.defaultMQProducer.getCompressMsgBodyOverHowmuch()) {                try {                    byte[] data = UtilAll.compress(body, zipCompressLevel);                    if (data != null) {
                        msg.setBody(data);                        return true;
                    }
                } catch (IOException e) {
                    log.error("tryToCompressMessage exception", e);
                    log.warn(msg.toString());
                }
            }
        }        return false;
    }

简单总结一下上面这坨代码:

  1. 根据topic获取其路由信息

  2. 计算重试次数

  3. 获取上次失败的broker,然后根据一个AtomicInteger的自增值,轮询topic下的所有队列,跳过上一个失败的

  4. 获取broker地址

  5. 构建SendMessageRequestHeader

  6. 发送
    这里最后一步发送会委托给MQClientAPIImpl去做,下面我们分析一下这个类的代码:

source-javapublic SendResult sendMessage(        final String addr,        final String brokerName,        final Message msg,        final SendMessageRequestHeader requestHeader,        final long timeoutMillis,        final CommunicationMode communicationMode,        final SendCallback sendCallback,        final TopicPublishInfo topicPublishInfo,        final MQClientInstance instance,        final int retryTimesWhenSendFailed,        final SendMessageContext context,        final DefaultMQProducerImpl producer
    ) throws RemotingException, MQBrokerException, InterruptedException {
        RemotingCommand request = null;        if (sendSmartMsg || msg instanceof MessageBatch) {
            SendMessageRequestHeaderV2 requestHeaderV2 = SendMessageRequestHeaderV2.createSendMessageRequestHeaderV2(requestHeader);
            request = RemotingCommand.createRequestCommand(msg instanceof MessageBatch ? RequestCode.SEND_BATCH_MESSAGE : RequestCode.SEND_MESSAGE_V2, requestHeaderV2);
        } else {            /*
                构建消息体
             */
            request = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE, requestHeader);
        }

        request.setBody(msg.getBody());        switch (communicationMode) {            case ONEWAY:                this.remotingClient.invokeOneway(addr, request, timeoutMillis);                return null;            case ASYNC:                final AtomicInteger times = new AtomicInteger();                this.sendMessageAsync(addr, brokerName, msg, timeoutMillis, request, sendCallback, topicPublishInfo, instance,
                    retryTimesWhenSendFailed, times, context, producer);                return null;            case SYNC:                return this.sendMessageSync(addr, brokerName, msg, timeoutMillis, request);            default:                assert false;                break;
        }        return null;
    }private void sendMessageAsync(        final String addr,        final String brokerName,        final Message msg,        final long timeoutMillis,        final RemotingCommand request,        final SendCallback sendCallback,        final TopicPublishInfo topicPublishInfo,        final MQClientInstance instance,        final int retryTimesWhenSendFailed,        final AtomicInteger times,        final SendMessageContext context,        final DefaultMQProducerImpl producer
    ) throws InterruptedException, RemotingException {        /*
            异步回调
         */
        this.remotingClient.invokeAsync(addr, request, timeoutMillis, new InvokeCallback() {            @Override
            public void operationComplete(ResponseFuture responseFuture) {
                RemotingCommand response = responseFuture.getResponseCommand();                if (null == sendCallback && response != null) {                    try {
                        SendResult sendResult = MQClientAPIImpl.this.processSendResponse(brokerName, msg, response);                        if (context != null && sendResult != null) {
                            context.setSendResult(sendResult);
                            context.getProducer().executeSendMessageHookAfter(context);
                        }
                    } catch (Throwable e) {
                    }

                    producer.updateFaultItem(brokerName, System.currentTimeMillis() - responseFuture.getBeginTimestamp(), false);                    return;
                }                if (response != null) {                    try {
                        SendResult sendResult = MQClientAPIImpl.this.processSendResponse(brokerName, msg, response);                        assert sendResult != null;                        if (context != null) {
                            context.setSendResult(sendResult);
                            context.getProducer().executeSendMessageHookAfter(context);
                        }                        try {
                            sendCallback.onSuccess(sendResult);
                        } catch (Throwable e) {
                        }

                        producer.updateFaultItem(brokerName, System.currentTimeMillis() - responseFuture.getBeginTimestamp(), false);
                    } catch (Exception e) {
                        producer.updateFaultItem(brokerName, System.currentTimeMillis() - responseFuture.getBeginTimestamp(), true);
                        onExceptionImpl(brokerName, msg, 0L, request, sendCallback, topicPublishInfo, instance,
                            retryTimesWhenSendFailed, times, e, context, false, producer);
                    }
                } else {
                    producer.updateFaultItem(brokerName, System.currentTimeMillis() - responseFuture.getBeginTimestamp(), true);                    if (!responseFuture.isSendRequestOK()) {
                        MQClientException ex = new MQClientException("send request failed", responseFuture.getCause());
                        onExceptionImpl(brokerName, msg, 0L, request, sendCallback, topicPublishInfo, instance,
                            retryTimesWhenSendFailed, times, ex, context, true, producer);
                    } else if (responseFuture.isTimeout()) {
                        MQClientException ex = new MQClientException("wait response timeout " + responseFuture.getTimeoutMillis() + "ms",
                            responseFuture.getCause());
                        onExceptionImpl(brokerName, msg, 0L, request, sendCallback, topicPublishInfo, instance,
                            retryTimesWhenSendFailed, times, ex, context, true, producer);
                    } else {
                        MQClientException ex = new MQClientException("unknow reseaon", responseFuture.getCause());
                        onExceptionImpl(brokerName, msg, 0L, request, sendCallback, topicPublishInfo, instance,
                            retryTimesWhenSendFailed, times, ex, context, true, producer);
                    }
                }
            }
        });
    }

这里又会去调用remotingClient,这里主要是网络IO相关的逻辑,RocketMQ采用了Netty去实现:

source-java@Override
    public void invokeAsync(String addr, RemotingCommand request, long timeoutMillis, InvokeCallback invokeCallback)
        throws InterruptedException, RemotingConnectException, RemotingTooMuchRequestException, RemotingTimeoutException,
        RemotingSendRequestException {        /*
            向broker建立连接
         */
        final Channel channel = this.getAndCreateChannel(addr);        if (channel != null && channel.isActive()) {            try {                if (this.rpcHook != null) {                    this.rpcHook.doBeforeRequest(addr, request);
                }                this.invokeAsyncImpl(channel, request, timeoutMillis, invokeCallback);
            } catch (RemotingSendRequestException e) {
                log.warn("invokeAsync: send request exception, so close the channel[{}]", addr);                this.closeChannel(addr, channel);                throw e;
            }
        } else {            this.closeChannel(addr, channel);            throw new RemotingConnectException(addr);
        }
    }private Channel getAndCreateChannel(final String addr) throws InterruptedException {        if (null == addr)            /*
                如果地址为空,则去抓取
             */
            return getAndCreateNameserverChannel();

        ChannelWrapper cw = this.channelTables.get(addr);        /*
            连接正常直接返回
         */
        if (cw != null && cw.isOK()) {            return cw.getChannel();
        }        return this.createChannel(addr);
    } private Channel createChannel(final String addr) throws InterruptedException {
        ChannelWrapper cw = this.channelTables.get(addr);        if (cw != null && cw.isOK()) {            return cw.getChannel();
        }        /*
            加锁保证互斥性
         */
        if (this.lockChannelTables.tryLock(LOCK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) {            try {                boolean createNewConnection = false;
                cw = this.channelTables.get(addr);                if (cw != null) {                    if (cw.isOK()) {                        return cw.getChannel();
                    } else if (!cw.getChannelFuture().isDone()) {
                        createNewConnection = false;
                    } else {                        this.channelTables.remove(addr);
                        createNewConnection = true;
                    }
                } else {
                    createNewConnection = true;
                }                if (createNewConnection) {                    /*
                        建立连接
                     */
                    ChannelFuture channelFuture = this.bootstrap.connect(RemotingHelper.string2SocketAddress(addr));
                    log.info("createChannel: begin to connect remote host[{}] asynchronously", addr);
                    cw = new ChannelWrapper(channelFuture);                    this.channelTables.put(addr, cw);
                }
            } catch (Exception e) {
                log.error("createChannel: create channel exception", e);
            } finally {                this.lockChannelTables.unlock();
            }
        } else {
            log.warn("createChannel: try to lock channel table, but timeout, {}ms", LOCK_TIMEOUT_MILLIS);
        }        if (cw != null) {
            ChannelFuture channelFuture = cw.getChannelFuture();            if (channelFuture.awaitUninterruptibly(this.nettyClientConfig.getConnectTimeoutMillis())) {                if (cw.isOK()) {
                    log.info("createChannel: connect remote host[{}] success, {}", addr, channelFuture.toString());                    return cw.getChannel();
                } else {                    /*
                        连接失败,打印日志
                     */
                    log.warn("createChannel: connect remote host[" + addr + "] failed, " + channelFuture.toString(), channelFuture.cause());
                }
            } else {                /*
                    超时时间内未建立连接成功
                 */
                log.warn("createChannel: connect remote host[{}] timeout {}ms, {}", addr, this.nettyClientConfig.getConnectTimeoutMillis(),
                    channelFuture.toString());
            }
        }        return null;
    }
public abstract class NettyRemotingAbstract {  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();        /*
            流控,用于保护系统资源,semaphoreAsync相当于一个令牌,默认65535相当于没限制了
         */
        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) {
                            log.warn("excute callback in writeAndFlush addListener, and callback throw", e);
                        } finally {
                            responseFuture.release();
                        }

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

这里可以看到有几个并发的小技巧,比如RocketMQ里的ResponseFuture#executeInvokeCallback方法,通过AtomicBoolean实现方法执行exactly once语义:

source-java    public void executeInvokeCallback() {        if (invokeCallback != null) {            /*
                保证callback只能执行一次
             */
            if (this.executeCallbackOnlyOnce.compareAndSet(false, true)) {
                invokeCallback.operationComplete(this);
            }
        }
    }

另外再举一个例子,比如CountDownLatch的使用,如果指定为1,那么就可以用于实现一些并发下创建连接等操作,比如线程A创建连接,线程B发现A已经在尝试建立连接了,那么B就可以阻塞在这里,等A完成后,再继续后续操作,这里依然拿ResponseFuture举例子:

source-javapublic RemotingCommand waitResponse(final long timeoutMillis) throws InterruptedException {        /*
            等待CountDownLatch.countDown执行
         */
        this.countDownLatch.await(timeoutMillis, TimeUnit.MILLISECONDS);        return this.responseCommand;
    }    public void putResponse(final RemotingCommand responseCommand) {        this.responseCommand = responseCommand;        //表明操作已经完成,唤醒await的线程
        this.countDownLatch.countDown();
    }

总结

相对来说,RocketMQ发送端这里逻辑还是比较简单的,提交一条消息后就通过Netty发送到Broker,而Kafa的会更复杂一点,Kafka这里会做一个合并,客户端提交是放到一个内存队列,然后有一个Sender线程负责根据当前的状态决定是否发送消息,这里还有一个队列会存储所有的回调,当执行完成后统计执行callback。



作者:Java架构
链接:https://www.jianshu.com/p/82c4329e34f7


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP