Search in sources :

Example 6 with UpdateConsumerOffsetRequestHeader

use of org.apache.rocketmq.common.protocol.header.UpdateConsumerOffsetRequestHeader in project rocketmq by apache.

the class RemoteBrokerOffsetStoreTest method testReadOffset_Success.

@Test
public void testReadOffset_Success() throws Exception {
    OffsetStore offsetStore = new RemoteBrokerOffsetStore(mQClientFactory, group);
    final MessageQueue messageQueue = new MessageQueue(topic, brokerName, 3);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            UpdateConsumerOffsetRequestHeader updateRequestHeader = mock.getArgument(1);
            when(mqClientAPI.queryConsumerOffset(anyString(), any(QueryConsumerOffsetRequestHeader.class), anyLong())).thenReturn(updateRequestHeader.getCommitOffset());
            return null;
        }
    }).when(mqClientAPI).updateConsumerOffsetOneway(any(String.class), any(UpdateConsumerOffsetRequestHeader.class), any(Long.class));
    offsetStore.updateOffset(messageQueue, 1024, false);
    offsetStore.persist(messageQueue);
    assertThat(offsetStore.readOffset(messageQueue, ReadOffsetType.READ_FROM_STORE)).isEqualTo(1024);
    offsetStore.updateOffset(messageQueue, 1023, false);
    offsetStore.persist(messageQueue);
    assertThat(offsetStore.readOffset(messageQueue, ReadOffsetType.READ_FROM_STORE)).isEqualTo(1023);
    offsetStore.updateOffset(messageQueue, 1022, true);
    offsetStore.persist(messageQueue);
    assertThat(offsetStore.readOffset(messageQueue, ReadOffsetType.READ_FROM_STORE)).isEqualTo(1023);
    offsetStore.updateOffset(messageQueue, 1025, false);
    offsetStore.persistAll(new HashSet<MessageQueue>(Collections.singletonList(messageQueue)));
    assertThat(offsetStore.readOffset(messageQueue, ReadOffsetType.READ_FROM_STORE)).isEqualTo(1025);
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) UpdateConsumerOffsetRequestHeader(org.apache.rocketmq.common.protocol.header.UpdateConsumerOffsetRequestHeader) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 7 with UpdateConsumerOffsetRequestHeader

use of org.apache.rocketmq.common.protocol.header.UpdateConsumerOffsetRequestHeader in project rocketmq by apache.

the class ConsumerManageProcessor method updateConsumerOffset.

private RemotingCommand updateConsumerOffset(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(UpdateConsumerOffsetResponseHeader.class);
    final UpdateConsumerOffsetRequestHeader requestHeader = (UpdateConsumerOffsetRequestHeader) request.decodeCommandCustomHeader(UpdateConsumerOffsetRequestHeader.class);
    this.brokerController.getConsumerOffsetManager().commitOffset(RemotingHelper.parseChannelRemoteAddr(ctx.channel()), requestHeader.getConsumerGroup(), requestHeader.getTopic(), requestHeader.getQueueId(), requestHeader.getCommitOffset());
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) UpdateConsumerOffsetRequestHeader(org.apache.rocketmq.common.protocol.header.UpdateConsumerOffsetRequestHeader)

Example 8 with UpdateConsumerOffsetRequestHeader

use of org.apache.rocketmq.common.protocol.header.UpdateConsumerOffsetRequestHeader in project rocketmq by apache.

the class RemoteBrokerOffsetStore method updateConsumeOffsetToBroker.

/**
 * Update the Consumer Offset synchronously, once the Master is off, updated to Slave,
 * here need to be optimized.
 */
@Override
public void updateConsumeOffsetToBroker(MessageQueue mq, long offset, boolean isOneway) throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
    FindBrokerResult findBrokerResult = this.mQClientFactory.findBrokerAddressInAdmin(mq.getBrokerName());
    if (null == findBrokerResult) {
        this.mQClientFactory.updateTopicRouteInfoFromNameServer(mq.getTopic());
        findBrokerResult = this.mQClientFactory.findBrokerAddressInAdmin(mq.getBrokerName());
    }
    if (findBrokerResult != null) {
        UpdateConsumerOffsetRequestHeader requestHeader = new UpdateConsumerOffsetRequestHeader();
        requestHeader.setTopic(mq.getTopic());
        requestHeader.setConsumerGroup(this.groupName);
        requestHeader.setQueueId(mq.getQueueId());
        requestHeader.setCommitOffset(offset);
        if (isOneway) {
            this.mQClientFactory.getMQClientAPIImpl().updateConsumerOffsetOneway(findBrokerResult.getBrokerAddr(), requestHeader, 1000 * 5);
        } else {
            this.mQClientFactory.getMQClientAPIImpl().updateConsumerOffset(findBrokerResult.getBrokerAddr(), requestHeader, 1000 * 5);
        }
    } else {
        throw new MQClientException("The broker[" + mq.getBrokerName() + "] not exist", null);
    }
}
Also used : UpdateConsumerOffsetRequestHeader(org.apache.rocketmq.common.protocol.header.UpdateConsumerOffsetRequestHeader) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 9 with UpdateConsumerOffsetRequestHeader

use of org.apache.rocketmq.common.protocol.header.UpdateConsumerOffsetRequestHeader in project rocketmq by apache.

the class DefaultMQAdminExtImpl method updateConsumeOffset.

@Override
public void updateConsumeOffset(String brokerAddr, String consumeGroup, MessageQueue mq, long offset) throws RemotingException, InterruptedException, MQBrokerException {
    UpdateConsumerOffsetRequestHeader requestHeader = new UpdateConsumerOffsetRequestHeader();
    requestHeader.setConsumerGroup(consumeGroup);
    requestHeader.setTopic(mq.getTopic());
    requestHeader.setQueueId(mq.getQueueId());
    requestHeader.setCommitOffset(offset);
    this.mqClientInstance.getMQClientAPIImpl().updateConsumerOffset(brokerAddr, requestHeader, timeoutMillis);
}
Also used : UpdateConsumerOffsetRequestHeader(org.apache.rocketmq.common.protocol.header.UpdateConsumerOffsetRequestHeader)

Example 10 with UpdateConsumerOffsetRequestHeader

use of org.apache.rocketmq.common.protocol.header.UpdateConsumerOffsetRequestHeader in project rocketmq by apache.

the class DefaultMQAdminExtImpl method resetOffsetConsumeOffset.

private RollbackStats resetOffsetConsumeOffset(String brokerAddr, String consumeGroup, MessageQueue queue, OffsetWrapper offsetWrapper, long timestamp, boolean force) throws RemotingException, InterruptedException, MQBrokerException {
    long resetOffset;
    if (timestamp == -1) {
        resetOffset = this.mqClientInstance.getMQClientAPIImpl().getMaxOffset(brokerAddr, queue.getTopic(), queue.getQueueId(), timeoutMillis);
    } else {
        resetOffset = this.mqClientInstance.getMQClientAPIImpl().searchOffset(brokerAddr, queue.getTopic(), queue.getQueueId(), timestamp, timeoutMillis);
    }
    RollbackStats rollbackStats = new RollbackStats();
    rollbackStats.setBrokerName(queue.getBrokerName());
    rollbackStats.setQueueId(queue.getQueueId());
    rollbackStats.setBrokerOffset(offsetWrapper.getBrokerOffset());
    rollbackStats.setConsumerOffset(offsetWrapper.getConsumerOffset());
    rollbackStats.setTimestampOffset(resetOffset);
    rollbackStats.setRollbackOffset(offsetWrapper.getConsumerOffset());
    if (force || resetOffset <= offsetWrapper.getConsumerOffset()) {
        rollbackStats.setRollbackOffset(resetOffset);
        UpdateConsumerOffsetRequestHeader requestHeader = new UpdateConsumerOffsetRequestHeader();
        requestHeader.setConsumerGroup(consumeGroup);
        requestHeader.setTopic(queue.getTopic());
        requestHeader.setQueueId(queue.getQueueId());
        requestHeader.setCommitOffset(resetOffset);
        this.mqClientInstance.getMQClientAPIImpl().updateConsumerOffset(brokerAddr, requestHeader, timeoutMillis);
    }
    return rollbackStats;
}
Also used : UpdateConsumerOffsetRequestHeader(org.apache.rocketmq.common.protocol.header.UpdateConsumerOffsetRequestHeader) RollbackStats(org.apache.rocketmq.common.admin.RollbackStats)

Aggregations

UpdateConsumerOffsetRequestHeader (org.apache.rocketmq.common.protocol.header.UpdateConsumerOffsetRequestHeader)10 MQClientException (org.apache.rocketmq.client.exception.MQClientException)2 FindBrokerResult (org.apache.rocketmq.client.impl.FindBrokerResult)2 RollbackStats (org.apache.rocketmq.common.admin.RollbackStats)2 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)2 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)2 Test (org.junit.Test)2 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2