You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by "healchow (via GitHub)" <gi...@apache.org> on 2023/04/11 08:04:29 UTC

[GitHub] [inlong] healchow commented on a diff in pull request #7808: [INLONG-7783][Agent] Support sink data to Kafka

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


##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sinks/KafkaSink.java:
##########
@@ -0,0 +1,445 @@
+/*
+ * 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.agent.plugin.sinks;
+
+import com.google.common.base.Preconditions;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.inlong.agent.common.AgentThreadFactory;
+import org.apache.inlong.agent.conf.AgentConfiguration;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.core.task.TaskPositionManager;
+import org.apache.inlong.agent.message.BatchProxyMessage;
+import org.apache.inlong.agent.message.EndMessage;
+import org.apache.inlong.agent.message.PackProxyMessage;
+import org.apache.inlong.agent.message.ProxyMessage;
+import org.apache.inlong.agent.metrics.audit.AuditUtils;
+import org.apache.inlong.agent.plugin.Message;
+import org.apache.inlong.agent.utils.AgentUtils;
+import org.apache.inlong.agent.utils.ThreadUtils;
+import org.apache.inlong.common.pojo.dataproxy.MQClusterInfo;
+
+import org.apache.kafka.clients.producer.Callback;
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.ProducerConfig;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.clients.producer.RecordMetadata;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.apache.inlong.agent.constant.AgentConstants.DEFAULT_ENABLE_ASYNC_SEND;
+import static org.apache.inlong.agent.constant.AgentConstants.DEFAULT_KAFKA_SINK_SEND_ACKS;
+import static org.apache.inlong.agent.constant.AgentConstants.DEFAULT_KAFKA_SINK_SEND_COMPRESSION_TYPE;
+import static org.apache.inlong.agent.constant.AgentConstants.DEFAULT_KAFKA_SINK_SEND_KEY_SERIALIZER;
+import static org.apache.inlong.agent.constant.AgentConstants.DEFAULT_KAFKA_SINK_SEND_VALUE_SERIALIZER;
+import static org.apache.inlong.agent.constant.AgentConstants.DEFAULT_KAFKA_SINK_SYNC_SEND_TIMEOUT_MS;
+import static org.apache.inlong.agent.constant.AgentConstants.DEFAULT_PRODUCER_NUM;
+import static org.apache.inlong.agent.constant.AgentConstants.DEFAULT_SEND_QUEUE_SIZE;
+import static org.apache.inlong.agent.constant.AgentConstants.KAFKA_PRODUCER_ENABLE_ASYNC_SEND;
+import static org.apache.inlong.agent.constant.AgentConstants.KAFKA_SINK_PRODUCER_NUM;
+import static org.apache.inlong.agent.constant.AgentConstants.KAFKA_SINK_SEND_QUEUE_SIZE;
+import static org.apache.kafka.clients.CommonClientConfigs.SECURITY_PROTOCOL_CONFIG;
+import static org.apache.kafka.common.config.SaslConfigs.SASL_JAAS_CONFIG;
+import static org.apache.kafka.common.config.SaslConfigs.SASL_MECHANISM;
+
+public class KafkaSink extends AbstractSink {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(KafkaSink.class);
+    private static final ExecutorService EXECUTOR_SERVICE = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
+            60L, TimeUnit.SECONDS, new SynchronousQueue<>(), new AgentThreadFactory("KafkaSink"));
+    private final AgentConfiguration agentConf = AgentConfiguration.getAgentConf();
+    private TaskPositionManager taskPositionManager;
+    private volatile boolean shutdown = false;
+
+    private List<MQClusterInfo> mqClusterInfos;
+    private String topic;
+    private List<KafkaSender> kafkaSenders;
+    private static final AtomicInteger KAFKA_SENDER_INDEX = new AtomicInteger(0);
+
+    private LinkedBlockingQueue<BatchProxyMessage> kafkaSendQueue;
+
+    private int producerNum;
+    private boolean asyncSend;
+
+    @Override
+    public void init(JobProfile jobConf) {
+        super.init(jobConf);
+        taskPositionManager = TaskPositionManager.getInstance();
+        int sendQueueSize = agentConf.getInt(KAFKA_SINK_SEND_QUEUE_SIZE, DEFAULT_SEND_QUEUE_SIZE);
+        kafkaSendQueue = new LinkedBlockingQueue<>(sendQueueSize);
+        producerNum = agentConf.getInt(KAFKA_SINK_PRODUCER_NUM, DEFAULT_PRODUCER_NUM);
+        asyncSend = agentConf.getBoolean(KAFKA_PRODUCER_ENABLE_ASYNC_SEND, DEFAULT_ENABLE_ASYNC_SEND);
+
+        mqClusterInfos = jobConf.getMqClusters();
+        Preconditions.checkArgument(ObjectUtils.isNotEmpty(jobConf.getMqTopic()) && jobConf.getMqTopic().isValid(),
+                "no valid kafka topic config");
+        topic = jobConf.getMqTopic().getTopic();
+
+        kafkaSenders = new ArrayList<>();
+        initKafkaSender();
+        EXECUTOR_SERVICE.execute(sendDataThread());
+        EXECUTOR_SERVICE.execute(flushCache());
+    }
+
+    @Override
+    public void write(Message message) {
+        try {
+            if (message != null) {

Review Comment:
   The nesting of this code is too deep, it is recommended to use guard statements to simplify.
   
   For example:
   ```java
   if (message == null) {
       return;
   }
   
   // other process in else
   ```
   



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