diff --git a/seatunnel-engine/seatunnel-engine-storage/imap-storage-plugins/imap-storage-file/src/main/java/org/apache/seatunnel/engine/imap/storage/file/disruptor/WALDisruptor.java b/seatunnel-engine/seatunnel-engine-storage/imap-storage-plugins/imap-storage-file/src/main/java/org/apache/seatunnel/engine/imap/storage/file/disruptor/WALDisruptor.java index 35db6d2842f9..d584762e3b11 100644 --- a/seatunnel-engine/seatunnel-engine-storage/imap-storage-plugins/imap-storage-file/src/main/java/org/apache/seatunnel/engine/imap/storage/file/disruptor/WALDisruptor.java +++ b/seatunnel-engine/seatunnel-engine-storage/imap-storage-plugins/imap-storage-file/src/main/java/org/apache/seatunnel/engine/imap/storage/file/disruptor/WALDisruptor.java @@ -64,14 +64,15 @@ public WALDisruptor( FileConfiguration fileConfiguration, String parentPath, Serializer serializer) { - // todo should support multi thread producer + // Hazelcast calls the map store from several partition threads at once, so events are + // published concurrently and a single-producer ring buffer would lose some of them. ThreadFactory threadFactory = DaemonThreadFactory.INSTANCE; this.disruptor = new Disruptor<>( FileWALEvent.FACTORY, DEFAULT_RING_BUFFER_SIZE, threadFactory, - ProducerType.SINGLE, + ProducerType.MULTI, new BlockingWaitStrategy()); disruptor.handleEventsWithWorkerPool( diff --git a/seatunnel-engine/seatunnel-engine-storage/imap-storage-plugins/imap-storage-file/src/test/java/org/apache/seatunnel/engine/imap/storage/file/disruptor/WALDisruptorTest.java b/seatunnel-engine/seatunnel-engine-storage/imap-storage-plugins/imap-storage-file/src/test/java/org/apache/seatunnel/engine/imap/storage/file/disruptor/WALDisruptorTest.java index a8bc80a89a7b..802310fd37aa 100644 --- a/seatunnel-engine/seatunnel-engine-storage/imap-storage-plugins/imap-storage-file/src/test/java/org/apache/seatunnel/engine/imap/storage/file/disruptor/WALDisruptorTest.java +++ b/seatunnel-engine/seatunnel-engine-storage/imap-storage-plugins/imap-storage-file/src/test/java/org/apache/seatunnel/engine/imap/storage/file/disruptor/WALDisruptorTest.java @@ -36,6 +36,15 @@ import org.junit.jupiter.api.condition.EnabledOnOs; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import static org.junit.jupiter.api.condition.OS.LINUX; import static org.junit.jupiter.api.condition.OS.MAC; @@ -80,6 +89,76 @@ void testProducerAndConsumer() throws IOException { DISRUPTOR.close(); } + /** + * Hazelcast partition threads publish concurrently, so every event must be handled even when + * several producers claim a slot at the same time or wait together for a full ring buffer. + */ + @Test + void testConcurrentProducers() throws Exception { + FS = FileSystem.get(CONF); + int producerCount = 8; + // More events than the ring buffer holds, so that producers also block on a full buffer. + int eventsPerProducer = 500; + WALDisruptor disruptor = + new WALDisruptor( + FS, + FileConfiguration.HDFS, + FILEPATH + "concurrent/", + new ProtoStuffSerializer()); + Map futures = new ConcurrentHashMap<>(); + ExecutorService producers = Executors.newFixedThreadPool(producerCount); + try { + CountDownLatch start = new CountDownLatch(1); + List> tasks = new ArrayList<>(); + for (int p = 0; p < producerCount; p++) { + String producer = "producer" + p; + tasks.add( + producers.submit( + () -> { + start.await(); + for (int i = 0; i < eventsPerProducer; i++) { + IMapFileData data = + IMapFileData.builder() + .deleted(false) + .key((producer + "-key" + i).getBytes()) + .keyClassName(String.class.getName()) + .value(("value" + i).getBytes()) + .valueClassName(String.class.getName()) + .timestamp(System.nanoTime()) + .build(); + long requestId = RequestFutureCache.getRequestId(); + RequestFuture future = new RequestFuture(); + futures.put(requestId, future); + RequestFutureCache.put(requestId, future); + disruptor.tryAppendPublish(data, requestId); + } + return null; + })); + } + start.countDown(); + + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(30); + for (Future task : tasks) { + task.get(deadline - System.nanoTime(), TimeUnit.NANOSECONDS); + } + // A lost event is never handled, so its request is never acknowledged. + int acknowledged = 0; + for (RequestFuture future : futures.values()) { + if (future.get(deadline - System.nanoTime(), TimeUnit.NANOSECONDS)) { + acknowledged++; + } + } + Assertions.assertEquals( + producerCount * eventsPerProducer, + acknowledged, + "Every concurrently published event must be acknowledged"); + } finally { + producers.shutdownNow(); + futures.keySet().forEach(RequestFutureCache::remove); + disruptor.close(); + } + } + @AfterAll public static void afterAll() throws IOException { Assertions.assertTrue(FS.delete(new Path(FILEPATH), true));