Search in sources :

Example 1 with Aeron

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

the class EmbeddedPingPong method runPing.

private static void runPing(final String embeddedDirName) throws InterruptedException {
    final Aeron.Context ctx = new Aeron.Context().availableImageHandler(EmbeddedPingPong::availablePongImageHandler).aeronDirectoryName(embeddedDirName);
    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 size of " + MESSAGE_LENGTH + " bytes");
    final FragmentAssembler dataHandler = new FragmentAssembler(EmbeddedPingPong::pongHandler);
    try (Aeron aeron = Aeron.connect(ctx);
        Publication pingPublication = aeron.addPublication(PING_CHANNEL, PING_STREAM_ID);
        Subscription pongSubscription = aeron.addSubscription(PONG_CHANNEL, PONG_STREAM_ID)) {
        System.out.println("Waiting for new image from Pong...");
        PONG_IMAGE_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(dataHandler, pingPublication, pongSubscription, WARMUP_NUMBER_OF_MESSAGES);
        }
        Thread.sleep(100);
        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        do {
            HISTOGRAM.reset();
            System.out.println("Pinging " + NUMBER_OF_MESSAGES + " messages");
            roundTripMessages(dataHandler, pingPublication, pongSubscription, NUMBER_OF_MESSAGES);
            System.out.println("Histogram of RTT latencies in microseconds.");
            HISTOGRAM.outputPercentileDistribution(System.out, 1000.0);
        } while (barrier.await());
    }
}
Also used : Publication(io.aeron.Publication) ContinueBarrier(org.agrona.console.ContinueBarrier) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron) FragmentAssembler(io.aeron.FragmentAssembler)

Example 2 with Aeron

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

the class EmbeddedPingPong method startPong.

private static Thread startPong(final String embeddedDirName) {
    return new Thread() {

        public void run() {
            System.out.println("Subscribing Ping at " + PING_CHANNEL + " on stream Id " + PING_STREAM_ID);
            System.out.println("Publishing Pong at " + PONG_CHANNEL + " on stream Id " + PONG_STREAM_ID);
            final Aeron.Context ctx = new Aeron.Context().aeronDirectoryName(embeddedDirName);
            try (Aeron aeron = Aeron.connect(ctx);
                Publication pongPublication = aeron.addPublication(PONG_CHANNEL, PONG_STREAM_ID);
                Subscription pingSubscription = aeron.addSubscription(PING_CHANNEL, PING_STREAM_ID)) {
                final FragmentAssembler dataHandler = new FragmentAssembler((buffer, offset, length, header) -> pingHandler(pongPublication, buffer, offset, length));
                while (RUNNING.get()) {
                    PING_HANDLER_IDLE_STRATEGY.idle(pingSubscription.poll(dataHandler, FRAME_COUNT_LIMIT));
                }
                System.out.println("Shutting down...");
            }
        }
    };
}
Also used : Publication(io.aeron.Publication) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron) FragmentAssembler(io.aeron.FragmentAssembler)

Example 3 with Aeron

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

the class EmbeddedThroughput method main.

public static void main(final String[] args) throws Exception {
    loadPropertiesFiles(args);
    final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), EmbeddedThroughput::printRate);
    final FragmentHandler rateReporterHandler = rateReporterHandler(reporter);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final AtomicBoolean running = new AtomicBoolean(true);
    try (MediaDriver ignore = MediaDriver.launch();
        Aeron aeron = Aeron.connect();
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID);
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
        executor.execute(reporter);
        executor.execute(() -> SamplesUtil.subscriberLoop(rateReporterHandler, FRAGMENT_COUNT_LIMIT, running).accept(subscription));
        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        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++) {
                ATOMIC_BUFFER.putLong(0, i);
                OFFER_IDLE_STRATEGY.reset();
                while (publication.offer(ATOMIC_BUFFER, 0, ATOMIC_BUFFER.capacity()) < 0) {
                    OFFER_IDLE_STRATEGY.idle();
                    backPressureCount++;
                }
            }
            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) FragmentHandler(io.aeron.logbuffer.FragmentHandler) ExecutorService(java.util.concurrent.ExecutorService) Publication(io.aeron.Publication) ContinueBarrier(org.agrona.console.ContinueBarrier) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron)

Example 4 with Aeron

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

the class MultiplePublishersWithFragmentation method main.

public static void main(final String[] args) {
    System.out.println("Publishing to " + CHANNEL + " on stream Id " + STREAM_ID_1 + " and stream Id " + STREAM_ID_2);
    try (Aeron aeron = Aeron.connect();
        Publication publication1 = aeron.addPublication(CHANNEL, STREAM_ID_1);
        Publication publication2 = aeron.addPublication(CHANNEL, STREAM_ID_2)) {
        int j = 1;
        int k = 1;
        final String message1 = "Hello World! " + j;
        BUFFER_1.putBytes(0, message1.getBytes());
        final String message2 = "Hello World! " + k;
        BUFFER_2.putBytes(0, message2.getBytes());
        while (j <= 5000 || k <= 5000) {
            boolean offerStatus1 = false;
            boolean offerStatus2 = false;
            long result1;
            long result2;
            while (!(offerStatus1 || offerStatus2)) {
                if (j <= 5000) {
                    result1 = publication1.offer(BUFFER_1, 0, BUFFER_1.capacity());
                    if (result1 < 0L) {
                        if (result1 == Publication.BACK_PRESSURED) {
                            System.out.println(" Offer failed due to back pressure for stream Id " + STREAM_ID_1);
                        } else if (result1 == Publication.NOT_CONNECTED) {
                            System.out.println(" Offer failed because publisher is not yet " + "connected to subscriber for stream Id " + STREAM_ID_1);
                        } else {
                            System.out.println(" Offer failed due to unknown reason");
                        }
                        offerStatus1 = false;
                    } else {
                        j++;
                        offerStatus1 = true;
                        System.out.println("Successfully sent data on stream " + STREAM_ID_1 + " and data length " + BUFFER_1.capacity() + " at offset " + result1);
                    }
                }
                if (k <= 5000) {
                    result2 = publication2.offer(BUFFER_2, 0, BUFFER_2.capacity());
                    if (result2 < 0L) {
                        if (result2 == Publication.BACK_PRESSURED) {
                            System.out.println(" Offer failed because publisher is not yet " + "connected to subscriber for stream Id " + STREAM_ID_2);
                        } else if (result2 == Publication.NOT_CONNECTED) {
                            System.out.println("Offer failed - publisher is not yet connected to subscriber" + STREAM_ID_2);
                        } else {
                            System.out.println("Offer failed due to unknown reason");
                        }
                        offerStatus2 = false;
                    } else {
                        k++;
                        offerStatus2 = true;
                        System.out.println("Successfully sent data on stream " + STREAM_ID_2 + " and data length " + BUFFER_2.capacity() + " at offset " + result2);
                    }
                }
            }
        }
        System.out.println("Done sending total messages for stream Id " + STREAM_ID_1 + " = " + (j - 1) + " and stream Id " + STREAM_ID_2 + " = " + (k - 1));
    }
}
Also used : Publication(io.aeron.Publication) Aeron(io.aeron.Aeron)

Example 5 with Aeron

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

the class MultipleSubscribersWithFragmentAssembly method main.

public static void main(final String[] args) {
    System.out.format("Subscribing to %s on stream ID %d and stream ID %d%n", CHANNEL, STREAM_ID_1, STREAM_ID_2);
    final Aeron.Context ctx = new Aeron.Context().availableImageHandler(MultipleSubscribersWithFragmentAssembly::eventAvailableImage).unavailableImageHandler(MultipleSubscribersWithFragmentAssembly::eventUnavailableImage);
    final FragmentAssembler dataHandler1 = new FragmentAssembler(reassembledStringMessage1(STREAM_ID_1));
    final FragmentAssembler dataHandler2 = new FragmentAssembler(reassembledStringMessage2(STREAM_ID_2));
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    try (Aeron aeron = Aeron.connect(ctx);
        Subscription subscription1 = aeron.addSubscription(CHANNEL, STREAM_ID_1);
        Subscription subscription2 = aeron.addSubscription(CHANNEL, STREAM_ID_2)) {
        final IdleStrategy idleStrategy = new BackoffIdleStrategy(100, 10, TimeUnit.MICROSECONDS.toNanos(1), TimeUnit.MICROSECONDS.toNanos(100));
        int idleCount = 0;
        while (running.get()) {
            final int fragmentsRead1 = subscription1.poll(dataHandler1, FRAGMENT_COUNT_LIMIT);
            final int fragmentsRead2 = subscription2.poll(dataHandler2, FRAGMENT_COUNT_LIMIT);
            if ((fragmentsRead1 + fragmentsRead2) == 0) {
                idleStrategy.idle(idleCount++);
            } else {
                idleCount = 0;
            }
        }
        System.out.println("Shutting down...");
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron) FragmentAssembler(io.aeron.FragmentAssembler)

Aggregations

Aeron (io.aeron.Aeron)59 MediaDriver (io.aeron.driver.MediaDriver)28 Publication (io.aeron.Publication)27 Subscription (io.aeron.Subscription)24 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)22 FragmentAssembler (io.aeron.FragmentAssembler)11 IdleStrategy (org.agrona.concurrent.IdleStrategy)11 ContinueBarrier (org.agrona.console.ContinueBarrier)9 FragmentHandler (io.aeron.logbuffer.FragmentHandler)8 ExecutorService (java.util.concurrent.ExecutorService)8 Test (org.junit.jupiter.api.Test)8 CommonContext (io.aeron.CommonContext)7 File (java.io.File)7 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)7 AeronArchive (io.aeron.archive.client.AeronArchive)6 DirectBuffer (org.agrona.DirectBuffer)6 Test (org.junit.Test)5 Archive (io.aeron.archive.Archive)4 TimeUnit (java.util.concurrent.TimeUnit)4 CloseHelper (org.agrona.CloseHelper)4