You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by GitBox <gi...@apache.org> on 2022/03/19 05:34:53 UTC

[GitHub] [incubator-inlong] baomingyu commented on a change in pull request #3227: [INLONG-3159][Audit] Store support TubeMQ

baomingyu commented on a change in pull request #3227:
URL: https://github.com/apache/incubator-inlong/pull/3227#discussion_r830442702



##########
File path: inlong-audit/audit-store/src/main/java/org/apache/inlong/audit/config/MessageQueueConfig.java
##########
@@ -47,4 +48,27 @@
     @Value("${audit.pulsar.client.concurrent.consumer.num:1}")
     private int concurrentConsumerNum = 1;
 
+    @Value("${audit.tube.masterlist}")
+    private String tubeMasterList;

Review comment:
       this is optional config, so need config default value.

##########
File path: inlong-audit/audit-store/src/main/java/org/apache/inlong/audit/service/consume/TubeConsume.java
##########
@@ -0,0 +1,140 @@
+/*
+ * 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.audit.service.consume;
+
+import com.google.common.base.Preconditions;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang.StringUtils;
+import org.apache.inlong.audit.config.MessageQueueConfig;
+import org.apache.inlong.audit.config.StoreConfig;
+import org.apache.inlong.audit.db.dao.AuditDataDao;
+import org.apache.inlong.audit.service.ElasticsearchService;
+import org.apache.inlong.tubemq.client.config.ConsumerConfig;
+import org.apache.inlong.tubemq.client.consumer.ConsumePosition;
+import org.apache.inlong.tubemq.client.consumer.ConsumerResult;
+import org.apache.inlong.tubemq.client.consumer.PullMessageConsumer;
+import org.apache.inlong.tubemq.client.exception.TubeClientException;
+import org.apache.inlong.tubemq.client.factory.TubeMultiSessionFactory;
+import org.apache.inlong.tubemq.corebase.Message;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+
+public class TubeConsume extends BaseConsume {
+
+    private static final Logger LOG = LoggerFactory.getLogger(TubeConsume.class);
+    private PullMessageConsumer pullConsumer;
+    private TubeMultiSessionFactory sessionFactory;
+    private String masterUrl;
+    private String topic;
+    private int fetchThreadCnt = 4;
+
+    public TubeConsume(AuditDataDao auditDataDao, ElasticsearchService esService, StoreConfig storeConfig,
+            MessageQueueConfig mqConfig) {
+        super(auditDataDao, esService, storeConfig, mqConfig);
+    }
+
+    @Override
+    public void start() {
+        masterUrl = mqConfig.getTubeMasterList();
+        Preconditions.checkArgument(StringUtils.isNotEmpty(masterUrl), "no tube masterUrlList specified");
+        topic = mqConfig.getTubeTopic();
+        Preconditions.checkArgument(StringUtils.isNotEmpty(topic), "no tube topic specified");
+        fetchThreadCnt = mqConfig.getTubeThreadNum();
+
+        initConsumer();
+
+        Thread[] fetchRunners = new Thread[fetchThreadCnt];
+        for (int i = 0; i < fetchThreadCnt; i++) {
+            fetchRunners[i] = new Thread(new Fetcher(pullConsumer, topic), "TubeConsume_Fetcher_Thread_" + i);
+            fetchRunners[i].start();
+        }
+    }
+
+    private void initConsumer() {
+        LOG.info("init tube consumer, topic:{}, masterList:{}", topic, masterUrl);
+        ConsumerConfig consumerConfig = new ConsumerConfig(masterUrl, mqConfig.getTubeConsumerGroupName());
+        consumerConfig.setConsumePosition(ConsumePosition.CONSUMER_FROM_LATEST_OFFSET);
+        try {
+            sessionFactory = new TubeMultiSessionFactory(consumerConfig);
+            pullConsumer = sessionFactory.createPullConsumer(consumerConfig);
+            pullConsumer.subscribe(topic, null);
+            pullConsumer.completeSubscribe();
+        } catch (TubeClientException e) {
+            LOG.error("init tube consumer error {}", e.getMessage());
+        }
+
+    }
+
+    public class Fetcher implements Runnable {
+
+        private final PullMessageConsumer pullMessageConsumer;
+        private String topic;
+
+        public Fetcher(PullMessageConsumer pullMessageConsumer, String topic) {
+            this.pullMessageConsumer = pullMessageConsumer;
+            this.topic = topic;
+        }
+
+        @Override
+        public void run() {
+            ConsumerResult csmResult;
+
+            // wait partition status ready
+            while (true) {
+                if (pullMessageConsumer.isPartitionsReady(5000) || pullMessageConsumer.isShutdown()) {

Review comment:
       add this stat log.

##########
File path: inlong-audit/audit-store/src/main/java/org/apache/inlong/audit/service/consume/TubeConsume.java
##########
@@ -0,0 +1,140 @@
+/*
+ * 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.audit.service.consume;
+
+import com.google.common.base.Preconditions;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang.StringUtils;
+import org.apache.inlong.audit.config.MessageQueueConfig;
+import org.apache.inlong.audit.config.StoreConfig;
+import org.apache.inlong.audit.db.dao.AuditDataDao;
+import org.apache.inlong.audit.service.ElasticsearchService;
+import org.apache.inlong.tubemq.client.config.ConsumerConfig;
+import org.apache.inlong.tubemq.client.consumer.ConsumePosition;
+import org.apache.inlong.tubemq.client.consumer.ConsumerResult;
+import org.apache.inlong.tubemq.client.consumer.PullMessageConsumer;
+import org.apache.inlong.tubemq.client.exception.TubeClientException;
+import org.apache.inlong.tubemq.client.factory.TubeMultiSessionFactory;
+import org.apache.inlong.tubemq.corebase.Message;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+
+public class TubeConsume extends BaseConsume {
+
+    private static final Logger LOG = LoggerFactory.getLogger(TubeConsume.class);
+    private PullMessageConsumer pullConsumer;
+    private TubeMultiSessionFactory sessionFactory;
+    private String masterUrl;
+    private String topic;
+    private int fetchThreadCnt = 4;
+
+    public TubeConsume(AuditDataDao auditDataDao, ElasticsearchService esService, StoreConfig storeConfig,
+            MessageQueueConfig mqConfig) {
+        super(auditDataDao, esService, storeConfig, mqConfig);
+    }
+
+    @Override
+    public void start() {
+        masterUrl = mqConfig.getTubeMasterList();
+        Preconditions.checkArgument(StringUtils.isNotEmpty(masterUrl), "no tube masterUrlList specified");
+        topic = mqConfig.getTubeTopic();
+        Preconditions.checkArgument(StringUtils.isNotEmpty(topic), "no tube topic specified");
+        fetchThreadCnt = mqConfig.getTubeThreadNum();
+
+        initConsumer();
+
+        Thread[] fetchRunners = new Thread[fetchThreadCnt];
+        for (int i = 0; i < fetchThreadCnt; i++) {
+            fetchRunners[i] = new Thread(new Fetcher(pullConsumer, topic), "TubeConsume_Fetcher_Thread_" + i);
+            fetchRunners[i].start();
+        }
+    }
+
+    private void initConsumer() {
+        LOG.info("init tube consumer, topic:{}, masterList:{}", topic, masterUrl);
+        ConsumerConfig consumerConfig = new ConsumerConfig(masterUrl, mqConfig.getTubeConsumerGroupName());
+        consumerConfig.setConsumePosition(ConsumePosition.CONSUMER_FROM_LATEST_OFFSET);
+        try {
+            sessionFactory = new TubeMultiSessionFactory(consumerConfig);
+            pullConsumer = sessionFactory.createPullConsumer(consumerConfig);
+            pullConsumer.subscribe(topic, null);
+            pullConsumer.completeSubscribe();
+        } catch (TubeClientException e) {
+            LOG.error("init tube consumer error {}", e.getMessage());
+        }
+
+    }
+
+    public class Fetcher implements Runnable {
+
+        private final PullMessageConsumer pullMessageConsumer;
+        private String topic;
+
+        public Fetcher(PullMessageConsumer pullMessageConsumer, String topic) {
+            this.pullMessageConsumer = pullMessageConsumer;
+            this.topic = topic;
+        }
+
+        @Override
+        public void run() {
+            ConsumerResult csmResult;
+
+            // wait partition status ready
+            while (true) {
+                if (pullMessageConsumer.isPartitionsReady(5000) || pullMessageConsumer.isShutdown()) {
+                    break;
+                }
+            }
+            // consume messages
+            while (true) {
+                if (pullMessageConsumer.isShutdown()) {
+                    LOG.warn("consumer is shutdown!");
+                    break;
+                }
+
+                try {
+                    csmResult = pullMessageConsumer.getMessage();
+                    if (csmResult.isSuccess()) {
+                        List<Message> messageList = csmResult.getMessageList();
+                        if (CollectionUtils.isNotEmpty(messageList)) {
+                            for (Message message : messageList) {
+                                if (StringUtils.equals(message.getTopic(), topic)) {
+                                    String body = new String(message.getData(), StandardCharsets.UTF_8);
+                                    handleMessage(body);
+                                }
+                            }
+                        }
+                        pullMessageConsumer.confirmConsume(csmResult.getConfirmContext(), true);
+                    } else {
+                        //TODO improve error handle
+                        LOG.error("receive messages errorCode is {}, error meddage is {}", csmResult.getErrCode(),

Review comment:
       Does this branch need ack?




-- 
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-unsubscribe@inlong.apache.org

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