Search in sources :

Example 1 with Publication

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

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

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

the class ArchiverProtocolProxy method sendNotification.

private void sendNotification(final int length) {
    final Publication publication = this.archiverNotifications;
    while (true) {
        final long result = publication.offer(outboundBuffer, 0, MessageHeaderEncoder.ENCODED_LENGTH + length);
        if (result > 0 || result == Publication.NOT_CONNECTED) {
            idleStrategy.reset();
            break;
        } else if (result == Publication.CLOSED) {
            throw new IllegalStateException();
        }
        idleStrategy.idle();
    }
}
Also used : Publication(io.aeron.Publication)

Example 4 with Publication

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

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

Aggregations

Publication (io.aeron.Publication)52 Aeron (io.aeron.Aeron)30 MediaDriver (io.aeron.driver.MediaDriver)15 Test (org.junit.jupiter.api.Test)15 Subscription (io.aeron.Subscription)14 CountersReader (org.agrona.concurrent.status.CountersReader)13 InterruptAfter (io.aeron.test.InterruptAfter)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 FragmentAssembler (io.aeron.FragmentAssembler)8 File (java.io.File)8 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)8 ContinueBarrier (org.agrona.console.ContinueBarrier)8 DirectBuffer (org.agrona.DirectBuffer)7 AeronArchive (io.aeron.archive.client.AeronArchive)6 ChannelUriStringBuilder (io.aeron.ChannelUriStringBuilder)5 ThreadingMode (io.aeron.driver.ThreadingMode)5 RecordingPos (io.aeron.archive.status.RecordingPos)4 InterruptingTestCallback (io.aeron.test.InterruptingTestCallback)4 Tests (io.aeron.test.Tests)4 CloseHelper (org.agrona.CloseHelper)4