Search in sources :

Example 66 with MediaDriver

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

the class RecordingDescriptorCollectorTest method shouldCollectPagesOfRecordingDescriptors.

@Test
@InterruptAfter(10)
void shouldCollectPagesOfRecordingDescriptors(@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))) {
        final int numRecordings = 10;
        createRecordings(aeronArchive, numRecordings);
        final RecordingDescriptorCollector collector = new RecordingDescriptorCollector(3);
        long fromRecordingId = 0;
        int count;
        while (0 < (count = aeronArchive.listRecordings(fromRecordingId, collector.poolSize(), collector.reset()))) {
            final List<RecordingDescriptor> descriptors = collector.descriptors();
            assertThat(count, lessThanOrEqualTo(collector.poolSize()));
            assertThat(descriptors.size(), lessThanOrEqualTo(collector.poolSize()));
            // noinspection OptionalGetWithoutIsPresent
            final long maxRecordingId = descriptors.stream().mapToLong(RecordingDescriptor::recordingId).max().getAsLong();
            fromRecordingId = maxRecordingId + 1;
        }
    }
}
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) InterruptAfter(io.aeron.test.InterruptAfter)

Example 67 with MediaDriver

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

the class RecordingDescriptorCollectorTest method shouldShouldNotReuseDescriptorIfPoolSizeIsZero.

@Test
void shouldShouldNotReuseDescriptorIfPoolSizeIsZero(@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(0);
        long fromRecordingId = 0;
        int count = aeronArchive.listRecordings(fromRecordingId, 1, collector.reset());
        assertEquals(1, count);
        final RecordingDescriptor desc0 = collector.descriptors().get(0);
        fromRecordingId += count;
        count = aeronArchive.listRecordings(fromRecordingId, 1, collector.reset());
        assertEquals(1, count);
        final RecordingDescriptor desc1 = collector.descriptors().get(0);
        assertNotEquals(desc0.recordingId(), desc1.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 68 with MediaDriver

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

the class Ping method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 * @throws InterruptedException if the thread is interrupted.
 */
public static void main(final String[] args) throws InterruptedException {
    final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
    final Aeron.Context ctx = new Aeron.Context().availableImageHandler(Ping::availablePongImageHandler);
    final FragmentHandler fragmentHandler = new FragmentAssembler(Ping::pongHandler);
    if (EMBEDDED_MEDIA_DRIVER) {
        ctx.aeronDirectoryName(driver.aeronDirectoryName());
    }
    System.out.println("Publishing Ping at " + PING_CHANNEL + " on stream id " + PING_STREAM_ID);
    System.out.println("Subscribing Pong at " + PONG_CHANNEL + " on stream id " + PONG_STREAM_ID);
    System.out.println("Message length of " + MESSAGE_LENGTH + " bytes");
    System.out.println("Using exclusive publications " + EXCLUSIVE_PUBLICATIONS);
    try (Aeron aeron = Aeron.connect(ctx);
        Subscription subscription = aeron.addSubscription(PONG_CHANNEL, PONG_STREAM_ID);
        Publication publication = EXCLUSIVE_PUBLICATIONS ? aeron.addExclusivePublication(PING_CHANNEL, PING_STREAM_ID) : aeron.addPublication(PING_CHANNEL, PING_STREAM_ID)) {
        System.out.println("Waiting for new image from Pong...");
        LATCH.await();
        System.out.println("Warming up... " + WARMUP_NUMBER_OF_ITERATIONS + " iterations of " + WARMUP_NUMBER_OF_MESSAGES + " messages");
        for (int i = 0; i < WARMUP_NUMBER_OF_ITERATIONS; i++) {
            roundTripMessages(fragmentHandler, publication, subscription, WARMUP_NUMBER_OF_MESSAGES);
            Thread.yield();
        }
        Thread.sleep(100);
        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        do {
            HISTOGRAM.reset();
            System.out.println("Pinging " + NUMBER_OF_MESSAGES + " messages");
            roundTripMessages(fragmentHandler, publication, subscription, NUMBER_OF_MESSAGES);
            System.out.println("Histogram of RTT latencies in microseconds.");
            HISTOGRAM.outputPercentileDistribution(System.out, 1000.0);
        } while (barrier.await());
    }
    CloseHelper.close(driver);
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) ContinueBarrier(org.agrona.console.ContinueBarrier)

Example 69 with MediaDriver

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

the class EmbeddedExclusiveThroughput 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), EmbeddedExclusiveThroughput::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);
        ExclusivePublication publication = aeron.addExclusivePublication(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) ContinueBarrier(org.agrona.console.ContinueBarrier)

Example 70 with MediaDriver

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

the class FileReceiver method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
public static void main(final String[] args) {
    final File storageDir;
    if (args.length > 1) {
        storageDir = new File(args[0]);
        if (!storageDir.isDirectory()) {
            System.out.println(args[0] + " is not a directory");
            System.exit(1);
        }
    } else {
        storageDir = new File(SystemUtil.tmpDirName());
    }
    System.out.println("Files stored to " + storageDir.getAbsolutePath());
    final IdleStrategy idleStrategy = new SleepingMillisIdleStrategy(1);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    try (MediaDriver mediaDriver = MediaDriver.launch();
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
        System.out.println("Receiving from " + CHANNEL + " on stream id " + STREAM_ID);
        final FileReceiver fileReceiver = new FileReceiver(storageDir, subscription);
        while (running.get()) {
            idleStrategy.idle(fileReceiver.doWork());
        }
    }
}
Also used : SleepingMillisIdleStrategy(org.agrona.concurrent.SleepingMillisIdleStrategy) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SleepingMillisIdleStrategy(org.agrona.concurrent.SleepingMillisIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) MediaDriver(io.aeron.driver.MediaDriver) Subscription(io.aeron.Subscription) File(java.io.File) Aeron(io.aeron.Aeron)

Aggregations

MediaDriver (io.aeron.driver.MediaDriver)78 Aeron (io.aeron.Aeron)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)24 Test (org.junit.jupiter.api.Test)20 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)17 InterruptAfter (io.aeron.test.InterruptAfter)16 Subscription (io.aeron.Subscription)14 TestMediaDriver (io.aeron.test.driver.TestMediaDriver)14 IdleStrategy (org.agrona.concurrent.IdleStrategy)14 ContinueBarrier (org.agrona.console.ContinueBarrier)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 Publication (io.aeron.Publication)10 Archive (io.aeron.archive.Archive)10 FragmentHandler (io.aeron.logbuffer.FragmentHandler)10 Tests (io.aeron.test.Tests)10 CommonContext (io.aeron.CommonContext)8 ExecutorService (java.util.concurrent.ExecutorService)8 DirectBuffer (org.agrona.DirectBuffer)7 BusySpinIdleStrategy (org.agrona.concurrent.BusySpinIdleStrategy)7 AeronCluster (io.aeron.cluster.client.AeronCluster)6