Search in sources :

Example 1 with Subscription

use of io.aeron.Subscription 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 Subscription

use of io.aeron.Subscription 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 Subscription

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

the class ArchiveReplayLoadTest method replay.

@Test(timeout = TEST_DURATION_SEC * 2000)
public void replay() throws InterruptedException {
    final String channel = archive.context().recordingEventsChannel();
    final int streamId = archive.context().recordingEventsStreamId();
    try (Publication publication = aeron.addPublication(PUBLISH_URI, PUBLISH_STREAM_ID);
        Subscription recordingEvents = aeron.addSubscription(channel, streamId)) {
        await(recordingEvents::isConnected);
        aeronArchive.startRecording(PUBLISH_URI, PUBLISH_STREAM_ID, SourceLocation.LOCAL);
        await(publication::isConnected);
        final CountDownLatch recordingStopped = prepAndSendMessages(recordingEvents, publication);
        assertNull(trackerError);
        recordingStopped.await();
        aeronArchive.stopRecording(PUBLISH_URI, PUBLISH_STREAM_ID);
        assertNull(trackerError);
        assertNotEquals(-1L, recordingId);
        assertEquals(expectedRecordingLength, recordedLength);
    }
    final long deadlineMs = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(TEST_DURATION_SEC);
    int i = 0;
    while (System.currentTimeMillis() < deadlineMs) {
        final long start = System.currentTimeMillis();
        replay(i);
        printScore(++i, System.currentTimeMillis() - start);
        Thread.sleep(100);
    }
}
Also used : Publication(io.aeron.Publication) Subscription(io.aeron.Subscription) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with Subscription

use of io.aeron.Subscription 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 5 with Subscription

use of io.aeron.Subscription 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

Subscription (io.aeron.Subscription)47 Aeron (io.aeron.Aeron)24 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 Publication (io.aeron.Publication)14 MediaDriver (io.aeron.driver.MediaDriver)14 FragmentAssembler (io.aeron.FragmentAssembler)12 IdleStrategy (org.agrona.concurrent.IdleStrategy)11 FragmentHandler (io.aeron.logbuffer.FragmentHandler)10 ContinueBarrier (org.agrona.console.ContinueBarrier)8 ExecutorService (java.util.concurrent.ExecutorService)5 BackoffIdleStrategy (org.agrona.concurrent.BackoffIdleStrategy)5 TimeUnit (java.util.concurrent.TimeUnit)4 SigInt (org.agrona.concurrent.SigInt)3 Test (org.junit.jupiter.api.Test)3 ExclusivePublication (io.aeron.ExclusivePublication)2 AeronArchive (io.aeron.archive.client.AeronArchive)2 Context (io.aeron.archive.client.AeronArchive.Context)2 io.aeron.logbuffer (io.aeron.logbuffer)2 File (java.io.File)2 CountDownLatch (java.util.concurrent.CountDownLatch)2