Search in sources :

Example 1 with SearchOffsetRequestHeader

use of org.apache.rocketmq.common.protocol.header.SearchOffsetRequestHeader in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientAPIImpl method searchOffset.

public long searchOffset(final String addr, final String topic, final int queueId, final long timestamp, final long timeoutMillis) throws RemotingException, MQBrokerException, InterruptedException {
    // 封装头信息
    SearchOffsetRequestHeader requestHeader = new SearchOffsetRequestHeader();
    requestHeader.setTopic(topic);
    requestHeader.setQueueId(queueId);
    requestHeader.setTimestamp(timestamp);
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.SEARCH_OFFSET_BY_TIMESTAMP, requestHeader);
    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis);
    assert response != null;
    switch(response.getCode()) {
        case ResponseCode.SUCCESS:
            {
                SearchOffsetResponseHeader responseHeader = (SearchOffsetResponseHeader) response.decodeCommandCustomHeader(SearchOffsetResponseHeader.class);
                return responseHeader.getOffset();
            }
        default:
            break;
    }
    throw new MQBrokerException(response.getCode(), response.getRemark());
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SearchOffsetRequestHeader(org.apache.rocketmq.common.protocol.header.SearchOffsetRequestHeader) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) SearchOffsetResponseHeader(org.apache.rocketmq.common.protocol.header.SearchOffsetResponseHeader)

Example 2 with SearchOffsetRequestHeader

use of org.apache.rocketmq.common.protocol.header.SearchOffsetRequestHeader in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class AdminBrokerProcessor method searchOffsetByTimestamp.

private RemotingCommand searchOffsetByTimestamp(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(SearchOffsetResponseHeader.class);
    final SearchOffsetResponseHeader responseHeader = (SearchOffsetResponseHeader) response.readCustomHeader();
    final SearchOffsetRequestHeader requestHeader = (SearchOffsetRequestHeader) request.decodeCommandCustomHeader(SearchOffsetRequestHeader.class);
    // 通过时间得到offset
    long offset = this.brokerController.getMessageStore().getOffsetInQueueByTime(requestHeader.getTopic(), requestHeader.getQueueId(), requestHeader.getTimestamp());
    responseHeader.setOffset(offset);
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SearchOffsetRequestHeader(org.apache.rocketmq.common.protocol.header.SearchOffsetRequestHeader) SearchOffsetResponseHeader(org.apache.rocketmq.common.protocol.header.SearchOffsetResponseHeader)

Example 3 with SearchOffsetRequestHeader

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

the class AdminBrokerProcessor method searchOffsetByTimestamp.

private RemotingCommand searchOffsetByTimestamp(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(SearchOffsetResponseHeader.class);
    final SearchOffsetResponseHeader responseHeader = (SearchOffsetResponseHeader) response.readCustomHeader();
    final SearchOffsetRequestHeader requestHeader = (SearchOffsetRequestHeader) request.decodeCommandCustomHeader(SearchOffsetRequestHeader.class);
    long offset = this.brokerController.getMessageStore().getOffsetInQueueByTime(requestHeader.getTopic(), requestHeader.getQueueId(), requestHeader.getTimestamp());
    responseHeader.setOffset(offset);
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SearchOffsetRequestHeader(org.apache.rocketmq.common.protocol.header.SearchOffsetRequestHeader) SearchOffsetResponseHeader(org.apache.rocketmq.common.protocol.header.SearchOffsetResponseHeader)

Example 4 with SearchOffsetRequestHeader

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

the class MQClientAPIImpl method searchOffset.

public long searchOffset(final String addr, final String topic, final int queueId, final long timestamp, final long timeoutMillis) throws RemotingException, MQBrokerException, InterruptedException {
    SearchOffsetRequestHeader requestHeader = new SearchOffsetRequestHeader();
    requestHeader.setTopic(topic);
    requestHeader.setQueueId(queueId);
    requestHeader.setTimestamp(timestamp);
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.SEARCH_OFFSET_BY_TIMESTAMP, requestHeader);
    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis);
    assert response != null;
    switch(response.getCode()) {
        case ResponseCode.SUCCESS:
            {
                SearchOffsetResponseHeader responseHeader = (SearchOffsetResponseHeader) response.decodeCommandCustomHeader(SearchOffsetResponseHeader.class);
                return responseHeader.getOffset();
            }
        default:
            break;
    }
    throw new MQBrokerException(response.getCode(), response.getRemark(), addr);
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SearchOffsetRequestHeader(org.apache.rocketmq.common.protocol.header.SearchOffsetRequestHeader) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) SearchOffsetResponseHeader(org.apache.rocketmq.common.protocol.header.SearchOffsetResponseHeader)

Example 5 with SearchOffsetRequestHeader

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

the class AdminBrokerProcessorTest method testSearchOffsetByTimestamp.

@Test
public void testSearchOffsetByTimestamp() throws Exception {
    messageStore = mock(MessageStore.class);
    when(messageStore.getOffsetInQueueByTime(anyString(), anyInt(), anyLong())).thenReturn(Long.MIN_VALUE);
    when(brokerController.getMessageStore()).thenReturn(messageStore);
    SearchOffsetRequestHeader searchOffsetRequestHeader = new SearchOffsetRequestHeader();
    searchOffsetRequestHeader.setTopic("topic");
    searchOffsetRequestHeader.setQueueId(0);
    searchOffsetRequestHeader.setTimestamp(System.currentTimeMillis());
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.SEARCH_OFFSET_BY_TIMESTAMP, searchOffsetRequestHeader);
    request.addExtField("topic", "topic");
    request.addExtField("queueId", "0");
    request.addExtField("timestamp", System.currentTimeMillis() + "");
    RemotingCommand response = adminBrokerProcessor.processRequest(handlerContext, request);
    assertThat(response.getCode()).isEqualTo(ResponseCode.SUCCESS);
}
Also used : DefaultMessageStore(org.apache.rocketmq.store.DefaultMessageStore) MessageStore(org.apache.rocketmq.store.MessageStore) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SearchOffsetRequestHeader(org.apache.rocketmq.common.protocol.header.SearchOffsetRequestHeader) Test(org.junit.Test)

Aggregations

SearchOffsetRequestHeader (org.apache.rocketmq.common.protocol.header.SearchOffsetRequestHeader)5 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)5 SearchOffsetResponseHeader (org.apache.rocketmq.common.protocol.header.SearchOffsetResponseHeader)4 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)2 DefaultMessageStore (org.apache.rocketmq.store.DefaultMessageStore)1 MessageStore (org.apache.rocketmq.store.MessageStore)1 Test (org.junit.Test)1