org.apache.rocketmq.common.protocol.header.UpdateConsumerOffsetRequestHeader Java Examples

The following examples show how to use org.apache.rocketmq.common.protocol.header.UpdateConsumerOffsetRequestHeader. 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: MQClientAPIImpl.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public void updateConsumerOffset(
    final String addr,
    final UpdateConsumerOffsetRequestHeader requestHeader,
    final long timeoutMillis
) throws RemotingException, MQBrokerException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UPDATE_CONSUMER_OFFSET, requestHeader);

    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr),
        request, timeoutMillis);
    assert response != null;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            return;
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #2
Source File: MQClientAPIImpl.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public void updateConsumerOffset(
    final String addr,
    final UpdateConsumerOffsetRequestHeader requestHeader,
    final long timeoutMillis
) throws RemotingException, MQBrokerException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UPDATE_CONSUMER_OFFSET, requestHeader);

    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr),
        request, timeoutMillis);
    assert response != null;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            return;
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #3
Source File: MQClientAPIImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
public void updateConsumerOffset(//
    final String addr, //
    final UpdateConsumerOffsetRequestHeader requestHeader, //
    final long timeoutMillis//
) throws RemotingException, MQBrokerException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UPDATE_CONSUMER_OFFSET, requestHeader);

    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr),
        request, timeoutMillis);
    assert response != null;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            return;
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #4
Source File: ConsumerManageProcessor.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
/**
 * 更新消费偏移量
 * @param ctx ctx
 * @param request ;
 * @return ;
 * @throws RemotingCommandException ;
 */
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;
}
 
Example #5
Source File: ConsumerManageProcessor.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
private RemotingCommand updateConsumerOffset(ChannelHandlerContext ctx, RemotingCommand request)
    throws RemotingCommandException {
    final RemotingCommand response =
        RemotingCommand.createResponseCommand(UpdateConsumerOffsetResponseHeader.class);
    final UpdateConsumerOffsetRequestHeader requestHeader =
        (UpdateConsumerOffsetRequestHeader) request
            .decodeCommandCustomHeader(UpdateConsumerOffsetRequestHeader.class);

    // 消息轨迹:记录已经消费成功并提交 offset 的消息记录


    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;
}
 
Example #6
Source File: MQClientAPIImpl.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
/**
 * 更新消费者的某个队列的消费偏移量
 * @param addr ;
 * @param requestHeader ;
 * @param timeoutMillis ;
 * @throws RemotingException ;
 * @throws MQBrokerException ;
 * @throws InterruptedException ;
 */
public void updateConsumerOffset(
    final String addr,
    final UpdateConsumerOffsetRequestHeader requestHeader,
    final long timeoutMillis
) throws RemotingException, MQBrokerException, InterruptedException {

    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UPDATE_CONSUMER_OFFSET, requestHeader);

    String acturallyAddr = getActurallyBrokerAddr(addr);
    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), acturallyAddr),
        request, timeoutMillis);
    assert response != null;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            return;
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #7
Source File: MQClientAPIImpl.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
public void updateConsumerOffset(
        final String addr,
        final UpdateConsumerOffsetRequestHeader requestHeader,
        final long timeoutMillis
    ) throws RemotingException, MQBrokerException, InterruptedException {
        RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UPDATE_CONSUMER_OFFSET, requestHeader);

//        =》
        RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr),
            request, timeoutMillis);
        assert response != null;
        switch (response.getCode()) {
            case ResponseCode.SUCCESS: {
                return;
            }
            default:
                break;
        }

        throw new MQBrokerException(response.getCode(), response.getRemark());
    }
 
Example #8
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public void updateConsumerOffset(
    final String addr,
    final UpdateConsumerOffsetRequestHeader requestHeader,
    final long timeoutMillis
) throws RemotingException, MQBrokerException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UPDATE_CONSUMER_OFFSET, requestHeader);

    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr),
        request, timeoutMillis);
    assert response != null;
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            return;
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example #9
Source File: ConsumerManageProcessor.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #10
Source File: DefaultMQAdminExtImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #11
Source File: RemoteBrokerOffsetStoreTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #12
Source File: ConsumerManageProcessor.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #13
Source File: DefaultMQAdminExtImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #14
Source File: DefaultMQAdminExtImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #15
Source File: RemoteBrokerOffsetStore.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
/**
 * 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) {
        // TODO Here may be heavily overhead for Name Server,need tuning
        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);
    }
}
 
Example #16
Source File: RemoteBrokerOffsetStore.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example #17
Source File: MQClientAPIImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public void updateConsumerOffsetOneway(//
    final String addr, //
    final UpdateConsumerOffsetRequestHeader requestHeader, //
    final long timeoutMillis//
) throws RemotingConnectException, RemotingTooMuchRequestException, RemotingTimeoutException, RemotingSendRequestException,
    InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UPDATE_CONSUMER_OFFSET, requestHeader);

    this.remotingClient.invokeOneway(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis);
}
 
Example #18
Source File: RemoteBrokerOffsetStoreTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #19
Source File: DefaultMQAdminExtImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #20
Source File: DefaultMQAdminExtImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #21
Source File: RemoteBrokerOffsetStoreTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #22
Source File: DefaultMQAdminExtImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #23
Source File: RemoteBrokerOffsetStore.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
/**
 * 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) {
        // TODO Here may be heavily overhead for Name Server,need tuning
        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);
    }
}
 
Example #24
Source File: RemoteBrokerOffsetStoreTest.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
@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<>(Collections.singletonList(messageQueue)));
    assertThat(offsetStore.readOffset(messageQueue, ReadOffsetType.READ_FROM_STORE)).isEqualTo(1025);
}
 
Example #25
Source File: DefaultMQAdminExtImpl.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #26
Source File: DefaultMQAdminExtImpl.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #27
Source File: RemoteBrokerOffsetStore.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example #28
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void updateConsumerOffsetOneway(
    final String addr,
    final UpdateConsumerOffsetRequestHeader requestHeader,
    final long timeoutMillis
) throws RemotingConnectException, RemotingTooMuchRequestException, RemotingTimeoutException, RemotingSendRequestException,
    InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UPDATE_CONSUMER_OFFSET, requestHeader);

    this.remotingClient.invokeOneway(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis);
}
 
Example #29
Source File: DefaultMQAdminExtImpl.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #30
Source File: MQClientAPIImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void updateConsumerOffsetOneway(
    final String addr,
    final UpdateConsumerOffsetRequestHeader requestHeader,
    final long timeoutMillis
) throws RemotingConnectException, RemotingTooMuchRequestException, RemotingTimeoutException, RemotingSendRequestException,
    InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UPDATE_CONSUMER_OFFSET, requestHeader);

    this.remotingClient.invokeOneway(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis);
}