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/10/25 06:37:51 UTC

[GitHub] [inlong] pocozh commented on a diff in pull request #6245: [INLONG-4986][Agent] Support MQTT Source

pocozh commented on code in PR #6245:
URL: https://github.com/apache/inlong/pull/6245#discussion_r1004057448


##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/MqttReader.java:
##########
@@ -0,0 +1,222 @@
+/*
+ * 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.sources.reader;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.plugin.Message;
+import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
+import org.eclipse.paho.client.mqttv3.MqttCallback;
+import org.eclipse.paho.client.mqttv3.MqttClient;
+import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
+import org.eclipse.paho.client.mqttv3.MqttException;
+import org.eclipse.paho.client.mqttv3.MqttMessage;
+import org.eclipse.paho.client.mqttv3.MqttSecurityException;
+import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.LinkedBlockingQueue;
+
+public class MqttReader extends AbstractReader {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(MqttReader.class);
+
+    public static final String JOB_MQTT_USERNAME = "job.mqttJob.userName";
+    public static final String JOB_MQTT_PASSWORD = "job.mqttJob.password";
+    public static final String JOB_MQTT_SERVER_URI = "job.mqttJob.serverURI";
+    public static final String JOB_MQTT_CONNECTION_TIMEOUT = "job.mqttJob.connectionTimeOut";
+    public static final String JOB_MQTT_KEEPALIVE_INTERVAL = "job.mqttJob.keepAliveInterval";
+    public static final String JOB_MQTT_QOS = "job.mqttJob.qos";
+    public static final String JOB_MQTT_CLEAN_SESSION = "job.mqttJob.cleanSession";
+    public static final String JOB_MQTT_CLIENT_ID_PREFIX = "job.mqttJob.clientIdPrefix";
+    public static final String JOB_MQTT_QUEUE_SIZE = "job.mqttJob.queueSize";
+    public static final String JOB_MQTT_AUTOMATIC_RECONNECT = "job.mqttJob.automaticReconnect";
+    public static final String JOB_MQTT_VERSION = "job.mqttJob.mqttVersion";
+
+    private boolean finished = false;
+
+    private boolean destroyed = false;
+
+    private MqttClient client;
+
+    private MqttConnectOptions options;
+
+    private String serverURI;
+    private String userName;
+    private String password;
+    private String topic;
+    private int qos;
+    private boolean cleanSession = false;
+    private boolean automaticReconnect = true;
+    private JobProfile jobProfile;
+    private String instanceId;
+    private String clientId;
+    private int mqttVersion = MqttConnectOptions.MQTT_VERSION_DEFAULT;
+
+    private LinkedBlockingQueue<DefaultMessage> mqttMessagesQueue;
+
+    public MqttReader(String topic) {
+        this.topic = topic;
+    }
+
+    @Override
+    public void init(JobProfile jobConf) {
+        super.init(jobConf);
+        jobProfile = jobConf;
+        LOGGER.info("init mqtt reader with jobConf {}", jobConf.toJsonStr());
+
+        mqttMessagesQueue = new LinkedBlockingQueue<>(jobConf.getInt(JOB_MQTT_QUEUE_SIZE, 1000));
+        instanceId = jobConf.getInstanceId();
+        userName = jobConf.get(JOB_MQTT_USERNAME);
+        password = jobConf.get(JOB_MQTT_PASSWORD);
+        serverURI = jobConf.get(JOB_MQTT_SERVER_URI);
+        clientId = jobConf.get(JOB_MQTT_CLIENT_ID_PREFIX, "mqtt_client") + "_" + UUID.randomUUID();
+        cleanSession = jobConf.getBoolean(JOB_MQTT_CLEAN_SESSION, false);
+        automaticReconnect = jobConf.getBoolean(JOB_MQTT_AUTOMATIC_RECONNECT, true);
+        qos = jobConf.getInt(JOB_MQTT_QOS, 1);
+        mqttVersion = jobConf.getInt(JOB_MQTT_VERSION, MqttConnectOptions.MQTT_VERSION_DEFAULT);
+
+        options = new MqttConnectOptions();
+        options.setCleanSession(cleanSession);
+        options.setConnectionTimeout(jobConf.getInt(JOB_MQTT_CONNECTION_TIMEOUT, 10));
+        options.setKeepAliveInterval(jobConf.getInt(JOB_MQTT_KEEPALIVE_INTERVAL, 20));
+        options.setUserName(userName);
+        options.setPassword(password.toCharArray());
+        options.setAutomaticReconnect(automaticReconnect);
+        options.setMqttVersion(mqttVersion);
+
+        try {
+            synchronized (MqttReader.class) {
+                client = new MqttClient(serverURI, clientId, new MemoryPersistence());
+                client.setCallback(new MqttCallback() {
+                    @Override
+                    public void connectionLost(Throwable cause) {
+                        LOGGER.info("the mqtt connection is lost, try to reconnect");

Review Comment:
   It's better to add  some necessary info such as jobId, connection etc in this log.



##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/MqttReader.java:
##########
@@ -0,0 +1,222 @@
+/*
+ * 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.sources.reader;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.plugin.Message;
+import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
+import org.eclipse.paho.client.mqttv3.MqttCallback;
+import org.eclipse.paho.client.mqttv3.MqttClient;
+import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
+import org.eclipse.paho.client.mqttv3.MqttException;
+import org.eclipse.paho.client.mqttv3.MqttMessage;
+import org.eclipse.paho.client.mqttv3.MqttSecurityException;
+import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.LinkedBlockingQueue;
+
+public class MqttReader extends AbstractReader {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(MqttReader.class);
+
+    public static final String JOB_MQTT_USERNAME = "job.mqttJob.userName";
+    public static final String JOB_MQTT_PASSWORD = "job.mqttJob.password";
+    public static final String JOB_MQTT_SERVER_URI = "job.mqttJob.serverURI";
+    public static final String JOB_MQTT_CONNECTION_TIMEOUT = "job.mqttJob.connectionTimeOut";
+    public static final String JOB_MQTT_KEEPALIVE_INTERVAL = "job.mqttJob.keepAliveInterval";
+    public static final String JOB_MQTT_QOS = "job.mqttJob.qos";
+    public static final String JOB_MQTT_CLEAN_SESSION = "job.mqttJob.cleanSession";
+    public static final String JOB_MQTT_CLIENT_ID_PREFIX = "job.mqttJob.clientIdPrefix";
+    public static final String JOB_MQTT_QUEUE_SIZE = "job.mqttJob.queueSize";
+    public static final String JOB_MQTT_AUTOMATIC_RECONNECT = "job.mqttJob.automaticReconnect";
+    public static final String JOB_MQTT_VERSION = "job.mqttJob.mqttVersion";
+
+    private boolean finished = false;
+
+    private boolean destroyed = false;
+
+    private MqttClient client;
+
+    private MqttConnectOptions options;
+
+    private String serverURI;
+    private String userName;
+    private String password;
+    private String topic;
+    private int qos;
+    private boolean cleanSession = false;
+    private boolean automaticReconnect = true;
+    private JobProfile jobProfile;
+    private String instanceId;
+    private String clientId;
+    private int mqttVersion = MqttConnectOptions.MQTT_VERSION_DEFAULT;
+
+    private LinkedBlockingQueue<DefaultMessage> mqttMessagesQueue;
+
+    public MqttReader(String topic) {
+        this.topic = topic;
+    }
+
+    @Override
+    public void init(JobProfile jobConf) {
+        super.init(jobConf);
+        jobProfile = jobConf;
+        LOGGER.info("init mqtt reader with jobConf {}", jobConf.toJsonStr());
+
+        mqttMessagesQueue = new LinkedBlockingQueue<>(jobConf.getInt(JOB_MQTT_QUEUE_SIZE, 1000));
+        instanceId = jobConf.getInstanceId();
+        userName = jobConf.get(JOB_MQTT_USERNAME);
+        password = jobConf.get(JOB_MQTT_PASSWORD);
+        serverURI = jobConf.get(JOB_MQTT_SERVER_URI);
+        clientId = jobConf.get(JOB_MQTT_CLIENT_ID_PREFIX, "mqtt_client") + "_" + UUID.randomUUID();
+        cleanSession = jobConf.getBoolean(JOB_MQTT_CLEAN_SESSION, false);
+        automaticReconnect = jobConf.getBoolean(JOB_MQTT_AUTOMATIC_RECONNECT, true);
+        qos = jobConf.getInt(JOB_MQTT_QOS, 1);
+        mqttVersion = jobConf.getInt(JOB_MQTT_VERSION, MqttConnectOptions.MQTT_VERSION_DEFAULT);
+
+        options = new MqttConnectOptions();
+        options.setCleanSession(cleanSession);
+        options.setConnectionTimeout(jobConf.getInt(JOB_MQTT_CONNECTION_TIMEOUT, 10));
+        options.setKeepAliveInterval(jobConf.getInt(JOB_MQTT_KEEPALIVE_INTERVAL, 20));
+        options.setUserName(userName);
+        options.setPassword(password.toCharArray());
+        options.setAutomaticReconnect(automaticReconnect);
+        options.setMqttVersion(mqttVersion);
+
+        try {
+            synchronized (MqttReader.class) {
+                client = new MqttClient(serverURI, clientId, new MemoryPersistence());
+                client.setCallback(new MqttCallback() {
+                    @Override
+                    public void connectionLost(Throwable cause) {
+                        LOGGER.info("the mqtt connection is lost, try to reconnect");
+                        reconnect();
+                    }
+
+                    @Override
+                    public void messageArrived(String topic, MqttMessage message) throws Exception {
+                        Map<String, String> headerMap = new HashMap<>();
+                        headerMap.put("record.topic", topic);
+                        headerMap.put("record.messageId", String.valueOf(message.getId()));
+                        headerMap.put("record.qos", String.valueOf(message.getQos()));
+                        byte[] recordValue = message.getPayload();
+                        mqttMessagesQueue.put(new DefaultMessage(recordValue, headerMap));
+
+                        LOGGER.debug("the mqtt receive message: {}", new String(recordValue));
+
+                        readerMetric.pluginReadSuccessCount.incrementAndGet();
+                        readerMetric.pluginReadCount.incrementAndGet();
+                    }
+
+                    @Override
+                    public void deliveryComplete(IMqttDeliveryToken token) {
+                    }
+                });
+                client.connect(options);
+                client.subscribe(topic, 1);
+            }
+            LOGGER.info("the mqtt subscribe topic is [{}], qos is [{}]", topic, qos);
+        } catch (Exception e) {
+            LOGGER.error("init mqtt client error ", e);
+        }
+    }
+
+    private void reconnect() {
+        if (!client.isConnected()) {
+            try {
+                client.connect(options);
+                LOGGER.info("the mqtt client reconnect success");
+            } catch (MqttSecurityException e) {
+                LOGGER.error("reconnect mqtt client error ", e);
+            } catch (MqttException e) {
+                LOGGER.error("reconnect mqtt client error ", e);
+            }
+        }
+    }
+
+    @Override
+    public Message read() {
+        if (!mqttMessagesQueue.isEmpty()) {
+            return getMqttMessage();
+        } else {
+            return null;
+        }
+    }
+
+    private DefaultMessage getMqttMessage() {
+        return mqttMessagesQueue.poll();
+    }
+
+    @Override
+    public boolean isFinished() {
+        return finished;
+    }
+
+    @Override
+    public String getReadSource() {
+        return instanceId;
+    }
+
+    @Override
+    public void setReadTimeout(long mill) {
+    }
+
+    @Override
+    public void setWaitMillisecond(long millis) {
+    }
+
+    @Override
+    public String getSnapshot() {

Review Comment:
   Could you please complete the strategy of snapshot?



##########
inlong-agent/agent-common/src/main/java/org/apache/inlong/agent/pojo/JobProfileDto.java:
##########
@@ -54,6 +54,10 @@ public class JobProfileDto {
      * mongo source
      */
     public static final String MONGO_SOURCE = "org.apache.inlong.agent.plugin.sources.MongoDBSource";
+    /**
+     * mqtt source
+     */
+    public static final String MQTT_SOURCE = "org.apache.agent.plugin.sources.MqttSource";

Review Comment:
   wrong package name



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