Search in sources :

Example 11 with FindBrokerResult

use of org.apache.rocketmq.client.impl.FindBrokerResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class DefaultMQPushConsumerTest method init.

@Before
public void init() throws Exception {
    consumerGroup = "FooBarGroup" + System.currentTimeMillis();
    pushConsumer = new DefaultMQPushConsumer(consumerGroup);
    pushConsumer.setNamesrvAddr("127.0.0.1:9876");
    pushConsumer.setPullInterval(60 * 1000);
    pushConsumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            return null;
        }
    });
    DefaultMQPushConsumerImpl pushConsumerImpl = pushConsumer.getDefaultMQPushConsumerImpl();
    rebalancePushImpl = spy(new RebalancePushImpl(pushConsumer.getDefaultMQPushConsumerImpl()));
    Field field = DefaultMQPushConsumerImpl.class.getDeclaredField("rebalanceImpl");
    field.setAccessible(true);
    field.set(pushConsumerImpl, rebalancePushImpl);
    pushConsumer.subscribe(topic, "*");
    pushConsumer.start();
    mQClientFactory = spy(pushConsumerImpl.getmQClientFactory());
    field = DefaultMQPushConsumerImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(pushConsumerImpl, mQClientFactory);
    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQClientAPIImpl);
    pullAPIWrapper = spy(new PullAPIWrapper(mQClientFactory, consumerGroup, false));
    field = DefaultMQPushConsumerImpl.class.getDeclaredField("pullAPIWrapper");
    field.setAccessible(true);
    field.set(pushConsumerImpl, pullAPIWrapper);
    pushConsumer.getDefaultMQPushConsumerImpl().getRebalanceImpl().setmQClientFactory(mQClientFactory);
    mQClientFactory.registerConsumer(consumerGroup, pushConsumerImpl);
    when(mQClientFactory.getMQClientAPIImpl().pullMessage(anyString(), any(PullMessageRequestHeader.class), anyLong(), any(CommunicationMode.class), nullable(PullCallback.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            PullMessageRequestHeader requestHeader = mock.getArgument(1);
            MessageClientExt messageClientExt = new MessageClientExt();
            messageClientExt.setTopic(topic);
            messageClientExt.setQueueId(0);
            messageClientExt.setMsgId("123");
            messageClientExt.setBody(new byte[] { 'a' });
            messageClientExt.setOffsetMsgId("234");
            messageClientExt.setBornHost(new InetSocketAddress(8080));
            messageClientExt.setStoreHost(new InetSocketAddress(8080));
            PullResult pullResult = createPullResult(requestHeader, PullStatus.FOUND, Collections.<MessageExt>singletonList(messageClientExt));
            ((PullCallback) mock.getArgument(4)).onSuccess(pullResult);
            return pullResult;
        }
    });
    doReturn(new FindBrokerResult("127.0.0.1:10911", false)).when(mQClientFactory).findBrokerAddressInSubscribe(anyString(), anyLong(), anyBoolean());
    doReturn(Collections.singletonList(mQClientFactory.getClientId())).when(mQClientFactory).findConsumerIdList(anyString(), anyString());
    Set<MessageQueue> messageQueueSet = new HashSet<MessageQueue>();
    messageQueueSet.add(createPullRequest().getMessageQueue());
    pushConsumer.getDefaultMQPushConsumerImpl().updateTopicSubscribeInfo(topic, messageQueueSet);
    doReturn(123L).when(rebalancePushImpl).computePullFromWhere(any(MessageQueue.class));
}
Also used : MessageClientExt(org.apache.rocketmq.common.message.MessageClientExt) CommunicationMode(org.apache.rocketmq.client.impl.CommunicationMode) InetSocketAddress(java.net.InetSocketAddress) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult) ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) DefaultMQPushConsumerImpl(org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl) Field(java.lang.reflect.Field) RebalancePushImpl(org.apache.rocketmq.client.impl.consumer.RebalancePushImpl) HashSet(java.util.HashSet) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) PullAPIWrapper(org.apache.rocketmq.client.impl.consumer.PullAPIWrapper) PullMessageRequestHeader(org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MQClientInstance(org.apache.rocketmq.client.impl.factory.MQClientInstance) Before(org.junit.Before)

Example 12 with FindBrokerResult

use of org.apache.rocketmq.client.impl.FindBrokerResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class RemoteBrokerOffsetStoreTest method init.

@Before
public void init() {
    System.setProperty("rocketmq.client.localOffsetStoreDir", System.getProperty("java.io.tmpdir") + ".rocketmq_offsets");
    String clientId = new ClientConfig().buildMQClientId() + "#TestNamespace" + System.currentTimeMillis();
    when(mQClientFactory.getClientId()).thenReturn(clientId);
    when(mQClientFactory.findBrokerAddressInAdmin(brokerName)).thenReturn(new FindBrokerResult("127.0.0.1", false));
    when(mQClientFactory.getMQClientAPIImpl()).thenReturn(mqClientAPI);
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ClientConfig(org.apache.rocketmq.client.ClientConfig) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult) Before(org.junit.Before)

Example 13 with FindBrokerResult

use of org.apache.rocketmq.client.impl.FindBrokerResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class RemoteBrokerOffsetStore method fetchConsumeOffsetFromBroker.

// 根据broker name获取broker地址,之后根据topic,group name和queue id获取消费的位置
private long fetchConsumeOffsetFromBroker(MessageQueue mq) 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) {
        QueryConsumerOffsetRequestHeader requestHeader = new QueryConsumerOffsetRequestHeader();
        requestHeader.setTopic(mq.getTopic());
        requestHeader.setConsumerGroup(this.groupName);
        requestHeader.setQueueId(mq.getQueueId());
        return this.mQClientFactory.getMQClientAPIImpl().queryConsumerOffset(findBrokerResult.getBrokerAddr(), requestHeader, 1000 * 5);
    } else {
        throw new MQClientException("The broker[" + mq.getBrokerName() + "] not exist", null);
    }
}
Also used : QueryConsumerOffsetRequestHeader(org.apache.rocketmq.common.protocol.header.QueryConsumerOffsetRequestHeader) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 14 with FindBrokerResult

use of org.apache.rocketmq.client.impl.FindBrokerResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

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) {
        // 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);
    }
}
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 15 with FindBrokerResult

use of org.apache.rocketmq.client.impl.FindBrokerResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class RebalanceImpl method lock.

public boolean lock(final MessageQueue mq) {
    FindBrokerResult findBrokerResult = this.mQClientFactory.findBrokerAddressInSubscribe(mq.getBrokerName(), MixAll.MASTER_ID, true);
    if (findBrokerResult != null) {
        LockBatchRequestBody requestBody = new LockBatchRequestBody();
        requestBody.setConsumerGroup(this.consumerGroup);
        requestBody.setClientId(this.mQClientFactory.getClientId());
        requestBody.getMqSet().add(mq);
        try {
            Set<MessageQueue> lockedMq = this.mQClientFactory.getMQClientAPIImpl().lockBatchMQ(findBrokerResult.getBrokerAddr(), requestBody, 1000);
            for (MessageQueue mmqq : lockedMq) {
                ProcessQueue processQueue = this.processQueueTable.get(mmqq);
                if (processQueue != null) {
                    processQueue.setLocked(true);
                    processQueue.setLastLockTimestamp(System.currentTimeMillis());
                }
            }
            boolean lockOK = lockedMq.contains(mq);
            log.info("the message queue lock {}, {} {}", lockOK ? "OK" : "Failed", this.consumerGroup, mq);
            return lockOK;
        } catch (Exception e) {
            log.error("lockBatchMQ exception, " + mq, e);
        }
    }
    return false;
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) LockBatchRequestBody(org.apache.rocketmq.common.protocol.body.LockBatchRequestBody) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult)

Aggregations

FindBrokerResult (org.apache.rocketmq.client.impl.FindBrokerResult)21 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)8 HashSet (java.util.HashSet)6 MQClientException (org.apache.rocketmq.client.exception.MQClientException)6 Before (org.junit.Before)6 Field (java.lang.reflect.Field)4 Set (java.util.Set)4 PullAPIWrapper (org.apache.rocketmq.client.impl.consumer.PullAPIWrapper)4 MQClientInstance (org.apache.rocketmq.client.impl.factory.MQClientInstance)4 LockBatchRequestBody (org.apache.rocketmq.common.protocol.body.LockBatchRequestBody)4 UnlockBatchRequestBody (org.apache.rocketmq.common.protocol.body.UnlockBatchRequestBody)4 PullMessageRequestHeader (org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 InetSocketAddress (java.net.InetSocketAddress)2 Entry (java.util.Map.Entry)2 ClientConfig (org.apache.rocketmq.client.ClientConfig)2 PullResult (org.apache.rocketmq.client.consumer.PullResult)2