Search in sources :

Example 1 with FragmentHandler

use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.

the class MultiSubscriberTest method shouldReceiveMessageOnSeparateSubscriptions.

@Test(timeout = 10_000)
public void shouldReceiveMessageOnSeparateSubscriptions() {
    final FragmentHandler mockFragmentHandlerOne = mock(FragmentHandler.class);
    final FragmentHandler mockFragmentHandlerTwo = mock(FragmentHandler.class);
    final FragmentAssembler adapterOne = new FragmentAssembler(mockFragmentHandlerOne);
    final FragmentAssembler adapterTwo = new FragmentAssembler(mockFragmentHandlerTwo);
    try (Publication publication = aeron.addPublication(CHANNEL_1, STREAM_ID);
        Subscription subscriptionOne = aeron.addSubscription(CHANNEL_1, STREAM_ID);
        Subscription subscriptionTwo = aeron.addSubscription(CHANNEL_2, STREAM_ID)) {
        final byte[] expectedBytes = "Hello, World! here is a small message".getBytes();
        final UnsafeBuffer srcBuffer = new UnsafeBuffer(expectedBytes);
        assertThat(subscriptionOne.poll(adapterOne, FRAGMENT_COUNT_LIMIT), is(0));
        assertThat(subscriptionTwo.poll(adapterTwo, FRAGMENT_COUNT_LIMIT), is(0));
        while (!subscriptionOne.isConnected() || !subscriptionTwo.isConnected()) {
            SystemTest.checkInterruptedStatus();
            Thread.yield();
        }
        while (publication.offer(srcBuffer) < 0L) {
            SystemTest.checkInterruptedStatus();
            Thread.yield();
        }
        while (subscriptionOne.poll(adapterOne, FRAGMENT_COUNT_LIMIT) == 0) {
            SystemTest.checkInterruptedStatus();
            Thread.yield();
        }
        while (subscriptionTwo.poll(adapterTwo, FRAGMENT_COUNT_LIMIT) == 0) {
            SystemTest.checkInterruptedStatus();
            Thread.yield();
        }
        verifyData(srcBuffer, mockFragmentHandlerOne);
        verifyData(srcBuffer, mockFragmentHandlerTwo);
    }
}
Also used : FragmentHandler(io.aeron.logbuffer.FragmentHandler) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Example 2 with FragmentHandler

use of io.aeron.logbuffer.FragmentHandler 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 3 with FragmentHandler

use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.

the class RateSubscriber method main.

public static void main(final String[] args) throws Exception {
    System.out.println("Subscribing to " + CHANNEL + " on stream Id " + STREAM_ID);
    final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
    final ExecutorService executor = Executors.newFixedThreadPool(1);
    final Aeron.Context ctx = new Aeron.Context().availableImageHandler(SamplesUtil::printAvailableImage).unavailableImageHandler(SamplesUtil::printUnavailableImage);
    if (EMBEDDED_MEDIA_DRIVER) {
        ctx.aeronDirectoryName(driver.aeronDirectoryName());
    }
    final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), SamplesUtil::printRate);
    final FragmentHandler rateReporterHandler = new FragmentAssembler(rateReporterHandler(reporter));
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> {
        reporter.halt();
        running.set(false);
    });
    try (Aeron aeron = Aeron.connect(ctx);
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
        final Future future = executor.submit(() -> SamplesUtil.subscriberLoop(rateReporterHandler, FRAGMENT_COUNT_LIMIT, running).accept(subscription));
        reporter.run();
        System.out.println("Shutting down...");
        future.get();
    }
    executor.shutdown();
    if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
        System.out.println("Warning: not all tasks completed promptly");
    }
    CloseHelper.quietClose(driver);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaDriver(io.aeron.driver.MediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron) FragmentAssembler(io.aeron.FragmentAssembler)

Example 4 with FragmentHandler

use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.

the class EmbeddedExclusiveSpiedThroughput method main.

public static void main(final String[] args) throws Exception {
    loadPropertiesFiles(args);
    final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), EmbeddedExclusiveSpiedThroughput::printRate);
    final FragmentHandler rateReporterHandler = rateReporterHandler(reporter);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final AtomicBoolean running = new AtomicBoolean(true);
    final MediaDriver.Context ctx = new MediaDriver.Context().spiesSimulateConnection(true);
    try (MediaDriver ignore = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect();
        ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID);
        Subscription subscription = aeron.addSubscription(CommonContext.SPY_PREFIX + 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) ContinueBarrier(org.agrona.console.ContinueBarrier) ExclusivePublication(io.aeron.ExclusivePublication) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron)

Example 5 with FragmentHandler

use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.

the class SubscriptionTest method shouldReadData.

@Test
public void shouldReadData() {
    subscription.addImage(imageOneMock);
    when(imageOneMock.poll(any(FragmentHandler.class), anyInt())).then((invocation) -> {
        final FragmentHandler handler = (FragmentHandler) invocation.getArguments()[0];
        handler.onFragment(atomicReadBuffer, HEADER_LENGTH, READ_BUFFER_CAPACITY - HEADER_LENGTH, header);
        return 1;
    });
    assertThat(subscription.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT), is(1));
    verify(fragmentHandler).onFragment(eq(atomicReadBuffer), eq(HEADER_LENGTH), eq(READ_BUFFER_CAPACITY - HEADER_LENGTH), any(Header.class));
}
Also used : Header(io.aeron.logbuffer.Header) FragmentHandler(io.aeron.logbuffer.FragmentHandler) Test(org.junit.Test)

Aggregations

FragmentHandler (io.aeron.logbuffer.FragmentHandler)50 MediaDriver (io.aeron.driver.MediaDriver)34 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)24 InterruptAfter (io.aeron.test.InterruptAfter)22 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)21 Tests (io.aeron.test.Tests)20 Test (org.junit.jupiter.api.Test)20 InterruptingTestCallback (io.aeron.test.InterruptingTestCallback)18 TestMediaDriver (io.aeron.test.driver.TestMediaDriver)18 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)18 ThreadingMode (io.aeron.driver.ThreadingMode)17 SystemTestWatcher (io.aeron.test.SystemTestWatcher)17 CloseHelper (org.agrona.CloseHelper)16 MutableInteger (org.agrona.collections.MutableInteger)15 AfterEach (org.junit.jupiter.api.AfterEach)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 TimeUnit (java.util.concurrent.TimeUnit)12 Subscription (io.aeron.Subscription)10 DirectBuffer (org.agrona.DirectBuffer)10