davidzollo commented on code in PR #10399:
URL: https://github.com/apache/seatunnel/pull/10399#discussion_r2736514178


##########
seatunnel-engine/seatunnel-engine-storage/imap-storage-plugins/imap-storage-file/src/main/java/org/apache/seatunnel/engine/imap/storage/file/disruptor/WALCompactionDisruptor.java:
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.seatunnel.engine.imap.storage.file.disruptor;
+
+import 
org.apache.seatunnel.engine.imap.storage.api.exception.IMapStorageException;
+import org.apache.seatunnel.engine.imap.storage.file.bean.IMapFileData;
+import org.apache.seatunnel.engine.imap.storage.file.common.WALLSMWriter;
+import org.apache.seatunnel.engine.imap.storage.file.common.WALWriter;
+import org.apache.seatunnel.engine.imap.storage.file.config.FileConfiguration;
+import org.apache.seatunnel.engine.serializer.api.Serializer;
+
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+
+import com.lmax.disruptor.BlockingWaitStrategy;
+import com.lmax.disruptor.EventTranslatorOneArg;
+import com.lmax.disruptor.TimeoutException;
+import com.lmax.disruptor.dsl.Disruptor;
+import com.lmax.disruptor.dsl.ProducerType;
+import com.lmax.disruptor.util.DaemonThreadFactory;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+
+@Slf4j
+public class WALCompactionDisruptor extends AbstractWALDisruptor {
+    private static final EventTranslatorOneArg<FileWALEvent, WALEventType> 
COMPACTION_TRANSLATOR =
+            (event, sequence, status) -> {
+                event.setData(null);
+                event.setType(status);
+                event.setRequestId(0L);
+            };
+
+    private Disruptor<FileWALEvent> compactionDisruptor;
+
+    public WALCompactionDisruptor(
+            FileSystem fs,
+            FileConfiguration fileConfiguration,
+            String parentPath,
+            Serializer serializer,
+            Map<String, Object> config) {
+        ThreadFactory threadFactory = DaemonThreadFactory.INSTANCE;
+        this.disruptor =
+                new Disruptor<>(
+                        FileWALEvent.FACTORY,
+                        DEFAULT_RING_BUFFER_SIZE,
+                        threadFactory,
+                        ProducerType.SINGLE,
+                        new BlockingWaitStrategy());
+
+        WALWriter writer;
+        try {
+            writer =
+                    new WALLSMWriter(
+                            fs, fileConfiguration, new Path(parentPath), 
serializer, config);
+        } catch (IOException e) {
+            throw new IMapStorageException(
+                    e, "create new current writer failed, parent path is %s", 
parentPath);
+        }
+
+        disruptor.handleEventsWithWorkerPool(new WALWorkHandler(writer));
+
+        disruptor.start();
+
+        this.compactionDisruptor =
+                new Disruptor<>(
+                        FileWALEvent.FACTORY,
+                        DEFAULT_RING_BUFFER_SIZE,
+                        threadFactory,
+                        ProducerType.SINGLE,
+                        new BlockingWaitStrategy());
+
+        compactionDisruptor.handleEventsWithWorkerPool(
+                new WALCompactionWorkHandler((WALLSMWriter) writer));
+
+        compactionDisruptor.start();
+    }
+
+    @Override
+    public boolean tryPublish(IMapFileData message, WALEventType status, long 
requestId) {
+        if (isClosed()) {
+            return false;
+        }
+        disruptor.getRingBuffer().publishEvent(TRANSLATOR, message, status, 
requestId);
+        
compactionDisruptor.getRingBuffer().publishEvent(COMPACTION_TRANSLATOR, status);
+        return true;

Review Comment:
   The current implementation couples the compaction trigger directly with the 
write path in a 1:1 ratio. In `WALCompactionDisruptor.tryPublish()`, **every 
single write request** publishes a corresponding event to the 
`compactionDisruptor`. This causes the `WALCompactionWorkHandler` to invoke the 
`writer.compaction()` method for every single record written to the WAL.
   
   Although the `compaction()` method performs a check (`totalBytes < 
threshold`) to return early, this design is fundamentally flawed for 
high-throughput scenarios:
       *   **Event Flooding**: A bulk write of 100,000 records will trigger 
100,000 unnecessary calls to the compaction logic.
       *   **Resource Waste**: The compaction thread is forced into a 
"busy-check" loop, consuming CPU cycles to check thresholds repeatedly instead 
of sleeping until needed.
       *   **Coupling**: Compaction is a heavy io maintenance task and should 
not be driven by the frequency of incoming data ingestion.
   
   
   **Recommendation**:
   Since compaction is a heavy I/O operation, it must be decoupled from the 
high-frequency write event stream. I think you can implement **multiple trigger 
strategies** to make it robust:
       1.  **Scheduled Trigger (Time-based)**: Use a `ScheduledExecutorService` 
to trigger the compaction check periodically (e.g., every 60 seconds). This is 
the most predictable method.
       2.  **Active Threshold Check**: Trigger only when the `totalBytes` 
counter crosses a specific watermark, rather than checking on every increment.
   
   Remove the `compactionDisruptor` event publishing from `tryPublish`. 
Initialize a background scheduler in `IMapFileStorage` or `AbstractLSMWriter` 
that invokes `compaction()` at a configurable interval (`compactionInterval`).
   
   



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