healchow commented on code in PR #5512:
URL: https://github.com/apache/inlong/pull/5512#discussion_r944317301


##########
inlong-sdk/sort-sdk/src/main/java/org/apache/inlong/sdk/sort/manager/InlongSingleTopicManager.java:
##########
@@ -0,0 +1,495 @@
+/*
+ * 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.inlong.sdk.sort.manager;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
+import org.apache.inlong.sdk.sort.api.ClientContext;
+import org.apache.inlong.sdk.sort.api.InLongTopicFetcher;
+import org.apache.inlong.sdk.sort.api.InlongTopicManager;
+import org.apache.inlong.sdk.sort.api.InlongTopicTypeEnum;
+import org.apache.inlong.sdk.sort.api.QueryConsumeConfig;
+import org.apache.inlong.sdk.sort.entity.ConsumeConfig;
+import org.apache.inlong.sdk.sort.entity.InLongTopic;
+import org.apache.inlong.sdk.sort.impl.kafka.InLongKafkaFetcherImpl;
+import org.apache.inlong.sdk.sort.impl.pulsar.InLongPulsarFetcherImpl;
+import org.apache.inlong.sdk.sort.impl.tube.InLongTubeFetcherImpl;
+import org.apache.inlong.sdk.sort.impl.tube.TubeConsumerCreater;
+import org.apache.inlong.sdk.sort.util.PeriodicTask;
+import org.apache.inlong.sdk.sort.util.StringUtil;
+import org.apache.inlong.tubemq.client.config.TubeClientConfig;
+import org.apache.inlong.tubemq.client.factory.MessageSessionFactory;
+import org.apache.inlong.tubemq.client.factory.TubeSingleSessionFactory;
+import org.apache.pulsar.client.api.AuthenticationFactory;
+import org.apache.pulsar.client.api.PulsarClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Inlong manager that maintain the single topic fetchers.
+ * It is suitable to the cases that each topic has its own configurations.
+ * And each consumer only consume the very one topic.
+ */
+public class InlongSingleTopicManager extends InlongTopicManager {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(InlongSingleTopicManager.class);
+
+    private final ConcurrentHashMap<String, InLongTopicFetcher> fetchers = new 
ConcurrentHashMap<>();
+    private final ConcurrentHashMap<String, PulsarClient> pulsarClients = new 
ConcurrentHashMap<>();
+    private final ConcurrentHashMap<String, TubeConsumerCreater> tubeFactories 
= new ConcurrentHashMap<>();
+
+    private final PeriodicTask updateMetaDataWorker;
+    private volatile List<String> toBeSelectFetchers = new ArrayList<>();
+    private boolean stopAssign = false;
+
+    public InlongSingleTopicManager(ClientContext context, QueryConsumeConfig 
queryConsumeConfig) {
+        super(context, queryConsumeConfig);
+        updateMetaDataWorker = new 
UpdateMetaDataThread(context.getConfig().getUpdateMetaDataIntervalSec(),
+                TimeUnit.SECONDS);
+        String threadName = "sortsdk_inlongtopic_manager_" + 
context.getConfig().getSortTaskId()
+                + "_" + StringUtil.formatDate(new Date(), "yyyy-MM-dd 
HH:mm:ss");
+        updateMetaDataWorker.start(threadName);
+    }
+
+    private void updateToBeSelectFetchers(Collection<String> c) {
+        toBeSelectFetchers = new ArrayList<>(c);
+    }
+
+    private boolean initFetcher(InLongTopicFetcher fetcher, InLongTopic 
inLongTopic) {
+        if 
(InlongTopicTypeEnum.PULSAR.getName().equalsIgnoreCase(inLongTopic.getTopicType()))
 {
+            LOGGER.info("create fetcher topic is pulsar {}", inLongTopic);
+            return 
fetcher.init(pulsarClients.get(inLongTopic.getInLongCluster().getClusterId()));
+        } else if 
(InlongTopicTypeEnum.KAFKA.getName().equalsIgnoreCase(inLongTopic.getTopicType()))
 {
+            LOGGER.info("create fetcher topic is kafka {}", inLongTopic);
+            return 
fetcher.init(inLongTopic.getInLongCluster().getBootstraps());
+        } else if 
(InlongTopicTypeEnum.TUBE.getName().equalsIgnoreCase(inLongTopic.getTopicType()))
 {
+            LOGGER.info("create fetcher topic is tube {}", inLongTopic);
+            return 
fetcher.init(tubeFactories.get(inLongTopic.getInLongCluster().getClusterId()));
+        } else {
+            LOGGER.error("create fetcher topic type not support " + 
inLongTopic.getTopicType());
+            return false;
+        }
+    }
+
+    @Override
+    public InLongTopicFetcher addFetcher(InLongTopic inLongTopic) {
+
+        try {
+            InLongTopicFetcher result = 
fetchers.get(inLongTopic.getTopicKey());
+            if (result == null) {
+                // create fetcher (pulsar,tube,kafka)
+                InLongTopicFetcher inLongTopicFetcher = 
createInLongTopicFetcher(inLongTopic);
+                InLongTopicFetcher preValue = 
fetchers.putIfAbsent(inLongTopic.getTopicKey(), inLongTopicFetcher);
+                LOGGER.info("addFetcher :{}", inLongTopic.getTopicKey());
+                if (preValue != null) {
+                    result = preValue;
+                    if (inLongTopicFetcher != null) {
+                        inLongTopicFetcher.close();
+                    }
+                    LOGGER.info("addFetcher create same fetcher {}", 
inLongTopic);
+                } else {
+                    result = inLongTopicFetcher;
+                    if (result != null
+                            && !initFetcher(result, inLongTopic)) {

Review Comment:
   Those two lines can be merged into one line.



-- 
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: commits-unsubscr...@inlong.apache.org

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

Reply via email to