Search in sources :

Example 26 with FragmentHandler

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;
}
Also used : FragmentHandler(io.aeron.logbuffer.FragmentHandler)

Example 27 with FragmentHandler

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());
        }
    }
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter) AfterEach(org.junit.jupiter.api.AfterEach) MutableReference(org.agrona.collections.MutableReference) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ThreadingMode(io.aeron.driver.ThreadingMode) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) CloseHelper(org.agrona.CloseHelper) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableReference(org.agrona.collections.MutableReference) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 28 with FragmentHandler

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);
}
Also used : DirectBuffer(org.agrona.DirectBuffer) MediaDriver(io.aeron.driver.MediaDriver) ErrorHandler(org.agrona.ErrorHandler) RegistrationException(io.aeron.exceptions.RegistrationException) io.aeron.test(io.aeron.test) CountersReader(org.agrona.concurrent.status.CountersReader) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) CommonContext(io.aeron.CommonContext) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ThreadingMode(io.aeron.driver.ThreadingMode) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Assertions(org.junit.jupiter.api.Assertions) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) CloseHelper(org.agrona.CloseHelper) DirectBuffer(org.agrona.DirectBuffer) MethodSource(org.junit.jupiter.params.provider.MethodSource) Mockito.mock(org.mockito.Mockito.mock) FragmentHandler(io.aeron.logbuffer.FragmentHandler) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 29 with FragmentHandler

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();
    }
}
Also used : FrameDescriptor(io.aeron.logbuffer.FrameDescriptor) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MediaDriver(io.aeron.driver.MediaDriver) SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) CsvSource(org.junit.jupiter.params.provider.CsvSource) RegistrationException(io.aeron.exceptions.RegistrationException) RandomWatcher(io.aeron.test.RandomWatcher) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Function(java.util.function.Function) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) InterruptAfter(io.aeron.test.InterruptAfter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ThreadingMode(io.aeron.driver.ThreadingMode) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) DirectBuffer(org.agrona.DirectBuffer) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) DirectBuffer(org.agrona.DirectBuffer) MediaDriver(io.aeron.driver.MediaDriver) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) InterruptAfter(io.aeron.test.InterruptAfter) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 30 with FragmentHandler

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();
    }
}
Also used : FrameDescriptor(io.aeron.logbuffer.FrameDescriptor) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MediaDriver(io.aeron.driver.MediaDriver) SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) CsvSource(org.junit.jupiter.params.provider.CsvSource) RegistrationException(io.aeron.exceptions.RegistrationException) RandomWatcher(io.aeron.test.RandomWatcher) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Function(java.util.function.Function) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) InterruptAfter(io.aeron.test.InterruptAfter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ThreadingMode(io.aeron.driver.ThreadingMode) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) DirectBuffer(org.agrona.DirectBuffer) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) DirectBuffer(org.agrona.DirectBuffer) MediaDriver(io.aeron.driver.MediaDriver) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) InterruptAfter(io.aeron.test.InterruptAfter) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

FragmentHandler (io.aeron.logbuffer.FragmentHandler)70 MediaDriver (io.aeron.driver.MediaDriver)50 InterruptAfter (io.aeron.test.InterruptAfter)44 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)42 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)42 Tests (io.aeron.test.Tests)40 Test (org.junit.jupiter.api.Test)40 InterruptingTestCallback (io.aeron.test.InterruptingTestCallback)36 TestMediaDriver (io.aeron.test.driver.TestMediaDriver)36 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)36 SystemTestWatcher (io.aeron.test.SystemTestWatcher)34 ThreadingMode (io.aeron.driver.ThreadingMode)30 AfterEach (org.junit.jupiter.api.AfterEach)30 CloseHelper (org.agrona.CloseHelper)28 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)28 MutableInteger (org.agrona.collections.MutableInteger)22 DirectBuffer (org.agrona.DirectBuffer)20 Assertions (org.junit.jupiter.api.Assertions)20 TimeUnit (java.util.concurrent.TimeUnit)18 MethodSource (org.junit.jupiter.params.provider.MethodSource)18