Search in sources :

Example 1 with RemotingTimeoutException

use of org.apache.rocketmq.remoting.exception.RemotingTimeoutException in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class DefaultMQProducerImpl method sendDefaultImpl.

// 发送消息核心
private // 
SendResult sendDefaultImpl(// 
Message msg, // 
final CommunicationMode communicationMode, // 
final SendCallback sendCallback, // 
final long timeout) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    // 判断服务是否可用
    this.makeSureStateOK();
    // 消息验证
    Validators.checkMessage(msg, this.defaultMQProducer);
    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;
        // 发送模式是sync 会有3次其他1次
        int timesTotal = communicationMode == CommunicationMode.SYNC ? 1 + this.defaultMQProducer.getRetryTimesWhenSendFailed() : 1;
        int times = 0;
        String[] brokersSent = new String[timesTotal];
        for (; times < timesTotal; times++) {
            // 第一次的确是null 但是如果第二次呢? 所以这里存在的意义
            String lastBrokerName = null == mq ? null : mq.getBrokerName();
            // 选择一个queue
            MessageQueue tmpmq = this.selectOneMessageQueue(topicPublishInfo, lastBrokerName);
            if (tmpmq != null) {
                mq = tmpmq;
                brokersSent[times] = mq.getBrokerName();
                try {
                    beginTimestampPrev = System.currentTimeMillis();
                    // 调用sendKernelImpl发送消息  发送消息核心
                    sendResult = this.sendKernelImpl(msg, mq, communicationMode, sendCallback, topicPublishInfo, timeout);
                    endTimestamp = System.currentTimeMillis();
                    // 更新Broker可用信息
                    this.updateFaultItem(mq.getBrokerName(), endTimestamp - beginTimestampPrev, false);
                    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;
                    }
                } 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()) {
                        // 如下异常continue,进行发送消息重试
                        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));
        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);
}
Also used : RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) TransactionSendResult(org.apache.rocketmq.client.producer.TransactionSendResult) SendResult(org.apache.rocketmq.client.producer.SendResult) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 2 with RemotingTimeoutException

use of org.apache.rocketmq.remoting.exception.RemotingTimeoutException in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientAPIImplTest method testSendMessageOneWay_WithException.

@Test
public void testSendMessageOneWay_WithException() throws RemotingException, InterruptedException, MQBrokerException {
    doThrow(new RemotingTimeoutException("Remoting Exception in Test")).when(remotingClient).invokeOneway(anyString(), any(RemotingCommand.class), anyLong());
    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ONEWAY, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(RemotingException.class);
    } catch (RemotingException e) {
        assertThat(e).hasMessage("Remoting Exception in Test");
    }
    doThrow(new InterruptedException("Interrupted Exception in Test")).when(remotingClient).invokeOneway(anyString(), any(RemotingCommand.class), anyLong());
    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ONEWAY, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(InterruptedException.class);
    } catch (InterruptedException e) {
        assertThat(e).hasMessage("Interrupted Exception in Test");
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) Test(org.junit.Test)

Example 3 with RemotingTimeoutException

use of org.apache.rocketmq.remoting.exception.RemotingTimeoutException in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class AdminBrokerProcessor method callConsumer.

private // 
RemotingCommand callConsumer(// 
final int requestCode, // 
final RemotingCommand request, // 
final String consumerGroup, final String clientId) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    ClientChannelInfo clientChannelInfo = this.brokerController.getConsumerManager().findChannel(consumerGroup, clientId);
    if (null == clientChannelInfo) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("The Consumer <%s> <%s> not online", consumerGroup, clientId));
        return response;
    }
    if (clientChannelInfo.getVersion() < MQVersion.Version.V3_1_8_SNAPSHOT.ordinal()) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(// 
        String.format(// 
        "The Consumer <%s> Version <%s> too low to finish, please upgrade it to V3_1_8_SNAPSHOT", // 
        clientId, MQVersion.getVersionDesc(clientChannelInfo.getVersion())));
        return response;
    }
    try {
        RemotingCommand newRequest = RemotingCommand.createRequestCommand(requestCode, null);
        newRequest.setExtFields(request.getExtFields());
        newRequest.setBody(request.getBody());
        return this.brokerController.getBroker2Client().callClient(clientChannelInfo.getChannel(), newRequest);
    } catch (RemotingTimeoutException e) {
        response.setCode(ResponseCode.CONSUME_MSG_TIMEOUT);
        response.setRemark(String.format("consumer <%s> <%s> Timeout: %s", consumerGroup, clientId, RemotingHelper.exceptionSimpleDesc(e)));
        return response;
    } catch (Exception e) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("invoke consumer <%s> <%s> Exception: %s", consumerGroup, clientId, RemotingHelper.exceptionSimpleDesc(e)));
        return response;
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ClientChannelInfo(org.apache.rocketmq.broker.client.ClientChannelInfo) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException)

Example 4 with RemotingTimeoutException

use of org.apache.rocketmq.remoting.exception.RemotingTimeoutException in project rocketmq by apache.

the class NettyRemotingAbstract method invokeOnewayImpl.

public void invokeOnewayImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis) throws InterruptedException, RemotingTooMuchRequestException, RemotingTimeoutException, RemotingSendRequestException {
    request.markOnewayRPC();
    boolean acquired = this.semaphoreOneway.tryAcquire(timeoutMillis, TimeUnit.MILLISECONDS);
    if (acquired) {
        final SemaphoreReleaseOnlyOnce once = new SemaphoreReleaseOnlyOnce(this.semaphoreOneway);
        try {
            channel.writeAndFlush(request).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture f) throws Exception {
                    once.release();
                    if (!f.isSuccess()) {
                        log.warn("send a request command to channel <" + channel.remoteAddress() + "> failed.");
                    }
                }
            });
        } catch (Exception e) {
            once.release();
            log.warn("write send a request command to channel <" + channel.remoteAddress() + "> failed.");
            throw new RemotingSendRequestException(RemotingHelper.parseChannelRemoteAddr(channel), e);
        }
    } else {
        if (timeoutMillis <= 0) {
            throw new RemotingTooMuchRequestException("invokeOnewayImpl invoke too fast");
        } else {
            String info = String.format("invokeOnewayImpl tryAcquire semaphore timeout, %dms, waiting thread nums: %d semaphoreAsyncValue: %d", timeoutMillis, this.semaphoreOneway.getQueueLength(), this.semaphoreOneway.availablePermits());
            log.warn(info);
            throw new RemotingTimeoutException(info);
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RemotingTooMuchRequestException(org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) SemaphoreReleaseOnlyOnce(org.apache.rocketmq.remoting.common.SemaphoreReleaseOnlyOnce) RemotingTooMuchRequestException(org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException)

Example 5 with RemotingTimeoutException

use of org.apache.rocketmq.remoting.exception.RemotingTimeoutException in project rocketmq by apache.

the class DefaultMQProducerImpl method sendDefaultImpl.

private SendResult sendDefaultImpl(Message msg, final CommunicationMode communicationMode, final SendCallback sendCallback, final long timeout) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    this.makeSureStateOK();
    Validators.checkMessage(msg, this.defaultMQProducer);
    final long invokeID = random.nextLong();
    long beginTimestampFirst = System.currentTimeMillis();
    long beginTimestampPrev = beginTimestampFirst;
    long endTimestamp = beginTimestampFirst;
    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++) {
            String lastBrokerName = null == mq ? null : mq.getBrokerName();
            MessageQueue mqSelected = this.selectOneMessageQueue(topicPublishInfo, lastBrokerName);
            if (mqSelected != null) {
                mq = mqSelected;
                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) {
                        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));
        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);
}
Also used : RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) TransactionSendResult(org.apache.rocketmq.client.producer.TransactionSendResult) SendResult(org.apache.rocketmq.client.producer.SendResult) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Aggregations

RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)20 RemotingSendRequestException (org.apache.rocketmq.remoting.exception.RemotingSendRequestException)12 RemotingConnectException (org.apache.rocketmq.remoting.exception.RemotingConnectException)8 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)8 ChannelFuture (io.netty.channel.ChannelFuture)6 ChannelFutureListener (io.netty.channel.ChannelFutureListener)6 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)6 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)6 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)6 RemotingTooMuchRequestException (org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException)6 IOException (java.io.IOException)4 SocketAddress (java.net.SocketAddress)4 UnknownHostException (java.net.UnknownHostException)4 Map (java.util.Map)4 Set (java.util.Set)4 TreeSet (java.util.TreeSet)4 SendMessageContext (org.apache.rocketmq.client.hook.SendMessageContext)4 ClusterInfo (org.apache.rocketmq.common.protocol.body.ClusterInfo)4 KVTable (org.apache.rocketmq.common.protocol.body.KVTable)4 SendMessageRequestHeader (org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader)4