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/02/09 03:05:36 UTC

[GitHub] [incubator-inlong] luchunliang commented on a change in pull request #2359: [INLONG-1896][Feature][Sort] Inlong-Sort-Standalone support to sort the events to Kafka clusters.

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



##########
File path: inlong-sort-standalone/sort-standalone-source/src/main/java/org/apache/inlong/sort/standalone/sink/kafka/KafkaFederationWorker.java
##########
@@ -0,0 +1,155 @@
+/**
+ * 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.sort.standalone.sink.kafka;
+
+import com.google.common.base.Preconditions;
+import org.apache.flume.Channel;
+import org.apache.flume.Event;
+import org.apache.flume.Transaction;
+import org.apache.flume.lifecycle.LifecycleState;
+import org.apache.inlong.sort.standalone.channel.ProfileEvent;
+import org.apache.inlong.sort.standalone.config.pojo.InlongId;
+import org.apache.inlong.sort.standalone.metrics.SortMetricItem;
+import org.apache.inlong.sort.standalone.utils.Constants;
+import org.apache.inlong.sort.standalone.utils.InlongLoggerFactory;
+import org.apache.pulsar.shade.org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class KafkaFederationWorker extends Thread {
+    public static final Logger LOG = InlongLoggerFactory.getLogger(KafkaFederationWorker.class);
+
+    private final String workerName;
+    private final KafkaFederationSinkContext context;
+
+    private KafkaProducerFederation producerFederation;
+    private LifecycleState status;
+    private Map<String, String> dimensions = new ConcurrentHashMap<>();
+
+    /**
+     * constructor of KafkaFederationWorker
+     *
+     * @param sinkName
+     * @param workerIndex
+     * @param context
+     */
+    public KafkaFederationWorker(
+            String sinkName, int workerIndex, KafkaFederationSinkContext context) {
+        super();
+        this.workerName = sinkName + "-" + workerIndex;
+        this.context = Preconditions.checkNotNull(context);
+        this.producerFederation =
+                new KafkaProducerFederation(String.valueOf(workerIndex), this.context);
+        this.status = LifecycleState.IDLE;
+        this.dimensions.put(SortMetricItem.KEY_CLUSTER_ID, this.context.getClusterId());
+        this.dimensions.put(SortMetricItem.KEY_TASK_NAME, this.context.getTaskName());
+        this.dimensions.put(SortMetricItem.KEY_SINK_ID, this.context.getSinkName());
+    }
+
+    /** entrance of KafkaFederationWorker */
+    @Override
+    public void start() {
+        LOG.info("start a new kafka worker {}", this.workerName);
+        this.producerFederation.start();
+        this.status = LifecycleState.START;
+        super.start();
+    }
+
+    /** close */
+    public void close() {
+        // close all producers
+        LOG.info("close a kafka worker {}", this.workerName);
+        this.producerFederation.close();
+        this.status = LifecycleState.STOP;
+    }
+
+    @Override
+    public void run() {
+        LOG.info("worker {} start to run, the state is {}", this.workerName, status.name());
+        while (status != LifecycleState.STOP) {
+            Transaction tx = null;
+            try {
+                Channel channel = context.getChannel();
+                if (channel == null) {
+                    LOG.error("in kafka worker, channel is null ");
+                    break;
+                }
+                tx = channel.getTransaction();
+                tx.begin();
+                Event rowEvent = channel.take();
+                if (rowEvent == null) {
+                    tx.commit();
+                    tx.close();
+                    sleepOneInterval();
+                    continue;
+                }
+                ProfileEvent event = (ProfileEvent) rowEvent;

Review comment:
       Miss check operation.
   rowEvent instanceof ProfileEvent.

##########
File path: inlong-sort-standalone/sort-standalone-source/src/main/java/org/apache/inlong/sort/standalone/sink/kafka/KafkaFederationSink.java
##########
@@ -0,0 +1,99 @@
+/**
+ * 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.sort.standalone.sink.kafka;
+
+import org.apache.flume.Context;
+import org.apache.flume.Sink;
+import org.apache.flume.conf.Configurable;
+import org.apache.flume.sink.AbstractSink;
+import org.apache.inlong.sort.standalone.metrics.SortMetricItem;
+import org.apache.inlong.sort.standalone.utils.InlongLoggerFactory;
+import org.slf4j.Logger;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class KafkaFederationSink extends AbstractSink implements Configurable {

Review comment:
       Please use KafkaZone and KafkaCluster name.

##########
File path: inlong-sort-standalone/sort-standalone-source/src/main/java/org/apache/inlong/sort/standalone/sink/kafka/KafkaFederationWorker.java
##########
@@ -0,0 +1,155 @@
+/**
+ * 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.sort.standalone.sink.kafka;
+
+import com.google.common.base.Preconditions;
+import org.apache.flume.Channel;
+import org.apache.flume.Event;
+import org.apache.flume.Transaction;
+import org.apache.flume.lifecycle.LifecycleState;
+import org.apache.inlong.sort.standalone.channel.ProfileEvent;
+import org.apache.inlong.sort.standalone.config.pojo.InlongId;
+import org.apache.inlong.sort.standalone.metrics.SortMetricItem;
+import org.apache.inlong.sort.standalone.utils.Constants;
+import org.apache.inlong.sort.standalone.utils.InlongLoggerFactory;
+import org.apache.pulsar.shade.org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class KafkaFederationWorker extends Thread {
+    public static final Logger LOG = InlongLoggerFactory.getLogger(KafkaFederationWorker.class);
+
+    private final String workerName;
+    private final KafkaFederationSinkContext context;
+
+    private KafkaProducerFederation producerFederation;
+    private LifecycleState status;
+    private Map<String, String> dimensions = new ConcurrentHashMap<>();
+
+    /**
+     * constructor of KafkaFederationWorker
+     *
+     * @param sinkName
+     * @param workerIndex
+     * @param context
+     */
+    public KafkaFederationWorker(
+            String sinkName, int workerIndex, KafkaFederationSinkContext context) {
+        super();
+        this.workerName = sinkName + "-" + workerIndex;
+        this.context = Preconditions.checkNotNull(context);
+        this.producerFederation =
+                new KafkaProducerFederation(String.valueOf(workerIndex), this.context);
+        this.status = LifecycleState.IDLE;
+        this.dimensions.put(SortMetricItem.KEY_CLUSTER_ID, this.context.getClusterId());
+        this.dimensions.put(SortMetricItem.KEY_TASK_NAME, this.context.getTaskName());
+        this.dimensions.put(SortMetricItem.KEY_SINK_ID, this.context.getSinkName());
+    }
+
+    /** entrance of KafkaFederationWorker */
+    @Override
+    public void start() {
+        LOG.info("start a new kafka worker {}", this.workerName);
+        this.producerFederation.start();
+        this.status = LifecycleState.START;
+        super.start();
+    }
+
+    /** close */
+    public void close() {
+        // close all producers
+        LOG.info("close a kafka worker {}", this.workerName);
+        this.producerFederation.close();
+        this.status = LifecycleState.STOP;
+    }
+
+    @Override
+    public void run() {
+        LOG.info("worker {} start to run, the state is {}", this.workerName, status.name());
+        while (status != LifecycleState.STOP) {
+            Transaction tx = null;
+            try {
+                Channel channel = context.getChannel();
+                if (channel == null) {
+                    LOG.error("in kafka worker, channel is null ");
+                    break;
+                }
+                tx = channel.getTransaction();
+                tx.begin();
+                Event rowEvent = channel.take();
+                if (rowEvent == null) {
+                    tx.commit();
+                    tx.close();
+                    sleepOneInterval();
+                    continue;
+                }
+                ProfileEvent event = (ProfileEvent) rowEvent;
+                this.fillTopic(event);
+                SortMetricItem.fillInlongId(event, dimensions);
+                this.dimensions.put(
+                        SortMetricItem.KEY_SINK_DATA_ID, event.getHeaders().get(Constants.TOPIC));
+                long msgTime = event.getRawLogTime();
+                long auditFormatTime = msgTime - msgTime % 60000L;

Review comment:
       Please add constant variable for 60000L.

##########
File path: inlong-sort-standalone/sort-standalone-source/src/main/java/org/apache/inlong/sort/standalone/sink/kafka/KafkaProducerCluster.java
##########
@@ -0,0 +1,235 @@
+/**
+ * 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.sort.standalone.sink.kafka;
+
+import com.google.common.base.Preconditions;
+import org.apache.flume.Context;
+import org.apache.flume.Event;
+import org.apache.flume.Transaction;
+import org.apache.flume.lifecycle.LifecycleAware;
+import org.apache.flume.lifecycle.LifecycleState;
+import org.apache.inlong.sort.standalone.config.holder.CommonPropertiesHolder;
+import org.apache.inlong.sort.standalone.config.pojo.CacheClusterConfig;
+import org.apache.inlong.sort.standalone.metrics.SortMetricItem;
+import org.apache.inlong.sort.standalone.metrics.audit.AuditUtils;
+import org.apache.inlong.sort.standalone.utils.Constants;
+import org.apache.inlong.sort.standalone.utils.InlongLoggerFactory;
+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.common.serialization.ByteArraySerializer;
+import org.apache.kafka.common.serialization.StringSerializer;
+import org.apache.pulsar.shade.org.apache.commons.lang.math.NumberUtils;
+import org.slf4j.Logger;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+/** wrapper of kafka producer */
+public class KafkaProducerCluster implements LifecycleAware {
+    public static final Logger LOG = InlongLoggerFactory.getLogger(KafkaProducerCluster.class);
+
+    private static final String KEY_RETRIES = "retries";
+    private static final String KEY_ACKS = "acks";
+    private static final String KEY_BATCH_SIZE = "batch.size";
+    private static final String KEY_LINGER_MS = "linger.ms";
+    private static final String KEY_MAX_REQUEST_SIZE = "max.request.size";
+    private static final String KEY_BUFFER_MEMORY = "buffer.memory";
+    private static final String KEY_RECEIVE_BUFFER_BYTES = "receive.buffer.bytes";
+    private static final String KEY_SEND_BUFFER_BYTES = "send.buffer.bytes";
+    private static final String KEY_METADATA_FETCH_TIMEOUT_MS = "metadata.fetch.timeout.ms";
+    private static final String KEY_METADATA_MAX_AGE_MS = "metadata.max.age.ms";
+    private static final String KEY_COMPRESSION_TYPE = "compression.type";
+    private static final String KEY_MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION =
+            "max.in.flight.requests.per.connection";
+    private static final String KEY_TIMEOUT_MS = "rpc.timeout.ms";
+    private static final String KEY_DELIVERY_TIMEOUT_MS = "delivery.timeout.ms";
+    private static final String KEY_REQUEST_TIMEOUT_MS = "request.timeout.ms";
+    private static final String KEY_ENABLE_REPLACE_PARTITION_FOR_NOT_LEADER =
+            "enable.replace.partition.for.not.leader";
+    private static final String KEY_ENABLE_REPLACE_PARTITION_FOR_CAN_RETRY =
+            "enable.replace.partition.for.can.retry";
+    private static final String KEY_ENABLE_TOPIC_PARTITION_CIRCUIT_BREAKER =
+            "enable.topic.partition.circuit.breaker";
+    private static final String KEY_MUTE_PARTITION_ERROR_MAX_TIMES =
+            "mute.partition.error.max.times";
+    private static final String KEY_MUTE_PARTITION_MAX_PERCENTAGE = "mute.partition.max.percentage";
+    private static final String KEY_UNMUTE_PARTITION_INTERVAL_MS = "unmute.partition.interval.ms";
+    private static final String KEY_MAX_BLOCK_MS = "max.block.ms";

Review comment:
       unusable constant variables.

##########
File path: inlong-sort-standalone/pom.xml
##########
@@ -43,14 +43,14 @@
         <flume.version>1.9.0</flume.version>
         <plugin.assembly.version>3.2.0</plugin.assembly.version>
         <pulsar.version>2.7.2</pulsar.version>
+        <kafka.version>2.4.1</kafka.version>
         <junit.version>4.13</junit.version>
         <powermock.version>2.0.2</powermock.version>
         <guava.version>19.0</guava.version>
         <skipTests>false</skipTests>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <compiler.source>1.8</compiler.source>
         <compiler.target>1.8</compiler.target>
-        <pulsar.version>2.7.2</pulsar.version>

Review comment:
       wrong removing operation
   <pulsar.version>2.7.2</pulsar.version>




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