You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/06/21 07:06:12 UTC

[GitHub] [pulsar] 315157973 commented on a diff in pull request #16003: [feature][pulsar-io-mongo] Add support for full message synchronization

315157973 commented on code in PR #16003:
URL: https://github.com/apache/pulsar/pull/16003#discussion_r902206842


##########
pulsar-io/mongo/src/main/java/org/apache/pulsar/io/mongodb/MongoSource.java:
##########
@@ -79,38 +78,46 @@ public MongoSource(Supplier<MongoClient> clientProvider) {
     public void open(Map<String, Object> config, SourceContext sourceContext) throws Exception {
         log.info("Open MongoDB Source");
 
-        mongoConfig = MongoConfig.load(config);
-        mongoConfig.validate(false, false);
+        mongoSourceConfig = MongoSourceConfig.load(config);
+        mongoSourceConfig.validate();
 
         if (clientProvider != null) {
             mongoClient = clientProvider.get();
         } else {
-            mongoClient = MongoClients.create(mongoConfig.getMongoUri());
+            mongoClient = MongoClients.create(mongoSourceConfig.getMongoUri());
         }
 
-        if (StringUtils.isEmpty(mongoConfig.getDatabase())) {
+        if (StringUtils.isEmpty(mongoSourceConfig.getDatabase())) {
             // Watch all databases
             log.info("Watch all");
             stream = mongoClient.watch();
 
         } else {
-            final MongoDatabase db = mongoClient.getDatabase(mongoConfig.getDatabase());
+            final MongoDatabase db = mongoClient.getDatabase(mongoSourceConfig.getDatabase());
 
-            if (StringUtils.isEmpty(mongoConfig.getCollection())) {
+            if (StringUtils.isEmpty(mongoSourceConfig.getCollection())) {
                 // Watch all collections in a database
                 log.info("Watch db: {}", db.getName());
                 stream = db.watch();
 
             } else {
                 // Watch a collection
-
-                final MongoCollection<Document> collection = db.getCollection(mongoConfig.getCollection());
-                log.info("Watch collection: {} {}", db.getName(), mongoConfig.getCollection());
+                final MongoCollection<Document> collection = db.getCollection(mongoSourceConfig.getCollection());
+                log.info("Watch collection: {}.{}", db.getName(), mongoSourceConfig.getCollection());
                 stream = collection.watch();
             }
         }
 
-        stream.batchSize(mongoConfig.getBatchSize()).fullDocument(FullDocument.UPDATE_LOOKUP);
+        stream.batchSize(mongoSourceConfig.getBatchSize())
+                .fullDocument(FullDocument.UPDATE_LOOKUP);
+
+        if (SyncType.FULL_SYNC.equals(mongoSourceConfig.getSyncType())) {

Review Comment:
   use `==` instead of `equals`



##########
pulsar-io/mongo/src/main/java/org/apache/pulsar/io/mongodb/MongoSourceConfig.java:
##########
@@ -0,0 +1,102 @@
+/**
+ * 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.pulsar.io.mongodb;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
+import java.io.File;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+import org.apache.pulsar.io.core.annotations.FieldDoc;
+
+/**
+ * Configuration class for the MongoDB Source Connectors.
+ */
+@Data
+@EqualsAndHashCode(callSuper = false)
+@Accessors(chain = true)
+public class MongoSourceConfig extends MongoAbstractConfig {
+
+    private static final long serialVersionUID = 1152890092264945317L;
+
+    public static final SyncType DEFAULT_SYNC_TYPE = SyncType.INCR_SYNC;
+
+    public static final String DEFAULT_SYNC_TYPE_STR = "incr";
+
+    @FieldDoc(
+            defaultValue = DEFAULT_SYNC_TYPE_STR,
+            help = "The message synchronization type of the source connector. "
+                    + "The field values can be of two types: incr and full. "
+                    + "When it is set to incr, the source connector will only watch for changes made from now on. "
+                    + "When it is set to full, the source connector will synchronize currently existing messages "
+                    + "and watch for future changes."
+    )
+    private SyncType syncType = DEFAULT_SYNC_TYPE;
+
+    public static MongoSourceConfig load(String yamlFile) throws IOException {
+        final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
+        final MongoSourceConfig cfg = mapper.readValue(new File(yamlFile), MongoSourceConfig.class);
+
+        return cfg;
+    }
+
+    public static MongoSourceConfig load(Map<String, Object> map) throws IOException {
+        final ObjectMapper mapper = new ObjectMapper();
+        final MongoSourceConfig cfg =
+                mapper.readValue(new ObjectMapper().writeValueAsString(map), MongoSourceConfig.class);
+
+        return cfg;
+    }
+
+    /**
+     * Override syncType setter method.
+     *
+     * @param syncType Sync type string.
+     */
+    public void setSyncType(String syncType) {

Review Comment:
   Why not use `@JsonCreator` ? 
   The type string must be consistent with the enumeration. If the type string cannot be parsed, an exception must be thrown.
   



##########
pulsar-io/mongo/src/main/java/org/apache/pulsar/io/mongodb/MongoSourceConfig.java:
##########
@@ -0,0 +1,102 @@
+/**
+ * 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.pulsar.io.mongodb;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
+import java.io.File;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+import org.apache.pulsar.io.core.annotations.FieldDoc;
+
+/**
+ * Configuration class for the MongoDB Source Connectors.
+ */
+@Data
+@EqualsAndHashCode(callSuper = false)
+@Accessors(chain = true)
+public class MongoSourceConfig extends MongoAbstractConfig {
+
+    private static final long serialVersionUID = 1152890092264945317L;
+
+    public static final SyncType DEFAULT_SYNC_TYPE = SyncType.INCR_SYNC;
+
+    public static final String DEFAULT_SYNC_TYPE_STR = "incr";
+
+    @FieldDoc(
+            defaultValue = DEFAULT_SYNC_TYPE_STR,
+            help = "The message synchronization type of the source connector. "
+                    + "The field values can be of two types: incr and full. "
+                    + "When it is set to incr, the source connector will only watch for changes made from now on. "
+                    + "When it is set to full, the source connector will synchronize currently existing messages "
+                    + "and watch for future changes."
+    )
+    private SyncType syncType = DEFAULT_SYNC_TYPE;
+
+    public static MongoSourceConfig load(String yamlFile) throws IOException {
+        final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
+        final MongoSourceConfig cfg = mapper.readValue(new File(yamlFile), MongoSourceConfig.class);
+
+        return cfg;
+    }
+
+    public static MongoSourceConfig load(Map<String, Object> map) throws IOException {
+        final ObjectMapper mapper = new ObjectMapper();
+        final MongoSourceConfig cfg =
+                mapper.readValue(new ObjectMapper().writeValueAsString(map), MongoSourceConfig.class);
+
+        return cfg;
+    }
+
+    /**
+     * Override syncType setter method.
+     *
+     * @param syncType Sync type string.
+     */
+    public void setSyncType(String syncType) {
+        if ("full".equalsIgnoreCase(syncType)
+                || "full_sync".equalsIgnoreCase(syncType)
+                || "full-sync".equalsIgnoreCase(syncType)) {
+
+            this.syncType = SyncType.FULL_SYNC;
+            return;
+        }
+
+        if ("incr".equalsIgnoreCase(syncType)
+                || "increment".equalsIgnoreCase(syncType)
+                || "incr_sync".equalsIgnoreCase(syncType)
+                || "incr-sync".equalsIgnoreCase(syncType)) {
+
+            this.syncType = SyncType.INCR_SYNC;
+            return;
+        }
+
+        this.syncType = DEFAULT_SYNC_TYPE;
+    }
+
+    @Override
+    public void validate() {
+        super.validate();
+        checkNotNull(getSyncType(), "syncType not set.");

Review Comment:
   This should never be null.
   



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

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