luchunliang commented on a change in pull request #2186:
URL: https://github.com/apache/incubator-inlong/pull/2186#discussion_r787414603



##########
File path: 
inlong-sdk/sort-sdk/src/main/java/org/apache/inlong/sdk/sort/impl/InLongTopicManagerImpl.java
##########
@@ -69,26 +76,25 @@ private void updateToBeSelectFetchers(Collection<String> c) 
{
 
     @Override
     public InLongTopicFetcher addFetcher(InLongTopic inLongTopic) {
+
         try {
             InLongTopicFetcher result = 
fetchers.get(inLongTopic.getTopicKey());
             if (result == null) {
-                InLongTopicFetcher inLongTopicFetcher = new 
InLongPulsarFetcherImpl(inLongTopic, context);
+                // 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;
-                    inLongTopicFetcher.close();
+                    if (inLongTopicFetcher != null) {
+                        inLongTopicFetcher.close();

Review comment:
       It is not good that code close fetcher after putting fail.
   It is better by the following flow.
   1. new object
   2. put object
   3. if put success, init object.

##########
File path: 
inlong-sdk/sort-sdk/src/main/java/org/apache/inlong/sdk/sort/entity/InLongTopic.java
##########
@@ -26,6 +27,7 @@
     private int partitionId;
     //pulsar,kafka,tube
     private String topicType;
+    private Map<String, Object> properties;

Review comment:
       Map<String, Object> properties
   It is not fit between variable name(properties) and type.
   java.util.Properties is better for variable name(properties) .

##########
File path: 
inlong-sdk/sort-sdk/src/main/java/org/apache/inlong/sdk/sort/impl/InLongTopicManagerImpl.java
##########
@@ -100,6 +106,25 @@ public InLongTopicFetcher addFetcher(InLongTopic 
inLongTopic) {
         }
     }
 
+    /**
+     * create fetcher (pulsar,tube,kafka)
+     *
+     * @param inLongTopic {@link InLongTopic}
+     * @return {@link InLongTopicFetcher}
+     */
+    private InLongTopicFetcher createInLongTopicFetcher(InLongTopic 
inLongTopic) {
+        if 
(InlongTopicTypeEnum.PULSAR.getName().equals(inLongTopic.getTopicType())) {
+            logger.info("the topic is pulsar {}", inLongTopic);
+            return new InLongPulsarFetcherImpl(inLongTopic, context);
+        } else if 
(InlongTopicTypeEnum.TUBE.getName().equals(inLongTopic.getTopicType())) {

Review comment:
       Is it better to use switch/case replacing if/else.

##########
File path: 
inlong-sdk/sort-sdk/src/main/java/org/apache/inlong/sdk/sort/impl/tube/InLongTubeFetcherImpl.java
##########
@@ -0,0 +1,291 @@
+/*
+ * 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.impl.tube;
+
+import com.google.common.base.Splitter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeSet;
+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.SysConstants;
+import org.apache.inlong.sdk.sort.entity.InLongMessage;
+import org.apache.inlong.sdk.sort.entity.InLongTopic;
+import org.apache.inlong.sdk.sort.entity.MessageRecord;
+import org.apache.inlong.sdk.sort.util.StringUtil;
+import org.apache.inlong.tubemq.client.config.ConsumerConfig;
+import org.apache.inlong.tubemq.client.config.TubeClientConfig;
+import org.apache.inlong.tubemq.client.consumer.ConsumerResult;
+import org.apache.inlong.tubemq.client.consumer.PullMessageConsumer;
+import org.apache.inlong.tubemq.corebase.Message;
+import org.apache.inlong.tubemq.corebase.TErrCodeConstants;
+import org.apache.pulsar.shade.org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class InLongTubeFetcherImpl extends InLongTopicFetcher {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(InLongTubeFetcherImpl.class);
+    private PullMessageConsumer messageConsumer;
+
+    private volatile boolean stopConsume = false;
+    private volatile Thread fetchThread;
+    private long sleepTime = 0L;
+    private int emptyPollTimes = 0;
+
+    public InLongTubeFetcherImpl(InLongTopic inLongTopic, ClientContext 
context) {
+        super(inLongTopic, context);
+    }
+
+    @Override
+    public boolean init(Object object) {
+        TubeConsumerCreater tubeConsumerCreater = (TubeConsumerCreater) object;
+        TubeClientConfig tubeClientConfig = 
tubeConsumerCreater.getTubeClientConfig();
+        try {
+            ConsumerConfig consumerConfig = new 
ConsumerConfig(tubeClientConfig.getMasterInfo(),
+                    context.getConfig().getSortTaskId());
+
+            messageConsumer = 
tubeConsumerCreater.getMessageSessionFactory().createPullConsumer(consumerConfig);
+            if (messageConsumer != null) {
+                TreeSet<String> filters = null;
+                if (inLongTopic.getProperties() != null && 
inLongTopic.getProperties().containsKey(
+                        SysConstants.TUBE_TOPIC_FILTER_KEY)) {
+                    filters = (TreeSet<String>) 
inLongTopic.getProperties().get(SysConstants.TUBE_TOPIC_FILTER_KEY);
+
+                }
+                messageConsumer.subscribe(inLongTopic.getTopic(), filters);
+                messageConsumer.completeSubscribe();
+
+                String threadName = "sort_sdk_fetch_thread_" + StringUtil
+                        .formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");

Review comment:
       It is not good that StringUtil support date format logic.

##########
File path: 
inlong-sdk/sort-sdk/src/main/java/org/apache/inlong/sdk/sort/impl/InLongTopicManagerImpl.java
##########
@@ -152,13 +176,15 @@ public boolean clean() {
             }
 
             closeFetcher();
-            result = true;
-            logger.info("close finished {} {}", sortTaskId, result);
+            closePulsarClient();
+            closeTubeSessionFactory();

Review comment:
       Is it better to move cache difference to InLongTopicFetcher instance?

##########
File path: 
inlong-sdk/sort-sdk/src/main/java/org/apache/inlong/sdk/sort/impl/tube/InLongTubeFetcherImpl.java
##########
@@ -0,0 +1,291 @@
+/*
+ * 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.impl.tube;
+
+import com.google.common.base.Splitter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeSet;
+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.SysConstants;
+import org.apache.inlong.sdk.sort.entity.InLongMessage;
+import org.apache.inlong.sdk.sort.entity.InLongTopic;
+import org.apache.inlong.sdk.sort.entity.MessageRecord;
+import org.apache.inlong.sdk.sort.util.StringUtil;
+import org.apache.inlong.tubemq.client.config.ConsumerConfig;
+import org.apache.inlong.tubemq.client.config.TubeClientConfig;
+import org.apache.inlong.tubemq.client.consumer.ConsumerResult;
+import org.apache.inlong.tubemq.client.consumer.PullMessageConsumer;
+import org.apache.inlong.tubemq.corebase.Message;
+import org.apache.inlong.tubemq.corebase.TErrCodeConstants;
+import org.apache.pulsar.shade.org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class InLongTubeFetcherImpl extends InLongTopicFetcher {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(InLongTubeFetcherImpl.class);
+    private PullMessageConsumer messageConsumer;
+
+    private volatile boolean stopConsume = false;
+    private volatile Thread fetchThread;
+    private long sleepTime = 0L;
+    private int emptyPollTimes = 0;
+
+    public InLongTubeFetcherImpl(InLongTopic inLongTopic, ClientContext 
context) {
+        super(inLongTopic, context);
+    }
+
+    @Override
+    public boolean init(Object object) {
+        TubeConsumerCreater tubeConsumerCreater = (TubeConsumerCreater) object;
+        TubeClientConfig tubeClientConfig = 
tubeConsumerCreater.getTubeClientConfig();
+        try {
+            ConsumerConfig consumerConfig = new 
ConsumerConfig(tubeClientConfig.getMasterInfo(),
+                    context.getConfig().getSortTaskId());
+
+            messageConsumer = 
tubeConsumerCreater.getMessageSessionFactory().createPullConsumer(consumerConfig);
+            if (messageConsumer != null) {
+                TreeSet<String> filters = null;
+                if (inLongTopic.getProperties() != null && 
inLongTopic.getProperties().containsKey(
+                        SysConstants.TUBE_TOPIC_FILTER_KEY)) {
+                    filters = (TreeSet<String>) 
inLongTopic.getProperties().get(SysConstants.TUBE_TOPIC_FILTER_KEY);
+
+                }
+                messageConsumer.subscribe(inLongTopic.getTopic(), filters);
+                messageConsumer.completeSubscribe();
+
+                String threadName = "sort_sdk_fetch_thread_" + StringUtil
+                        .formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
+                this.fetchThread = new Thread(new Fetcher(), threadName);
+                this.fetchThread.start();

Review comment:
       fetch Thread can use thread pool.
   One topic has one thread, the thread is too much.




-- 
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]


Reply via email to