Search in sources :

Example 1 with AllocateMessageQueueAveragely

use of org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely in project rocketmq by apache.

the class AllocateMQSubCommand method execute.

@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    DefaultMQAdminExt adminExt = new DefaultMQAdminExt(rpcHook);
    adminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    try {
        adminExt.start();
        String topic = commandLine.getOptionValue('t').trim();
        String ips = commandLine.getOptionValue('i').trim();
        final String[] split = ips.split(",");
        final List<String> ipList = new LinkedList<String>();
        for (String ip : split) {
            ipList.add(ip);
        }
        final TopicRouteData topicRouteData = adminExt.examineTopicRouteInfo(topic);
        final Set<MessageQueue> mqs = MQClientInstance.topicRouteData2TopicSubscribeInfo(topic, topicRouteData);
        final AllocateMessageQueueAveragely averagely = new AllocateMessageQueueAveragely();
        RebalanceResult rr = new RebalanceResult();
        for (String i : ipList) {
            final List<MessageQueue> mqResult = averagely.allocate("aa", i, new ArrayList<MessageQueue>(mqs), ipList);
            rr.getResult().put(i, mqResult);
        }
        final String json = RemotingSerializable.toJson(rr, false);
        System.out.printf("%s%n", json);
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        adminExt.shutdown();
    }
}
Also used : AllocateMessageQueueAveragely(org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) LinkedList(java.util.LinkedList) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt)

Example 2 with AllocateMessageQueueAveragely

use of org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely in project rocketmq by apache.

the class RebalancePushImplTest method testMessageQueueChanged_ConsumerRuntimeInfo.

@Test
public void testMessageQueueChanged_ConsumerRuntimeInfo() throws MQClientException {
    RebalancePushImpl rebalancePush = new RebalancePushImpl(consumerGroup, MessageModel.CLUSTERING, new AllocateMessageQueueAveragely(), mqClientInstance, defaultMQPushConsumer);
    init(rebalancePush);
    defaultMQPushConsumer.getDefaultMQPushConsumer().setPullThresholdSizeForQueue(1024);
    defaultMQPushConsumer.getDefaultMQPushConsumer().setPullThresholdForQueue(1024);
    Set<MessageQueue> allocateResultSet = new HashSet<MessageQueue>();
    allocateResultSet.add(new MessageQueue(topic, "BrokerA", 0));
    allocateResultSet.add(new MessageQueue(topic, "BrokerA", 1));
    doRebalanceForcibly(rebalancePush, allocateResultSet);
    defaultMQPushConsumer.setConsumeMessageService(new ConsumeMessageConcurrentlyService(defaultMQPushConsumer, null));
    assertThat(defaultMQPushConsumer.consumerRunningInfo().getProperties().get("pullThresholdSizeForQueue")).isEqualTo("1024");
    assertThat(defaultMQPushConsumer.consumerRunningInfo().getProperties().get("pullThresholdForQueue")).isEqualTo("1024");
    assertThat(defaultMQPushConsumer.consumerRunningInfo().getProperties().get("pullThresholdSizeForTopic")).isEqualTo("-1");
    assertThat(defaultMQPushConsumer.consumerRunningInfo().getProperties().get("pullThresholdForTopic")).isEqualTo("-1");
    defaultMQPushConsumer.getDefaultMQPushConsumer().setPullThresholdSizeForTopic(1024);
    defaultMQPushConsumer.getDefaultMQPushConsumer().setPullThresholdForTopic(1024);
    doRebalanceForcibly(rebalancePush, allocateResultSet);
    assertThat(defaultMQPushConsumer.consumerRunningInfo().getProperties().get("pullThresholdSizeForQueue")).isEqualTo("512");
    assertThat(defaultMQPushConsumer.consumerRunningInfo().getProperties().get("pullThresholdForQueue")).isEqualTo("512");
    assertThat(defaultMQPushConsumer.consumerRunningInfo().getProperties().get("pullThresholdSizeForTopic")).isEqualTo("1024");
    assertThat(defaultMQPushConsumer.consumerRunningInfo().getProperties().get("pullThresholdForTopic")).isEqualTo("1024");
    // Change message queue allocate result
    allocateResultSet.add(new MessageQueue(topic, "BrokerA", 2));
    doRebalanceForcibly(rebalancePush, allocateResultSet);
    assertThat(defaultMQPushConsumer.consumerRunningInfo().getProperties().get("pullThresholdSizeForQueue")).isEqualTo("341");
    assertThat(defaultMQPushConsumer.consumerRunningInfo().getProperties().get("pullThresholdForQueue")).isEqualTo("341");
    assertThat(defaultMQPushConsumer.consumerRunningInfo().getProperties().get("pullThresholdSizeForTopic")).isEqualTo("1024");
    assertThat(defaultMQPushConsumer.consumerRunningInfo().getProperties().get("pullThresholdForTopic")).isEqualTo("1024");
}
Also used : AllocateMessageQueueAveragely(org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with AllocateMessageQueueAveragely

use of org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely in project rocketmq by apache.

the class RebalancePushImplTest method testMessageQueueChanged_CountThreshold.

@Test
public void testMessageQueueChanged_CountThreshold() {
    RebalancePushImpl rebalancePush = new RebalancePushImpl(consumerGroup, MessageModel.CLUSTERING, new AllocateMessageQueueAveragely(), mqClientInstance, defaultMQPushConsumer);
    init(rebalancePush);
    // Just set pullThresholdForQueue
    defaultMQPushConsumer.getDefaultMQPushConsumer().setPullThresholdForQueue(1024);
    Set<MessageQueue> allocateResultSet = new HashSet<MessageQueue>();
    allocateResultSet.add(new MessageQueue(topic, "BrokerA", 0));
    allocateResultSet.add(new MessageQueue(topic, "BrokerA", 1));
    doRebalanceForcibly(rebalancePush, allocateResultSet);
    assertThat(defaultMQPushConsumer.getDefaultMQPushConsumer().getPullThresholdForQueue()).isEqualTo(1024);
    // Set pullThresholdForTopic
    defaultMQPushConsumer.getDefaultMQPushConsumer().setPullThresholdForTopic(1024);
    doRebalanceForcibly(rebalancePush, allocateResultSet);
    assertThat(defaultMQPushConsumer.getDefaultMQPushConsumer().getPullThresholdForQueue()).isEqualTo(512);
    // Change message queue allocate result
    allocateResultSet.add(new MessageQueue(topic, "BrokerA", 2));
    doRebalanceForcibly(rebalancePush, allocateResultSet);
    assertThat(defaultMQPushConsumer.getDefaultMQPushConsumer().getPullThresholdForQueue()).isEqualTo(341);
}
Also used : AllocateMessageQueueAveragely(org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with AllocateMessageQueueAveragely

use of org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely in project rocketmq by apache.

the class RebalancePushImplTest method testMessageQueueChanged_SizeThreshold.

@Test
public void testMessageQueueChanged_SizeThreshold() {
    RebalancePushImpl rebalancePush = new RebalancePushImpl(consumerGroup, MessageModel.CLUSTERING, new AllocateMessageQueueAveragely(), mqClientInstance, defaultMQPushConsumer);
    init(rebalancePush);
    // Just set pullThresholdSizeForQueue
    defaultMQPushConsumer.getDefaultMQPushConsumer().setPullThresholdSizeForQueue(1024);
    Set<MessageQueue> allocateResultSet = new HashSet<MessageQueue>();
    allocateResultSet.add(new MessageQueue(topic, "BrokerA", 0));
    allocateResultSet.add(new MessageQueue(topic, "BrokerA", 1));
    doRebalanceForcibly(rebalancePush, allocateResultSet);
    assertThat(defaultMQPushConsumer.getDefaultMQPushConsumer().getPullThresholdSizeForQueue()).isEqualTo(1024);
    // Set pullThresholdSizeForTopic
    defaultMQPushConsumer.getDefaultMQPushConsumer().setPullThresholdSizeForTopic(1024);
    doRebalanceForcibly(rebalancePush, allocateResultSet);
    assertThat(defaultMQPushConsumer.getDefaultMQPushConsumer().getPullThresholdSizeForQueue()).isEqualTo(512);
    // Change message queue allocate result
    allocateResultSet.add(new MessageQueue(topic, "BrokerA", 2));
    doRebalanceForcibly(rebalancePush, allocateResultSet);
    assertThat(defaultMQPushConsumer.getDefaultMQPushConsumer().getPullThresholdSizeForQueue()).isEqualTo(341);
}
Also used : AllocateMessageQueueAveragely(org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with AllocateMessageQueueAveragely

use of org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely in project canal by alibaba.

the class CanalRocketMQConsumer method connect.

@Override
public void connect() {
    RPCHook rpcHook = null;
    if (null != accessKey && accessKey.length() > 0 && null != secretKey && secretKey.length() > 0) {
        SessionCredentials sessionCredentials = new SessionCredentials();
        sessionCredentials.setAccessKey(accessKey);
        sessionCredentials.setSecretKey(secretKey);
        rpcHook = new AclClientRPCHook(sessionCredentials);
    }
    rocketMQConsumer = new DefaultMQPushConsumer(groupName, rpcHook, new AllocateMessageQueueAveragely(), enableMessageTrace, customizedTraceTopic);
    rocketMQConsumer.setVipChannelEnabled(false);
    if (CLOUD_ACCESS_CHANNEL.equals(this.accessChannel)) {
        rocketMQConsumer.setAccessChannel(AccessChannel.CLOUD);
    }
    if (!StringUtils.isEmpty(this.namespace)) {
        rocketMQConsumer.setNamespace(this.namespace);
    }
    if (!StringUtils.isBlank(nameServer)) {
        rocketMQConsumer.setNamesrvAddr(nameServer);
    }
    if (batchSize != -1) {
        rocketMQConsumer.setConsumeMessageBatchMaxSize(batchSize);
    }
    try {
        if (rocketMQConsumer == null) {
            this.connect();
        }
        rocketMQConsumer.subscribe(this.topic, this.filter);
        rocketMQConsumer.registerMessageListener((MessageListenerOrderly) (messageExts, context) -> {
            context.setAutoCommit(true);
            boolean isSuccess = process(messageExts);
            if (isSuccess) {
                return ConsumeOrderlyStatus.SUCCESS;
            } else {
                return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
            }
        });
        rocketMQConsumer.start();
    } catch (MQClientException ex) {
        logger.error("Start RocketMQ consumer error", ex);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) RPCHook(org.apache.rocketmq.remoting.RPCHook) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) LoggerFactory(org.slf4j.LoggerFactory) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageUtil(com.alibaba.otter.canal.connector.core.util.MessageUtil) CanalMessageSerializerUtil(com.alibaba.otter.canal.connector.core.util.CanalMessageSerializerUtil) ArrayList(java.util.ArrayList) MQClientException(org.apache.rocketmq.client.exception.MQClientException) CanalConstants(com.alibaba.otter.canal.connector.core.config.CanalConstants) RocketMQConstants(com.alibaba.otter.canal.connector.rocketmq.config.RocketMQConstants) CanalMsgConsumer(com.alibaba.otter.canal.connector.core.spi.CanalMsgConsumer) Properties(java.util.Properties) Logger(org.slf4j.Logger) Message(com.alibaba.otter.canal.protocol.Message) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) BlockingQueue(java.util.concurrent.BlockingQueue) SPI(com.alibaba.otter.canal.connector.core.spi.SPI) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) AllocateMessageQueueAveragely(org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely) TimeUnit(java.util.concurrent.TimeUnit) MessageListenerOrderly(org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly) List(java.util.List) SessionCredentials(org.apache.rocketmq.acl.common.SessionCredentials) JSON(com.alibaba.fastjson.JSON) CommonMessage(com.alibaba.otter.canal.connector.core.consumer.CommonMessage) AccessChannel(org.apache.rocketmq.client.AccessChannel) ConsumeOrderlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus) SessionCredentials(org.apache.rocketmq.acl.common.SessionCredentials) AllocateMessageQueueAveragely(org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) RPCHook(org.apache.rocketmq.remoting.RPCHook) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) AclClientRPCHook(org.apache.rocketmq.acl.common.AclClientRPCHook) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Aggregations

AllocateMessageQueueAveragely (org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely)7 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)5 HashSet (java.util.HashSet)3 Test (org.junit.Test)3 LinkedList (java.util.LinkedList)2 AclClientRPCHook (org.apache.rocketmq.acl.common.AclClientRPCHook)2 SessionCredentials (org.apache.rocketmq.acl.common.SessionCredentials)2 DefaultMQPushConsumer (org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)2 TopicRouteData (org.apache.rocketmq.common.protocol.route.TopicRouteData)2 RPCHook (org.apache.rocketmq.remoting.RPCHook)2 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)2 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)2 JSON (com.alibaba.fastjson.JSON)1 CanalConstants (com.alibaba.otter.canal.connector.core.config.CanalConstants)1 CommonMessage (com.alibaba.otter.canal.connector.core.consumer.CommonMessage)1 CanalMsgConsumer (com.alibaba.otter.canal.connector.core.spi.CanalMsgConsumer)1 SPI (com.alibaba.otter.canal.connector.core.spi.SPI)1 CanalMessageSerializerUtil (com.alibaba.otter.canal.connector.core.util.CanalMessageSerializerUtil)1 MessageUtil (com.alibaba.otter.canal.connector.core.util.MessageUtil)1 RocketMQConstants (com.alibaba.otter.canal.connector.rocketmq.config.RocketMQConstants)1