Search in sources :

Example 21 with MediaDriver

use of io.aeron.driver.MediaDriver 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 22 with MediaDriver

use of io.aeron.driver.MediaDriver in project Aeron by real-logic.

the class RecordingDescriptorCollectorTest method shouldAllowUserToRetainDescriptorsToPreventReuse.

@Test
void shouldAllowUserToRetainDescriptorsToPreventReuse(@TempDir final Path tempDir) {
    try (MediaDriver mediaDriver = MediaDriver.launch(new MediaDriver.Context().dirDeleteOnStart(true));
        Archive ignore = Archive.launch(new Archive.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()).archiveDir(tempDir.resolve("archive").toFile()).deleteArchiveOnStart(true));
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
        AeronArchive aeronArchive = AeronArchive.connect(new AeronArchive.Context().aeron(aeron).ownsAeronClient(false))) {
        createRecordings(aeronArchive, 3);
        final RecordingDescriptorCollector collector = new RecordingDescriptorCollector(1);
        long fromRecordingId = 0;
        int count = aeronArchive.listRecordings(fromRecordingId, collector.poolSize(), collector.reset());
        assertEquals(1, count);
        final RecordingDescriptor desc0 = collector.descriptors().get(0);
        fromRecordingId += count;
        count = aeronArchive.listRecordings(fromRecordingId, collector.poolSize(), collector.reset());
        assertEquals(1, count);
        final RecordingDescriptor desc1 = collector.descriptors().get(0);
        assertEquals(desc0.recordingId(), desc1.recordingId());
        desc1.retain();
        fromRecordingId += count;
        count = aeronArchive.listRecordings(fromRecordingId, collector.poolSize(), collector.reset());
        assertEquals(1, count);
        final RecordingDescriptor desc2 = collector.descriptors().get(0);
        assertNotEquals(desc1.recordingId(), desc2.recordingId());
    }
}
Also used : Archive(io.aeron.archive.Archive) AeronArchive(io.aeron.archive.client.AeronArchive) MediaDriver(io.aeron.driver.MediaDriver) AeronArchive(io.aeron.archive.client.AeronArchive) Aeron(io.aeron.Aeron) SlowTest(io.aeron.test.SlowTest) Test(org.junit.jupiter.api.Test)

Example 23 with MediaDriver

use of io.aeron.driver.MediaDriver in project Aeron by real-logic.

the class GapFillLossTest method shouldGapFillWhenLossOccurs.

@Test
@InterruptAfter(10)
void shouldGapFillWhenLossOccurs() throws Exception {
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(MSG_LENGTH));
    srcBuffer.setMemory(0, MSG_LENGTH, (byte) 7);
    final MediaDriver.Context ctx = new MediaDriver.Context().errorHandler(Tests::onError).threadingMode(ThreadingMode.SHARED).dirDeleteOnStart(true).publicationTermBufferLength(LogBufferDescriptor.TERM_MIN_LENGTH);
    final LossGenerator noLossGenerator = DebugChannelEndpointConfiguration.lossGeneratorSupplier(0, 0);
    ctx.sendChannelEndpointSupplier((udpChannel, statusIndicator, context) -> new DebugSendChannelEndpoint(udpChannel, statusIndicator, context, noLossGenerator, noLossGenerator));
    TestMediaDriver.enableLossGenerationOnReceive(ctx, 0.20, 0xcafebabeL, true, false);
    try (TestMediaDriver mediaDriver = TestMediaDriver.launch(ctx, watcher);
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
        Subscription subscription = aeron.addSubscription(UNRELIABLE_CHANNEL, STREAM_ID);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
        final Subscriber subscriber = new Subscriber(subscription);
        final Thread subscriberThread = new Thread(subscriber);
        subscriberThread.setDaemon(true);
        subscriberThread.start();
        long position = 0;
        for (int i = 0; i < NUM_MESSAGES; i++) {
            srcBuffer.putLong(0, i);
            while ((position = publication.offer(srcBuffer)) < 0L) {
                Tests.yield();
            }
        }
        FINAL_POSITION.set(position);
        subscriberThread.join();
        verifyLossOccurredForStream(ctx.aeronDirectoryName(), STREAM_ID);
        assertThat(subscriber.messageCount, lessThan(NUM_MESSAGES));
    } finally {
        ctx.deleteDirectory();
    }
}
Also used : LossGenerator(io.aeron.driver.ext.LossGenerator) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) DebugSendChannelEndpoint(io.aeron.driver.ext.DebugSendChannelEndpoint) DebugSendChannelEndpoint(io.aeron.driver.ext.DebugSendChannelEndpoint) MediaDriver(io.aeron.driver.MediaDriver) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 24 with MediaDriver

use of io.aeron.driver.MediaDriver in project Aeron by real-logic.

the class MultiClusteredServicesTest method shouldSupportMultipleServicesPerNode.

@Test
@InterruptAfter(20)
public void shouldSupportMultipleServicesPerNode() {
    final List<TestCluster.NodeContext> nodeContexts = new ArrayList<>();
    final List<TestCluster.ServiceContext> serviceContexts = new ArrayList<>();
    final List<ClusteredMediaDriver> clusteredMediaDrivers = new ArrayList<>();
    final List<ClusteredServiceContainer> clusteredServiceContainers = new ArrayList<>();
    nodeContexts.add(TestCluster.nodeContext(0, true));
    nodeContexts.add(TestCluster.nodeContext(1, true));
    nodeContexts.add(TestCluster.nodeContext(2, true));
    serviceContexts.add(TestCluster.serviceContext(0, 0, nodeContexts.get(0), ServiceA::new));
    serviceContexts.add(TestCluster.serviceContext(0, 1, nodeContexts.get(0), ServiceB::new));
    serviceContexts.add(TestCluster.serviceContext(1, 0, nodeContexts.get(1), ServiceA::new));
    serviceContexts.add(TestCluster.serviceContext(1, 1, nodeContexts.get(1), ServiceB::new));
    serviceContexts.add(TestCluster.serviceContext(2, 0, nodeContexts.get(2), ServiceA::new));
    serviceContexts.add(TestCluster.serviceContext(2, 1, nodeContexts.get(2), ServiceB::new));
    nodeContexts.forEach((context) -> {
        try {
            clusteredMediaDrivers.add(ClusteredMediaDriver.launch(context.mediaDriverCtx, context.archiveCtx, context.consensusModuleCtx));
        } finally {
            systemTestWatcher.dataCollector().add(context.mediaDriverCtx.aeronDirectory());
            systemTestWatcher.dataCollector().add(context.archiveCtx.archiveDir());
            systemTestWatcher.dataCollector().add(context.consensusModuleCtx.clusterDir());
        }
    });
    serviceContexts.forEach((context) -> {
        context.serviceContainerCtx.aeronDirectoryName(context.aeronCtx.aeronDirectoryName());
        try {
            clusteredServiceContainers.add(ClusteredServiceContainer.launch(context.serviceContainerCtx));
        } finally {
            systemTestWatcher.dataCollector().add(context.serviceContainerCtx.clusterDir());
        }
    });
    final String aeronDirName = CommonContext.getAeronDirectoryName();
    final MediaDriver clientMediaDriver = MediaDriver.launch(new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirDeleteOnStart(true).aeronDirectoryName(aeronDirName).nameResolver(new RedirectingNameResolver(TestCluster.DEFAULT_NODE_MAPPINGS)));
    final AeronCluster client = AeronCluster.connect(new AeronCluster.Context().aeronDirectoryName(aeronDirName).ingressChannel(CommonContext.UDP_CHANNEL).ingressEndpoints(TestCluster.ingressEndpoints(0, 3)));
    try {
        final DirectBuffer buffer = new UnsafeBuffer(new byte[100]);
        while (client.offer(buffer, 0, 100) < 0) {
            Tests.yield();
        }
        Tests.awaitValue(serviceAMessageCount, 3);
        Tests.awaitValue(serviceBMessageCount, 3);
    } finally {
        CloseHelper.closeAll(client, clientMediaDriver);
        clusteredMediaDrivers.forEach((clusteredMediaDriver) -> clusteredMediaDriver.consensusModule().close());
        CloseHelper.closeAll(clusteredServiceContainers);
        CloseHelper.closeAll(clusteredMediaDrivers);
        clientMediaDriver.context().deleteDirectory();
    }
}
Also used : CommonContext(io.aeron.CommonContext) AeronCluster(io.aeron.cluster.client.AeronCluster) ArrayList(java.util.ArrayList) DirectBuffer(org.agrona.DirectBuffer) MediaDriver(io.aeron.driver.MediaDriver) RedirectingNameResolver(io.aeron.test.driver.RedirectingNameResolver) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) Test(org.junit.jupiter.api.Test)

Example 25 with MediaDriver

use of io.aeron.driver.MediaDriver in project Aeron by real-logic.

the class TestMediaDriverTest method connectToCMediaDriverWithoutSpecifyingAeronDir.

@Test
void connectToCMediaDriverWithoutSpecifyingAeronDir() {
    assumeTrue(TestMediaDriver.shouldRunCMediaDriver());
    final MediaDriver.Context context = new MediaDriver.Context().dirDeleteOnStart(true).dirDeleteOnShutdown(false);
    assertEquals(CommonContext.getAeronDirectoryName(), context.aeronDirectoryName());
    try (TestMediaDriver mediaDriver = CTestMediaDriver.launch(context, false, null);
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(context.aeronDirectoryName()))) {
        final File aeronDirectory = aeron.context().aeronDirectory();
        assertNotNull(aeronDirectory);
        assertTrue(aeronDirectory.exists());
        assertNotNull(mediaDriver);
    } finally {
        context.deleteDirectory();
    }
}
Also used : CommonContext(io.aeron.CommonContext) MediaDriver(io.aeron.driver.MediaDriver) File(java.io.File) Aeron(io.aeron.Aeron) Test(org.junit.jupiter.api.Test)

Aggregations

MediaDriver (io.aeron.driver.MediaDriver)59 Aeron (io.aeron.Aeron)22 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)22 Subscription (io.aeron.Subscription)13 FragmentHandler (io.aeron.logbuffer.FragmentHandler)11 ContinueBarrier (org.agrona.console.ContinueBarrier)11 Test (org.junit.jupiter.api.Test)10 Publication (io.aeron.Publication)9 IdleStrategy (org.agrona.concurrent.IdleStrategy)9 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)9 InterruptAfter (io.aeron.test.InterruptAfter)8 ExecutorService (java.util.concurrent.ExecutorService)8 BusySpinIdleStrategy (org.agrona.concurrent.BusySpinIdleStrategy)8 TestMediaDriver (io.aeron.test.driver.TestMediaDriver)7 Archive (io.aeron.archive.Archive)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 Tests (io.aeron.test.Tests)5 DirectBuffer (org.agrona.DirectBuffer)5 CommonContext (io.aeron.CommonContext)4 File (java.io.File)4