You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2022/08/22 11:27:31 UTC

[GitHub] [rocketmq] ShannonDing commented on a diff in pull request #4834: [ISSUE #4832] Remove innerProducer and innerConsumer in EscapeBridge

ShannonDing commented on code in PR #4834:
URL: https://github.com/apache/rocketmq/pull/4834#discussion_r951324117


##########
broker/src/main/java/org/apache/rocketmq/broker/topic/TopicRouteInfoManager.java:
##########
@@ -0,0 +1,276 @@
+/*
+ * 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.broker.topic;
+
+import com.google.common.collect.Sets;
+import java.util.Map;
+import org.apache.rocketmq.broker.BrokerController;
+import org.apache.rocketmq.client.exception.MQBrokerException;
+import org.apache.rocketmq.client.impl.factory.MQClientInstance;
+import org.apache.rocketmq.client.impl.producer.TopicPublishInfo;
+import org.apache.rocketmq.common.MixAll;
+import org.apache.rocketmq.common.constant.LoggerName;
+import org.apache.rocketmq.common.message.MessageQueue;
+import org.apache.rocketmq.common.protocol.NamespaceUtil;
+import org.apache.rocketmq.common.protocol.ResponseCode;
+import org.apache.rocketmq.common.protocol.route.BrokerData;
+import org.apache.rocketmq.common.protocol.route.TopicRouteData;
+import org.apache.rocketmq.logging.InternalLogger;
+import org.apache.rocketmq.logging.InternalLoggerFactory;
+import org.apache.rocketmq.remoting.exception.RemotingException;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+public class TopicRouteInfoManager {
+
+    private static final long SEND_TIMEOUT = 3000L;
+    private static final long LOCK_TIMEOUT_MILLIS = 3000L;
+    private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.BROKER_LOGGER_NAME);
+
+    private final Lock lockNamesrv = new ReentrantLock();
+    private final ConcurrentMap<String/* Topic */, TopicRouteData> topicRouteTable = new ConcurrentHashMap<>();
+    private final ConcurrentMap<String/* Broker Name */, HashMap<Long/* brokerId */, String/* address */>> brokerAddrTable =
+        new ConcurrentHashMap<>();
+    private final ConcurrentMap<String/* topic */, TopicPublishInfo> topicPublishInfoTable = new ConcurrentHashMap<>();
+
+    private final ConcurrentHashMap<String, Set<MessageQueue>> topicSubscribeInfoTable = new ConcurrentHashMap<>();
+
+    private ScheduledExecutorService scheduledExecutorService;
+    private BrokerController brokerController;
+
+    public TopicRouteInfoManager(BrokerController brokerController) {
+        this.brokerController = brokerController;
+    }
+
+    public void start() {
+        this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
+            @Override
+            public Thread newThread(Runnable r) {
+                return new Thread(r, "TopicRouteInfoManagerScheduledThread");
+            }
+        });
+
+        this.scheduledExecutorService.scheduleAtFixedRate(() -> {
+            try {
+                updateTopicRouteInfoFromNameServer();
+            } catch (Exception e) {
+                log.error("ScheduledTask: failed to pull TopicRouteData from NameServer", e);
+            }
+        }, 1000, this.brokerController.getBrokerConfig().getLoadBalancePollNameServerInterval(), TimeUnit.MILLISECONDS);
+    }
+
+    private void updateTopicRouteInfoFromNameServer() {
+        final Set<String> topicSetForAssignmentMgr = this.brokerController.getAssignmentManager().getTopicSetForAssignment();
+        final Set<String> topicSetForEscapeBridge = this.topicRouteTable.keySet();
+        final Set<String> topicsAll = Sets.union(topicSetForAssignmentMgr, topicSetForEscapeBridge);
+
+        for (String topic : topicsAll) {
+            boolean isNeedUpdatePublishInfo = topicSetForEscapeBridge.contains(topic);
+            boolean isNeedUpdateSubscribeInfo = topicSetForAssignmentMgr.contains(topic);
+            updateTopicRouteInfoFromNameServer(topic, isNeedUpdatePublishInfo, isNeedUpdateSubscribeInfo);
+        }
+    }
+
+    public void updateTopicRouteInfoFromNameServer(String topic, boolean isNeedUpdatePublishInfo,
+        boolean isNeedUpdateSubscribeInfo) {
+        try {
+            if (this.lockNamesrv.tryLock(LOCK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) {
+                try {
+                    final TopicRouteData topicRouteData = this.brokerController.getBrokerOuterAPI()
+                        .getTopicRouteInfoFromNameServer(topic, SEND_TIMEOUT);
+                    if (null == topicRouteData) {
+                        log.warn("TopicRouteInfoManager: updateTopicRouteInfoFromNameServer, getTopicRouteInfoFromNameServer return null, Topic: {}.", topic);
+                        return;
+                    }
+
+                    if (isNeedUpdateSubscribeInfo) {
+                        this.updateSubscribeInfoTable(topicRouteData, topic);
+                    }
+
+                    if (isNeedUpdatePublishInfo) {
+                        this.updateTopicRouteTable(topic, topicRouteData);
+                    }
+                } catch (RemotingException e) {
+                    log.error("updateTopicRouteInfoFromNameServer Exception", e);
+                } catch (MQBrokerException e) {
+                    log.error("updateTopicRouteInfoFromNameServer Exception", e);
+                    if (!NamespaceUtil.isRetryTopic(topic)
+                        && ResponseCode.TOPIC_NOT_EXIST == e.getResponseCode()) {
+                        // clean no used topic
+                        cleanNoneRouteTopic(topic);
+                    }
+                } finally {
+                    this.lockNamesrv.unlock();
+                }
+            }
+        } catch (InterruptedException e) {
+            log.warn("updateTopicRouteInfoFromNameServer Exception", e);
+        }
+    }
+
+    private boolean updateTopicRouteTable(String topic, TopicRouteData topicRouteData) {
+        TopicRouteData old = this.topicRouteTable.get(topic);
+        boolean changed = this.topicRouteDataIsChange(old, topicRouteData);
+        if (!changed) {
+            changed = this.isNeedUpdateTopicRouteInfo(topic);
+        } else {
+            log.info("the topic[{}] route info changed, old[{}] ,new[{}]", topic, old, topicRouteData);
+        }
+
+        if (!changed) {
+            return false;

Review Comment:
   
   
   why not
   ```
         if (!changed) {
               changed = this.isNeedUpdateTopicRouteInfo(topic);
            if (!changed) {
                 return false;
              }
           } else {
               log.info("the topic[{}] route info changed, old[{}] ,new[{}]", topic, old, topicRouteData);
           }
   
      
   ```



-- 
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: dev-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org