Jiabao-Sun commented on code in PR #12:
URL: 
https://github.com/apache/flink-connector-mongodb/pull/12#discussion_r1244727850


##########
flink-connector-mongodb/src/main/java/org/apache/flink/connector/mongodb/sink/writer/MongoWriter.java:
##########
@@ -105,10 +119,37 @@ public MongoWriter(
 
         // Initialize the mongo client.
         this.mongoClient = MongoClients.create(connectionOptions.getUri());
+
+        boolean flushOnlyOnCheckpoint = batchIntervalMs == -1 && batchSize == 
-1;
+
+        if (!flushOnlyOnCheckpoint && batchIntervalMs > 0) {
+            this.scheduler =
+                    Executors.newScheduledThreadPool(1, new 
ExecutorThreadFactory("mongo-writer"));
+
+            this.scheduledFuture =
+                    this.scheduler.scheduleWithFixedDelay(
+                            () -> {
+                                synchronized (MongoWriter.this) {
+                                    if (!closed && 
isOverMaxBatchIntervalLimit()) {
+                                        try {
+                                            doBulkWrite();
+                                        } catch (Exception e) {
+                                            flushException = e;
+                                        }
+                                    }
+                                }
+                            },
+                            batchIntervalMs,
+                            batchIntervalMs,
+                            TimeUnit.MILLISECONDS);
+        }
     }
 
     @Override
-    public void write(IN element, Context context) throws IOException, 
InterruptedException {
+    public synchronized void write(IN element, Context context)

Review Comment:
   Thanks @6harat with this comment.
   Adding `synchronized` here because we may add an other thread to check 
whether the last write time is over limit.
   The thread may call `doBulkWrite` method and clear the `bulkRequests`  list 
which is write by `write` method introduces a race condition.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to