use of io.aeron.logbuffer.FragmentHandler in project Aeron by real-logic.
the class ExclusivePublicationTest method shouldPublishFromConcurrentExclusivePublications.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldPublishFromConcurrentExclusivePublications(final String channel) {
try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
ExclusivePublication publicationOne = aeron.addExclusivePublication(channel, STREAM_ID);
ExclusivePublication publicationTwo = aeron.addExclusivePublication(channel, STREAM_ID)) {
final int expectedNumberOfFragments = 20_000;
final int fragmentsPerThread = expectedNumberOfFragments / 2;
final MutableInteger messageCount = new MutableInteger();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
assertEquals(MESSAGE_LENGTH, length);
messageCount.value++;
};
Tests.awaitConnections(subscription, 2);
final ExecutorService threadPool = Executors.newFixedThreadPool(2);
try {
final CountDownLatch latch = new CountDownLatch(2);
threadPool.submit(() -> {
latch.countDown();
latch.await();
for (int count = 0; count < fragmentsPerThread; count++) {
while (publicationOne.offer(srcBuffer, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
}
}
return null;
});
threadPool.submit(() -> {
latch.countDown();
latch.await();
for (int count = 0; count < fragmentsPerThread; count++) {
while (publicationTwo.offer(srcBuffer, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
}
}
return null;
});
int totalFragmentsRead = 0;
do {
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
} while (totalFragmentsRead < expectedNumberOfFragments);
} finally {
threadPool.shutdownNow();
}
assertEquals(expectedNumberOfFragments, messageCount.value);
}
}
use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class ClusterTimerTest method launchReschedulingService.
private void launchReschedulingService(final AtomicLong triggeredTimersCounter) {
final ClusteredService service = new StubClusteredService() {
private int timerId = 1;
public void onTimerEvent(final long correlationId, final long timestamp) {
triggeredTimersCounter.getAndIncrement();
scheduleNext(serviceCorrelationId(timerId++), timestamp + INTERVAL_MS);
}
public void onStart(final Cluster cluster, final Image snapshotImage) {
super.onStart(cluster, snapshotImage);
this.cluster = cluster;
if (null != snapshotImage) {
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> timerId = buffer.getInt(offset);
while (true) {
final int fragments = snapshotImage.poll(fragmentHandler, 1);
if (fragments == 1 || snapshotImage.isEndOfStream()) {
break;
}
idleStrategy.idle();
}
}
}
public void onTakeSnapshot(final ExclusivePublication snapshotPublication) {
final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer(SIZE_OF_INT);
buffer.putInt(0, timerId);
idleStrategy.reset();
while (snapshotPublication.offer(buffer, 0, SIZE_OF_INT) < 0) {
idleStrategy.idle();
}
}
public void onNewLeadershipTermEvent(final long leadershipTermId, final long logPosition, final long timestamp, final long termBaseLogPosition, final int leaderMemberId, final int logSessionId, final TimeUnit timeUnit, final int appVersion) {
scheduleNext(serviceCorrelationId(timerId++), timestamp + INTERVAL_MS);
}
private void scheduleNext(final long correlationId, final long deadlineMs) {
idleStrategy.reset();
while (!cluster.scheduleTimer(correlationId, deadlineMs)) {
idleStrategy.idle();
}
}
};
container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
}
use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class ClusterNodeRestartTest method launchService.
private void launchService(final AtomicLong msgCounter) {
final ClusteredService service = new StubClusteredService() {
private int nextCorrelationId = 0;
private int counterValue = 0;
public void onStart(final Cluster cluster, final Image snapshotImage) {
super.onStart(cluster, snapshotImage);
if (null != snapshotImage) {
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
nextCorrelationId = buffer.getInt(offset);
offset += SIZE_OF_INT;
counterValue = buffer.getInt(offset);
offset += SIZE_OF_INT;
serviceState.set(buffer.getStringAscii(offset));
};
while (true) {
final int fragments = snapshotImage.poll(fragmentHandler, 1);
if (fragments == 1 || snapshotImage.isEndOfStream()) {
break;
}
idleStrategy.idle();
}
}
}
public void onSessionMessage(final ClientSession session, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
final int sentValue = buffer.getInt(offset + MESSAGE_VALUE_OFFSET);
assertEquals(counterValue, sentValue);
counterValue++;
serviceState.set(Integer.toString(counterValue));
msgCounter.getAndIncrement();
if (TIMER_MESSAGE_LENGTH == length) {
final long correlationId = serviceCorrelationId(nextCorrelationId++);
final long deadlineMs = timestamp + buffer.getLong(offset + TIMER_MESSAGE_DELAY_OFFSET);
while (!cluster.scheduleTimer(correlationId, deadlineMs)) {
idleStrategy.idle();
}
}
}
public void onTakeSnapshot(final ExclusivePublication snapshotPublication) {
final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();
int length = 0;
buffer.putInt(length, nextCorrelationId);
length += SIZE_OF_INT;
buffer.putInt(length, counterValue);
length += SIZE_OF_INT;
length += buffer.putStringAscii(length, Integer.toString(counterValue));
snapshotPublication.offer(buffer, 0, length);
}
};
container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
}
use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class ClusterNodeRestartTest method launchReschedulingService.
private void launchReschedulingService(final AtomicLong triggeredTimersCounter) {
final ClusteredService service = new StubClusteredService() {
public void onSessionMessage(final ClientSession session, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
scheduleNext(serviceCorrelationId(7), timestamp + 200);
}
public void onTimerEvent(final long correlationId, final long timestamp) {
triggeredTimersCounter.getAndIncrement();
scheduleNext(correlationId, timestamp + 200);
}
public void onStart(final Cluster cluster, final Image snapshotImage) {
super.onStart(cluster, snapshotImage);
if (null != snapshotImage) {
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> triggeredTimersCounter.set(buffer.getLong(offset));
while (true) {
final int fragments = snapshotImage.poll(fragmentHandler, 1);
if (fragments == 1 || snapshotImage.isEndOfStream()) {
break;
}
idleStrategy.idle();
}
}
}
public void onTakeSnapshot(final ExclusivePublication snapshotPublication) {
final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();
buffer.putLong(0, triggeredTimersCounter.get());
while (snapshotPublication.offer(buffer, 0, SIZE_OF_INT) < 0) {
idleStrategy.idle();
}
}
private void scheduleNext(final long correlationId, final long deadline) {
idleStrategy.reset();
while (!cluster.scheduleTimer(correlationId, deadline)) {
idleStrategy.idle();
}
}
};
container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
}
use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class SubscriptionTest method shouldReadDataFromMultipleSources.
@Test
public void shouldReadDataFromMultipleSources() {
subscription.addImage(imageOneMock);
subscription.addImage(imageTwoMock);
when(imageOneMock.poll(any(FragmentHandler.class), anyInt())).then((invocation) -> {
final FragmentHandler handler = (FragmentHandler) invocation.getArguments()[0];
handler.onFragment(atomicReadBuffer, HEADER_LENGTH, READ_BUFFER_CAPACITY - HEADER_LENGTH, header);
return 1;
});
when(imageTwoMock.poll(any(FragmentHandler.class), anyInt())).then((invocation) -> {
final FragmentHandler handler = (FragmentHandler) invocation.getArguments()[0];
handler.onFragment(atomicReadBuffer, HEADER_LENGTH, READ_BUFFER_CAPACITY - HEADER_LENGTH, header);
return 1;
});
assertEquals(2, subscription.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT));
}
Aggregations