Search in sources :

Example 1 with SelectMappedBufferResult

use of org.apache.rocketmq.store.SelectMappedBufferResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class QueryMessageProcessor method viewMessageById.

public RemotingCommand viewMessageById(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final ViewMessageRequestHeader requestHeader = (ViewMessageRequestHeader) request.decodeCommandCustomHeader(ViewMessageRequestHeader.class);
    response.setOpaque(request.getOpaque());
    final SelectMappedBufferResult selectMappedBufferResult = this.brokerController.getMessageStore().selectOneMessageByOffset(requestHeader.getOffset());
    if (selectMappedBufferResult != null) {
        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);
        try {
            FileRegion fileRegion = new OneMessageTransfer(response.encodeHeader(selectMappedBufferResult.getSize()), selectMappedBufferResult);
            ctx.channel().writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    selectMappedBufferResult.release();
                    if (!future.isSuccess()) {
                        log.error("Transfer one message from page cache failed, ", future.cause());
                    }
                }
            });
        } catch (Throwable e) {
            log.error("", e);
            selectMappedBufferResult.release();
        }
        return null;
    } else {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark("can not find message by the offset, " + requestHeader.getOffset());
    }
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ViewMessageRequestHeader(org.apache.rocketmq.common.protocol.header.ViewMessageRequestHeader) ChannelFuture(io.netty.channel.ChannelFuture) OneMessageTransfer(org.apache.rocketmq.broker.pagecache.OneMessageTransfer) FileRegion(io.netty.channel.FileRegion) SelectMappedBufferResult(org.apache.rocketmq.store.SelectMappedBufferResult) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException)

Example 2 with SelectMappedBufferResult

use of org.apache.rocketmq.store.SelectMappedBufferResult in project rocketmq by apache.

the class AdminBrokerProcessor method consumeMessageDirectly.

private RemotingCommand consumeMessageDirectly(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final ConsumeMessageDirectlyResultRequestHeader requestHeader = (ConsumeMessageDirectlyResultRequestHeader) request.decodeCommandCustomHeader(ConsumeMessageDirectlyResultRequestHeader.class);
    request.getExtFields().put("brokerName", this.brokerController.getBrokerConfig().getBrokerName());
    SelectMappedBufferResult selectMappedBufferResult = null;
    try {
        MessageId messageId = MessageDecoder.decodeMessageId(requestHeader.getMsgId());
        selectMappedBufferResult = this.brokerController.getMessageStore().selectOneMessageByOffset(messageId.getOffset());
        byte[] body = new byte[selectMappedBufferResult.getSize()];
        selectMappedBufferResult.getByteBuffer().get(body);
        request.setBody(body);
    } catch (UnknownHostException e) {
    } finally {
        if (selectMappedBufferResult != null) {
            selectMappedBufferResult.release();
        }
    }
    return this.callConsumer(RequestCode.CONSUME_MESSAGE_DIRECTLY, request, requestHeader.getConsumerGroup(), requestHeader.getClientId());
}
Also used : UnknownHostException(java.net.UnknownHostException) ConsumeMessageDirectlyResultRequestHeader(org.apache.rocketmq.common.protocol.header.ConsumeMessageDirectlyResultRequestHeader) SelectMappedBufferResult(org.apache.rocketmq.store.SelectMappedBufferResult) MessageId(org.apache.rocketmq.common.message.MessageId)

Example 3 with SelectMappedBufferResult

use of org.apache.rocketmq.store.SelectMappedBufferResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class AdminBrokerProcessor method consumeMessageDirectly.

private RemotingCommand consumeMessageDirectly(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final ConsumeMessageDirectlyResultRequestHeader requestHeader = (ConsumeMessageDirectlyResultRequestHeader) request.decodeCommandCustomHeader(ConsumeMessageDirectlyResultRequestHeader.class);
    request.getExtFields().put("brokerName", this.brokerController.getBrokerConfig().getBrokerName());
    SelectMappedBufferResult selectMappedBufferResult = null;
    try {
        MessageId messageId = MessageDecoder.decodeMessageId(requestHeader.getMsgId());
        selectMappedBufferResult = this.brokerController.getMessageStore().selectOneMessageByOffset(messageId.getOffset());
        byte[] body = new byte[selectMappedBufferResult.getSize()];
        selectMappedBufferResult.getByteBuffer().get(body);
        request.setBody(body);
    } catch (UnknownHostException e) {
    } finally {
        if (selectMappedBufferResult != null) {
            selectMappedBufferResult.release();
        }
    }
    // 通过Broker直接向某个Consumer发送一条消息,并立刻消费,返回结果给broker,再返回给调用方
    return this.callConsumer(RequestCode.CONSUME_MESSAGE_DIRECTLY, request, requestHeader.getConsumerGroup(), requestHeader.getClientId());
}
Also used : UnknownHostException(java.net.UnknownHostException) ConsumeMessageDirectlyResultRequestHeader(org.apache.rocketmq.common.protocol.header.ConsumeMessageDirectlyResultRequestHeader) SelectMappedBufferResult(org.apache.rocketmq.store.SelectMappedBufferResult) MessageId(org.apache.rocketmq.common.message.MessageId)

Example 4 with SelectMappedBufferResult

use of org.apache.rocketmq.store.SelectMappedBufferResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class AdminBrokerProcessor method queryConsumeQueue.

private RemotingCommand queryConsumeQueue(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    QueryConsumeQueueRequestHeader requestHeader = (QueryConsumeQueueRequestHeader) request.decodeCommandCustomHeader(QueryConsumeQueueRequestHeader.class);
    RemotingCommand response = RemotingCommand.createResponseCommand(null);
    ConsumeQueue consumeQueue = this.brokerController.getMessageStore().getConsumeQueue(requestHeader.getTopic(), requestHeader.getQueueId());
    if (consumeQueue == null) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("%d@%s is not exist!", requestHeader.getQueueId(), requestHeader.getTopic()));
        return response;
    }
    QueryConsumeQueueResponseBody body = new QueryConsumeQueueResponseBody();
    response.setCode(ResponseCode.SUCCESS);
    response.setBody(body.encode());
    body.setMaxQueueIndex(consumeQueue.getMaxOffsetInQueue());
    body.setMinQueueIndex(consumeQueue.getMinOffsetInQueue());
    MessageFilter messageFilter = null;
    if (requestHeader.getConsumerGroup() != null) {
        SubscriptionData subscriptionData = this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getConsumerGroup(), requestHeader.getTopic());
        body.setSubscriptionData(subscriptionData);
        if (subscriptionData == null) {
            body.setFilterData(String.format("%s@%s is not online!", requestHeader.getConsumerGroup(), requestHeader.getTopic()));
        } else {
            ConsumerFilterData filterData = this.brokerController.getConsumerFilterManager().get(requestHeader.getTopic(), requestHeader.getConsumerGroup());
            body.setFilterData(JSON.toJSONString(filterData, true));
            messageFilter = new ExpressionMessageFilter(subscriptionData, filterData, this.brokerController.getConsumerFilterManager());
        }
    }
    SelectMappedBufferResult result = consumeQueue.getIndexBuffer(requestHeader.getIndex());
    if (result == null) {
        response.setRemark(String.format("Index %d of %d@%s is not exist!", requestHeader.getIndex(), requestHeader.getQueueId(), requestHeader.getTopic()));
        return response;
    }
    try {
        List<ConsumeQueueData> queues = new ArrayList<>();
        for (int i = 0; i < result.getSize() && i < requestHeader.getCount() * ConsumeQueue.CQ_STORE_UNIT_SIZE; i += ConsumeQueue.CQ_STORE_UNIT_SIZE) {
            ConsumeQueueData one = new ConsumeQueueData();
            one.setPhysicOffset(result.getByteBuffer().getLong());
            one.setPhysicSize(result.getByteBuffer().getInt());
            one.setTagsCode(result.getByteBuffer().getLong());
            if (!consumeQueue.isExtAddr(one.getTagsCode())) {
                queues.add(one);
                continue;
            }
            ConsumeQueueExt.CqExtUnit cqExtUnit = consumeQueue.getExt(one.getTagsCode());
            if (cqExtUnit != null) {
                one.setExtendDataJson(JSON.toJSONString(cqExtUnit));
                if (cqExtUnit.getFilterBitMap() != null) {
                    one.setBitMap(BitsArray.create(cqExtUnit.getFilterBitMap()).toString());
                }
                if (messageFilter != null) {
                    one.setEval(messageFilter.isMatchedByConsumeQueue(cqExtUnit.getTagsCode(), cqExtUnit));
                }
            } else {
                one.setMsg("Cq extend not exist!addr: " + one.getTagsCode());
            }
            queues.add(one);
        }
        body.setQueueData(queues);
    } finally {
        result.release();
    }
    return response;
}
Also used : ConsumeQueueData(org.apache.rocketmq.common.protocol.body.ConsumeQueueData) ConsumeQueueExt(org.apache.rocketmq.store.ConsumeQueueExt) QueryConsumeQueueResponseBody(org.apache.rocketmq.common.protocol.body.QueryConsumeQueueResponseBody) ExpressionMessageFilter(org.apache.rocketmq.broker.filter.ExpressionMessageFilter) ArrayList(java.util.ArrayList) SelectMappedBufferResult(org.apache.rocketmq.store.SelectMappedBufferResult) QueryConsumeQueueRequestHeader(org.apache.rocketmq.common.protocol.header.QueryConsumeQueueRequestHeader) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SubscriptionData(org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData) ConsumerFilterData(org.apache.rocketmq.broker.filter.ConsumerFilterData) MessageFilter(org.apache.rocketmq.store.MessageFilter) ExpressionMessageFilter(org.apache.rocketmq.broker.filter.ExpressionMessageFilter) ConsumeQueue(org.apache.rocketmq.store.ConsumeQueue)

Example 5 with SelectMappedBufferResult

use of org.apache.rocketmq.store.SelectMappedBufferResult in project rocketmq by apache.

the class AdminBrokerProcessor method queryConsumeQueue.

private RemotingCommand queryConsumeQueue(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    QueryConsumeQueueRequestHeader requestHeader = (QueryConsumeQueueRequestHeader) request.decodeCommandCustomHeader(QueryConsumeQueueRequestHeader.class);
    RemotingCommand response = RemotingCommand.createResponseCommand(null);
    ConsumeQueue consumeQueue = this.brokerController.getMessageStore().getConsumeQueue(requestHeader.getTopic(), requestHeader.getQueueId());
    if (consumeQueue == null) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("%d@%s is not exist!", requestHeader.getQueueId(), requestHeader.getTopic()));
        return response;
    }
    QueryConsumeQueueResponseBody body = new QueryConsumeQueueResponseBody();
    response.setCode(ResponseCode.SUCCESS);
    response.setBody(body.encode());
    body.setMaxQueueIndex(consumeQueue.getMaxOffsetInQueue());
    body.setMinQueueIndex(consumeQueue.getMinOffsetInQueue());
    MessageFilter messageFilter = null;
    if (requestHeader.getConsumerGroup() != null) {
        SubscriptionData subscriptionData = this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getConsumerGroup(), requestHeader.getTopic());
        body.setSubscriptionData(subscriptionData);
        if (subscriptionData == null) {
            body.setFilterData(String.format("%s@%s is not online!", requestHeader.getConsumerGroup(), requestHeader.getTopic()));
        } else {
            ConsumerFilterData filterData = this.brokerController.getConsumerFilterManager().get(requestHeader.getTopic(), requestHeader.getConsumerGroup());
            body.setFilterData(JSON.toJSONString(filterData, true));
            messageFilter = new ExpressionMessageFilter(subscriptionData, filterData, this.brokerController.getConsumerFilterManager());
        }
    }
    SelectMappedBufferResult result = consumeQueue.getIndexBuffer(requestHeader.getIndex());
    if (result == null) {
        response.setRemark(String.format("Index %d of %d@%s is not exist!", requestHeader.getIndex(), requestHeader.getQueueId(), requestHeader.getTopic()));
        return response;
    }
    try {
        List<ConsumeQueueData> queues = new ArrayList<>();
        for (int i = 0; i < result.getSize() && i < requestHeader.getCount() * ConsumeQueue.CQ_STORE_UNIT_SIZE; i += ConsumeQueue.CQ_STORE_UNIT_SIZE) {
            ConsumeQueueData one = new ConsumeQueueData();
            one.setPhysicOffset(result.getByteBuffer().getLong());
            one.setPhysicSize(result.getByteBuffer().getInt());
            one.setTagsCode(result.getByteBuffer().getLong());
            if (!consumeQueue.isExtAddr(one.getTagsCode())) {
                queues.add(one);
                continue;
            }
            ConsumeQueueExt.CqExtUnit cqExtUnit = consumeQueue.getExt(one.getTagsCode());
            if (cqExtUnit != null) {
                one.setExtendDataJson(JSON.toJSONString(cqExtUnit));
                if (cqExtUnit.getFilterBitMap() != null) {
                    one.setBitMap(BitsArray.create(cqExtUnit.getFilterBitMap()).toString());
                }
                if (messageFilter != null) {
                    one.setEval(messageFilter.isMatchedByConsumeQueue(cqExtUnit.getTagsCode(), cqExtUnit));
                }
            } else {
                one.setMsg("Cq extend not exist!addr: " + one.getTagsCode());
            }
            queues.add(one);
        }
        body.setQueueData(queues);
    } finally {
        result.release();
    }
    return response;
}
Also used : ConsumeQueueData(org.apache.rocketmq.common.protocol.body.ConsumeQueueData) ConsumeQueueExt(org.apache.rocketmq.store.ConsumeQueueExt) QueryConsumeQueueResponseBody(org.apache.rocketmq.common.protocol.body.QueryConsumeQueueResponseBody) ExpressionMessageFilter(org.apache.rocketmq.broker.filter.ExpressionMessageFilter) ArrayList(java.util.ArrayList) SelectMappedBufferResult(org.apache.rocketmq.store.SelectMappedBufferResult) QueryConsumeQueueRequestHeader(org.apache.rocketmq.common.protocol.header.QueryConsumeQueueRequestHeader) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SubscriptionData(org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData) ConsumerFilterData(org.apache.rocketmq.broker.filter.ConsumerFilterData) MessageFilter(org.apache.rocketmq.store.MessageFilter) ExpressionMessageFilter(org.apache.rocketmq.broker.filter.ExpressionMessageFilter) ConsumeQueue(org.apache.rocketmq.store.ConsumeQueue)

Aggregations

SelectMappedBufferResult (org.apache.rocketmq.store.SelectMappedBufferResult)6 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)4 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 FileRegion (io.netty.channel.FileRegion)2 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 ConsumerFilterData (org.apache.rocketmq.broker.filter.ConsumerFilterData)2 ExpressionMessageFilter (org.apache.rocketmq.broker.filter.ExpressionMessageFilter)2 OneMessageTransfer (org.apache.rocketmq.broker.pagecache.OneMessageTransfer)2 MessageId (org.apache.rocketmq.common.message.MessageId)2 ConsumeQueueData (org.apache.rocketmq.common.protocol.body.ConsumeQueueData)2 QueryConsumeQueueResponseBody (org.apache.rocketmq.common.protocol.body.QueryConsumeQueueResponseBody)2 ConsumeMessageDirectlyResultRequestHeader (org.apache.rocketmq.common.protocol.header.ConsumeMessageDirectlyResultRequestHeader)2 QueryConsumeQueueRequestHeader (org.apache.rocketmq.common.protocol.header.QueryConsumeQueueRequestHeader)2 ViewMessageRequestHeader (org.apache.rocketmq.common.protocol.header.ViewMessageRequestHeader)2 SubscriptionData (org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData)2 RemotingCommandException (org.apache.rocketmq.remoting.exception.RemotingCommandException)2 ConsumeQueue (org.apache.rocketmq.store.ConsumeQueue)2 ConsumeQueueExt (org.apache.rocketmq.store.ConsumeQueueExt)2