leaves12138 commented on code in PR #8857:
URL: https://github.com/apache/paimon/pull/8857#discussion_r3655916909


##########
paimon-common/src/main/java/org/apache/paimon/lookup/sort/db/AsyncLsmCompactor.java:
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.paimon.lookup.sort.db;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+/** Coordinates asynchronous compaction, failure propagation, backpressure, 
and shutdown. */
+class AsyncLsmCompactor extends LsmCompactor {
+
+    private final ExecutorService executor;
+
+    @Nullable private Future<?> compactionFuture;
+
+    AsyncLsmCompactor(
+            LsmLevels levels,
+            CompactorFactory compactorFactory,
+            int levelZeroFileCountTrigger,
+            UniversalCompactor.FileSupplier fileSupplier,
+            UniversalCompactor.FileDeleter fileDeleter,
+            ExecutorService executor) {
+        super(levels, compactorFactory, levelZeroFileCountTrigger, 
fileSupplier, fileDeleter);
+        this.executor = executor;
+    }
+
+    @Override
+    void scheduleIfNeeded() throws IOException {
+        checkFailure();
+        if (!needsCompaction() || compactionFuture != null) {

Review Comment:
   Could we coordinate worker termination and scheduling so a flush cannot miss 
compaction while the current `Future` is about to complete?
   
   There is a race here:
   
   1. The worker finishes a compaction round and its final `needsCompaction()` 
observes `false`.
   2. Before that worker's `Future` completes, the foreground flush adds enough 
L0 files and calls `scheduleIfNeeded()`.
   3. `needsCompaction()` is now `true`, but `compactionFuture != null`, so 
this method does not submit a new task.
   4. The old worker then returns, leaving the newly flushed L0 files 
uncompacted until another write (and a close in the same window can miss it as 
well).
   
   I reproduced this deterministically by pausing the worker after its final 
false stability check, adding a new L0 file, calling `scheduleIfNeeded()`, and 
then releasing the worker. After awaiting the old future, the L0 file count 
remains `1` and no task is pending. At the backpressure threshold, 
`applyBackpressure()` can similarly await the old future and return without 
scheduling the required compaction.
   
   The running/pending state and the final stability check need a shared 
synchronization boundary, or an equivalent rescheduling handshake, rather than 
relying only on the non-null `Future` reference.



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