com.alibaba.rocketmq.store.SelectMapedBufferResult Java Examples

The following examples show how to use com.alibaba.rocketmq.store.SelectMapedBufferResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AdminBrokerProcessor.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 6 votes vote down vote up
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());
    SelectMapedBufferResult selectMapedBufferResult = null;
    try {
        MessageId messageId = MessageDecoder.decodeMessageId(requestHeader.getMsgId());
        selectMapedBufferResult = this.brokerController.getMessageStore().selectOneMessageByOffset(messageId.getOffset());

        byte[] body = new byte[selectMapedBufferResult.getSize()];
        selectMapedBufferResult.getByteBuffer().get(body);
        request.setBody(body);
    }
    catch (UnknownHostException e) {
    }
    finally {
        if (selectMapedBufferResult != null) {
            selectMapedBufferResult.release();
        }
    }

    return this.callConsumer(RequestCode.CONSUME_MESSAGE_DIRECTLY, request, requestHeader.getConsumerGroup(),
        requestHeader.getClientId());
}
 
Example #2
Source File: DefaultTransactionCheckExecuter.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Override
public void gotoCheck(int producerGroupHashCode, long tranStateTableOffset, long commitLogOffset,
        int msgSize) {
    final ClientChannelInfo clientChannelInfo =
            this.brokerController.getProducerManager().pickProducerChannelRandomly(producerGroupHashCode);
    if (null == clientChannelInfo) {
        log.warn("check a producer transaction state, but not find any channel of this group[{}]",
            producerGroupHashCode);
        return;
    }

     SelectMapedBufferResult selectMapedBufferResult =
            this.brokerController.getMessageStore().selectOneMessageByOffset(commitLogOffset, msgSize);
    if (null == selectMapedBufferResult) {
        log.warn(
            "check a producer transaction state, but not find message by commitLogOffset: {}, msgSize: ",
            commitLogOffset, msgSize);
        return;
    }

    final CheckTransactionStateRequestHeader requestHeader = new CheckTransactionStateRequestHeader();
    requestHeader.setCommitLogOffset(commitLogOffset);
    requestHeader.setTranStateTableOffset(tranStateTableOffset);
    this.brokerController.getBroker2Client().checkProducerTransactionState(
        clientChannelInfo.getChannel(), requestHeader, selectMapedBufferResult);
}
 
Example #3
Source File: AdminBrokerProcessor.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
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());
    SelectMapedBufferResult selectMapedBufferResult = null;
    try {
        MessageId messageId = MessageDecoder.decodeMessageId(requestHeader.getMsgId());
        selectMapedBufferResult = this.brokerController.getMessageStore().selectOneMessageByOffset(messageId.getOffset());

        byte[] body = new byte[selectMapedBufferResult.getSize()];
        selectMapedBufferResult.getByteBuffer().get(body);
        request.setBody(body);
    } catch (UnknownHostException e) {
    } finally {
        if (selectMapedBufferResult != null) {
            selectMapedBufferResult.release();
        }
    }

    return this.callConsumer(RequestCode.CONSUME_MESSAGE_DIRECTLY, request, requestHeader.getConsumerGroup(),
            requestHeader.getClientId());
}
 
Example #4
Source File: Broker2Client.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
public void checkProducerTransactionState(//
        final Channel channel,//
        final CheckTransactionStateRequestHeader requestHeader,//
        final SelectMapedBufferResult selectMapedBufferResult//
) {
    RemotingCommand request =
            RemotingCommand.createRequestCommand(RequestCode.CHECK_TRANSACTION_STATE, requestHeader);
    request.markOnewayRPC();

    try {
        FileRegion fileRegion =
                new OneMessageTransfer(request.encodeHeader(selectMapedBufferResult.getSize()),
                    selectMapedBufferResult);
        channel.writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                selectMapedBufferResult.release();
                if (!future.isSuccess()) {
                    log.error("invokeProducer failed,", future.cause());
                }
            }
        });
    }
    catch (Throwable e) {
        log.error("invokeProducer exception", e);
        selectMapedBufferResult.release();
    }
}
 
Example #5
Source File: Broker2Client.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void checkProducerTransactionState(//
                                          final Channel channel,//
                                          final CheckTransactionStateRequestHeader requestHeader,//
                                          final SelectMapedBufferResult selectMapedBufferResult//
) {
    RemotingCommand request =
            RemotingCommand.createRequestCommand(RequestCode.CHECK_TRANSACTION_STATE, requestHeader);
    request.markOnewayRPC();

    try {
        FileRegion fileRegion =
                new OneMessageTransfer(request.encodeHeader(selectMapedBufferResult.getSize()),
                        selectMapedBufferResult);
        channel.writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                selectMapedBufferResult.release();
                if (!future.isSuccess()) {
                    log.error("invokeProducer failed,", future.cause());
                }
            }
        });
    } catch (Throwable e) {
        log.error("invokeProducer exception", e);
        selectMapedBufferResult.release();
    }
}
 
Example #6
Source File: TransactionStateService.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private SelectMapedBufferResult findTransactionBuffer(final long tsOffset) {
    final int mapedFileSize =
            this.defaultMessageStore.getMessageStoreConfig().getTranStateTableMapedFileSize();
    final long offset = tsOffset * TSStoreUnitSize;
    MapedFile mapedFile = this.tranStateTable.findMapedFileByOffset(offset);
    if (mapedFile != null) {
        SelectMapedBufferResult result = mapedFile.selectMapedBuffer((int) (offset % mapedFileSize));
        return result;
    }

    return null;
}
 
Example #7
Source File: AdminBrokerProcessor.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
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());
    SelectMapedBufferResult selectMapedBufferResult = null;
    try {
        MessageId messageId = MessageDecoder.decodeMessageId(requestHeader.getMsgId());
        selectMapedBufferResult =
                this.brokerController.getMessageStore().selectOneMessageByOffset(messageId.getOffset());

        byte[] body = new byte[selectMapedBufferResult.getSize()];
        selectMapedBufferResult.getByteBuffer().get(body);
        request.setBody(body);
    }
    catch (UnknownHostException e) {
    }
    finally {
        if (selectMapedBufferResult != null) {
            selectMapedBufferResult.release();
        }
    }

    return this.callConsumer(RequestCode.CONSUME_MESSAGE_DIRECTLY, request,
        requestHeader.getConsumerGroup(), requestHeader.getClientId());
}
 
Example #8
Source File: Broker2Client.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
/**
 * Broker主动回查Producer事务状态,Oneway
 */
public void checkProducerTransactionState(//
        final Channel channel, //
        final CheckTransactionStateRequestHeader requestHeader, //
        final SelectMapedBufferResult selectMapedBufferResult//
) {
    RemotingCommand request =
            RemotingCommand.createRequestCommand(RequestCode.CHECK_TRANSACTION_STATE, requestHeader);
    request.markOnewayRPC();

    try {
        FileRegion fileRegion = new OneMessageTransfer(
            request.encodeHeader(selectMapedBufferResult.getSize()), selectMapedBufferResult);
        channel.writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                selectMapedBufferResult.release();
                if (!future.isSuccess()) {
                    log.error("invokeProducer failed,", future.cause());
                }
            }
        });
    }
    catch (Throwable e) {
        log.error("invokeProducer exception", e);
        selectMapedBufferResult.release();
    }
}
 
Example #9
Source File: OneMessageTransfer.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 4 votes vote down vote up
public OneMessageTransfer(ByteBuffer byteBufferHeader, SelectMapedBufferResult selectMapedBufferResult) {
    this.byteBufferHeader = byteBufferHeader;
    this.selectMapedBufferResult = selectMapedBufferResult;
}
 
Example #10
Source File: QueryMessageProcessor.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 4 votes vote down vote up
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 SelectMapedBufferResult selectMapedBufferResult =
            this.brokerController.getMessageStore().selectOneMessageByOffset(requestHeader.getOffset());
    if (selectMapedBufferResult != null) {
        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);

        try {
            FileRegion fileRegion =
                    new OneMessageTransfer(response.encodeHeader(selectMapedBufferResult.getSize()),
                        selectMapedBufferResult);
            ctx.channel().writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    selectMapedBufferResult.release();
                    if (!future.isSuccess()) {
                        log.error("transfer one message by pagecache failed, ", future.cause());
                    }
                }
            });
        }
        catch (Throwable e) {
            log.error("", e);
            selectMapedBufferResult.release();
        }

        return null;
    }
    else {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark("can not find message by the offset, " + requestHeader.getOffset());
    }

    return response;
}
 
Example #11
Source File: OneMessageTransfer.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public OneMessageTransfer(ByteBuffer byteBufferHeader, SelectMapedBufferResult selectMapedBufferResult) {
    this.byteBufferHeader = byteBufferHeader;
    this.selectMapedBufferResult = selectMapedBufferResult;
}
 
Example #12
Source File: QueryMessageProcessor.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
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 SelectMapedBufferResult selectMapedBufferResult =
            this.brokerController.getMessageStore().selectOneMessageByOffset(requestHeader.getOffset());
    if (selectMapedBufferResult != null) {
        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);

        try {
            FileRegion fileRegion =
                    new OneMessageTransfer(response.encodeHeader(selectMapedBufferResult.getSize()),
                            selectMapedBufferResult);
            ctx.channel().writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    selectMapedBufferResult.release();
                    if (!future.isSuccess()) {
                        log.error("transfer one message by pagecache failed, ", future.cause());
                    }
                }
            });
        } catch (Throwable e) {
            log.error("", e);
            selectMapedBufferResult.release();
        }

        return null;
    } else {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark("can not find message by the offset, " + requestHeader.getOffset());
    }

    return response;
}
 
Example #13
Source File: TransactionStateService.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
private void recreateStateTable() {
    this.tranStateTable =
            new MapedFileQueue(defaultMessageStore.getMessageStoreConfig().getTranStateTableStorePath(),
                defaultMessageStore.getMessageStoreConfig().getTranStateTableMapedFileSize(), null);

    final TreeSet<Long> preparedItemSet = new TreeSet<Long>();

    final long minOffset = this.tranRedoLog.getMinOffsetInQuque();
    long processOffset = minOffset;
    while (true) {
        SelectMapedBufferResult bufferConsumeQueue = this.tranRedoLog.getIndexBuffer(processOffset);
        if (bufferConsumeQueue != null) {
            try {
                long i = 0;
                for (; i < bufferConsumeQueue.getSize(); i += ConsumeQueue.CQStoreUnitSize) {
                    long offsetMsg = bufferConsumeQueue.getByteBuffer().getLong();
                    int sizeMsg = bufferConsumeQueue.getByteBuffer().getInt();
                    long tagsCode = bufferConsumeQueue.getByteBuffer().getLong();

                    // Prepared
                    if (TransactionStateService.PreparedMessageTagsCode == tagsCode) {
                        preparedItemSet.add(offsetMsg);
                    }
                    // Commit/Rollback
                    else {
                        preparedItemSet.remove(tagsCode);
                    }
                }

                processOffset += i;
            }
            finally {
                bufferConsumeQueue.release();
            }
        }
        else {
            break;
        }
    }

    log.info("scan transaction redolog over, End offset: {},  Prepared Transaction Count: {}",
        processOffset, preparedItemSet.size());

    Iterator<Long> it = preparedItemSet.iterator();
    while (it.hasNext()) {
        Long offset = it.next();

        MessageExt msgExt = this.defaultMessageStore.lookMessageByOffset(offset);
        if (msgExt != null) {

            this.appendPreparedTransaction(msgExt.getCommitLogOffset(), msgExt.getStoreSize(),
                (int) (msgExt.getStoreTimestamp() / 1000),
                msgExt.getProperty(MessageConst.PROPERTY_PRODUCER_GROUP).hashCode());
            this.tranStateTableOffset.incrementAndGet();
        }
    }
}
 
Example #14
Source File: TransactionStateService.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public boolean updateTransactionState(//
        final long tsOffset,//
        final long clOffset,//
        final int groupHashCode,//
        final int state//
) {
    SelectMapedBufferResult selectMapedBufferResult = this.findTransactionBuffer(tsOffset);
    if (selectMapedBufferResult != null) {
        try {
            final long clOffset_read = selectMapedBufferResult.getByteBuffer().getLong();
            final int size_read = selectMapedBufferResult.getByteBuffer().getInt();
            final int timestamp_read = selectMapedBufferResult.getByteBuffer().getInt();
            final int groupHashCode_read = selectMapedBufferResult.getByteBuffer().getInt();
            final int state_read = selectMapedBufferResult.getByteBuffer().getInt();

            if (clOffset != clOffset_read) {
                log.error("updateTransactionState error clOffset: {} clOffset_read: {}", clOffset,
                    clOffset_read);
                return false;
            }

            if (groupHashCode != groupHashCode_read) {
                log.error("updateTransactionState error groupHashCode: {} groupHashCode_read: {}",
                    groupHashCode, groupHashCode_read);
                return false;
            }

            if (MessageSysFlag.TransactionPreparedType != state_read) {
                log.warn("updateTransactionState error, the transaction is updated before.");
                return true;
            }

            selectMapedBufferResult.getByteBuffer().putInt(TS_STATE_POS, state);
        }
        catch (Exception e) {
            log.error("updateTransactionState exception", e);
        }
        finally {
            selectMapedBufferResult.release();
        }
    }

    return false;
}
 
Example #15
Source File: OneMessageTransfer.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public OneMessageTransfer(ByteBuffer byteBufferHeader, SelectMapedBufferResult selectMapedBufferResult) {
    this.byteBufferHeader = byteBufferHeader;
    this.selectMapedBufferResult = selectMapedBufferResult;
}
 
Example #16
Source File: QueryMessageProcessor.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public RemotingCommand viewMessageById(ChannelHandlerContext ctx, RemotingCommand request)
        throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final ViewMessageRequestHeader requestHeader =
            (ViewMessageRequestHeader) request.decodeCommandCustomHeader(ViewMessageRequestHeader.class);

    // 由于使用sendfile,所以必须要设置
    response.setOpaque(request.getOpaque());

    final SelectMapedBufferResult selectMapedBufferResult =
            this.brokerController.getMessageStore().selectOneMessageByOffset(requestHeader.getOffset());
    if (selectMapedBufferResult != null) {
        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);

        try {
            FileRegion fileRegion = new OneMessageTransfer(
                response.encodeHeader(selectMapedBufferResult.getSize()), selectMapedBufferResult);
            ctx.channel().writeAndFlush(fileRegion).addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    selectMapedBufferResult.release();
                    if (!future.isSuccess()) {
                        log.error("transfer one message by pagecache failed, ", future.cause());
                    }
                }
            });
        }
        catch (Throwable e) {
            log.error("", e);
            selectMapedBufferResult.release();
        }

        return null;
    }
    else {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark("can not find message by the offset, " + requestHeader.getOffset());
    }

    return response;
}