Search in sources :

Example 11 with RemotingCommandException

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

the class QueryMessageProcessor method queryMessage.

public RemotingCommand queryMessage(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(QueryMessageResponseHeader.class);
    final QueryMessageResponseHeader responseHeader = (QueryMessageResponseHeader) response.readCustomHeader();
    final QueryMessageRequestHeader requestHeader = (QueryMessageRequestHeader) request.decodeCommandCustomHeader(QueryMessageRequestHeader.class);
    response.setOpaque(request.getOpaque());
    String isUniqueKey = request.getExtFields().get(MixAll.UNIQUE_MSG_QUERY_FLAG);
    if (isUniqueKey != null && isUniqueKey.equals("true")) {
        requestHeader.setMaxNum(this.brokerController.getMessageStoreConfig().getDefaultQueryMaxNum());
    }
    final QueryMessageResult queryMessageResult = this.brokerController.getMessageStore().queryMessage(requestHeader.getTopic(), requestHeader.getKey(), requestHeader.getMaxNum(), requestHeader.getBeginTimestamp(), requestHeader.getEndTimestamp());
    assert queryMessageResult != null;
    responseHeader.setIndexLastUpdatePhyoffset(queryMessageResult.getIndexLastUpdatePhyoffset());
    responseHeader.setIndexLastUpdateTimestamp(queryMessageResult.getIndexLastUpdateTimestamp());
    if (queryMessageResult.getBufferTotalSize() > 0) {
        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);
        try {
            FileRegion fileRegion = new QueryMessageTransfer(response.encodeHeader(queryMessageResult.getBufferTotalSize()), queryMessageResult);
            ctx.channel().writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    queryMessageResult.release();
                    if (!future.isSuccess()) {
                        log.error("transfer query message by page cache failed, ", future.cause());
                    }
                }
            });
        } catch (Throwable e) {
            log.error("", e);
            queryMessageResult.release();
        }
        return null;
    }
    response.setCode(ResponseCode.QUERY_NOT_FOUND);
    response.setRemark("can not find message, maybe time range not correct");
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ChannelFuture(io.netty.channel.ChannelFuture) QueryMessageTransfer(org.apache.rocketmq.broker.pagecache.QueryMessageTransfer) QueryMessageResponseHeader(org.apache.rocketmq.common.protocol.header.QueryMessageResponseHeader) QueryMessageRequestHeader(org.apache.rocketmq.common.protocol.header.QueryMessageRequestHeader) QueryMessageResult(org.apache.rocketmq.store.QueryMessageResult) FileRegion(io.netty.channel.FileRegion) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException)

Example 12 with RemotingCommandException

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

the class ClientRemotingProcessor method notifyConsumerIdsChanged.

public RemotingCommand notifyConsumerIdsChanged(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    try {
        final NotifyConsumerIdsChangedRequestHeader requestHeader = (NotifyConsumerIdsChangedRequestHeader) request.decodeCommandCustomHeader(NotifyConsumerIdsChangedRequestHeader.class);
        log.info("receive broker's notification[{}], the consumer group: {} changed, rebalance immediately", RemotingHelper.parseChannelRemoteAddr(ctx.channel()), requestHeader.getConsumerGroup());
        this.mqClientFactory.rebalanceImmediately();
    } catch (Exception e) {
        log.error("notifyConsumerIdsChanged exception", RemotingHelper.exceptionSimpleDesc(e));
    }
    return null;
}
Also used : NotifyConsumerIdsChangedRequestHeader(org.apache.rocketmq.common.protocol.header.NotifyConsumerIdsChangedRequestHeader) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException)

Example 13 with RemotingCommandException

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

the class MQAdminImpl method queryMessage.

protected QueryResult queryMessage(String topic, String key, int maxNum, long begin, long end, boolean isUniqKey) throws MQClientException, InterruptedException {
    TopicRouteData topicRouteData = this.mQClientFactory.getAnExistTopicRouteData(topic);
    if (null == topicRouteData) {
        this.mQClientFactory.updateTopicRouteInfoFromNameServer(topic);
        topicRouteData = this.mQClientFactory.getAnExistTopicRouteData(topic);
    }
    if (topicRouteData != null) {
        List<String> brokerAddrs = new LinkedList<String>();
        for (BrokerData brokerData : topicRouteData.getBrokerDatas()) {
            String addr = brokerData.selectBrokerAddr();
            if (addr != null) {
                brokerAddrs.add(addr);
            }
        }
        if (!brokerAddrs.isEmpty()) {
            final CountDownLatch countDownLatch = new CountDownLatch(brokerAddrs.size());
            final List<QueryResult> queryResultList = new LinkedList<QueryResult>();
            final ReadWriteLock lock = new ReentrantReadWriteLock(false);
            for (String addr : brokerAddrs) {
                try {
                    QueryMessageRequestHeader requestHeader = new QueryMessageRequestHeader();
                    requestHeader.setTopic(topic);
                    requestHeader.setKey(key);
                    requestHeader.setMaxNum(maxNum);
                    requestHeader.setBeginTimestamp(begin);
                    requestHeader.setEndTimestamp(end);
                    this.mQClientFactory.getMQClientAPIImpl().queryMessage(addr, requestHeader, timeoutMillis * 3, new InvokeCallback() {

                        @Override
                        public void operationComplete(ResponseFuture responseFuture) {
                            try {
                                RemotingCommand response = responseFuture.getResponseCommand();
                                if (response != null) {
                                    switch(response.getCode()) {
                                        case ResponseCode.SUCCESS:
                                            {
                                                QueryMessageResponseHeader responseHeader = null;
                                                try {
                                                    responseHeader = (QueryMessageResponseHeader) response.decodeCommandCustomHeader(QueryMessageResponseHeader.class);
                                                } catch (RemotingCommandException e) {
                                                    log.error("decodeCommandCustomHeader exception", e);
                                                    return;
                                                }
                                                List<MessageExt> wrappers = MessageDecoder.decodes(ByteBuffer.wrap(response.getBody()), true);
                                                QueryResult qr = new QueryResult(responseHeader.getIndexLastUpdateTimestamp(), wrappers);
                                                try {
                                                    lock.writeLock().lock();
                                                    queryResultList.add(qr);
                                                } finally {
                                                    lock.writeLock().unlock();
                                                }
                                                break;
                                            }
                                        default:
                                            log.warn("getResponseCommand failed, {} {}", response.getCode(), response.getRemark());
                                            break;
                                    }
                                } else {
                                    log.warn("getResponseCommand return null");
                                }
                            } finally {
                                countDownLatch.countDown();
                            }
                        }
                    }, isUniqKey);
                } catch (Exception e) {
                    log.warn("queryMessage exception", e);
                }
            }
            boolean ok = countDownLatch.await(timeoutMillis * 4, TimeUnit.MILLISECONDS);
            if (!ok) {
                log.warn("queryMessage, maybe some broker failed");
            }
            long indexLastUpdateTimestamp = 0;
            List<MessageExt> messageList = new LinkedList<MessageExt>();
            for (QueryResult qr : queryResultList) {
                if (qr.getIndexLastUpdateTimestamp() > indexLastUpdateTimestamp) {
                    indexLastUpdateTimestamp = qr.getIndexLastUpdateTimestamp();
                }
                for (MessageExt msgExt : qr.getMessageList()) {
                    if (isUniqKey) {
                        if (msgExt.getMsgId().equals(key)) {
                            if (messageList.size() > 0) {
                                if (messageList.get(0).getStoreTimestamp() > msgExt.getStoreTimestamp()) {
                                    messageList.clear();
                                    messageList.add(msgExt);
                                }
                            } else {
                                messageList.add(msgExt);
                            }
                        } else {
                            log.warn("queryMessage by uniqKey, find message key not matched, maybe hash duplicate {}", msgExt.toString());
                        }
                    } else {
                        String keys = msgExt.getKeys();
                        if (keys != null) {
                            boolean matched = false;
                            String[] keyArray = keys.split(MessageConst.KEY_SEPARATOR);
                            if (keyArray != null) {
                                for (String k : keyArray) {
                                    if (key.equals(k)) {
                                        matched = true;
                                        break;
                                    }
                                }
                            }
                            if (matched) {
                                messageList.add(msgExt);
                            } else {
                                log.warn("queryMessage, find message key not matched, maybe hash duplicate {}", msgExt.toString());
                            }
                        }
                    }
                }
            }
            if (!messageList.isEmpty()) {
                return new QueryResult(indexLastUpdateTimestamp, messageList);
            } else {
                throw new MQClientException(ResponseCode.NO_MESSAGE, "query message by key finished, but no message.");
            }
        }
    }
    throw new MQClientException(ResponseCode.TOPIC_NOT_EXIST, "The topic[" + topic + "] not matched route info");
}
Also used : InvokeCallback(org.apache.rocketmq.remoting.InvokeCallback) QueryMessageRequestHeader(org.apache.rocketmq.common.protocol.header.QueryMessageRequestHeader) BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) ResponseFuture(org.apache.rocketmq.remoting.netty.ResponseFuture) CountDownLatch(java.util.concurrent.CountDownLatch) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) LinkedList(java.util.LinkedList) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) MessageExt(org.apache.rocketmq.common.message.MessageExt) QueryResult(org.apache.rocketmq.client.QueryResult) QueryMessageResponseHeader(org.apache.rocketmq.common.protocol.header.QueryMessageResponseHeader) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 14 with RemotingCommandException

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

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 15 with RemotingCommandException

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

the class AdminBrokerProcessor method updateAndCreateTopic.

private RemotingCommand updateAndCreateTopic(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final CreateTopicRequestHeader requestHeader = (CreateTopicRequestHeader) request.decodeCommandCustomHeader(CreateTopicRequestHeader.class);
    log.info("updateAndCreateTopic called by {}", RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
    if (requestHeader.getTopic().equals(this.brokerController.getBrokerConfig().getBrokerClusterName())) {
        String errorMsg = "the topic[" + requestHeader.getTopic() + "] is conflict with system reserved words.";
        log.warn(errorMsg);
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(errorMsg);
        return response;
    }
    try {
        response.setCode(ResponseCode.SUCCESS);
        response.setOpaque(request.getOpaque());
        response.markResponseType();
        response.setRemark(null);
        ctx.writeAndFlush(response);
    } catch (Exception e) {
        log.error("Failed to produce a proper response", e);
    }
    TopicConfig topicConfig = new TopicConfig(requestHeader.getTopic());
    topicConfig.setReadQueueNums(requestHeader.getReadQueueNums());
    topicConfig.setWriteQueueNums(requestHeader.getWriteQueueNums());
    topicConfig.setTopicFilterType(requestHeader.getTopicFilterTypeEnum());
    topicConfig.setPerm(requestHeader.getPerm());
    topicConfig.setTopicSysFlag(requestHeader.getTopicSysFlag() == null ? 0 : requestHeader.getTopicSysFlag());
    this.brokerController.getTopicConfigManager().updateTopicConfig(topicConfig);
    this.brokerController.registerBrokerAll(false, true);
    return null;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) CreateTopicRequestHeader(org.apache.rocketmq.common.protocol.header.CreateTopicRequestHeader) TopicConfig(org.apache.rocketmq.common.TopicConfig) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException)

Aggregations

RemotingCommandException (org.apache.rocketmq.remoting.exception.RemotingCommandException)26 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)22 ChannelFuture (io.netty.channel.ChannelFuture)8 ChannelFutureListener (io.netty.channel.ChannelFutureListener)8 FileRegion (io.netty.channel.FileRegion)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 UnknownHostException (java.net.UnknownHostException)4 MQClientException (org.apache.rocketmq.client.exception.MQClientException)4 TopicConfig (org.apache.rocketmq.common.TopicConfig)4 QueryMessageRequestHeader (org.apache.rocketmq.common.protocol.header.QueryMessageRequestHeader)4 QueryMessageResponseHeader (org.apache.rocketmq.common.protocol.header.QueryMessageResponseHeader)4 SubscriptionData (org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData)4 TopicRouteData (org.apache.rocketmq.common.protocol.route.TopicRouteData)4 CommandCustomHeader (org.apache.rocketmq.remoting.CommandCustomHeader)4 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)4 JSONField (com.alibaba.fastjson.annotation.JSONField)2 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 LinkedList (java.util.LinkedList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2