Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Long, RequestFuture> futures = new ConcurrentHashMap<>();
ExecutorService producers = Executors.newFixedThreadPool(producerCount);
try {
CountDownLatch start = new CountDownLatch(1);
List<Future<?>> 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));
Expand Down