use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class ArchiveTest method validateActiveRecordingReplay.
private Thread validateActiveRecordingReplay(final ArchiveProxy archiveProxy, final int termBufferLength, final int initialTermId, final int maxPayloadLength, final CountDownLatch completeLatch) {
final Thread thread = new Thread(() -> {
while (0 == recorded) {
Tests.sleep(1);
}
try (Subscription replay = client.addSubscription(REPLAY_URI, REPLAY_STREAM_ID)) {
final long replayCorrelationId = client.nextCorrelationId();
if (!archiveProxy.replay(recordingId, startPosition, Long.MAX_VALUE, REPLAY_URI, REPLAY_STREAM_ID, replayCorrelationId, controlSessionId)) {
throw new IllegalStateException("failed to start replay");
}
ArchiveTests.awaitOk(controlResponse, replayCorrelationId);
Tests.awaitConnected(replay);
final Image image = replay.images().get(0);
assertEquals(initialTermId, image.initialTermId());
assertEquals(maxPayloadLength + HEADER_LENGTH, image.mtuLength());
assertEquals(termBufferLength, image.termBufferLength());
assertEquals(startPosition, image.position());
messageCount = 0;
remaining = totalDataLength;
final FragmentHandler fragmentHandler = this::validateFragment;
while (messageCount < MESSAGE_COUNT) {
final int fragments = replay.poll(fragmentHandler, 10);
if (0 == fragments) {
if (!replay.isConnected()) {
break;
}
Tests.yield();
}
}
}
completeLatch.countDown();
});
thread.setName("replay-consumer");
thread.setDaemon(true);
thread.start();
return thread;
}
use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class TwoBufferOfferMessageTest method shouldTransferUnfragmentedTwoPartMessage.
@Test
@InterruptAfter(10)
void shouldTransferUnfragmentedTwoPartMessage() {
final UnsafeBuffer expectedBuffer = new UnsafeBuffer(new byte[256]);
final UnsafeBuffer bufferOne = new UnsafeBuffer(expectedBuffer, 0, 32);
final UnsafeBuffer bufferTwo = new UnsafeBuffer(expectedBuffer, 32, expectedBuffer.capacity() - 32);
bufferOne.setMemory(0, bufferOne.capacity(), (byte) 'a');
bufferTwo.setMemory(0, bufferTwo.capacity(), (byte) 'b');
final String expectedMessage = expectedBuffer.getStringWithoutLengthAscii(0, expectedBuffer.capacity());
final MutableReference<String> receivedMessage = new MutableReference<>();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> receivedMessage.set(buffer.getStringWithoutLengthAscii(offset, length));
try (Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
try (Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
publishMessage(bufferOne, bufferTwo, publication);
pollForMessage(subscription, receivedMessage, fragmentHandler);
assertEquals(expectedMessage, receivedMessage.get());
}
try (Publication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID)) {
publishMessage(bufferOne, bufferTwo, publication);
pollForMessage(subscription, receivedMessage, fragmentHandler);
assertEquals(expectedMessage, receivedMessage.get());
}
}
}
use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class SessionSpecificPublicationTest method shouldNotAddPublicationWithSameSessionUntilLingerCompletes.
@ParameterizedTest
@MethodSource("data")
@InterruptAfter(20)
@SlowTest
void shouldNotAddPublicationWithSameSessionUntilLingerCompletes(final ChannelUriStringBuilder builder) {
final DirectBuffer msg = new UnsafeBuffer(new byte[8]);
final String channel = builder.sessionId(SESSION_ID_1).build();
final String subscriptionChannel = "ipc".equals(builder.media()) ? channel : SPY_PREFIX + channel;
final Publication publication1 = aeron.addPublication(channel, STREAM_ID);
final Subscription subscription = aeron.addSubscription(subscriptionChannel, STREAM_ID);
final int positionLimitId = publication1.positionLimitId();
assertEquals(CountersReader.RECORD_ALLOCATED, aeron.countersReader().getCounterState(positionLimitId));
while (publication1.offer(msg) < 0) {
Tests.yieldingIdle("Failed to offer message");
}
publication1.close();
assertThrows(RegistrationException.class, () -> {
aeron.addPublication(channel, STREAM_ID);
fail("Exception should have been thrown due lingering publication keeping session id active");
});
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
};
while (subscription.poll(fragmentHandler, 10) <= 0) {
Tests.yieldingIdle("Failed to drain message");
}
subscription.close();
while (CountersReader.RECORD_ALLOCATED == aeron.countersReader().getCounterState(positionLimitId)) {
Tests.yieldingIdle("Publication never cleaned up");
}
aeron.addPublication(channel, STREAM_ID);
}
use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class SpecifiedPositionPublicationTest method shouldValidateSpecifiedPositionForConcurrentPublications.
@InterruptAfter(5)
@ParameterizedTest
@CsvSource({ CommonContext.IPC_CHANNEL, "aeron:udp?endpoint=localhost:24325" })
void shouldValidateSpecifiedPositionForConcurrentPublications(final String initialUri) {
final MediaDriver.Context context = new MediaDriver.Context().dirDeleteOnStart(true).ipcPublicationTermWindowLength(LogBufferDescriptor.TERM_MIN_LENGTH).threadingMode(ThreadingMode.SHARED);
final DirectBuffer msg = new UnsafeBuffer(new byte[64]);
final int termLength = 1 << 16;
final int initialTermId = randomWatcher.random().nextInt();
final int activeTermId = initialTermId + randomWatcher.random().nextInt(Integer.MAX_VALUE);
final int positionBitsToShift = LogBufferDescriptor.positionBitsToShift(termLength);
final int termOffset = randomWatcher.random().nextInt(termLength) & -FrameDescriptor.FRAME_ALIGNMENT;
final long startPosition = LogBufferDescriptor.computePosition(activeTermId, termOffset, positionBitsToShift, initialTermId);
final int totalMessageLength = DataHeaderFlyweight.HEADER_LENGTH + msg.capacity();
final PositionCalculator positionCalculator = new PositionCalculator(startPosition, termLength, termOffset);
final long positionMsg1 = positionCalculator.addMessage(totalMessageLength);
final long positionMsg2 = positionCalculator.addMessage(totalMessageLength);
final long positionMsg3 = positionCalculator.addMessage(totalMessageLength);
final String channel = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition, initialTermId, termLength).build();
final String invalidPositionUri = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition + FrameDescriptor.FRAME_ALIGNMENT, initialTermId, termLength).build();
final String invalidInitialTermIdUri = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition, initialTermId + 1, termLength).build();
final String invalidTermLengthUri = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition, initialTermId, termLength << 1).build();
final int streamId = 1001;
try (TestMediaDriver mediaDriver = TestMediaDriver.launch(context, testWatcher);
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(initialUri, streamId);
Publication publication = aeron.addPublication(channel, streamId)) {
Tests.awaitConnected(subscription);
Tests.awaitConnected(publication);
assertEquals(startPosition, publication.position());
Tests.await(() -> publication.offer(msg) > 0);
assertEquals(positionMsg1, publication.position());
Tests.await(() -> subscription.poll((buffer, offset, length, header) -> {
}, 1) == 1);
try (Publication publication2 = aeron.addPublication(channel, streamId)) {
assertEquals(positionMsg1, publication2.position());
Tests.await(() -> publication.offer(msg) > 0);
assertEquals(positionMsg2, publication2.position());
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> assertEquals(positionMsg2, header.position());
Tests.await(() -> subscription.poll(fragmentHandler, 1) == 1);
}
try (Publication publication3 = aeron.addPublication(initialUri, streamId)) {
assertEquals(positionMsg2, publication3.position());
Tests.await(() -> publication.offer(msg) > 0);
assertEquals(positionMsg3, publication3.position());
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> assertEquals(positionMsg3, header.position());
Tests.await(() -> subscription.poll(fragmentHandler, 1) == 1);
}
assertThrows(RegistrationException.class, () -> aeron.addPublication(invalidPositionUri, streamId));
assertThrows(RegistrationException.class, () -> aeron.addPublication(invalidInitialTermIdUri, streamId));
assertThrows(RegistrationException.class, () -> aeron.addPublication(invalidTermLengthUri, streamId));
} finally {
context.deleteDirectory();
}
}
use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class SpecifiedPositionPublicationTest method shouldStartAtSpecifiedPositionForPublications.
@InterruptAfter(5)
@ParameterizedTest
@CsvSource({ CommonContext.IPC_CHANNEL + ",true", "aeron:udp?endpoint=localhost:24325,true", CommonContext.IPC_CHANNEL + ",false", "aeron:udp?endpoint=localhost:24325,false" })
void shouldStartAtSpecifiedPositionForPublications(final String initialUri, final boolean exclusive) {
final MediaDriver.Context context = new MediaDriver.Context().dirDeleteOnStart(true).ipcPublicationTermWindowLength(LogBufferDescriptor.TERM_MIN_LENGTH).threadingMode(ThreadingMode.SHARED);
final DirectBuffer msg = new UnsafeBuffer(new byte[64]);
final int termLength = 1 << 16;
final int initialTermId = randomWatcher.random().nextInt();
final int activeTermId = initialTermId + randomWatcher.random().nextInt(Integer.MAX_VALUE);
final int positionBitsToShift = LogBufferDescriptor.positionBitsToShift(termLength);
final int termOffset = randomWatcher.random().nextInt(termLength) & -FrameDescriptor.FRAME_ALIGNMENT;
final long startPosition = LogBufferDescriptor.computePosition(activeTermId, termOffset, positionBitsToShift, initialTermId);
final PositionCalculator positionCalculator = new PositionCalculator(startPosition, termLength, termOffset);
final long nextPosition = positionCalculator.addMessage(DataHeaderFlyweight.HEADER_LENGTH + msg.capacity());
final String channel = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition, initialTermId, termLength).build();
final int streamId = 1001;
final Function<Aeron, Publication> publicationSupplier = exclusive ? (a) -> a.addExclusivePublication(channel, streamId) : (a) -> a.addPublication(channel, streamId);
try (TestMediaDriver mediaDriver = TestMediaDriver.launch(context, testWatcher);
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(initialUri, streamId);
Publication publication = publicationSupplier.apply(aeron)) {
Tests.awaitConnected(subscription);
Tests.awaitConnected(publication);
assertEquals(startPosition, publication.position());
Tests.await(() -> publication.offer(msg) > 0);
assertEquals(nextPosition, publication.position());
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> assertEquals(nextPosition, header.position());
Tests.await(() -> subscription.poll(fragmentHandler, 1) == 1);
} finally {
context.deleteDirectory();
}
}
Aggregations