Search in sources :

Example 41 with Publication

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

the class EgressPublisher method sendMembershipResponse.

public boolean sendMembershipResponse(final ClusterSession session, final byte[] encodedResponse) {
    final Publication publication = session.responsePublication();
    if (!publication.isConnected()) {
        return false;
    }
    membershipQueryResponseEncoder.wrapAndApplyHeader(buffer, 0, messageHeaderEncoder).clusterSessionId(session.id()).correlationId(session.lastCorrelationId()).putEncodedResponse(encodedResponse, 0, encodedResponse.length);
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + membershipQueryResponseEncoder.encodedLength();
    int attempts = SEND_ATTEMPTS;
    do {
        final long result = publication.offer(buffer, 0, length);
        if (result > 0) {
            return true;
        }
    } while (--attempts > 0);
    return false;
}
Also used : Publication(io.aeron.Publication)

Example 42 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 payload length 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 43 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(() -> {
        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 44 with Publication

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

the class DriverLoggingAgentTest method testLogMediaDriverEvents.

private void testLogMediaDriverEvents(final String channel, final String enabledEvents, final EnumSet<DriverEventCode> expectedEvents) {
    before(enabledEvents, expectedEvents);
    final MediaDriver.Context driverCtx = new MediaDriver.Context().errorHandler(Tests::onError).publicationLingerTimeoutNs(0).timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(1));
    try (MediaDriver mediaDriver = MediaDriver.launch(driverCtx)) {
        try (Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
            Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
            Publication publication = aeron.addPublication(channel, STREAM_ID)) {
            final UnsafeBuffer offerBuffer = new UnsafeBuffer(new byte[32]);
            while (publication.offer(offerBuffer) < 0) {
                Tests.yield();
            }
            final MutableInteger counter = new MutableInteger();
            final FragmentHandler handler = (buffer, offset, length, header) -> counter.value++;
            while (0 == subscription.poll(handler, 1)) {
                Tests.yield();
            }
            assertEquals(counter.get(), 1);
        }
        final Supplier<String> errorMessage = () -> "Pending events: " + WAIT_LIST;
        while (!WAIT_LIST.isEmpty()) {
            Tests.yieldingIdle(errorMessage);
        }
    }
}
Also used : Tests(io.aeron.test.Tests) DriverEventCode(io.aeron.agent.DriverEventCode) Subscription(io.aeron.Subscription) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) EnumSource(org.junit.jupiter.params.provider.EnumSource) Supplier(java.util.function.Supplier) Collections.synchronizedSet(java.util.Collections.synchronizedSet) MessageHandler(org.agrona.concurrent.MessageHandler) EVENT_READER_FRAME_LIMIT(io.aeron.agent.EventConfiguration.EVENT_READER_FRAME_LIMIT) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) EVENT_RING_BUFFER(io.aeron.agent.EventConfiguration.EVENT_RING_BUFFER) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Publication(io.aeron.Publication) Agent(org.agrona.concurrent.Agent) MutableInteger(org.agrona.collections.MutableInteger) EnumSet(java.util.EnumSet) MediaDriver(io.aeron.driver.MediaDriver) Aeron(io.aeron.Aeron) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) EnumMap(java.util.EnumMap) Set(java.util.Set) IPC_CHANNEL(io.aeron.CommonContext.IPC_CHANNEL) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) InterruptAfter(io.aeron.test.InterruptAfter) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) INCLUDE(org.junit.jupiter.params.provider.EnumSource.Mode.INCLUDE) MutableDirectBuffer(org.agrona.MutableDirectBuffer) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) Publication(io.aeron.Publication) Tests(io.aeron.test.Tests) Aeron(io.aeron.Aeron) MediaDriver(io.aeron.driver.MediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) Subscription(io.aeron.Subscription) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Example 45 with Publication

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

the class AeronArchiveTest method closeOwningAeronClient.

@Test
void closeOwningAeronClient() {
    final long controlSessionId = 42;
    final Aeron.Context aeronContext = mock(Aeron.Context.class);
    when(aeronContext.nanoClock()).thenReturn(SystemNanoClock.INSTANCE);
    when(aeron.context()).thenReturn(aeronContext);
    final IllegalMonitorStateException aeronException = new IllegalMonitorStateException("aeron closed");
    doThrow(aeronException).when(aeron).close();
    final Publication publication = mock(Publication.class);
    when(publication.isConnected()).thenReturn(true);
    doThrow(new IllegalStateException("publication is closed")).when(publication).close();
    final Subscription subscription = mock(Subscription.class);
    when(controlResponsePoller.subscription()).thenReturn(subscription);
    doThrow(new IndexOutOfBoundsException("subscription")).when(subscription).close();
    when(archiveProxy.publication()).thenReturn(publication);
    final IndexOutOfBoundsException closeSessionException = new IndexOutOfBoundsException();
    when(archiveProxy.closeSession(controlSessionId)).thenThrow(closeSessionException);
    final Context context = new Context().aeron(aeron).idleStrategy(NoOpIdleStrategy.INSTANCE).messageTimeoutNs(100).lock(NoOpLock.INSTANCE).errorHandler(errorHandler).ownsAeronClient(true);
    final AeronArchive aeronArchive = new AeronArchive(context, controlResponsePoller, archiveProxy, controlSessionId);
    final IllegalMonitorStateException ex = assertThrows(IllegalMonitorStateException.class, aeronArchive::close);
    assertSame(aeronException, ex);
    final InOrder inOrder = inOrder(errorHandler);
    inOrder.verify(errorHandler).onError(closeSessionException);
    inOrder.verifyNoMoreInteractions();
}
Also used : Context(io.aeron.archive.client.AeronArchive.Context) InOrder(org.mockito.InOrder) Publication(io.aeron.Publication) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron) Test(org.junit.jupiter.api.Test)

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