Search in sources :

Example 56 with FragmentHandler

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

the class BufferClaimMessageTest method shouldReceivePublishedMessageWithInterleavedAbort.

@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldReceivePublishedMessageWithInterleavedAbort(final String channel) {
    final MutableInteger fragmentCount = new MutableInteger();
    final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> fragmentCount.value++;
    final BufferClaim bufferClaim = new BufferClaim();
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocate(MESSAGE_LENGTH));
    try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
        Publication publication = aeron.addPublication(channel, STREAM_ID)) {
        publishMessage(srcBuffer, publication);
        while (publication.tryClaim(MESSAGE_LENGTH, bufferClaim) < 0L) {
            Tests.yield();
        }
        publishMessage(srcBuffer, publication);
        bufferClaim.abort();
        final int expectedNumberOfFragments = 2;
        int numFragments = 0;
        do {
            final int fragments = subscription.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT);
            if (0 == fragments) {
                Tests.yield();
            }
            numFragments += fragments;
        } while (numFragments < expectedNumberOfFragments);
        assertEquals(expectedNumberOfFragments, fragmentCount.value);
    }
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) MutableBoolean(org.agrona.collections.MutableBoolean) Arrays(java.util.Arrays) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) ByteBuffer(java.nio.ByteBuffer) InterruptAfter(io.aeron.test.InterruptAfter) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ThreadingMode(io.aeron.driver.ThreadingMode) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) BufferClaim(io.aeron.logbuffer.BufferClaim) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) MutableInteger(org.agrona.collections.MutableInteger) CloseHelper(org.agrona.CloseHelper) MethodSource(org.junit.jupiter.params.provider.MethodSource) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) BufferClaim(io.aeron.logbuffer.BufferClaim) InterruptAfter(io.aeron.test.InterruptAfter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 57 with FragmentHandler

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

the class ReplayedBasicSubscriber method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
public static void main(final String[] args) {
    System.out.println("Subscribing to " + CHANNEL + " on stream id " + STREAM_ID);
    final FragmentHandler fragmentHandler = SamplesUtil.printAsciiMessage(STREAM_ID);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    // Create a unique response stream id so not to clash with other archive clients.
    final AeronArchive.Context archiveCtx = new AeronArchive.Context().controlResponseStreamId(AeronArchive.Configuration.controlResponseStreamId() + 2);
    try (AeronArchive archive = AeronArchive.connect(archiveCtx)) {
        final long recordingId = findLatestRecording(archive);
        final long position = 0L;
        final long length = Long.MAX_VALUE;
        final long sessionId = archive.startReplay(recordingId, position, length, CHANNEL, REPLAY_STREAM_ID);
        final String channel = ChannelUri.addSessionId(CHANNEL, (int) sessionId);
        try (Subscription subscription = archive.context().aeron().addSubscription(channel, REPLAY_STREAM_ID)) {
            SamplesUtil.subscriberLoop(fragmentHandler, FRAGMENT_COUNT_LIMIT, running).accept(subscription);
            System.out.println("Shutting down...");
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FragmentHandler(io.aeron.logbuffer.FragmentHandler) Subscription(io.aeron.Subscription) AeronArchive(io.aeron.archive.client.AeronArchive)

Example 58 with FragmentHandler

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

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

the class SimpleSubscriber method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
public static void main(final String[] args) {
    // Maximum number of message fragments to receive during a single 'poll' operation
    final int fragmentLimitCount = 10;
    // The channel (an endpoint identifier) to receive messages from
    final String channel = "aeron:udp?endpoint=localhost:40123";
    // A unique identifier for a stream within a channel. Stream ID 0 is reserved
    // for internal use and should not be used by applications.
    final int streamId = 10;
    System.out.println("Subscribing to " + channel + " on stream id " + streamId);
    final AtomicBoolean running = new AtomicBoolean(true);
    // Register a SIGINT handler for graceful shutdown.
    SigInt.register(() -> running.set(false));
    // dataHandler method is called for every new datagram received
    final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
        final byte[] data = new byte[length];
        buffer.getBytes(offset, data);
        System.out.printf("Received message (%s) to stream %d from session %x term id %x term offset %d (%d@%d)%n", new String(data), streamId, header.sessionId(), header.termId(), header.termOffset(), length, offset);
        // Received the intended message, time to exit the program
        running.set(false);
    };
    // Create a context, needed for client connection to media driver
    // A separate media driver process need to run prior to running this application
    final Aeron.Context ctx = new Aeron.Context();
    // clean up resources when this try block is finished.
    try (Aeron aeron = Aeron.connect(ctx);
        Subscription subscription = aeron.addSubscription(channel, streamId)) {
        final IdleStrategy idleStrategy = new BackoffIdleStrategy(100, 10, TimeUnit.MICROSECONDS.toNanos(1), TimeUnit.MICROSECONDS.toNanos(100));
        // Try to read the data from subscriber
        while (running.get()) {
            // poll delivers messages to the dataHandler as they arrive
            // and returns number of fragments read, or 0
            // if no data is available.
            final int fragmentsRead = subscription.poll(fragmentHandler, fragmentLimitCount);
            // Give the IdleStrategy a chance to spin/yield/sleep to reduce CPU
            // use if no messages were received.
            idleStrategy.idle(fragmentsRead);
        }
        System.out.println("Shutting down...");
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Aeron(io.aeron.Aeron) Subscription(io.aeron.Subscription) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FragmentHandler(io.aeron.logbuffer.FragmentHandler) SigInt(org.agrona.concurrent.SigInt) IdleStrategy(org.agrona.concurrent.IdleStrategy) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) FragmentHandler(io.aeron.logbuffer.FragmentHandler) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron)

Example 60 with FragmentHandler

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

the class BasicSubscriber method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
public static void main(final String[] args) {
    System.out.println("Subscribing to " + CHANNEL + " on stream id " + STREAM_ID);
    final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
    final Aeron.Context ctx = new Aeron.Context().availableImageHandler(SamplesUtil::printAvailableImage).unavailableImageHandler(SamplesUtil::printUnavailableImage);
    if (EMBEDDED_MEDIA_DRIVER) {
        ctx.aeronDirectoryName(driver.aeronDirectoryName());
    }
    final FragmentHandler fragmentHandler = SamplesUtil.printAsciiMessage(STREAM_ID);
    final AtomicBoolean running = new AtomicBoolean(true);
    // Register a SIGINT handler for graceful shutdown.
    SigInt.register(() -> running.set(false));
    // clean up resources when this try block is finished
    try (Aeron aeron = Aeron.connect(ctx);
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
        SamplesUtil.subscriberLoop(fragmentHandler, FRAGMENT_COUNT_LIMIT, running).accept(subscription);
        System.out.println("Shutting down...");
    }
    CloseHelper.close(driver);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaDriver(io.aeron.driver.MediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron)

Aggregations

FragmentHandler (io.aeron.logbuffer.FragmentHandler)70 MediaDriver (io.aeron.driver.MediaDriver)50 InterruptAfter (io.aeron.test.InterruptAfter)44 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)42 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)42 Tests (io.aeron.test.Tests)40 Test (org.junit.jupiter.api.Test)40 InterruptingTestCallback (io.aeron.test.InterruptingTestCallback)36 TestMediaDriver (io.aeron.test.driver.TestMediaDriver)36 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)36 SystemTestWatcher (io.aeron.test.SystemTestWatcher)34 ThreadingMode (io.aeron.driver.ThreadingMode)30 AfterEach (org.junit.jupiter.api.AfterEach)30 CloseHelper (org.agrona.CloseHelper)28 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)28 MutableInteger (org.agrona.collections.MutableInteger)22 DirectBuffer (org.agrona.DirectBuffer)20 Assertions (org.junit.jupiter.api.Assertions)20 TimeUnit (java.util.concurrent.TimeUnit)18 MethodSource (org.junit.jupiter.params.provider.MethodSource)18