Search in sources :

Example 46 with DirectBuffer

use of org.agrona.DirectBuffer 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 47 with DirectBuffer

use of org.agrona.DirectBuffer 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)

Example 48 with DirectBuffer

use of org.agrona.DirectBuffer in project aeron by real-logic.

the class RecordingPos method isActive.

/**
 * Is the recording counter still active.
 *
 * @param counters    to search within.
 * @param counterId   to search for.
 * @param recordingId to confirm it is still the same value.
 * @return true if the counter is still active otherwise false.
 */
public static boolean isActive(final CountersReader counters, final int counterId, final long recordingId) {
    final DirectBuffer buffer = counters.metaDataBuffer();
    final int recordOffset = CountersReader.metaDataOffset(counterId);
    return counters.getCounterTypeId(counterId) == RECORDING_POSITION_TYPE_ID && buffer.getLong(recordOffset + KEY_OFFSET + RECORDING_ID_OFFSET) == recordingId && counters.getCounterState(counterId) == RECORD_ALLOCATED;
}
Also used : DirectBuffer(org.agrona.DirectBuffer)

Example 49 with DirectBuffer

use of org.agrona.DirectBuffer in project aeron by real-logic.

the class SamplesUtil method mapCounters.

/**
 * Map an existing CnC file.
 *
 * @return the {@link CountersReader} over the CnC file.
 */
public static CountersReader mapCounters() {
    final File cncFile = CommonContext.newDefaultCncFile();
    System.out.println("Command `n Control file " + cncFile);
    final MappedByteBuffer cncByteBuffer = mapExistingFileReadOnly(cncFile);
    final DirectBuffer cncMetaData = createMetaDataBuffer(cncByteBuffer);
    final int cncVersion = cncMetaData.getInt(cncVersionOffset(0));
    checkVersion(cncVersion);
    return new CountersReader(createCountersMetaDataBuffer(cncByteBuffer, cncMetaData), createCountersValuesBuffer(cncByteBuffer, cncMetaData));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) CountersReader(org.agrona.concurrent.status.CountersReader)

Example 50 with DirectBuffer

use of org.agrona.DirectBuffer in project aeron by real-logic.

the class SamplesUtil method mapCounters.

/**
 * Map an existing CnC file.
 *
 * @param cncFileVersion to set as value of file.
 * @return the {@link CountersReader} over the CnC file.
 */
public static CountersReader mapCounters(final MutableInteger cncFileVersion) {
    final File cncFile = CommonContext.newDefaultCncFile();
    System.out.println("Command `n Control file " + cncFile);
    final MappedByteBuffer cncByteBuffer = mapExistingFileReadOnly(cncFile);
    final DirectBuffer cncMetaData = createMetaDataBuffer(cncByteBuffer);
    final int cncVersion = cncMetaData.getInt(cncVersionOffset(0));
    cncFileVersion.set(cncVersion);
    checkVersion(cncVersion);
    return new CountersReader(createCountersMetaDataBuffer(cncByteBuffer, cncMetaData), createCountersValuesBuffer(cncByteBuffer, cncMetaData));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) CountersReader(org.agrona.concurrent.status.CountersReader)

Aggregations

DirectBuffer (org.agrona.DirectBuffer)116 Header (io.aeron.logbuffer.Header)21 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)21 MutableDirectBuffer (org.agrona.MutableDirectBuffer)20 File (java.io.File)19 MediaDriver (io.aeron.driver.MediaDriver)18 CountersReader (org.agrona.concurrent.status.CountersReader)15 ThreadingMode (io.aeron.driver.ThreadingMode)12 FragmentHandler (io.aeron.logbuffer.FragmentHandler)12 MappedByteBuffer (java.nio.MappedByteBuffer)12 ClusteredService (io.aeron.cluster.service.ClusteredService)10 InterruptAfter (io.aeron.test.InterruptAfter)10 Test (org.junit.jupiter.api.Test)10 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 Publication (io.aeron.Publication)9 ClientSession (io.aeron.cluster.service.ClientSession)9 ClusteredServiceContainer (io.aeron.cluster.service.ClusteredServiceContainer)8 Assertions (org.junit.jupiter.api.Assertions)8 Mockito.mock (org.mockito.Mockito.mock)8