Search in sources :

Example 41 with MediaDriver

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

the class DriverLoggingAgentTest method testLogMediaDriverEvents.

private void testLogMediaDriverEvents(final String channel, final String enabledEvents, final EnumSet<DriverEventCode> expectedEvents) {
    before(enabledEvents, expectedEvents);
    final MediaDriver.Context driverCtx = new MediaDriver.Context().errorHandler(Tests::onError).publicationLingerTimeoutNs(0).timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(1));
    try (MediaDriver mediaDriver = MediaDriver.launch(driverCtx)) {
        try (Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
            Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
            Publication publication = aeron.addPublication(channel, STREAM_ID)) {
            final UnsafeBuffer offerBuffer = new UnsafeBuffer(new byte[32]);
            while (publication.offer(offerBuffer) < 0) {
                Tests.yield();
            }
            final MutableInteger counter = new MutableInteger();
            final FragmentHandler handler = (buffer, offset, length, header) -> counter.value++;
            while (0 == subscription.poll(handler, 1)) {
                Tests.yield();
            }
            assertEquals(counter.get(), 1);
        }
        final Supplier<String> errorMessage = () -> "Pending events: " + WAIT_LIST;
        while (!WAIT_LIST.isEmpty()) {
            Tests.yieldingIdle(errorMessage);
        }
    }
}
Also used : Tests(io.aeron.test.Tests) DriverEventCode(io.aeron.agent.DriverEventCode) Subscription(io.aeron.Subscription) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) EnumSource(org.junit.jupiter.params.provider.EnumSource) Supplier(java.util.function.Supplier) Collections.synchronizedSet(java.util.Collections.synchronizedSet) MessageHandler(org.agrona.concurrent.MessageHandler) EVENT_READER_FRAME_LIMIT(io.aeron.agent.EventConfiguration.EVENT_READER_FRAME_LIMIT) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) EVENT_RING_BUFFER(io.aeron.agent.EventConfiguration.EVENT_RING_BUFFER) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Publication(io.aeron.Publication) Agent(org.agrona.concurrent.Agent) MutableInteger(org.agrona.collections.MutableInteger) EnumSet(java.util.EnumSet) MediaDriver(io.aeron.driver.MediaDriver) Aeron(io.aeron.Aeron) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) EnumMap(java.util.EnumMap) Set(java.util.Set) IPC_CHANNEL(io.aeron.CommonContext.IPC_CHANNEL) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) InterruptAfter(io.aeron.test.InterruptAfter) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) INCLUDE(org.junit.jupiter.params.provider.EnumSource.Mode.INCLUDE) MutableDirectBuffer(org.agrona.MutableDirectBuffer) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) Publication(io.aeron.Publication) Tests(io.aeron.test.Tests) Aeron(io.aeron.Aeron) MediaDriver(io.aeron.driver.MediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) Subscription(io.aeron.Subscription) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Example 42 with MediaDriver

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

the class ArchivingMediaDriver method main.

/**
 * Launch an {@link Archive} with an embedded {@link MediaDriver} and await a shutdown signal.
 *
 * @param args command line argument which is a list for properties files as URLs or filenames.
 */
@SuppressWarnings("try")
public static void main(final String[] args) {
    loadPropertiesFiles(args);
    final ShutdownSignalBarrier barrier = new ShutdownSignalBarrier();
    final MediaDriver.Context ctx = new MediaDriver.Context().terminationHook(barrier::signalAll);
    try (ArchivingMediaDriver ignore = launch(ctx, new Archive.Context())) {
        barrier.await();
        System.out.println("Shutdown Archive...");
    }
}
Also used : ShutdownSignalBarrier(org.agrona.concurrent.ShutdownSignalBarrier) MediaDriver(io.aeron.driver.MediaDriver)

Example 43 with MediaDriver

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

the class EmbeddedDualExclusiveThroughput method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 * @throws InterruptedException if the thread sleep delay is interrupted.
 */
@SuppressWarnings("MethodLength")
public static void main(final String[] args) throws InterruptedException {
    loadPropertiesFiles(args);
    final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), EmbeddedDualExclusiveThroughput::printRate);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final AtomicBoolean running = new AtomicBoolean(true);
    final AvailableImageHandler handler = (image) -> System.out.println("source connection=" + image.sourceIdentity());
    final ChannelUriStringBuilder builder = new ChannelUriStringBuilder().media(CommonContext.UDP_MEDIA).controlMode(CommonContext.MDC_CONTROL_MODE_MANUAL);
    final String sourceUriOne = builder.controlEndpoint("localhost:20550").tags("1").build();
    final String sourceUriTwo = builder.controlEndpoint("localhost:20551").tags("2").build();
    try (MediaDriver mediaDriver = MediaDriver.launch();
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID, handler, null);
        ExclusivePublication publicationOne = aeron.addExclusivePublication(sourceUriOne, STREAM_ID);
        ExclusivePublication publicationTwo = aeron.addExclusivePublication(sourceUriTwo, STREAM_ID)) {
        publicationOne.addDestination(CHANNEL);
        publicationTwo.addDestination(CHANNEL);
        while (subscription.imageCount() < 2) {
            Thread.yield();
        }
        executor.execute(reporter);
        executor.execute(() -> SamplesUtil.subscriberLoop(rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));
        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();
        do {
            System.out.format("%nStreaming %,d messages of payload length %d bytes to %s on stream id %d%n", NUMBER_OF_MESSAGES * 2, MESSAGE_LENGTH, CHANNEL, STREAM_ID);
            printingActive = true;
            long backPressureCountOne = 0;
            long backPressureCountTwo = 0;
            for (long a = 0, b = 0; a < NUMBER_OF_MESSAGES || b < NUMBER_OF_MESSAGES; ) {
                idleStrategy.reset();
                boolean failedOne = false;
                boolean failedTwo = false;
                if (a < NUMBER_OF_MESSAGES) {
                    if (publicationOne.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null) > 0) {
                        a++;
                    } else {
                        backPressureCountOne++;
                        failedOne = true;
                    }
                }
                if (b < NUMBER_OF_MESSAGES) {
                    if (publicationTwo.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null) > 0) {
                        b++;
                    } else {
                        backPressureCountTwo++;
                        failedTwo = true;
                    }
                }
                if (failedOne || failedTwo) {
                    idleStrategy.idle();
                }
            }
            System.out.println("Done streaming." + " backPressureRatioOne=" + ((double) backPressureCountOne / NUMBER_OF_MESSAGES) + " backPressureRatioTwo=" + ((double) backPressureCountTwo / NUMBER_OF_MESSAGES));
            if (LINGER_TIMEOUT_MS > 0) {
                System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
                Thread.sleep(LINGER_TIMEOUT_MS);
            }
            printingActive = false;
        } while (barrier.await());
        running.set(false);
        reporter.halt();
        executor.shutdown();
    }
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) SystemUtil.loadPropertiesFiles(org.agrona.SystemUtil.loadPropertiesFiles) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BufferUtil(org.agrona.BufferUtil) BitUtil(org.agrona.BitUtil) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) io.aeron(io.aeron) SamplesUtil.rateReporterHandler(io.aeron.samples.SamplesUtil.rateReporterHandler) BufferClaim(io.aeron.logbuffer.BufferClaim) ContinueBarrier(org.agrona.console.ContinueBarrier) ExecutorService(java.util.concurrent.ExecutorService) IdleStrategy(org.agrona.concurrent.IdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) ContinueBarrier(org.agrona.console.ContinueBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaDriver(io.aeron.driver.MediaDriver) ExecutorService(java.util.concurrent.ExecutorService)

Example 44 with MediaDriver

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

the class StreamingPublisher method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 * @throws InterruptedException if interrupted during linger.
 */
public static void main(final String[] args) throws InterruptedException {
    if (MESSAGE_LENGTH < SIZE_OF_LONG) {
        throw new IllegalArgumentException("Message length must be at least " + SIZE_OF_LONG + " bytes");
    }
    final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
    final Aeron.Context context = new Aeron.Context();
    if (EMBEDDED_MEDIA_DRIVER) {
        context.aeronDirectoryName(driver.aeronDirectoryName());
    }
    final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), StreamingPublisher::printRate);
    final ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.execute(reporter);
    // clean up resources when this try block is finished.
    try (Aeron aeron = Aeron.connect(context);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();
        do {
            printingActive = true;
            System.out.format("%nStreaming %,d messages of%s size %d bytes to %s on stream id %d%n", NUMBER_OF_MESSAGES, RANDOM_MESSAGE_LENGTH ? " random" : "", MESSAGE_LENGTH, CHANNEL, STREAM_ID);
            long backPressureCount = 0;
            for (long i = 0; i < NUMBER_OF_MESSAGES; i++) {
                final int length = LENGTH_GENERATOR.getAsInt();
                OFFER_BUFFER.putLong(0, i);
                idleStrategy.reset();
                while (publication.offer(OFFER_BUFFER, 0, length, null) < 0L) {
                    // The offer failed, which is usually due to the publication
                    // being temporarily blocked. Retry the offer after a short
                    // spin/yield/sleep, depending on the chosen IdleStrategy.
                    backPressureCount++;
                    idleStrategy.idle();
                }
                reporter.onMessage(length);
            }
            System.out.println("Done streaming. Back pressure ratio " + ((double) backPressureCount / NUMBER_OF_MESSAGES));
            if (LINGER_TIMEOUT_MS > 0) {
                System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
                Thread.sleep(LINGER_TIMEOUT_MS);
            }
            printingActive = false;
        } while (barrier.await());
    }
    reporter.halt();
    executor.shutdown();
    CloseHelper.close(driver);
}
Also used : IdleStrategy(org.agrona.concurrent.IdleStrategy) Publication(io.aeron.Publication) ContinueBarrier(org.agrona.console.ContinueBarrier) Aeron(io.aeron.Aeron) MediaDriver(io.aeron.driver.MediaDriver) ExecutorService(java.util.concurrent.ExecutorService)

Example 45 with MediaDriver

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

the class EmbeddedThroughput method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 * @throws InterruptedException if the thread is interrupted during linger.
 */
public static void main(final String[] args) throws InterruptedException {
    loadPropertiesFiles(args);
    final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), EmbeddedThroughput::printRate);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final AtomicBoolean running = new AtomicBoolean(true);
    try (MediaDriver mediaDriver = MediaDriver.launch();
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
        executor.execute(reporter);
        executor.execute(() -> SamplesUtil.subscriberLoop(rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));
        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();
        do {
            System.out.format("%nStreaming %,d messages of payload length %d bytes to %s on stream id %d%n", NUMBER_OF_MESSAGES, MESSAGE_LENGTH, CHANNEL, STREAM_ID);
            printingActive = true;
            long backPressureCount = 0;
            for (long i = 0; i < NUMBER_OF_MESSAGES; i++) {
                OFFER_BUFFER.putLong(0, i);
                idleStrategy.reset();
                while (publication.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null) < 0) {
                    backPressureCount++;
                    idleStrategy.idle();
                }
            }
            System.out.println("Done streaming. backPressureRatio=" + ((double) backPressureCount / NUMBER_OF_MESSAGES));
            if (LINGER_TIMEOUT_MS > 0) {
                System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
                Thread.sleep(LINGER_TIMEOUT_MS);
            }
            printingActive = false;
        } while (barrier.await());
        running.set(false);
        reporter.halt();
        executor.shutdown();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaDriver(io.aeron.driver.MediaDriver) IdleStrategy(org.agrona.concurrent.IdleStrategy) ExecutorService(java.util.concurrent.ExecutorService) ConcurrentPublication(io.aeron.ConcurrentPublication) Publication(io.aeron.Publication) ContinueBarrier(org.agrona.console.ContinueBarrier) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron)

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