rocketmq icon indicating copy to clipboard operation
rocketmq copied to clipboard

Retry doesn't work when sending msg in async mode(eg. when broker is down)

Open xiangwangcheng opened this issue 7 years ago • 16 comments

BUG REPORT

  1. Please describe the issue you observed: When sending messages in async mode, the retry times is set to be 1. The code is below:

image

And if the chosen broker is down at that moment, messages sending will fail with no retry.

  • What did you do (The steps to reproduce)?
  1. Send messages in async mode.
  2. Pause the code after choosing a queue(Before really sending it)
  3. shutdown the chosen broker
  4. continue the code
  • What did you expect to see? The producer will retry another broker/queue.
  • What did you see instead? The producer throw exception without retrying.
  1. Please tell us about your environment: RMQ 4.6.0
  2. Other information (e.g. detailed explanation, logs, related issues, suggestions how to fix, etc):

xiangwangcheng avatar Nov 12 '19 07:11 xiangwangcheng

Though we have the retry strategy at the callback part, a retry logic is still needed at the sending step in case of the situation of the broker-down.

xiangwangcheng avatar Nov 12 '19 07:11 xiangwangcheng

你代码在点点进去就看到另一个控制属性了 @xiangwangcheng

CodeIngL avatar Nov 14 '19 09:11 CodeIngL

你代码在点点进去就看到另一个控制属性了 @xiangwangcheng

Could you please show me where?

xiangwangcheng avatar Nov 15 '19 01:11 xiangwangcheng

Message retry resynchronization in RocketMQ is implemented by calling the sendKeynelImpl() method in a for loop in DefaultMQProducerImpl#sendDefaultImpl(). The number of retries is obtained by DefaultMQProducer#getRetryTimesWhenSendFailed(). Asynchronous message sending retry is implemented by MQClientAPIImpl#onExceptionImpl() recursively calling MQClientAPIImpl#sendMessageAsync() method in case of exception. The number of retries is obtained by DefaultMQProducerImpl#.getRetryTimesWhenSendAsyncFailed(). The default value is 2. Therefore, if you do not set the number of asynchronous retries, you will retry at least 2 times, and the message will not be sent because there is no retry. Only the location of the asynchronous and synchronous send retry implementations is different, resulting in such a judgment.

在 RocketMQ 中同步的消息发送重试是通过DefaultMQProducerImpl#sendDefaultImpl()中的 for 循环,循环调用sendKeynelImpl()方法实现的。重试次数由DefaultMQProducer#getRetryTimesWhenSendFailed()获得。 而异步的消息发送重试是在出现异常的情况下,由MQClientAPIImpl#onExceptionImpl()递归调用MQClientAPIImpl#sendMessageAsync()方法实现的。重试次数由DefaultMQProducerImpl#.getRetryTimesWhenSendAsyncFailed()获得,缺省值为 2。 所以在不设置异步重试次数的情况下,也会至少重试 2 次,不会因为没有重试而导致消息发送失败。只是异步和同步发送重试实现的位置不一样,导致产生了这样判断。

MountainOne avatar Nov 16 '19 13:11 MountainOne

@MountainOne Yeah, I totally know what are u talking about. But onExceptionImpl is invoked inside the callback. That happens when MQClientAPIImpl#sendMessageAsync() success and something is wrong after that. But what about the broker is already down when MQClientAPIImpl#sendMessageAsync() is invoked ? I haven't found any retry logic and I already reproduce this situation.

xiangwangcheng avatar Nov 18 '19 07:11 xiangwangcheng

@MountainOne I don't know if I have made myself clear or not.... it's a little bit complicated. @duhengforever @zongtanghu What do u guys think?

xiangwangcheng avatar Nov 18 '19 07:11 xiangwangcheng

@xiangwangcheng producer won't retry if there's a response returned, image and will retry when the response is null.

duhenglucky avatar Nov 18 '19 09:11 duhenglucky

@xiangwangcheng I fully understand what you mean. But the callback function passed to remotingClient.invokeAsync will always be called, not only when the message is sent successfully. The callback function that you said is only called when the message is sent successfully should be the parameter sendCallback passed to MQClientAPIImpl#sendMessageAsync. When the broker has gone down, the message sent will not be returned. producer will compare with the timeoutMillis according to the time the message has been sent. If it is greater than that, the message will be sent timeout. Set the send status of responseFuture to false and execute the InvokeCallback callback function passed in remotingClient.invokeAsync(). Regardless of whether the message was sent successfully or not, InvokerCallback() will be executed. Then, based on the returned response, if response returns and no exception occurs, it will not be retried; if it is null or an exception occurs, it will be retried. The specific async communication logic can refer to NettyRemotingClient#invokeAsync and NettyRemotingAbstract#invokeAsyncImpl in the remoting module. Here is a list of key codes.

我完全明白您的意思。 但传给remotingClient.invokeAsync的回调函数总是会被调用的,不仅仅是在消息发送成功的时候。您所说的在只有在消息发送成功时才调用的回调函数应该是传给MQClientAPIImpl#sendMessageAsync的参数sendCallback。 当 broker 已经宕机时,发送的消息将无法返回。producer会根据消息已经发出的时间与timeoutMillis比较,大于则判断消息发送超时。将responseFuture的发送状态设置为 false 并执行remotingClient.invokeAsync()中传入的InvokeCallback回调函数。不管消息发送成功与否,InvokerCallback()都会执行。然后根据返回的 response 判断,如果 response 返回了且没有出现异常,就不会重试;如果为 null 或者出现异常,就会进行重试。 具体的异步通信逻辑可以参考 remoting 模块中的NettyRemotingClient#invokeAsync和NettyRemotingAbstract#invokeAsyncImpl。这里列一下关键代码,在NettyRemotingAbstract#invokeAsyncImpl中。

channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
    @Override
    public void operationComplete(ChannelFuture f) throws Exception {
        if (f.isSuccess()) {
            responseFuture.setSendRequestOK(true);
            return;
        }
        requestFail(opaque);
        log.warn("send a request command to channel <{}> failed.", RemotingHelper.parseChannelRemoteAddr(channel));
    }
});

MountainOne avatar Nov 18 '19 13:11 MountainOne

@duhenglucky @MountainOne Sorry, seems I still haven't made myself clear. I mean until one message queue is selected, everything is fine. image

And after that(I set a breakpoint here), I shut down the broker that the selected queue is running on. The producer fails here(channel is null and this.invokeAsyncImpl won't be invoked): image

And InvokeCallback won't be invoked. image And no retry logic for situations like that.

xiangwangcheng avatar Nov 19 '19 08:11 xiangwangcheng

I understand now, thank you for your tips. In the case of a broker downtime, NettyRemotingAbstract#invokeAsyncImpl will not be executed and the retry logic will not be executed. So we need to execute the retry logic before throwing the exception, or the message will be lost. Maybe we have to make sure that InvokeCallback() is executed.

MountainOne avatar Nov 19 '19 12:11 MountainOne

I think reuse the for-loop retry logic of sending messages in sync mode is also OK. image int timesTotal = communicationMode != CommunicationMode.ONEWAY ? 1 + this.defaultMQProducer.getRetryTimesWhenSendFailed() : 1;

xiangwangcheng avatar Nov 20 '19 09:11 xiangwangcheng

If you modify it as you said, after the message is sent asynchronously, regardless of the result , the message will be resent in the for loop immediately, causing the message to be sent repeatedly. If the blocking waits during the sending process, it does not reflect the advantage of asynchronous sending, which affects performance. So I think the retry logic for asynchronous sent should still be in the callback function. You can consider canceling if (channel != null && channel.isActive()) and passing the logic that handle the exception to the callback function (which is actually done, such as onExceptionImpl()), so that every asynchronous call will be Triggered the callback function. But this solution is not elegant enough to directly modify the function of the remoting module, I am wondering if there is a better solution.

MountainOne avatar Nov 20 '19 13:11 MountainOne

No, if no exception is thrown during the sending process, the message will not be sent again.

switch (communicationMode) {
                            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;

xiangwangcheng avatar Nov 21 '19 01:11 xiangwangcheng

You are right, I am not thinking about it. This is a viable solution.

MountainOne avatar Nov 21 '19 02:11 MountainOne

同步发送,当broker无响应时,照样不会重试,可以参考分析。

gaoyf avatar Jan 09 '20 09:01 gaoyf

@xiangwangcheng +1, I think you are right.

RongtongJin avatar Feb 12 '20 03:02 RongtongJin