ifplusor commented on a change in pull request #2983: URL: https://github.com/apache/rocketmq/pull/2983#discussion_r730326368
########## File path: client/src/main/java/org/apache/rocketmq/client/impl/consumer/ConsumeMessageStagedConcurrentlyService.java ########## @@ -0,0 +1,872 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.rocketmq.client.impl.consumer; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; +import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus; +import org.apache.rocketmq.client.consumer.listener.ConsumeReturnType; +import org.apache.rocketmq.client.consumer.listener.ConsumeStagedConcurrentlyContext; +import org.apache.rocketmq.client.consumer.listener.MessageListenerStagedConcurrently; +import org.apache.rocketmq.client.consumer.store.ReadOffsetType; +import org.apache.rocketmq.client.consumer.store.StageOffsetStore; +import org.apache.rocketmq.client.hook.ConsumeMessageContext; +import org.apache.rocketmq.client.log.ClientLogger; +import org.apache.rocketmq.client.stat.ConsumerStatsManager; +import org.apache.rocketmq.common.MixAll; +import org.apache.rocketmq.common.ThreadFactoryImpl; +import org.apache.rocketmq.common.UtilAll; +import org.apache.rocketmq.common.concurrent.PriorityConcurrentEngine; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.common.message.MessageAccessor; +import org.apache.rocketmq.common.message.MessageConst; +import org.apache.rocketmq.common.message.MessageExt; +import org.apache.rocketmq.common.message.MessageQueue; +import org.apache.rocketmq.common.protocol.NamespaceUtil; +import org.apache.rocketmq.common.protocol.body.CMResult; +import org.apache.rocketmq.common.protocol.body.ConsumeMessageDirectlyResult; +import org.apache.rocketmq.common.protocol.heartbeat.MessageModel; +import org.apache.rocketmq.common.utils.ThreadUtils; +import org.apache.rocketmq.logging.InternalLogger; +import org.apache.rocketmq.remoting.common.RemotingHelper; + +public class ConsumeMessageStagedConcurrentlyService implements ConsumeMessageService { + private static final String NULL = "null"; + private static final InternalLogger log = ClientLogger.getLog(); + private final static long MAX_TIME_CONSUME_CONTINUOUSLY = + Long.parseLong(System.getProperty("rocketmq.client.maxTimeConsumeContinuously", "60000")); + private final DefaultMQPushConsumerImpl defaultMQPushConsumerImpl; + private final DefaultMQPushConsumer defaultMQPushConsumer; + private final MessageListenerStagedConcurrently messageListener; + private final BlockingQueue<Runnable> consumeRequestQueue; + private final ThreadPoolExecutor dispatchExecutor; + private final ThreadPoolExecutor consumeExecutor; + private final PriorityConcurrentEngine engine; + private final String consumerGroup; + private final MessageQueueLock messageQueueLock = new MessageQueueLock(); + private final ScheduledExecutorService scheduledExecutorService; + private volatile boolean stopped = false; + private final Map<String/*strategyId*/, List<Integer>/*StageDefinition*/> summedStageDefinitionMap; + private final ConcurrentMap<String/*topic*/, ConcurrentMap<String/*strategyId*/, ConcurrentMap<String/*groupId*/, AtomicInteger/*currentStageOffset*/>>> currentStageOffsetMap = new ConcurrentHashMap<>(); + private final int pullBatchSize; + private final StageOffsetStore stageOffsetStore; + + public ConsumeMessageStagedConcurrentlyService(DefaultMQPushConsumerImpl defaultMQPushConsumerImpl, + MessageListenerStagedConcurrently messageListener) { + this.defaultMQPushConsumerImpl = defaultMQPushConsumerImpl; + this.messageListener = messageListener; + this.summedStageDefinitionMap = new ConcurrentHashMap<>(); + this.refreshStageDefinition(); + + this.stageOffsetStore = this.defaultMQPushConsumerImpl.getStageOffsetStore(); + + this.defaultMQPushConsumer = this.defaultMQPushConsumerImpl.getDefaultMQPushConsumer(); + this.consumerGroup = this.defaultMQPushConsumer.getConsumerGroup(); + this.pullBatchSize = this.defaultMQPushConsumer.getPullBatchSize(); + this.consumeRequestQueue = new LinkedBlockingQueue<Runnable>(); + + int consumeThreadMin = this.defaultMQPushConsumer.getConsumeThreadMin(); + int consumeThreadMax = this.defaultMQPushConsumer.getConsumeThreadMax(); + this.dispatchExecutor = new ThreadPoolExecutor( + (int) Math.ceil(consumeThreadMin * 1.0 / this.pullBatchSize), + (int) Math.ceil(consumeThreadMax * 1.0 / this.pullBatchSize), + 1000 * 60, + TimeUnit.MILLISECONDS, + new LinkedBlockingQueue<Runnable>(), + new ThreadFactoryImpl("DispatchMessageThread_")); + // when the number of threads is equal to + // the topic consumeQueue size multiplied by this.pullBatchSize, + // good performance can be obtained + this.consumeExecutor = new ThreadPoolExecutor( + consumeThreadMin, + consumeThreadMax, + 1000 * 60, + TimeUnit.MILLISECONDS, + this.consumeRequestQueue, + new ThreadFactoryImpl("ConsumeMessageThread_")); + engine = new PriorityConcurrentEngine(this.consumeExecutor); + + this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl("ConsumeMessageScheduledThread_")); + } + + private void refreshStageDefinition() { + Map<String, List<Integer>> strategies = messageListener.getStageDefinitionStrategies(); + if (MapUtils.isNotEmpty(strategies)) { + for (Map.Entry<String, List<Integer>> entry : strategies.entrySet()) { + String strategyId = entry.getKey(); + List<Integer> definitions = entry.getValue(); + List<Integer> summedStageDefinitions = new ArrayList<>(); + if (definitions != null) { + int sum = 0; + for (Integer stageDefinition : definitions) { + summedStageDefinitions.add(sum = sum + stageDefinition); + } + } + summedStageDefinitionMap.put(strategyId, summedStageDefinitions); + } + } + } + + @Override + public void start() { + engine.start(); + if (MessageModel.CLUSTERING.equals(ConsumeMessageStagedConcurrentlyService.this.defaultMQPushConsumerImpl.messageModel())) { Review comment: because `start` is member of `ConsumeMessageStagedConcurrentlyService` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
