Search in sources :

Example 26 with Publication

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

the class ClusterTest method shouldRejectAnAdminRequestIfLeadershipTermIsInvalid.

@Test
@InterruptAfter(10)
void shouldRejectAnAdminRequestIfLeadershipTermIsInvalid() {
    cluster = aCluster().withStaticNodes(3).start();
    systemTestWatcher.cluster(cluster);
    cluster.awaitLeader();
    AeronCluster client = cluster.connectClient();
    final long requestCorrelationId = System.nanoTime();
    final long expectedLeadershipTermId = client.leadershipTermId();
    final long invalidLeadershipTermId = expectedLeadershipTermId - 1000;
    final AdminRequestType requestType = AdminRequestType.NULL_VAL;
    final MutableBoolean responseReceived = injectAdminResponseEgressListener(requestCorrelationId, requestType, AdminResponseCode.ERROR, "Invalid leadership term: expected " + expectedLeadershipTermId + ", got " + invalidLeadershipTermId);
    client = cluster.connectClient();
    final AdminRequestEncoder adminRequestEncoder = new AdminRequestEncoder().wrapAndApplyHeader(cluster.msgBuffer(), 0, new MessageHeaderEncoder()).leadershipTermId(invalidLeadershipTermId).clusterSessionId(client.clusterSessionId()).correlationId(requestCorrelationId).requestType(requestType);
    final Publication ingressPublication = client.ingressPublication();
    while (ingressPublication.offer(adminRequestEncoder.buffer(), 0, MessageHeaderEncoder.ENCODED_LENGTH + adminRequestEncoder.encodedLength()) < 0) {
        Tests.yield();
    }
    while (!responseReceived.get()) {
        client.pollEgress();
        Tests.yield();
    }
}
Also used : AeronCluster(io.aeron.cluster.client.AeronCluster) MutableBoolean(org.agrona.collections.MutableBoolean) Publication(io.aeron.Publication) Test(org.junit.jupiter.api.Test)

Example 27 with Publication

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

the class ControlResponseProxy method sendDescriptor.

int sendDescriptor(final long controlSessionId, final long correlationId, final UnsafeBuffer descriptorBuffer, final ControlSession session) {
    final int messageLength = Catalog.descriptorLength(descriptorBuffer) + MESSAGE_HEADER_LENGTH;
    final int contentLength = messageLength - recordingIdEncodingOffset() - MESSAGE_HEADER_LENGTH;
    recordingDescriptorEncoder.wrapAndApplyHeader(buffer, 0, messageHeaderEncoder).controlSessionId(controlSessionId).correlationId(correlationId);
    int attempts = SEND_ATTEMPTS;
    final Publication publication = session.controlPublication();
    do {
        final long result = publication.offer(buffer, 0, DESCRIPTOR_PREFIX_LENGTH, descriptorBuffer, DESCRIPTOR_CONTENT_OFFSET, contentLength);
        if (result > 0) {
            return messageLength;
        }
        checkResult(session, result);
    } while (--attempts > 0);
    return 0;
}
Also used : Publication(io.aeron.Publication)

Example 28 with Publication

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

the class EmbeddedPingPong method startPong.

private static Thread startPong(final Aeron aeron) {
    return new Thread(() -> {
        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);
        try (Subscription pingSubscription = aeron.addSubscription(PING_CHANNEL, PING_STREAM_ID);
            Publication pongPublication = EXCLUSIVE_PUBLICATIONS ? aeron.addExclusivePublication(PONG_CHANNEL, PONG_STREAM_ID) : aeron.addPublication(PONG_CHANNEL, PONG_STREAM_ID)) {
            final BufferClaim bufferClaim = new BufferClaim();
            final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> pingHandler(bufferClaim, pongPublication, buffer, offset, length, header);
            while (RUNNING.get()) {
                PING_HANDLER_IDLE_STRATEGY.idle(pingSubscription.poll(fragmentHandler, FRAME_COUNT_LIMIT));
            }
            System.out.println("Shutting down...");
        }
    });
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) Aeron(io.aeron.Aeron) Subscription(io.aeron.Subscription) Image(io.aeron.Image) SystemUtil.loadPropertiesFiles(org.agrona.SystemUtil.loadPropertiesFiles) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FragmentAssembler(io.aeron.FragmentAssembler) BufferUtil(org.agrona.BufferUtil) org.agrona.concurrent(org.agrona.concurrent) BitUtil(org.agrona.BitUtil) TimeUnit(java.util.concurrent.TimeUnit) Histogram(org.HdrHistogram.Histogram) CountDownLatch(java.util.concurrent.CountDownLatch) ThreadingMode(io.aeron.driver.ThreadingMode) io.aeron.logbuffer(io.aeron.logbuffer) ContinueBarrier(org.agrona.console.ContinueBarrier) Publication(io.aeron.Publication) DirectBuffer(org.agrona.DirectBuffer) Publication(io.aeron.Publication) Subscription(io.aeron.Subscription)

Example 29 with Publication

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

the class EmbeddedPingPong method runPing.

private static void runPing(final Aeron aeron) throws InterruptedException {
    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 payload length of " + MESSAGE_LENGTH + " bytes");
    System.out.println("Using exclusive publications: " + EXCLUSIVE_PUBLICATIONS);
    final FragmentAssembler dataHandler = new FragmentAssembler(EmbeddedPingPong::pongHandler);
    try (Subscription pongSubscription = aeron.addSubscription(PONG_CHANNEL, PONG_STREAM_ID, EmbeddedPingPong::availablePongImageHandler, null);
        Publication pingPublication = 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...");
        PONG_IMAGE_LATCH.await();
        System.out.format("Warming up... %d iterations of %,d messages%n", WARMUP_NUMBER_OF_ITERATIONS, WARMUP_NUMBER_OF_MESSAGES);
        for (int i = 0; i < WARMUP_NUMBER_OF_ITERATIONS; i++) {
            roundTripMessages(dataHandler, pingPublication, pongSubscription, WARMUP_NUMBER_OF_MESSAGES);
            Thread.yield();
        }
        Thread.sleep(100);
        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        do {
            HISTOGRAM.reset();
            System.out.format("Pinging %,d messages%n", NUMBER_OF_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) FragmentAssembler(io.aeron.FragmentAssembler)

Example 30 with Publication

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

the class MultiplePublishersWithFragmentation method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
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 > 0) {
                        j++;
                        offerStatus1 = true;
                        System.out.println("Successfully sent data on stream id " + STREAM_ID_1 + " and data length " + BUFFER_1.capacity() + " at offset " + result1);
                    } else {
                        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 unexpected reason: " + result1);
                        }
                        offerStatus1 = false;
                    }
                }
                if (k <= 5000) {
                    result2 = publication2.offer(BUFFER_2, 0, BUFFER_2.capacity());
                    if (result2 > 0) {
                        k++;
                        offerStatus2 = true;
                        System.out.println("Successfully sent data on stream id " + STREAM_ID_2 + " and data length " + BUFFER_2.capacity() + " at offset " + result2);
                    } else {
                        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 unexpected reason: " + result2);
                        }
                        offerStatus2 = false;
                    }
                }
            }
        }
        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)

Aggregations

Publication (io.aeron.Publication)71 Aeron (io.aeron.Aeron)37 Test (org.junit.jupiter.api.Test)30 CountersReader (org.agrona.concurrent.status.CountersReader)24 InterruptAfter (io.aeron.test.InterruptAfter)22 MediaDriver (io.aeron.driver.MediaDriver)21 Subscription (io.aeron.Subscription)19 File (java.io.File)14 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 ChannelUriStringBuilder (io.aeron.ChannelUriStringBuilder)10 AeronArchive (io.aeron.archive.client.AeronArchive)10 ThreadingMode (io.aeron.driver.ThreadingMode)10 InterruptingTestCallback (io.aeron.test.InterruptingTestCallback)10 Tests (io.aeron.test.Tests)10 ContinueBarrier (org.agrona.console.ContinueBarrier)10 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)10 FragmentAssembler (io.aeron.FragmentAssembler)9 CloseHelper (org.agrona.CloseHelper)9 DirectBuffer (org.agrona.DirectBuffer)9