Search in sources :

Example 51 with MediaDriver

use of io.aeron.driver.MediaDriver 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 52 with MediaDriver

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

the class FileReceiver method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
public static void main(final String[] args) {
    final File storageDir;
    if (args.length > 1) {
        storageDir = new File(args[0]);
        if (!storageDir.isDirectory()) {
            System.out.println(args[0] + " is not a directory");
            System.exit(1);
        }
    } else {
        storageDir = new File(SystemUtil.tmpDirName());
    }
    System.out.println("Files stored to " + storageDir.getAbsolutePath());
    final IdleStrategy idleStrategy = new SleepingMillisIdleStrategy(1);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    try (MediaDriver mediaDriver = MediaDriver.launch();
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
        System.out.println("Receiving from " + CHANNEL + " on stream id " + STREAM_ID);
        final FileReceiver fileReceiver = new FileReceiver(storageDir, subscription);
        while (running.get()) {
            idleStrategy.idle(fileReceiver.doWork());
        }
    }
}
Also used : SleepingMillisIdleStrategy(org.agrona.concurrent.SleepingMillisIdleStrategy) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SleepingMillisIdleStrategy(org.agrona.concurrent.SleepingMillisIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) MediaDriver(io.aeron.driver.MediaDriver) Subscription(io.aeron.Subscription) File(java.io.File) Aeron(io.aeron.Aeron)

Example 53 with MediaDriver

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

the class RegistrationAndOwnerTest method shouldHaveCorrectOwnershipOnEntities.

@ParameterizedTest
@ValueSource(strings = { "aeron:udp?endpoint=localhost:24325", "aeron:ipc" })
void shouldHaveCorrectOwnershipOnEntities(final String channel) {
    final MediaDriver.Context ctx = new MediaDriver.Context().errorHandler(Tests::onError).dirDeleteOnStart(true);
    try (TestMediaDriver mediaDriver = TestMediaDriver.launch(ctx, testWatcher);
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
        Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
        Publication publication = aeron.addPublication(channel, STREAM_ID);
        Counter userCounter = aeron.addCounter(1002, "Test Counter")) {
        awaitConnected(subscription);
        awaitConnected(publication);
        final CountersReader countersReader = aeron.countersReader();
        final int subscriberPositionId = subscription.imageAtIndex(0).subscriberPositionId();
        assertEquals(aeron.clientId(), countersReader.getCounterOwnerId(subscriberPositionId));
        assertEquals(aeron.clientId(), countersReader.getCounterOwnerId(publication.positionLimitId()));
        assertEquals(aeron.clientId(), countersReader.getCounterOwnerId(userCounter.id()));
        assertEquals(subscription.registrationId(), countersReader.getCounterRegistrationId(subscriberPositionId));
        assertEquals(publication.registrationId(), countersReader.getCounterRegistrationId(publication.positionLimitId()));
        assertEquals(userCounter.registrationId(), countersReader.getCounterRegistrationId(userCounter.id()));
    } finally {
        ctx.deleteDirectory();
    }
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) CountersReader(org.agrona.concurrent.status.CountersReader) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 54 with MediaDriver

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

the class SpecifiedPositionPublicationTest method shouldStartAtSpecifiedPositionForPublications.

@InterruptAfter(5)
@ParameterizedTest
@CsvSource({ CommonContext.IPC_CHANNEL + ",true", "aeron:udp?endpoint=localhost:24325,true", CommonContext.IPC_CHANNEL + ",false", "aeron:udp?endpoint=localhost:24325,false" })
void shouldStartAtSpecifiedPositionForPublications(final String initialUri, final boolean exclusive) {
    final MediaDriver.Context context = new MediaDriver.Context().dirDeleteOnStart(true).ipcPublicationTermWindowLength(LogBufferDescriptor.TERM_MIN_LENGTH).threadingMode(ThreadingMode.SHARED);
    final DirectBuffer msg = new UnsafeBuffer(new byte[64]);
    final int termLength = 1 << 16;
    final int initialTermId = randomWatcher.random().nextInt();
    final int activeTermId = initialTermId + randomWatcher.random().nextInt(Integer.MAX_VALUE);
    final int positionBitsToShift = LogBufferDescriptor.positionBitsToShift(termLength);
    final int termOffset = randomWatcher.random().nextInt(termLength) & -FrameDescriptor.FRAME_ALIGNMENT;
    final long startPosition = LogBufferDescriptor.computePosition(activeTermId, termOffset, positionBitsToShift, initialTermId);
    final PositionCalculator positionCalculator = new PositionCalculator(startPosition, termLength, termOffset);
    final long nextPosition = positionCalculator.addMessage(DataHeaderFlyweight.HEADER_LENGTH + msg.capacity());
    final String channel = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition, initialTermId, termLength).build();
    final int streamId = 1001;
    final Function<Aeron, Publication> publicationSupplier = exclusive ? (a) -> a.addExclusivePublication(channel, streamId) : (a) -> a.addPublication(channel, streamId);
    try (TestMediaDriver mediaDriver = TestMediaDriver.launch(context, testWatcher);
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
        Subscription subscription = aeron.addSubscription(initialUri, streamId);
        Publication publication = publicationSupplier.apply(aeron)) {
        Tests.awaitConnected(subscription);
        Tests.awaitConnected(publication);
        assertEquals(startPosition, publication.position());
        Tests.await(() -> publication.offer(msg) > 0);
        assertEquals(nextPosition, publication.position());
        final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> assertEquals(nextPosition, header.position());
        Tests.await(() -> subscription.poll(fragmentHandler, 1) == 1);
    } finally {
        context.deleteDirectory();
    }
}
Also used : FrameDescriptor(io.aeron.logbuffer.FrameDescriptor) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MediaDriver(io.aeron.driver.MediaDriver) SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) CsvSource(org.junit.jupiter.params.provider.CsvSource) RegistrationException(io.aeron.exceptions.RegistrationException) RandomWatcher(io.aeron.test.RandomWatcher) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Function(java.util.function.Function) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) InterruptAfter(io.aeron.test.InterruptAfter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ThreadingMode(io.aeron.driver.ThreadingMode) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) DirectBuffer(org.agrona.DirectBuffer) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) DirectBuffer(org.agrona.DirectBuffer) MediaDriver(io.aeron.driver.MediaDriver) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) InterruptAfter(io.aeron.test.InterruptAfter) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 55 with MediaDriver

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

the class ClusterNetworkTopologyTest method connectAndSendMessages.

private void connectAndSendMessages(final String ingressChannel, final String ingressEndpoints, final Selector selector, final double messageCount) {
    final String message = "Hello World!";
    final MutableDirectBuffer messageBuffer = new UnsafeBuffer(ByteBuffer.allocate(128));
    final int length = messageBuffer.putStringAscii(0, message);
    final MutableReference<String> egressResponse = new MutableReference<>();
    final EgressListener egressListener = (clusterSessionId, timestamp, buffer, offset, length1, header) -> {
        final String stringAscii = buffer.getStringAscii(offset);
        egressResponse.set(stringAscii);
    };
    try (MediaDriver mediaDriver = MediaDriver.launchEmbedded(new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirDeleteOnStart(true).dirDeleteOnShutdown(true));
        AeronCluster.AsyncConnect asyncConnect = AeronCluster.asyncConnect(new AeronCluster.Context().messageTimeoutNs(TimeUnit.SECONDS.toNanos(CLUSTER_START_ELECTION_TIMEOUT_S * 2)).egressListener(egressListener).egressChannel("aeron:udp?endpoint=10.42.0.1:0").aeronDirectoryName(mediaDriver.aeronDirectoryName()).ingressChannel(ingressChannel).ingressEndpoints(ingressEndpoints));
        AeronCluster aeronCluster = pollUntilConnected(asyncConnect, selector)) {
        for (int i = 0; i < messageCount; i++) {
            Tests.await(() -> {
                final long position = aeronCluster.offer(messageBuffer, 0, length);
                pollSelector(selector);
                return 0 < position;
            }, SECONDS.toNanos(5));
            Tests.await(() -> {
                aeronCluster.pollEgress();
                pollSelector(selector);
                return message.equals(egressResponse.get());
            }, SECONDS.toNanos(5));
        }
    }
}
Also used : VirtualMachineDescriptor(com.sun.tools.attach.VirtualMachineDescriptor) AeronCluster(io.aeron.cluster.client.AeronCluster) BeforeEach(org.junit.jupiter.api.BeforeEach) java.util(java.util) io.aeron.test(io.aeron.test) Selector(java.nio.channels.Selector) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) IoUtil(org.agrona.IoUtil) FileResolveUtil(io.aeron.test.launcher.FileResolveUtil) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult) MutableReference(org.agrona.collections.MutableReference) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) SocketChannel(java.nio.channels.SocketChannel) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Duration(java.time.Duration) BasicAuctionClusterClient(io.aeron.samples.cluster.tutorial.BasicAuctionClusterClient) RemoteLaunchClient(io.aeron.test.launcher.RemoteLaunchClient) MethodSource(org.junit.jupiter.params.provider.MethodSource) EchoServiceNode(io.aeron.samples.cluster.EchoServiceNode) MediaDriver(io.aeron.driver.MediaDriver) ReadableByteChannel(java.nio.channels.ReadableByteChannel) VirtualMachine(com.sun.tools.attach.VirtualMachine) CharBuffer(java.nio.CharBuffer) SelectionKey(java.nio.channels.SelectionKey) IOException(java.io.IOException) CharsetDecoder(java.nio.charset.CharsetDecoder) CommonContext(io.aeron.CommonContext) Arguments(org.junit.jupiter.params.provider.Arguments) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) UncheckedIOException(java.io.UncheckedIOException) TimeUnit(java.util.concurrent.TimeUnit) EgressListener(io.aeron.cluster.client.EgressListener) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions(org.junit.jupiter.api.Assertions) ClusterConfig(io.aeron.samples.cluster.ClusterConfig) MutableDirectBuffer(org.agrona.MutableDirectBuffer) Pattern(java.util.regex.Pattern) SECONDS(java.util.concurrent.TimeUnit.SECONDS) JRE(org.junit.jupiter.api.condition.JRE) CommonContext(io.aeron.CommonContext) AeronCluster(io.aeron.cluster.client.AeronCluster) MutableReference(org.agrona.collections.MutableReference) MutableDirectBuffer(org.agrona.MutableDirectBuffer) EgressListener(io.aeron.cluster.client.EgressListener) MediaDriver(io.aeron.driver.MediaDriver) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Aggregations

MediaDriver (io.aeron.driver.MediaDriver)78 Aeron (io.aeron.Aeron)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)24 Test (org.junit.jupiter.api.Test)20 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)17 InterruptAfter (io.aeron.test.InterruptAfter)16 Subscription (io.aeron.Subscription)14 TestMediaDriver (io.aeron.test.driver.TestMediaDriver)14 IdleStrategy (org.agrona.concurrent.IdleStrategy)14 ContinueBarrier (org.agrona.console.ContinueBarrier)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 Publication (io.aeron.Publication)10 Archive (io.aeron.archive.Archive)10 FragmentHandler (io.aeron.logbuffer.FragmentHandler)10 Tests (io.aeron.test.Tests)10 CommonContext (io.aeron.CommonContext)8 ExecutorService (java.util.concurrent.ExecutorService)8 DirectBuffer (org.agrona.DirectBuffer)7 BusySpinIdleStrategy (org.agrona.concurrent.BusySpinIdleStrategy)7 AeronCluster (io.aeron.cluster.client.AeronCluster)6