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/09/05 07:39:17 UTC

[GitHub] [inlong] iamsee123 opened a new pull request, #5780: [Inlong-5722][Agent] Support Redis Source

iamsee123 opened a new pull request, #5780:
URL: https://github.com/apache/inlong/pull/5780

   ### Prepare a Pull Request
   
   - Fixes #5722
   
   ### Motivation
   make agent module support redis source
   
   ### Modifications
   
   + add RedisSource and RedisReader
   + add unit test for redisReader
   
   ### Verifying this change
   
   *(Please pick either of the following options)*
   
   - [ ] This change is a trivial rework/code cleanup without any test coverage.
   
   - [ ] This change is already covered by existing tests, such as:
     *(please describe tests)*
   
   - [x] This change added tests and can be verified as follows:
     - TestRedisReader
   


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


[GitHub] [inlong] dockerzhang merged pull request #5780: [INLONG-5722][Agent] Support Redis Source

Posted by GitBox <gi...@apache.org>.
dockerzhang merged PR #5780:
URL: https://github.com/apache/inlong/pull/5780


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


[GitHub] [inlong] dockerzhang commented on pull request #5780: [INLONG-5722][Agent] Support Redis Source

Posted by GitBox <gi...@apache.org>.
dockerzhang commented on PR #5780:
URL: https://github.com/apache/inlong/pull/5780#issuecomment-1243880491

   @iamsee123 please fix the comment and conficts


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


[GitHub] [inlong] EMsnap commented on a diff in pull request #5780: [INLONG-5722][Agent] Support Redis Source

Posted by GitBox <gi...@apache.org>.
EMsnap commented on code in PR #5780:
URL: https://github.com/apache/inlong/pull/5780#discussion_r973844258


##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/RedisReader.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.sources.reader;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.moilioncircle.redis.replicator.RedisReplicator;
+import com.moilioncircle.redis.replicator.Replicator;
+import com.moilioncircle.redis.replicator.cmd.CommandName;
+import com.moilioncircle.redis.replicator.cmd.impl.DefaultCommand;
+import com.moilioncircle.redis.replicator.cmd.parser.DefaultCommandParser;
+import com.moilioncircle.redis.replicator.event.Event;
+import com.moilioncircle.redis.replicator.event.EventListener;
+import com.moilioncircle.redis.replicator.event.PostRdbSyncEvent;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueHash;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueList;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueString;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueZSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyValuePair;
+import com.moilioncircle.redis.replicator.rdb.datatype.ZSetEntry;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.metrics.audit.AuditUtils;
+import org.apache.inlong.agent.plugin.Message;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/**
+ * Redis data reader
+ */
+public class RedisReader extends AbstractReader {
+
+    public static final String REDIS_READER_TAG_NAME = "AgentRedisMetric";
+    public static final String JOB_REDIS_PORT = "job.redisJob.port";
+    public static final String JOB_REDIS_HOSTNAME = "job.redisJob.hostname";
+    public static final String JOB_REDIS_SSL = "job.redisJob.ssl";
+    public static final String JOB_REDIS_AUTHUSER = "job.redisJob.authUser";
+    public static final String JOB_REDIS_AUTHPASSWORD = "job.redisJob.authPassword";
+    public static final String JOB_REDIS_READTIMEOUT = "job.redisJob.readTimeout";
+    public static final String JOB_REDIS_QUEUE_SIZE = "job.redisJob.queueSize";
+    public static final String JOB_REDIS_REPLID = "job.redisJob.replId";
+    public static final String JOB_REDIS_OFFSET = "job.redisJob.offset";
+    private static final Logger LOGGER = LoggerFactory.getLogger(RedisReader.class);
+    private String port;
+    private String hostName;
+    private boolean ssl;
+    private String authUser;
+    private String authPassword;
+    private String readTimeout;
+    private String instanceId;
+    private String replId;
+    private String snapShot;
+    private boolean destroyed;
+    private Replicator redisReplicator;
+    private LinkedBlockingQueue<String> redisMessageQueue;
+    private boolean finished = false;
+    private ExecutorService executor;
+    private Gson gson;
+
+    @Override
+    public void init(JobProfile jobConf) {
+        super.init(jobConf);
+        LOGGER.info("Init redis reader with jobConf {}", jobConf.toJsonStr());
+        port = jobConf.get(JOB_REDIS_PORT);
+        hostName = jobConf.get(JOB_REDIS_HOSTNAME);
+        ssl = jobConf.getBoolean(JOB_REDIS_SSL, false);
+        authUser = jobConf.get(JOB_REDIS_AUTHUSER, "");
+        authPassword = jobConf.get(JOB_REDIS_AUTHPASSWORD, "");
+        readTimeout = jobConf.get(JOB_REDIS_READTIMEOUT, "");
+        replId = jobConf.get(JOB_REDIS_REPLID, "");
+        snapShot = jobConf.get(JOB_REDIS_OFFSET, "-1");
+        instanceId = jobConf.getInstanceId();
+        finished = false;
+        redisMessageQueue = new LinkedBlockingQueue<>(jobConf.getInt(JOB_REDIS_QUEUE_SIZE, 10000));
+        initGson();
+        String uri = getRedisUri();
+        try {
+            redisReplicator = new RedisReplicator(uri);
+            initReplicator();
+            redisReplicator.addEventListener(new EventListener() {
+                @Override
+                public void onEvent(Replicator replicator, Event event) {
+                    try {
+                        if (event instanceof DefaultCommand || event instanceof KeyValuePair<?, ?>) {
+                            redisMessageQueue.put(gson.toJson(event));
+                            AuditUtils.add(AuditUtils.AUDIT_ID_AGENT_READ_SUCCESS, inlongGroupId, inlongStreamId,
+                                    System.currentTimeMillis(), 1);
+                            readerMetric.pluginReadCount.incrementAndGet();
+                        }
+                        if (event instanceof PostRdbSyncEvent) {
+                            snapShot = String.valueOf(replicator.getConfiguration().getReplOffset());
+                            LOGGER.info("after rdb snapShot is: {}", snapShot);
+                        }
+                    } catch (InterruptedException e) {
+                        readerMetric.pluginReadFailCount.incrementAndGet();
+                        LOGGER.error("Read redis data error", e);
+                    }
+                }
+            });
+            executor = Executors.newSingleThreadExecutor();
+            executor.execute(new Thread(() -> {
+                try {
+                    redisReplicator.open();
+                } catch (IOException e) {
+                    LOGGER.error("Redis source error", e);
+                }
+            }));
+        } catch (URISyntaxException | IOException e) {
+            readerMetric.pluginReadFailCount.addAndGet(1);
+            LOGGER.error("Connect to redis {}:{} failed.", hostName, port);
+        }
+    }
+
+    private String getRedisUri() {
+        StringBuffer sb = new StringBuffer("redis://");
+        sb.append(hostName).append(":").append(port);
+        sb.append("?");
+        if (authPassword != null && !authPassword.equals("")) {
+            sb.append("authPassword=").append(authPassword).append("&");
+        }
+        if (authUser != null && !authUser.equals("")) {
+            sb.append("authUser=").append(authUser).append("&");
+        }
+        if (readTimeout != null && !readTimeout.equals("")) {
+            sb.append("readTimeout=").append(readTimeout).append("&");
+        }
+        if (ssl) {
+            sb.append("ssl=").append("yes").append("&");
+        }
+        if (snapShot != null && !snapShot.equals("")) {
+            sb.append("replOffset=").append(snapShot).append("&");
+        }
+        if (replId != null && !replId.equals("")) {
+            sb.append("replId=").append(replId).append("&");
+        }
+        if (sb.charAt(sb.length() - 1) == '?' || sb.charAt(sb.length() - 1) == '&') {
+            sb.deleteCharAt(sb.length() - 1);
+        }
+        return sb.toString();
+    }
+
+    @Override
+    public void destroy() {
+        synchronized (this) {
+            if (!destroyed) {
+                try {
+                    executor.shutdown();
+                    redisReplicator.close();
+                } catch (IOException e) {
+                    LOGGER.error("Redis reader close failed.");
+                }
+                destroyed = true;
+            }
+        }
+    }
+
+    @Override
+    public Message read() {
+        if (!redisMessageQueue.isEmpty()) {
+            readerMetric.pluginReadCount.incrementAndGet();
+            return new DefaultMessage(redisMessageQueue.poll().getBytes());
+        } else {
+            return null;
+        }
+    }
+
+    @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() {
+        return snapShot;
+    }
+
+    @Override
+    public void finishRead() {
+        finished = true;
+    }
+
+    @Override
+    public boolean isSourceExist() {
+        return true;
+    }
+
+    /**
+     * init GSON parser
+     */
+    private void initGson() {
+        gson = new GsonBuilder().registerTypeAdapter(KeyStringValueHash.class, new TypeAdapter<KeyStringValueHash>() {
+
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueHash kv) throws IOException {
+                        out.beginObject();
+                        out.name("DB").beginObject();
+                        out.name("dbNumber").value(kv.getDb().getDbNumber());
+                        out.name("dbSize").value(kv.getDb().getDbsize());
+                        out.name("expires").value(kv.getDb().getExpires());
+                        out.endObject();
+                        out.name("valueRdbType").value(kv.getValueRdbType());
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").beginObject();
+                        for (byte[] b : kv.getValue().keySet()) {
+                            out.name(new String(b)).value(new String(kv.getValue().get(b)));
+                        }
+                        out.endObject();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueHash read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                }).registerTypeAdapter(DefaultCommand.class, new TypeAdapter<DefaultCommand>() {
+                    @Override
+                    public void write(JsonWriter out, DefaultCommand dc) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(dc.getCommand()));
+                        out.name("value").beginArray();
+                        for (byte[] bytes : dc.getArgs()) {
+                            out.value(new String(bytes));
+                        }
+                        out.endArray();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public DefaultCommand read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .registerTypeAdapter(KeyStringValueList.class, new TypeAdapter<KeyStringValueList>() {
+
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueList kv) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").beginArray();
+                        for (byte[] bytes : kv.getValue()) {
+                            out.value(new String(bytes));
+                        }
+                        out.endArray();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueList read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .registerTypeAdapter(KeyStringValueSet.class, new TypeAdapter<KeyStringValueSet>() {
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueSet kv) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").beginArray();
+                        for (byte[] bytes : kv.getValue()) {
+                            out.value(new String(bytes));
+                        }
+                        out.endArray();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueSet read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .registerTypeAdapter(KeyStringValueString.class, new TypeAdapter<KeyStringValueString>() {
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueString kv) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").value(new String(kv.getValue()));
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueString read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .registerTypeAdapter(KeyStringValueZSet.class, new TypeAdapter<KeyStringValueZSet>() {
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueZSet kv) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").beginArray();
+                        for (ZSetEntry entry : kv.getValue()) {
+                            out.beginObject();
+                            out.name("element").value(new String(entry.getElement()));
+                            out.name("score").value(entry.getScore());
+                            out.endObject();
+                        }
+                        out.endArray();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueZSet read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .create();
+    }
+
+    /**
+     * init replicator's commandParser
+     */
+    private void initReplicator() {
+        redisReplicator.addCommandParser(CommandName.name("APPEND"), new DefaultCommandParser());

Review Comment:
   can we reuse  DefaultCommandParser()  here ?



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


[GitHub] [inlong] GanfengTan commented on a diff in pull request #5780: [INLONG-5722][Agent] Support Redis Source

Posted by GitBox <gi...@apache.org>.
GanfengTan commented on code in PR #5780:
URL: https://github.com/apache/inlong/pull/5780#discussion_r963211137


##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/RedisReader.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.sources.reader;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.moilioncircle.redis.replicator.RedisReplicator;
+import com.moilioncircle.redis.replicator.Replicator;
+import com.moilioncircle.redis.replicator.cmd.CommandName;
+import com.moilioncircle.redis.replicator.cmd.impl.DefaultCommand;
+import com.moilioncircle.redis.replicator.cmd.parser.DefaultCommandParser;
+import com.moilioncircle.redis.replicator.event.Event;
+import com.moilioncircle.redis.replicator.event.EventListener;
+import com.moilioncircle.redis.replicator.event.PostRdbSyncEvent;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueHash;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueList;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueString;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueZSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyValuePair;
+import com.moilioncircle.redis.replicator.rdb.datatype.ZSetEntry;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.metrics.audit.AuditUtils;
+import org.apache.inlong.agent.plugin.Message;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/**
+ * Redis data reader
+ */
+public class RedisReader extends AbstractReader {
+
+    public static final String REDIS_READER_TAG_NAME = "AgentRedisMetric";
+    public static final String JOB_REDIS_PORT = "job.redisJob.port";
+    public static final String JOB_REDIS_HOSTNAME = "job.redisJob.hostname";
+    public static final String JOB_REDIS_SSL = "job.redisJob.ssl";
+    public static final String JOB_REDIS_AUTHUSER = "job.redisJob.authUser";
+    public static final String JOB_REDIS_AUTHPASSWORD = "job.redisJob.authPassword";
+    public static final String JOB_REDIS_READTIMEOUT = "job.redisJob.readTimeout";
+    public static final String JOB_REDIS_QUEUE_SIZE = "job.redisJob.queueSize";
+    public static final String JOB_REDIS_REPLID = "job.redisJob.replId";
+    public static final String JOB_REDIS_OFFSET = "job.redisJob.offset";
+    private static final Logger LOGGER = LoggerFactory.getLogger(RedisReader.class);
+    private String port;
+    private String hostName;
+    private boolean ssl;
+    private String authUser;
+    private String authPassword;
+    private String readTimeout;
+    private String instanceId;
+    private String replId;
+    private String snapShot;
+    private boolean destroyed;
+    private Replicator redisReplicator;
+    private LinkedBlockingQueue<String> redisMessageQueue;
+    private boolean finished = false;
+    private ExecutorService executor;
+    private Gson gson;
+

Review Comment:
   Suggest to add filter 



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


[GitHub] [inlong] GanfengTan commented on a diff in pull request #5780: [INLONG-5722][Agent] Support Redis Source

Posted by GitBox <gi...@apache.org>.
GanfengTan commented on code in PR #5780:
URL: https://github.com/apache/inlong/pull/5780#discussion_r972564411


##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/RedisReader.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.sources.reader;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.moilioncircle.redis.replicator.RedisReplicator;
+import com.moilioncircle.redis.replicator.Replicator;
+import com.moilioncircle.redis.replicator.cmd.CommandName;
+import com.moilioncircle.redis.replicator.cmd.impl.DefaultCommand;
+import com.moilioncircle.redis.replicator.cmd.parser.DefaultCommandParser;
+import com.moilioncircle.redis.replicator.event.Event;
+import com.moilioncircle.redis.replicator.event.EventListener;
+import com.moilioncircle.redis.replicator.event.PostRdbSyncEvent;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueHash;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueList;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueString;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueZSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyValuePair;
+import com.moilioncircle.redis.replicator.rdb.datatype.ZSetEntry;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.metrics.audit.AuditUtils;
+import org.apache.inlong.agent.plugin.Message;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/**
+ * Redis data reader
+ */
+public class RedisReader extends AbstractReader {
+
+    public static final String REDIS_READER_TAG_NAME = "AgentRedisMetric";
+    public static final String JOB_REDIS_PORT = "job.redisJob.port";
+    public static final String JOB_REDIS_HOSTNAME = "job.redisJob.hostname";
+    public static final String JOB_REDIS_SSL = "job.redisJob.ssl";
+    public static final String JOB_REDIS_AUTHUSER = "job.redisJob.authUser";
+    public static final String JOB_REDIS_AUTHPASSWORD = "job.redisJob.authPassword";
+    public static final String JOB_REDIS_READTIMEOUT = "job.redisJob.readTimeout";
+    public static final String JOB_REDIS_QUEUE_SIZE = "job.redisJob.queueSize";
+    public static final String JOB_REDIS_REPLID = "job.redisJob.replId";
+    public static final String JOB_REDIS_OFFSET = "job.redisJob.offset";
+    private static final Logger LOGGER = LoggerFactory.getLogger(RedisReader.class);
+    private String port;
+    private String hostName;
+    private boolean ssl;
+    private String authUser;
+    private String authPassword;
+    private String readTimeout;
+    private String instanceId;
+    private String replId;
+    private String snapShot;
+    private boolean destroyed;
+    private Replicator redisReplicator;
+    private LinkedBlockingQueue<String> redisMessageQueue;
+    private boolean finished = false;
+    private ExecutorService executor;
+    private Gson gson;
+

Review Comment:
   OK 



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


[GitHub] [inlong] iamsee123 commented on a diff in pull request #5780: [INLONG-5722][Agent] Support Redis Source

Posted by GitBox <gi...@apache.org>.
iamsee123 commented on code in PR #5780:
URL: https://github.com/apache/inlong/pull/5780#discussion_r969101231


##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/RedisReader.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.sources.reader;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.moilioncircle.redis.replicator.RedisReplicator;
+import com.moilioncircle.redis.replicator.Replicator;
+import com.moilioncircle.redis.replicator.cmd.CommandName;
+import com.moilioncircle.redis.replicator.cmd.impl.DefaultCommand;
+import com.moilioncircle.redis.replicator.cmd.parser.DefaultCommandParser;
+import com.moilioncircle.redis.replicator.event.Event;
+import com.moilioncircle.redis.replicator.event.EventListener;
+import com.moilioncircle.redis.replicator.event.PostRdbSyncEvent;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueHash;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueList;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueString;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueZSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyValuePair;
+import com.moilioncircle.redis.replicator.rdb.datatype.ZSetEntry;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.metrics.audit.AuditUtils;
+import org.apache.inlong.agent.plugin.Message;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/**
+ * Redis data reader
+ */
+public class RedisReader extends AbstractReader {
+
+    public static final String REDIS_READER_TAG_NAME = "AgentRedisMetric";
+    public static final String JOB_REDIS_PORT = "job.redisJob.port";
+    public static final String JOB_REDIS_HOSTNAME = "job.redisJob.hostname";
+    public static final String JOB_REDIS_SSL = "job.redisJob.ssl";
+    public static final String JOB_REDIS_AUTHUSER = "job.redisJob.authUser";
+    public static final String JOB_REDIS_AUTHPASSWORD = "job.redisJob.authPassword";
+    public static final String JOB_REDIS_READTIMEOUT = "job.redisJob.readTimeout";
+    public static final String JOB_REDIS_QUEUE_SIZE = "job.redisJob.queueSize";
+    public static final String JOB_REDIS_REPLID = "job.redisJob.replId";
+    public static final String JOB_REDIS_OFFSET = "job.redisJob.offset";
+    private static final Logger LOGGER = LoggerFactory.getLogger(RedisReader.class);
+    private String port;
+    private String hostName;
+    private boolean ssl;
+    private String authUser;
+    private String authPassword;
+    private String readTimeout;
+    private String instanceId;
+    private String replId;
+    private String snapShot;
+    private boolean destroyed;
+    private Replicator redisReplicator;
+    private LinkedBlockingQueue<String> redisMessageQueue;
+    private boolean finished = false;
+    private ExecutorService executor;
+    private Gson gson;
+

Review Comment:
   what kind of filter



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


[GitHub] [inlong] iamsee123 commented on a diff in pull request #5780: [INLONG-5722][Agent] Support Redis Source

Posted by GitBox <gi...@apache.org>.
iamsee123 commented on code in PR #5780:
URL: https://github.com/apache/inlong/pull/5780#discussion_r973875169


##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/RedisReader.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.sources.reader;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.moilioncircle.redis.replicator.RedisReplicator;
+import com.moilioncircle.redis.replicator.Replicator;
+import com.moilioncircle.redis.replicator.cmd.CommandName;
+import com.moilioncircle.redis.replicator.cmd.impl.DefaultCommand;
+import com.moilioncircle.redis.replicator.cmd.parser.DefaultCommandParser;
+import com.moilioncircle.redis.replicator.event.Event;
+import com.moilioncircle.redis.replicator.event.EventListener;
+import com.moilioncircle.redis.replicator.event.PostRdbSyncEvent;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueHash;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueList;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueString;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueZSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyValuePair;
+import com.moilioncircle.redis.replicator.rdb.datatype.ZSetEntry;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.metrics.audit.AuditUtils;
+import org.apache.inlong.agent.plugin.Message;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/**
+ * Redis data reader
+ */
+public class RedisReader extends AbstractReader {
+
+    public static final String REDIS_READER_TAG_NAME = "AgentRedisMetric";
+    public static final String JOB_REDIS_PORT = "job.redisJob.port";
+    public static final String JOB_REDIS_HOSTNAME = "job.redisJob.hostname";
+    public static final String JOB_REDIS_SSL = "job.redisJob.ssl";
+    public static final String JOB_REDIS_AUTHUSER = "job.redisJob.authUser";
+    public static final String JOB_REDIS_AUTHPASSWORD = "job.redisJob.authPassword";
+    public static final String JOB_REDIS_READTIMEOUT = "job.redisJob.readTimeout";
+    public static final String JOB_REDIS_QUEUE_SIZE = "job.redisJob.queueSize";
+    public static final String JOB_REDIS_REPLID = "job.redisJob.replId";
+    public static final String JOB_REDIS_OFFSET = "job.redisJob.offset";
+    private static final Logger LOGGER = LoggerFactory.getLogger(RedisReader.class);
+    private String port;
+    private String hostName;
+    private boolean ssl;
+    private String authUser;
+    private String authPassword;
+    private String readTimeout;
+    private String instanceId;
+    private String replId;
+    private String snapShot;
+    private boolean destroyed;
+    private Replicator redisReplicator;
+    private LinkedBlockingQueue<String> redisMessageQueue;
+    private boolean finished = false;
+    private ExecutorService executor;
+    private Gson gson;
+
+    @Override
+    public void init(JobProfile jobConf) {
+        super.init(jobConf);
+        LOGGER.info("Init redis reader with jobConf {}", jobConf.toJsonStr());
+        port = jobConf.get(JOB_REDIS_PORT);
+        hostName = jobConf.get(JOB_REDIS_HOSTNAME);
+        ssl = jobConf.getBoolean(JOB_REDIS_SSL, false);
+        authUser = jobConf.get(JOB_REDIS_AUTHUSER, "");
+        authPassword = jobConf.get(JOB_REDIS_AUTHPASSWORD, "");
+        readTimeout = jobConf.get(JOB_REDIS_READTIMEOUT, "");
+        replId = jobConf.get(JOB_REDIS_REPLID, "");
+        snapShot = jobConf.get(JOB_REDIS_OFFSET, "-1");
+        instanceId = jobConf.getInstanceId();
+        finished = false;
+        redisMessageQueue = new LinkedBlockingQueue<>(jobConf.getInt(JOB_REDIS_QUEUE_SIZE, 10000));
+        initGson();
+        String uri = getRedisUri();
+        try {
+            redisReplicator = new RedisReplicator(uri);
+            initReplicator();
+            redisReplicator.addEventListener(new EventListener() {
+                @Override
+                public void onEvent(Replicator replicator, Event event) {
+                    try {
+                        if (event instanceof DefaultCommand || event instanceof KeyValuePair<?, ?>) {
+                            redisMessageQueue.put(gson.toJson(event));
+                            AuditUtils.add(AuditUtils.AUDIT_ID_AGENT_READ_SUCCESS, inlongGroupId, inlongStreamId,
+                                    System.currentTimeMillis(), 1);
+                            readerMetric.pluginReadCount.incrementAndGet();
+                        }
+                        if (event instanceof PostRdbSyncEvent) {
+                            snapShot = String.valueOf(replicator.getConfiguration().getReplOffset());
+                            LOGGER.info("after rdb snapShot is: {}", snapShot);
+                        }
+                    } catch (InterruptedException e) {
+                        readerMetric.pluginReadFailCount.incrementAndGet();
+                        LOGGER.error("Read redis data error", e);
+                    }
+                }
+            });
+            executor = Executors.newSingleThreadExecutor();
+            executor.execute(new Thread(() -> {
+                try {
+                    redisReplicator.open();
+                } catch (IOException e) {
+                    LOGGER.error("Redis source error", e);
+                }
+            }));
+        } catch (URISyntaxException | IOException e) {
+            readerMetric.pluginReadFailCount.addAndGet(1);
+            LOGGER.error("Connect to redis {}:{} failed.", hostName, port);
+        }
+    }
+
+    private String getRedisUri() {
+        StringBuffer sb = new StringBuffer("redis://");
+        sb.append(hostName).append(":").append(port);
+        sb.append("?");
+        if (authPassword != null && !authPassword.equals("")) {
+            sb.append("authPassword=").append(authPassword).append("&");
+        }
+        if (authUser != null && !authUser.equals("")) {
+            sb.append("authUser=").append(authUser).append("&");
+        }
+        if (readTimeout != null && !readTimeout.equals("")) {
+            sb.append("readTimeout=").append(readTimeout).append("&");
+        }
+        if (ssl) {
+            sb.append("ssl=").append("yes").append("&");
+        }
+        if (snapShot != null && !snapShot.equals("")) {
+            sb.append("replOffset=").append(snapShot).append("&");
+        }
+        if (replId != null && !replId.equals("")) {
+            sb.append("replId=").append(replId).append("&");
+        }
+        if (sb.charAt(sb.length() - 1) == '?' || sb.charAt(sb.length() - 1) == '&') {
+            sb.deleteCharAt(sb.length() - 1);
+        }
+        return sb.toString();
+    }
+
+    @Override
+    public void destroy() {
+        synchronized (this) {
+            if (!destroyed) {
+                try {
+                    executor.shutdown();
+                    redisReplicator.close();
+                } catch (IOException e) {
+                    LOGGER.error("Redis reader close failed.");
+                }
+                destroyed = true;
+            }
+        }
+    }
+
+    @Override
+    public Message read() {
+        if (!redisMessageQueue.isEmpty()) {
+            readerMetric.pluginReadCount.incrementAndGet();
+            return new DefaultMessage(redisMessageQueue.poll().getBytes());
+        } else {
+            return null;
+        }
+    }
+
+    @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() {
+        return snapShot;
+    }
+
+    @Override
+    public void finishRead() {
+        finished = true;
+    }
+
+    @Override
+    public boolean isSourceExist() {
+        return true;
+    }
+
+    /**
+     * init GSON parser
+     */
+    private void initGson() {
+        gson = new GsonBuilder().registerTypeAdapter(KeyStringValueHash.class, new TypeAdapter<KeyStringValueHash>() {
+
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueHash kv) throws IOException {
+                        out.beginObject();
+                        out.name("DB").beginObject();
+                        out.name("dbNumber").value(kv.getDb().getDbNumber());
+                        out.name("dbSize").value(kv.getDb().getDbsize());
+                        out.name("expires").value(kv.getDb().getExpires());
+                        out.endObject();
+                        out.name("valueRdbType").value(kv.getValueRdbType());
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").beginObject();
+                        for (byte[] b : kv.getValue().keySet()) {
+                            out.name(new String(b)).value(new String(kv.getValue().get(b)));
+                        }
+                        out.endObject();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueHash read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                }).registerTypeAdapter(DefaultCommand.class, new TypeAdapter<DefaultCommand>() {
+                    @Override
+                    public void write(JsonWriter out, DefaultCommand dc) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(dc.getCommand()));
+                        out.name("value").beginArray();
+                        for (byte[] bytes : dc.getArgs()) {
+                            out.value(new String(bytes));
+                        }
+                        out.endArray();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public DefaultCommand read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .registerTypeAdapter(KeyStringValueList.class, new TypeAdapter<KeyStringValueList>() {
+
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueList kv) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").beginArray();
+                        for (byte[] bytes : kv.getValue()) {
+                            out.value(new String(bytes));
+                        }
+                        out.endArray();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueList read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .registerTypeAdapter(KeyStringValueSet.class, new TypeAdapter<KeyStringValueSet>() {
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueSet kv) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").beginArray();
+                        for (byte[] bytes : kv.getValue()) {
+                            out.value(new String(bytes));
+                        }
+                        out.endArray();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueSet read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .registerTypeAdapter(KeyStringValueString.class, new TypeAdapter<KeyStringValueString>() {
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueString kv) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").value(new String(kv.getValue()));
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueString read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .registerTypeAdapter(KeyStringValueZSet.class, new TypeAdapter<KeyStringValueZSet>() {
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueZSet kv) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").beginArray();
+                        for (ZSetEntry entry : kv.getValue()) {
+                            out.beginObject();
+                            out.name("element").value(new String(entry.getElement()));
+                            out.name("score").value(entry.getScore());
+                            out.endObject();
+                        }
+                        out.endArray();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueZSet read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .create();
+    }
+
+    /**
+     * init replicator's commandParser
+     */
+    private void initReplicator() {
+        redisReplicator.addCommandParser(CommandName.name("APPEND"), new DefaultCommandParser());

Review Comment:
   Each command has its own commandParser, so I have to change it into unified parser



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


[GitHub] [inlong] iamsee123 commented on a diff in pull request #5780: [INLONG-5722][Agent] Support Redis Source

Posted by GitBox <gi...@apache.org>.
iamsee123 commented on code in PR #5780:
URL: https://github.com/apache/inlong/pull/5780#discussion_r974003475


##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/RedisReader.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.sources.reader;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.moilioncircle.redis.replicator.RedisReplicator;
+import com.moilioncircle.redis.replicator.Replicator;
+import com.moilioncircle.redis.replicator.cmd.CommandName;
+import com.moilioncircle.redis.replicator.cmd.impl.DefaultCommand;
+import com.moilioncircle.redis.replicator.cmd.parser.DefaultCommandParser;
+import com.moilioncircle.redis.replicator.event.Event;
+import com.moilioncircle.redis.replicator.event.EventListener;
+import com.moilioncircle.redis.replicator.event.PostRdbSyncEvent;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueHash;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueList;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueString;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueZSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyValuePair;
+import com.moilioncircle.redis.replicator.rdb.datatype.ZSetEntry;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.metrics.audit.AuditUtils;
+import org.apache.inlong.agent.plugin.Message;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/**
+ * Redis data reader
+ */
+public class RedisReader extends AbstractReader {
+
+    public static final String REDIS_READER_TAG_NAME = "AgentRedisMetric";
+    public static final String JOB_REDIS_PORT = "job.redisJob.port";
+    public static final String JOB_REDIS_HOSTNAME = "job.redisJob.hostname";
+    public static final String JOB_REDIS_SSL = "job.redisJob.ssl";
+    public static final String JOB_REDIS_AUTHUSER = "job.redisJob.authUser";
+    public static final String JOB_REDIS_AUTHPASSWORD = "job.redisJob.authPassword";
+    public static final String JOB_REDIS_READTIMEOUT = "job.redisJob.readTimeout";
+    public static final String JOB_REDIS_QUEUE_SIZE = "job.redisJob.queueSize";
+    public static final String JOB_REDIS_REPLID = "job.redisJob.replId";
+    public static final String JOB_REDIS_OFFSET = "job.redisJob.offset";
+    private static final Logger LOGGER = LoggerFactory.getLogger(RedisReader.class);
+    private String port;
+    private String hostName;
+    private boolean ssl;
+    private String authUser;
+    private String authPassword;
+    private String readTimeout;
+    private String instanceId;
+    private String replId;
+    private String snapShot;
+    private boolean destroyed;
+    private Replicator redisReplicator;
+    private LinkedBlockingQueue<String> redisMessageQueue;
+    private boolean finished = false;
+    private ExecutorService executor;
+    private Gson gson;
+
+    @Override
+    public void init(JobProfile jobConf) {
+        super.init(jobConf);
+        LOGGER.info("Init redis reader with jobConf {}", jobConf.toJsonStr());
+        port = jobConf.get(JOB_REDIS_PORT);
+        hostName = jobConf.get(JOB_REDIS_HOSTNAME);
+        ssl = jobConf.getBoolean(JOB_REDIS_SSL, false);
+        authUser = jobConf.get(JOB_REDIS_AUTHUSER, "");
+        authPassword = jobConf.get(JOB_REDIS_AUTHPASSWORD, "");
+        readTimeout = jobConf.get(JOB_REDIS_READTIMEOUT, "");
+        replId = jobConf.get(JOB_REDIS_REPLID, "");
+        snapShot = jobConf.get(JOB_REDIS_OFFSET, "-1");
+        instanceId = jobConf.getInstanceId();
+        finished = false;
+        redisMessageQueue = new LinkedBlockingQueue<>(jobConf.getInt(JOB_REDIS_QUEUE_SIZE, 10000));
+        initGson();
+        String uri = getRedisUri();
+        try {
+            redisReplicator = new RedisReplicator(uri);
+            initReplicator();
+            redisReplicator.addEventListener(new EventListener() {
+                @Override
+                public void onEvent(Replicator replicator, Event event) {
+                    try {
+                        if (event instanceof DefaultCommand || event instanceof KeyValuePair<?, ?>) {
+                            redisMessageQueue.put(gson.toJson(event));
+                            AuditUtils.add(AuditUtils.AUDIT_ID_AGENT_READ_SUCCESS, inlongGroupId, inlongStreamId,
+                                    System.currentTimeMillis(), 1);
+                            readerMetric.pluginReadCount.incrementAndGet();
+                        }
+                        if (event instanceof PostRdbSyncEvent) {
+                            snapShot = String.valueOf(replicator.getConfiguration().getReplOffset());
+                            LOGGER.info("after rdb snapShot is: {}", snapShot);
+                        }
+                    } catch (InterruptedException e) {
+                        readerMetric.pluginReadFailCount.incrementAndGet();
+                        LOGGER.error("Read redis data error", e);
+                    }
+                }
+            });
+            executor = Executors.newSingleThreadExecutor();
+            executor.execute(new Thread(() -> {
+                try {
+                    redisReplicator.open();
+                } catch (IOException e) {
+                    LOGGER.error("Redis source error", e);
+                }
+            }));
+        } catch (URISyntaxException | IOException e) {
+            readerMetric.pluginReadFailCount.addAndGet(1);
+            LOGGER.error("Connect to redis {}:{} failed.", hostName, port);
+        }
+    }
+
+    private String getRedisUri() {
+        StringBuffer sb = new StringBuffer("redis://");
+        sb.append(hostName).append(":").append(port);
+        sb.append("?");
+        if (authPassword != null && !authPassword.equals("")) {
+            sb.append("authPassword=").append(authPassword).append("&");
+        }
+        if (authUser != null && !authUser.equals("")) {
+            sb.append("authUser=").append(authUser).append("&");
+        }
+        if (readTimeout != null && !readTimeout.equals("")) {
+            sb.append("readTimeout=").append(readTimeout).append("&");
+        }
+        if (ssl) {
+            sb.append("ssl=").append("yes").append("&");
+        }
+        if (snapShot != null && !snapShot.equals("")) {
+            sb.append("replOffset=").append(snapShot).append("&");
+        }
+        if (replId != null && !replId.equals("")) {
+            sb.append("replId=").append(replId).append("&");
+        }
+        if (sb.charAt(sb.length() - 1) == '?' || sb.charAt(sb.length() - 1) == '&') {
+            sb.deleteCharAt(sb.length() - 1);
+        }
+        return sb.toString();
+    }
+
+    @Override
+    public void destroy() {
+        synchronized (this) {
+            if (!destroyed) {
+                try {
+                    executor.shutdown();
+                    redisReplicator.close();
+                } catch (IOException e) {
+                    LOGGER.error("Redis reader close failed.");
+                }
+                destroyed = true;
+            }
+        }
+    }
+
+    @Override
+    public Message read() {
+        if (!redisMessageQueue.isEmpty()) {
+            readerMetric.pluginReadCount.incrementAndGet();
+            return new DefaultMessage(redisMessageQueue.poll().getBytes());
+        } else {
+            return null;
+        }
+    }
+
+    @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() {
+        return snapShot;
+    }
+
+    @Override
+    public void finishRead() {
+        finished = true;
+    }
+
+    @Override
+    public boolean isSourceExist() {
+        return true;
+    }
+
+    /**
+     * init GSON parser
+     */
+    private void initGson() {
+        gson = new GsonBuilder().registerTypeAdapter(KeyStringValueHash.class, new TypeAdapter<KeyStringValueHash>() {
+
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueHash kv) throws IOException {
+                        out.beginObject();
+                        out.name("DB").beginObject();
+                        out.name("dbNumber").value(kv.getDb().getDbNumber());
+                        out.name("dbSize").value(kv.getDb().getDbsize());
+                        out.name("expires").value(kv.getDb().getExpires());
+                        out.endObject();
+                        out.name("valueRdbType").value(kv.getValueRdbType());
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").beginObject();
+                        for (byte[] b : kv.getValue().keySet()) {
+                            out.name(new String(b)).value(new String(kv.getValue().get(b)));
+                        }
+                        out.endObject();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueHash read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                }).registerTypeAdapter(DefaultCommand.class, new TypeAdapter<DefaultCommand>() {
+                    @Override
+                    public void write(JsonWriter out, DefaultCommand dc) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(dc.getCommand()));
+                        out.name("value").beginArray();
+                        for (byte[] bytes : dc.getArgs()) {
+                            out.value(new String(bytes));
+                        }
+                        out.endArray();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public DefaultCommand read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .registerTypeAdapter(KeyStringValueList.class, new TypeAdapter<KeyStringValueList>() {
+
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueList kv) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").beginArray();
+                        for (byte[] bytes : kv.getValue()) {
+                            out.value(new String(bytes));
+                        }
+                        out.endArray();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueList read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .registerTypeAdapter(KeyStringValueSet.class, new TypeAdapter<KeyStringValueSet>() {
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueSet kv) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").beginArray();
+                        for (byte[] bytes : kv.getValue()) {
+                            out.value(new String(bytes));
+                        }
+                        out.endArray();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueSet read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .registerTypeAdapter(KeyStringValueString.class, new TypeAdapter<KeyStringValueString>() {
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueString kv) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").value(new String(kv.getValue()));
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueString read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .registerTypeAdapter(KeyStringValueZSet.class, new TypeAdapter<KeyStringValueZSet>() {
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueZSet kv) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").beginArray();
+                        for (ZSetEntry entry : kv.getValue()) {
+                            out.beginObject();
+                            out.name("element").value(new String(entry.getElement()));
+                            out.name("score").value(entry.getScore());
+                            out.endObject();
+                        }
+                        out.endArray();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueZSet read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .create();
+    }
+
+    /**
+     * init replicator's commandParser
+     */
+    private void initReplicator() {
+        redisReplicator.addCommandParser(CommandName.name("APPEND"), new DefaultCommandParser());

Review Comment:
   I my test we can reuse DefaultCommandParser().



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


[GitHub] [inlong] iamsee123 commented on a diff in pull request #5780: [INLONG-5722][Agent] Support Redis Source

Posted by GitBox <gi...@apache.org>.
iamsee123 commented on code in PR #5780:
URL: https://github.com/apache/inlong/pull/5780#discussion_r970846441


##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/RedisReader.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.sources.reader;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.moilioncircle.redis.replicator.RedisReplicator;
+import com.moilioncircle.redis.replicator.Replicator;
+import com.moilioncircle.redis.replicator.cmd.CommandName;
+import com.moilioncircle.redis.replicator.cmd.impl.DefaultCommand;
+import com.moilioncircle.redis.replicator.cmd.parser.DefaultCommandParser;
+import com.moilioncircle.redis.replicator.event.Event;
+import com.moilioncircle.redis.replicator.event.EventListener;
+import com.moilioncircle.redis.replicator.event.PostRdbSyncEvent;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueHash;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueList;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueString;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueZSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyValuePair;
+import com.moilioncircle.redis.replicator.rdb.datatype.ZSetEntry;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.metrics.audit.AuditUtils;
+import org.apache.inlong.agent.plugin.Message;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/**
+ * Redis data reader
+ */
+public class RedisReader extends AbstractReader {
+
+    public static final String REDIS_READER_TAG_NAME = "AgentRedisMetric";
+    public static final String JOB_REDIS_PORT = "job.redisJob.port";
+    public static final String JOB_REDIS_HOSTNAME = "job.redisJob.hostname";
+    public static final String JOB_REDIS_SSL = "job.redisJob.ssl";
+    public static final String JOB_REDIS_AUTHUSER = "job.redisJob.authUser";
+    public static final String JOB_REDIS_AUTHPASSWORD = "job.redisJob.authPassword";
+    public static final String JOB_REDIS_READTIMEOUT = "job.redisJob.readTimeout";
+    public static final String JOB_REDIS_QUEUE_SIZE = "job.redisJob.queueSize";
+    public static final String JOB_REDIS_REPLID = "job.redisJob.replId";
+    public static final String JOB_REDIS_OFFSET = "job.redisJob.offset";
+    private static final Logger LOGGER = LoggerFactory.getLogger(RedisReader.class);
+    private String port;
+    private String hostName;
+    private boolean ssl;
+    private String authUser;
+    private String authPassword;
+    private String readTimeout;
+    private String instanceId;
+    private String replId;
+    private String snapShot;
+    private boolean destroyed;
+    private Replicator redisReplicator;
+    private LinkedBlockingQueue<String> redisMessageQueue;
+    private boolean finished = false;
+    private ExecutorService executor;
+    private Gson gson;
+

Review Comment:
   By using lua-replicate-commands configuration directive and [redis.replicate_commands()](https://redis.io/topics/lua-api#redis.replicate_commands) Lua API can enable effects replication, then redisReader can read the command directly. However if using verbatim replication, we just read the eval command but the change of data can be read by rdb scan at the beginning. @GanfengTan



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


[GitHub] [inlong] EMsnap commented on a diff in pull request #5780: [INLONG-5722][Agent] Support Redis Source

Posted by GitBox <gi...@apache.org>.
EMsnap commented on code in PR #5780:
URL: https://github.com/apache/inlong/pull/5780#discussion_r973843813


##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/RedisReader.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.sources.reader;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.moilioncircle.redis.replicator.RedisReplicator;
+import com.moilioncircle.redis.replicator.Replicator;
+import com.moilioncircle.redis.replicator.cmd.CommandName;
+import com.moilioncircle.redis.replicator.cmd.impl.DefaultCommand;
+import com.moilioncircle.redis.replicator.cmd.parser.DefaultCommandParser;
+import com.moilioncircle.redis.replicator.event.Event;
+import com.moilioncircle.redis.replicator.event.EventListener;
+import com.moilioncircle.redis.replicator.event.PostRdbSyncEvent;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueHash;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueList;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueString;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueZSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyValuePair;
+import com.moilioncircle.redis.replicator.rdb.datatype.ZSetEntry;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.metrics.audit.AuditUtils;
+import org.apache.inlong.agent.plugin.Message;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/**
+ * Redis data reader
+ */
+public class RedisReader extends AbstractReader {
+
+    public static final String REDIS_READER_TAG_NAME = "AgentRedisMetric";
+    public static final String JOB_REDIS_PORT = "job.redisJob.port";
+    public static final String JOB_REDIS_HOSTNAME = "job.redisJob.hostname";
+    public static final String JOB_REDIS_SSL = "job.redisJob.ssl";
+    public static final String JOB_REDIS_AUTHUSER = "job.redisJob.authUser";
+    public static final String JOB_REDIS_AUTHPASSWORD = "job.redisJob.authPassword";
+    public static final String JOB_REDIS_READTIMEOUT = "job.redisJob.readTimeout";
+    public static final String JOB_REDIS_QUEUE_SIZE = "job.redisJob.queueSize";
+    public static final String JOB_REDIS_REPLID = "job.redisJob.replId";
+    public static final String JOB_REDIS_OFFSET = "job.redisJob.offset";
+    private static final Logger LOGGER = LoggerFactory.getLogger(RedisReader.class);
+    private String port;
+    private String hostName;
+    private boolean ssl;
+    private String authUser;
+    private String authPassword;
+    private String readTimeout;
+    private String instanceId;
+    private String replId;
+    private String snapShot;
+    private boolean destroyed;
+    private Replicator redisReplicator;
+    private LinkedBlockingQueue<String> redisMessageQueue;
+    private boolean finished = false;
+    private ExecutorService executor;
+    private Gson gson;
+
+    @Override
+    public void init(JobProfile jobConf) {
+        super.init(jobConf);
+        LOGGER.info("Init redis reader with jobConf {}", jobConf.toJsonStr());
+        port = jobConf.get(JOB_REDIS_PORT);
+        hostName = jobConf.get(JOB_REDIS_HOSTNAME);
+        ssl = jobConf.getBoolean(JOB_REDIS_SSL, false);
+        authUser = jobConf.get(JOB_REDIS_AUTHUSER, "");
+        authPassword = jobConf.get(JOB_REDIS_AUTHPASSWORD, "");
+        readTimeout = jobConf.get(JOB_REDIS_READTIMEOUT, "");
+        replId = jobConf.get(JOB_REDIS_REPLID, "");
+        snapShot = jobConf.get(JOB_REDIS_OFFSET, "-1");
+        instanceId = jobConf.getInstanceId();
+        finished = false;
+        redisMessageQueue = new LinkedBlockingQueue<>(jobConf.getInt(JOB_REDIS_QUEUE_SIZE, 10000));
+        initGson();
+        String uri = getRedisUri();
+        try {
+            redisReplicator = new RedisReplicator(uri);
+            initReplicator();
+            redisReplicator.addEventListener(new EventListener() {
+                @Override
+                public void onEvent(Replicator replicator, Event event) {
+                    try {
+                        if (event instanceof DefaultCommand || event instanceof KeyValuePair<?, ?>) {
+                            redisMessageQueue.put(gson.toJson(event));
+                            AuditUtils.add(AuditUtils.AUDIT_ID_AGENT_READ_SUCCESS, inlongGroupId, inlongStreamId,
+                                    System.currentTimeMillis(), 1);
+                            readerMetric.pluginReadCount.incrementAndGet();
+                        }
+                        if (event instanceof PostRdbSyncEvent) {
+                            snapShot = String.valueOf(replicator.getConfiguration().getReplOffset());
+                            LOGGER.info("after rdb snapShot is: {}", snapShot);
+                        }
+                    } catch (InterruptedException e) {
+                        readerMetric.pluginReadFailCount.incrementAndGet();
+                        LOGGER.error("Read redis data error", e);
+                    }
+                }
+            });
+            executor = Executors.newSingleThreadExecutor();
+            executor.execute(new Thread(() -> {
+                try {
+                    redisReplicator.open();
+                } catch (IOException e) {
+                    LOGGER.error("Redis source error", e);
+                }
+            }));
+        } catch (URISyntaxException | IOException e) {
+            readerMetric.pluginReadFailCount.addAndGet(1);
+            LOGGER.error("Connect to redis {}:{} failed.", hostName, port);
+        }
+    }
+
+    private String getRedisUri() {
+        StringBuffer sb = new StringBuffer("redis://");
+        sb.append(hostName).append(":").append(port);
+        sb.append("?");
+        if (authPassword != null && !authPassword.equals("")) {

Review Comment:
   StringUtils.isEmpty



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


[GitHub] [inlong] iamsee123 commented on a diff in pull request #5780: [INLONG-5722][Agent] Support Redis Source

Posted by GitBox <gi...@apache.org>.
iamsee123 commented on code in PR #5780:
URL: https://github.com/apache/inlong/pull/5780#discussion_r974002773


##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/RedisReader.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.sources.reader;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.moilioncircle.redis.replicator.RedisReplicator;
+import com.moilioncircle.redis.replicator.Replicator;
+import com.moilioncircle.redis.replicator.cmd.CommandName;
+import com.moilioncircle.redis.replicator.cmd.impl.DefaultCommand;
+import com.moilioncircle.redis.replicator.cmd.parser.DefaultCommandParser;
+import com.moilioncircle.redis.replicator.event.Event;
+import com.moilioncircle.redis.replicator.event.EventListener;
+import com.moilioncircle.redis.replicator.event.PostRdbSyncEvent;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueHash;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueList;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueString;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueZSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyValuePair;
+import com.moilioncircle.redis.replicator.rdb.datatype.ZSetEntry;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.metrics.audit.AuditUtils;
+import org.apache.inlong.agent.plugin.Message;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/**
+ * Redis data reader
+ */
+public class RedisReader extends AbstractReader {
+
+    public static final String REDIS_READER_TAG_NAME = "AgentRedisMetric";
+    public static final String JOB_REDIS_PORT = "job.redisJob.port";
+    public static final String JOB_REDIS_HOSTNAME = "job.redisJob.hostname";
+    public static final String JOB_REDIS_SSL = "job.redisJob.ssl";
+    public static final String JOB_REDIS_AUTHUSER = "job.redisJob.authUser";
+    public static final String JOB_REDIS_AUTHPASSWORD = "job.redisJob.authPassword";
+    public static final String JOB_REDIS_READTIMEOUT = "job.redisJob.readTimeout";
+    public static final String JOB_REDIS_QUEUE_SIZE = "job.redisJob.queueSize";
+    public static final String JOB_REDIS_REPLID = "job.redisJob.replId";
+    public static final String JOB_REDIS_OFFSET = "job.redisJob.offset";
+    private static final Logger LOGGER = LoggerFactory.getLogger(RedisReader.class);
+    private String port;
+    private String hostName;
+    private boolean ssl;
+    private String authUser;
+    private String authPassword;
+    private String readTimeout;
+    private String instanceId;
+    private String replId;
+    private String snapShot;
+    private boolean destroyed;
+    private Replicator redisReplicator;
+    private LinkedBlockingQueue<String> redisMessageQueue;
+    private boolean finished = false;
+    private ExecutorService executor;
+    private Gson gson;
+
+    @Override
+    public void init(JobProfile jobConf) {
+        super.init(jobConf);
+        LOGGER.info("Init redis reader with jobConf {}", jobConf.toJsonStr());
+        port = jobConf.get(JOB_REDIS_PORT);
+        hostName = jobConf.get(JOB_REDIS_HOSTNAME);
+        ssl = jobConf.getBoolean(JOB_REDIS_SSL, false);
+        authUser = jobConf.get(JOB_REDIS_AUTHUSER, "");
+        authPassword = jobConf.get(JOB_REDIS_AUTHPASSWORD, "");
+        readTimeout = jobConf.get(JOB_REDIS_READTIMEOUT, "");
+        replId = jobConf.get(JOB_REDIS_REPLID, "");
+        snapShot = jobConf.get(JOB_REDIS_OFFSET, "-1");
+        instanceId = jobConf.getInstanceId();
+        finished = false;
+        redisMessageQueue = new LinkedBlockingQueue<>(jobConf.getInt(JOB_REDIS_QUEUE_SIZE, 10000));
+        initGson();
+        String uri = getRedisUri();
+        try {
+            redisReplicator = new RedisReplicator(uri);
+            initReplicator();
+            redisReplicator.addEventListener(new EventListener() {
+                @Override
+                public void onEvent(Replicator replicator, Event event) {
+                    try {
+                        if (event instanceof DefaultCommand || event instanceof KeyValuePair<?, ?>) {
+                            redisMessageQueue.put(gson.toJson(event));
+                            AuditUtils.add(AuditUtils.AUDIT_ID_AGENT_READ_SUCCESS, inlongGroupId, inlongStreamId,
+                                    System.currentTimeMillis(), 1);
+                            readerMetric.pluginReadCount.incrementAndGet();
+                        }
+                        if (event instanceof PostRdbSyncEvent) {
+                            snapShot = String.valueOf(replicator.getConfiguration().getReplOffset());
+                            LOGGER.info("after rdb snapShot is: {}", snapShot);
+                        }
+                    } catch (InterruptedException e) {
+                        readerMetric.pluginReadFailCount.incrementAndGet();
+                        LOGGER.error("Read redis data error", e);
+                    }
+                }
+            });
+            executor = Executors.newSingleThreadExecutor();
+            executor.execute(new Thread(() -> {
+                try {
+                    redisReplicator.open();
+                } catch (IOException e) {
+                    LOGGER.error("Redis source error", e);
+                }
+            }));
+        } catch (URISyntaxException | IOException e) {
+            readerMetric.pluginReadFailCount.addAndGet(1);
+            LOGGER.error("Connect to redis {}:{} failed.", hostName, port);
+        }
+    }
+
+    private String getRedisUri() {
+        StringBuffer sb = new StringBuffer("redis://");
+        sb.append(hostName).append(":").append(port);
+        sb.append("?");
+        if (authPassword != null && !authPassword.equals("")) {

Review Comment:
   fixed



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


[GitHub] [inlong] iamsee123 commented on a diff in pull request #5780: [INLONG-5722][Agent] Support Redis Source

Posted by GitBox <gi...@apache.org>.
iamsee123 commented on code in PR #5780:
URL: https://github.com/apache/inlong/pull/5780#discussion_r970846441


##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/RedisReader.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.sources.reader;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.moilioncircle.redis.replicator.RedisReplicator;
+import com.moilioncircle.redis.replicator.Replicator;
+import com.moilioncircle.redis.replicator.cmd.CommandName;
+import com.moilioncircle.redis.replicator.cmd.impl.DefaultCommand;
+import com.moilioncircle.redis.replicator.cmd.parser.DefaultCommandParser;
+import com.moilioncircle.redis.replicator.event.Event;
+import com.moilioncircle.redis.replicator.event.EventListener;
+import com.moilioncircle.redis.replicator.event.PostRdbSyncEvent;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueHash;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueList;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueString;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueZSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyValuePair;
+import com.moilioncircle.redis.replicator.rdb.datatype.ZSetEntry;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.metrics.audit.AuditUtils;
+import org.apache.inlong.agent.plugin.Message;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/**
+ * Redis data reader
+ */
+public class RedisReader extends AbstractReader {
+
+    public static final String REDIS_READER_TAG_NAME = "AgentRedisMetric";
+    public static final String JOB_REDIS_PORT = "job.redisJob.port";
+    public static final String JOB_REDIS_HOSTNAME = "job.redisJob.hostname";
+    public static final String JOB_REDIS_SSL = "job.redisJob.ssl";
+    public static final String JOB_REDIS_AUTHUSER = "job.redisJob.authUser";
+    public static final String JOB_REDIS_AUTHPASSWORD = "job.redisJob.authPassword";
+    public static final String JOB_REDIS_READTIMEOUT = "job.redisJob.readTimeout";
+    public static final String JOB_REDIS_QUEUE_SIZE = "job.redisJob.queueSize";
+    public static final String JOB_REDIS_REPLID = "job.redisJob.replId";
+    public static final String JOB_REDIS_OFFSET = "job.redisJob.offset";
+    private static final Logger LOGGER = LoggerFactory.getLogger(RedisReader.class);
+    private String port;
+    private String hostName;
+    private boolean ssl;
+    private String authUser;
+    private String authPassword;
+    private String readTimeout;
+    private String instanceId;
+    private String replId;
+    private String snapShot;
+    private boolean destroyed;
+    private Replicator redisReplicator;
+    private LinkedBlockingQueue<String> redisMessageQueue;
+    private boolean finished = false;
+    private ExecutorService executor;
+    private Gson gson;
+

Review Comment:
   By using lua-replicate-commands configuration directive and [redis.replicate_commands()](https://redis.io/topics/lua-api#redis.replicate_commands) Lua API can enable effects replication, then redisReader can read the command directly. However if using verbatim replication, we just read the eval command but the change of data can be read by rdb scan at the beginning.



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


[GitHub] [inlong] GanfengTan commented on a diff in pull request #5780: [INLONG-5722][Agent] Support Redis Source

Posted by GitBox <gi...@apache.org>.
GanfengTan commented on code in PR #5780:
URL: https://github.com/apache/inlong/pull/5780#discussion_r970243092


##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/RedisReader.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.sources.reader;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.moilioncircle.redis.replicator.RedisReplicator;
+import com.moilioncircle.redis.replicator.Replicator;
+import com.moilioncircle.redis.replicator.cmd.CommandName;
+import com.moilioncircle.redis.replicator.cmd.impl.DefaultCommand;
+import com.moilioncircle.redis.replicator.cmd.parser.DefaultCommandParser;
+import com.moilioncircle.redis.replicator.event.Event;
+import com.moilioncircle.redis.replicator.event.EventListener;
+import com.moilioncircle.redis.replicator.event.PostRdbSyncEvent;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueHash;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueList;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueString;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueZSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyValuePair;
+import com.moilioncircle.redis.replicator.rdb.datatype.ZSetEntry;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.metrics.audit.AuditUtils;
+import org.apache.inlong.agent.plugin.Message;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/**
+ * Redis data reader
+ */
+public class RedisReader extends AbstractReader {
+
+    public static final String REDIS_READER_TAG_NAME = "AgentRedisMetric";
+    public static final String JOB_REDIS_PORT = "job.redisJob.port";
+    public static final String JOB_REDIS_HOSTNAME = "job.redisJob.hostname";
+    public static final String JOB_REDIS_SSL = "job.redisJob.ssl";
+    public static final String JOB_REDIS_AUTHUSER = "job.redisJob.authUser";
+    public static final String JOB_REDIS_AUTHPASSWORD = "job.redisJob.authPassword";
+    public static final String JOB_REDIS_READTIMEOUT = "job.redisJob.readTimeout";
+    public static final String JOB_REDIS_QUEUE_SIZE = "job.redisJob.queueSize";
+    public static final String JOB_REDIS_REPLID = "job.redisJob.replId";
+    public static final String JOB_REDIS_OFFSET = "job.redisJob.offset";
+    private static final Logger LOGGER = LoggerFactory.getLogger(RedisReader.class);
+    private String port;
+    private String hostName;
+    private boolean ssl;
+    private String authUser;
+    private String authPassword;
+    private String readTimeout;
+    private String instanceId;
+    private String replId;
+    private String snapShot;
+    private boolean destroyed;
+    private Replicator redisReplicator;
+    private LinkedBlockingQueue<String> redisMessageQueue;
+    private boolean finished = false;
+    private ExecutorService executor;
+    private Gson gson;
+

Review Comment:
   https://redis.io/docs/manual/programmability/eval-intro/
   @iamsee123 I would like you support scripting with Lua, thanks answer to the question.



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


[GitHub] [inlong] iamsee123 commented on a diff in pull request #5780: [INLONG-5722][Agent] Support Redis Source

Posted by GitBox <gi...@apache.org>.
iamsee123 commented on code in PR #5780:
URL: https://github.com/apache/inlong/pull/5780#discussion_r973875169


##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/RedisReader.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.sources.reader;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.moilioncircle.redis.replicator.RedisReplicator;
+import com.moilioncircle.redis.replicator.Replicator;
+import com.moilioncircle.redis.replicator.cmd.CommandName;
+import com.moilioncircle.redis.replicator.cmd.impl.DefaultCommand;
+import com.moilioncircle.redis.replicator.cmd.parser.DefaultCommandParser;
+import com.moilioncircle.redis.replicator.event.Event;
+import com.moilioncircle.redis.replicator.event.EventListener;
+import com.moilioncircle.redis.replicator.event.PostRdbSyncEvent;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueHash;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueList;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueString;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyStringValueZSet;
+import com.moilioncircle.redis.replicator.rdb.datatype.KeyValuePair;
+import com.moilioncircle.redis.replicator.rdb.datatype.ZSetEntry;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.metrics.audit.AuditUtils;
+import org.apache.inlong.agent.plugin.Message;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/**
+ * Redis data reader
+ */
+public class RedisReader extends AbstractReader {
+
+    public static final String REDIS_READER_TAG_NAME = "AgentRedisMetric";
+    public static final String JOB_REDIS_PORT = "job.redisJob.port";
+    public static final String JOB_REDIS_HOSTNAME = "job.redisJob.hostname";
+    public static final String JOB_REDIS_SSL = "job.redisJob.ssl";
+    public static final String JOB_REDIS_AUTHUSER = "job.redisJob.authUser";
+    public static final String JOB_REDIS_AUTHPASSWORD = "job.redisJob.authPassword";
+    public static final String JOB_REDIS_READTIMEOUT = "job.redisJob.readTimeout";
+    public static final String JOB_REDIS_QUEUE_SIZE = "job.redisJob.queueSize";
+    public static final String JOB_REDIS_REPLID = "job.redisJob.replId";
+    public static final String JOB_REDIS_OFFSET = "job.redisJob.offset";
+    private static final Logger LOGGER = LoggerFactory.getLogger(RedisReader.class);
+    private String port;
+    private String hostName;
+    private boolean ssl;
+    private String authUser;
+    private String authPassword;
+    private String readTimeout;
+    private String instanceId;
+    private String replId;
+    private String snapShot;
+    private boolean destroyed;
+    private Replicator redisReplicator;
+    private LinkedBlockingQueue<String> redisMessageQueue;
+    private boolean finished = false;
+    private ExecutorService executor;
+    private Gson gson;
+
+    @Override
+    public void init(JobProfile jobConf) {
+        super.init(jobConf);
+        LOGGER.info("Init redis reader with jobConf {}", jobConf.toJsonStr());
+        port = jobConf.get(JOB_REDIS_PORT);
+        hostName = jobConf.get(JOB_REDIS_HOSTNAME);
+        ssl = jobConf.getBoolean(JOB_REDIS_SSL, false);
+        authUser = jobConf.get(JOB_REDIS_AUTHUSER, "");
+        authPassword = jobConf.get(JOB_REDIS_AUTHPASSWORD, "");
+        readTimeout = jobConf.get(JOB_REDIS_READTIMEOUT, "");
+        replId = jobConf.get(JOB_REDIS_REPLID, "");
+        snapShot = jobConf.get(JOB_REDIS_OFFSET, "-1");
+        instanceId = jobConf.getInstanceId();
+        finished = false;
+        redisMessageQueue = new LinkedBlockingQueue<>(jobConf.getInt(JOB_REDIS_QUEUE_SIZE, 10000));
+        initGson();
+        String uri = getRedisUri();
+        try {
+            redisReplicator = new RedisReplicator(uri);
+            initReplicator();
+            redisReplicator.addEventListener(new EventListener() {
+                @Override
+                public void onEvent(Replicator replicator, Event event) {
+                    try {
+                        if (event instanceof DefaultCommand || event instanceof KeyValuePair<?, ?>) {
+                            redisMessageQueue.put(gson.toJson(event));
+                            AuditUtils.add(AuditUtils.AUDIT_ID_AGENT_READ_SUCCESS, inlongGroupId, inlongStreamId,
+                                    System.currentTimeMillis(), 1);
+                            readerMetric.pluginReadCount.incrementAndGet();
+                        }
+                        if (event instanceof PostRdbSyncEvent) {
+                            snapShot = String.valueOf(replicator.getConfiguration().getReplOffset());
+                            LOGGER.info("after rdb snapShot is: {}", snapShot);
+                        }
+                    } catch (InterruptedException e) {
+                        readerMetric.pluginReadFailCount.incrementAndGet();
+                        LOGGER.error("Read redis data error", e);
+                    }
+                }
+            });
+            executor = Executors.newSingleThreadExecutor();
+            executor.execute(new Thread(() -> {
+                try {
+                    redisReplicator.open();
+                } catch (IOException e) {
+                    LOGGER.error("Redis source error", e);
+                }
+            }));
+        } catch (URISyntaxException | IOException e) {
+            readerMetric.pluginReadFailCount.addAndGet(1);
+            LOGGER.error("Connect to redis {}:{} failed.", hostName, port);
+        }
+    }
+
+    private String getRedisUri() {
+        StringBuffer sb = new StringBuffer("redis://");
+        sb.append(hostName).append(":").append(port);
+        sb.append("?");
+        if (authPassword != null && !authPassword.equals("")) {
+            sb.append("authPassword=").append(authPassword).append("&");
+        }
+        if (authUser != null && !authUser.equals("")) {
+            sb.append("authUser=").append(authUser).append("&");
+        }
+        if (readTimeout != null && !readTimeout.equals("")) {
+            sb.append("readTimeout=").append(readTimeout).append("&");
+        }
+        if (ssl) {
+            sb.append("ssl=").append("yes").append("&");
+        }
+        if (snapShot != null && !snapShot.equals("")) {
+            sb.append("replOffset=").append(snapShot).append("&");
+        }
+        if (replId != null && !replId.equals("")) {
+            sb.append("replId=").append(replId).append("&");
+        }
+        if (sb.charAt(sb.length() - 1) == '?' || sb.charAt(sb.length() - 1) == '&') {
+            sb.deleteCharAt(sb.length() - 1);
+        }
+        return sb.toString();
+    }
+
+    @Override
+    public void destroy() {
+        synchronized (this) {
+            if (!destroyed) {
+                try {
+                    executor.shutdown();
+                    redisReplicator.close();
+                } catch (IOException e) {
+                    LOGGER.error("Redis reader close failed.");
+                }
+                destroyed = true;
+            }
+        }
+    }
+
+    @Override
+    public Message read() {
+        if (!redisMessageQueue.isEmpty()) {
+            readerMetric.pluginReadCount.incrementAndGet();
+            return new DefaultMessage(redisMessageQueue.poll().getBytes());
+        } else {
+            return null;
+        }
+    }
+
+    @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() {
+        return snapShot;
+    }
+
+    @Override
+    public void finishRead() {
+        finished = true;
+    }
+
+    @Override
+    public boolean isSourceExist() {
+        return true;
+    }
+
+    /**
+     * init GSON parser
+     */
+    private void initGson() {
+        gson = new GsonBuilder().registerTypeAdapter(KeyStringValueHash.class, new TypeAdapter<KeyStringValueHash>() {
+
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueHash kv) throws IOException {
+                        out.beginObject();
+                        out.name("DB").beginObject();
+                        out.name("dbNumber").value(kv.getDb().getDbNumber());
+                        out.name("dbSize").value(kv.getDb().getDbsize());
+                        out.name("expires").value(kv.getDb().getExpires());
+                        out.endObject();
+                        out.name("valueRdbType").value(kv.getValueRdbType());
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").beginObject();
+                        for (byte[] b : kv.getValue().keySet()) {
+                            out.name(new String(b)).value(new String(kv.getValue().get(b)));
+                        }
+                        out.endObject();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueHash read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                }).registerTypeAdapter(DefaultCommand.class, new TypeAdapter<DefaultCommand>() {
+                    @Override
+                    public void write(JsonWriter out, DefaultCommand dc) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(dc.getCommand()));
+                        out.name("value").beginArray();
+                        for (byte[] bytes : dc.getArgs()) {
+                            out.value(new String(bytes));
+                        }
+                        out.endArray();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public DefaultCommand read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .registerTypeAdapter(KeyStringValueList.class, new TypeAdapter<KeyStringValueList>() {
+
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueList kv) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").beginArray();
+                        for (byte[] bytes : kv.getValue()) {
+                            out.value(new String(bytes));
+                        }
+                        out.endArray();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueList read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .registerTypeAdapter(KeyStringValueSet.class, new TypeAdapter<KeyStringValueSet>() {
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueSet kv) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").beginArray();
+                        for (byte[] bytes : kv.getValue()) {
+                            out.value(new String(bytes));
+                        }
+                        out.endArray();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueSet read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .registerTypeAdapter(KeyStringValueString.class, new TypeAdapter<KeyStringValueString>() {
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueString kv) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").value(new String(kv.getValue()));
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueString read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .registerTypeAdapter(KeyStringValueZSet.class, new TypeAdapter<KeyStringValueZSet>() {
+                    @Override
+                    public void write(JsonWriter out, KeyStringValueZSet kv) throws IOException {
+                        out.beginObject();
+                        out.name("key").value(new String(kv.getKey()));
+                        out.name("value").beginArray();
+                        for (ZSetEntry entry : kv.getValue()) {
+                            out.beginObject();
+                            out.name("element").value(new String(entry.getElement()));
+                            out.name("score").value(entry.getScore());
+                            out.endObject();
+                        }
+                        out.endArray();
+                        out.endObject();
+                    }
+
+                    @Override
+                    public KeyStringValueZSet read(JsonReader in) throws IOException {
+                        return null;
+                    }
+                })
+                .create();
+    }
+
+    /**
+     * init replicator's commandParser
+     */
+    private void initReplicator() {
+        redisReplicator.addCommandParser(CommandName.name("APPEND"), new DefaultCommandParser());

Review Comment:
   Each command has its own commandParser, so I have to change it into unified parser



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


[GitHub] [inlong] dockerzhang commented on pull request #5780: [INLONG-5722][Agent] Support Redis Source

Posted by GitBox <gi...@apache.org>.
dockerzhang commented on PR #5780:
URL: https://github.com/apache/inlong/pull/5780#issuecomment-1236868531

   @pocozh @EMsnap PTAL


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