JingsongLi commented on code in PR #1690:
URL: https://github.com/apache/incubator-paimon/pull/1690#discussion_r1280254879


##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/AppendOnlyTableCompactionWorkerOperator.java:
##########
@@ -52,25 +70,105 @@ public AppendOnlyTableCompactionWorkerOperator(
 
     @Override
     public void open() throws Exception {
+        LOG.debug("Opened a append-only table compaction worker.");
         this.worker = new AppendOnlyTableCompactionWorker(table, commitUser);
-        this.result = new ArrayList<>();
+        this.result = new LinkedList<>();
     }
 
     @Override
     protected List<Committable> prepareCommit(boolean waitCompaction, long 
checkpointId)
             throws IOException {
-        // ignore waitCompaction tag
-        ArrayList<CommitMessage> tempList = new ArrayList<>(result);
-        result.clear();
-        return tempList.stream()
-                .map(s -> new Committable(checkpointId, Committable.Kind.FILE, 
s))
-                .collect(Collectors.toList());
+        List<CommitMessage> tempList = new ArrayList<>();
+        try {
+            while (!result.isEmpty()) {
+                Future<List<CommitMessage>> future = result.peek();
+                if (!future.isDone()) {

Review Comment:
   ```
   if (!future.isDone() && !waitCompaction) {
       break;
   }
   ....
   ```



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/AppendOnlyTableCompactionWorkerOperator.java:
##########
@@ -52,25 +70,105 @@ public AppendOnlyTableCompactionWorkerOperator(
 
     @Override
     public void open() throws Exception {
+        LOG.debug("Opened a append-only table compaction worker.");
         this.worker = new AppendOnlyTableCompactionWorker(table, commitUser);
-        this.result = new ArrayList<>();
+        this.result = new LinkedList<>();
     }
 
     @Override
     protected List<Committable> prepareCommit(boolean waitCompaction, long 
checkpointId)
             throws IOException {
-        // ignore waitCompaction tag
-        ArrayList<CommitMessage> tempList = new ArrayList<>(result);
-        result.clear();
-        return tempList.stream()
-                .map(s -> new Committable(checkpointId, Committable.Kind.FILE, 
s))
-                .collect(Collectors.toList());
+        List<CommitMessage> tempList = new ArrayList<>();
+        try {
+            while (!result.isEmpty()) {
+                Future<List<CommitMessage>> future = result.peek();
+                if (!future.isDone()) {
+                    if (waitCompaction) {
+                        while (!future.isDone()) {
+                            Thread.sleep(1000L);
+                        }
+                    } else {
+                        break;
+                    }
+                }
+
+                result.poll();
+                tempList.addAll(future.get());
+            }
+            return tempList.stream()
+                    .map(s -> new Committable(checkpointId, 
Committable.Kind.FILE, s))
+                    .collect(Collectors.toList());
+        } catch (InterruptedException e) {
+            throw new RuntimeException("Interrupted while waiting tasks 
done.", e);
+        } catch (Exception e) {
+            throw new RuntimeException("Encountered an error while do 
compaction", e);
+        }
     }
 
     @Override
     public void processElement(StreamRecord<AppendOnlyCompactionTask> element) 
throws Exception {
         AppendOnlyCompactionTask task = element.getValue();
-        worker.accept(task);
-        result.addAll(worker.doCompact());
+        result.add(
+                workerExecutor()
+                        .submit(
+                                () -> {
+                                    worker.accept(task);

Review Comment:
   Can we refine the `AppendOnlyTableCompactionWorker`?
   Actually, we don't need `AppendOnlyTableCompactionWorker`?



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/AppendOnlyTableCompactionWorkerOperator.java:
##########
@@ -52,25 +70,105 @@ public AppendOnlyTableCompactionWorkerOperator(
 
     @Override
     public void open() throws Exception {
+        LOG.debug("Opened a append-only table compaction worker.");
         this.worker = new AppendOnlyTableCompactionWorker(table, commitUser);
-        this.result = new ArrayList<>();
+        this.result = new LinkedList<>();
     }
 
     @Override
     protected List<Committable> prepareCommit(boolean waitCompaction, long 
checkpointId)
             throws IOException {
-        // ignore waitCompaction tag
-        ArrayList<CommitMessage> tempList = new ArrayList<>(result);
-        result.clear();
-        return tempList.stream()
-                .map(s -> new Committable(checkpointId, Committable.Kind.FILE, 
s))
-                .collect(Collectors.toList());
+        List<CommitMessage> tempList = new ArrayList<>();
+        try {
+            while (!result.isEmpty()) {
+                Future<List<CommitMessage>> future = result.peek();
+                if (!future.isDone()) {
+                    if (waitCompaction) {
+                        while (!future.isDone()) {
+                            Thread.sleep(1000L);
+                        }
+                    } else {
+                        break;
+                    }
+                }
+
+                result.poll();
+                tempList.addAll(future.get());
+            }
+            return tempList.stream()
+                    .map(s -> new Committable(checkpointId, 
Committable.Kind.FILE, s))
+                    .collect(Collectors.toList());
+        } catch (InterruptedException e) {
+            throw new RuntimeException("Interrupted while waiting tasks 
done.", e);
+        } catch (Exception e) {
+            throw new RuntimeException("Encountered an error while do 
compaction", e);
+        }
     }
 
     @Override
     public void processElement(StreamRecord<AppendOnlyCompactionTask> element) 
throws Exception {
         AppendOnlyCompactionTask task = element.getValue();
-        worker.accept(task);
-        result.addAll(worker.doCompact());
+        result.add(
+                workerExecutor()
+                        .submit(
+                                () -> {
+                                    worker.accept(task);
+                                    return worker.doCompact();
+                                }));
+    }
+
+    private ExecutorService workerExecutor() {
+        if (lazyCompactExecutor == null) {
+            lazyCompactExecutor =
+                    Executors.newSingleThreadScheduledExecutor(
+                            new ExecutorThreadFactory(
+                                    Thread.currentThread().getName()
+                                            + "-append-only-compact-worker"));
+        }
+        return lazyCompactExecutor;
+    }
+
+    @Override
+    public void close() throws Exception {
+        shutdown();
+    }
+
+    @VisibleForTesting
+    void shutdown() {
+        if (lazyCompactExecutor != null) {
+            // ignore runnable tasks in queue
+            lazyCompactExecutor.shutdownNow();
+
+            // remove the file already written
+            FileIO fileIO = table.fileIO();
+
+            for (Future<List<CommitMessage>> resultFuture : result) {
+                if (!resultFuture.isDone()) {
+                    // the later tasks should be stopped running
+                    break;
+                }
+                List<CommitMessage> messages;
+                try {
+                    messages = resultFuture.get();
+                } catch (Exception exception) {
+                    // exception should already be handled
+                    continue;
+                }
+                for (CommitMessage uncommitMessage : messages) {
+                    CommitMessageImpl uncommitMessageImpl = 
(CommitMessageImpl) uncommitMessage;
+                    List<DataFileMeta> fileMetas =

Review Comment:
   See `TableCommit.abort`?



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