Java Code Examples for io.aeron.Aeron#connect()

The following examples show how to use io.aeron.Aeron#connect() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileSender.java    From aeron with Apache License 2.0 6 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    if (args.length != 1)
    {
        System.out.println("Filename to be sent must be supplied as a command line argument");
        System.exit(1);
    }

    try (Aeron aeron = Aeron.connect();
        Publication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID))
    {
        while (!publication.isConnected())
        {
            Thread.sleep(1);
        }

        final File file = new File(args[0]);
        final UnsafeBuffer buffer = new UnsafeBuffer(IoUtil.mapExistingFile(file, "sending"));
        final long correlationId = aeron.nextCorrelationId();

        sendFileCreate(publication, correlationId, buffer.capacity(), file.getName());
        streamChunks(publication, correlationId, buffer);
    }
}
 
Example 2
Source File: EmbeddedRecordingThroughput.java    From aeron with Apache License 2.0 6 votes vote down vote up
public EmbeddedRecordingThroughput()
{
    final String archiveDirName = Archive.Configuration.archiveDirName();
    final File archiveDir = ARCHIVE_DIR_DEFAULT.equals(archiveDirName) ?
        Samples.createTempDir() : new File(archiveDirName);

    archivingMediaDriver = ArchivingMediaDriver.launch(
        new MediaDriver.Context()
            .spiesSimulateConnection(true)
            .dirDeleteOnStart(true),
        new Archive.Context()
            .recordingEventsEnabled(false)
            .archiveDir(archiveDir));

    aeron = Aeron.connect();

    aeronArchive = AeronArchive.connect(
        new AeronArchive.Context()
            .aeron(aeron));
}
 
Example 3
Source File: FixMessageLogger.java    From artio with Apache License 2.0 6 votes vote down vote up
public FixMessageLogger(
    final Consumer<String> fixMessageConsumer,
    final Aeron.Context context,
    final String libraryAeronChannel,
    final int inboundStreamId,
    final int outboundStreamId,
    final int outboundReplayStreamId)
{
    aeron = Aeron.connect(context);
    inboundSubscription = aeron.addSubscription(libraryAeronChannel, inboundStreamId);
    outboundSubscription = aeron.addSubscription(libraryAeronChannel, outboundStreamId);
    replaySubscription = aeron.addSubscription(libraryAeronChannel, outboundReplayStreamId);

    final LogEntryHandler logEntryHandler = new LogEntryHandler((message, buffer, offset, length, header) ->
        fixMessageConsumer.accept(message.body()));
    fragmentAssembler = new FragmentAssembler(logEntryHandler);
}
 
Example 4
Source File: EmbeddedPingPong.java    From aeron with Apache License 2.0 6 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    loadPropertiesFiles(args);

    final MediaDriver.Context ctx = new MediaDriver.Context()
        .threadingMode(ThreadingMode.DEDICATED)
        .conductorIdleStrategy(new BackoffIdleStrategy(1, 1, 1000, 1000))
        .receiverIdleStrategy(NoOpIdleStrategy.INSTANCE)
        .senderIdleStrategy(NoOpIdleStrategy.INSTANCE);

    try (MediaDriver ignored = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect())
    {
        final Thread pongThread = startPong(aeron);
        pongThread.start();

        runPing(aeron);
        RUNNING.set(false);
        pongThread.join();

        System.out.println("Shutdown Driver...");
    }
}
 
Example 5
Source File: ParameterServerNodeTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void before() throws Exception {
    mediaDriver = MediaDriver.launchEmbedded(AeronUtil.getMediaDriverContext(parameterLength));
    System.setProperty("play.server.dir", "/tmp");
    aeron = Aeron.connect(getContext());
    parameterServerNode = new ParameterServerNode(mediaDriver, statusPort);
    parameterServerNode.runMain(new String[] {"-m", "true", "-s", "1," + String.valueOf(parameterLength), "-p",
                    String.valueOf(masterStatusPort), "-h", "localhost", "-id", "11", "-md",
                    mediaDriver.aeronDirectoryName(), "-sp", String.valueOf(statusPort), "-sh", "localhost", "-u",
                    String.valueOf(Runtime.getRuntime().availableProcessors())});

    while (!parameterServerNode.subscriberLaunched()) {
        Thread.sleep(10000);
    }

}
 
Example 6
Source File: ClusterTool.java    From aeron with Apache License 2.0 6 votes vote down vote up
public static boolean nextBackupQueryDeadlineMs(final ClusterMarkFile markFile, final long timeMs)
{
    final String aeronDirectoryName = markFile.decoder().aeronDirectory();
    final MutableBoolean result = new MutableBoolean(false);

    try (Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(aeronDirectoryName)))
    {
        final CountersReader countersReader = aeron.countersReader();

        countersReader.forEach(
            (counterId, typeId, keyBuffer, label) ->
            {
                if (ClusterBackup.QUERY_DEADLINE_TYPE_ID == typeId)
                {
                    final AtomicCounter atomicCounter = new AtomicCounter(
                        countersReader.valuesBuffer(), counterId, null);

                    atomicCounter.setOrdered(timeMs);
                    result.value = true;
                }
            });
    }

    return result.value;
}
 
Example 7
Source File: SetControllableIdleStrategy.java    From aeron with Apache License 2.0 6 votes vote down vote up
public static void main(final String[] args)
{
    if (args.length != 1)
    {
        System.out.format("Usage: SetControllableIdleStrategy <n>");
        System.exit(0);
    }

    try (Aeron aeron = Aeron.connect())
    {
        final CountersReader countersReader = aeron.countersReader();
        final StatusIndicator statusIndicator = StatusUtil.controllableIdleStrategy(countersReader);

        if (null != statusIndicator)
        {
            final int status = Integer.parseInt(args[0]);
            statusIndicator.setOrdered(status);
            System.out.println("Set ControllableIdleStrategy status to " + status);
        }
        else
        {
            System.out.println("Could not find ControllableIdleStrategy status.");
        }
    }
}
 
Example 8
Source File: ClusterTool.java    From aeron with Apache License 2.0 6 votes vote down vote up
public static boolean removeMember(final ClusterMarkFile markFile, final int memberId, final boolean isPassive)
{
    final String aeronDirectoryName = markFile.decoder().aeronDirectory();
    final String controlChannel = markFile.decoder().controlChannel();
    final int consensusModuleStreamId = markFile.decoder().consensusModuleStreamId();

    try (Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(aeronDirectoryName));
        ConsensusModuleProxy consensusModuleProxy = new ConsensusModuleProxy(
            aeron.addPublication(controlChannel, consensusModuleStreamId)))
    {
        if (consensusModuleProxy.removeMember(memberId, isPassive ? BooleanType.TRUE : BooleanType.FALSE))
        {
            return true;
        }
    }

    return false;
}
 
Example 9
Source File: AeronUdpTransport.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public AeronUdpTransport(@NonNull String ownIp, int ownPort, @NonNull String rootIp, int rootPort, @NonNull VoidConfiguration configuration) {
    super("aeron:udp?endpoint=" + ownIp + ":" + ownPort, "aeron:udp?endpoint=" + rootIp + ":" + rootPort, configuration);

    Preconditions.checkArgument(ownPort > 0 && ownPort < 65536, "Own UDP port should be positive value in range of 1 and 65536");
    Preconditions.checkArgument(rootPort > 0 && rootPort < 65536, "Master node UDP port should be positive value in range of 1 and 65536");

    setProperty("aeron.client.liveness.timeout", "30000000000");

    // setting this property to try to increase maxmessage length, not sure if it still works though
    //Term buffer length: must be power of 2 and in range 64kB to 1GB: https://github.com/real-logic/aeron/wiki/Configuration-Options
    String p = System.getProperty(ND4JSystemProperties.AERON_TERM_BUFFER_PROP);
    if(p == null){
        System.setProperty(ND4JSystemProperties.AERON_TERM_BUFFER_PROP, String.valueOf(DEFAULT_TERM_BUFFER_PROP));
    }

    splitter = MessageSplitter.getInstance();

    context = new Aeron.Context().driverTimeoutMs(30000)
            .keepAliveInterval(100000000);
    AeronUtil.setDaemonizedThreadFactories(context);

    final MediaDriver.Context mediaDriverCtx = new MediaDriver.Context();
    AeronUtil.setDaemonizedThreadFactories(mediaDriverCtx);

    driver = MediaDriver.launchEmbedded(mediaDriverCtx);
    context.aeronDirectoryName(driver.aeronDirectoryName());
    aeron = Aeron.connect(context);

    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        this.shutdown();
    }));
}
 
Example 10
Source File: BenchServer.java    From rpc-bench with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("PMD.NullAssignment")
public BenchServer() {
  running = new AtomicBoolean(true);
  SigInt.register(() -> running.set(false));
  driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
  ctx = new Aeron.Context();
  if (EMBEDDED_MEDIA_DRIVER) {
    ctx.aeronDirectoryName(driver.aeronDirectoryName());
  }
  fragmentHandler = new FragmentAssembler(this::onMessage);
  aeron = Aeron.connect(ctx);
  publication = aeron.addPublication(REP_CHAN, REP_STREAM_ID);
  subscription = aeron.addSubscription(REQ_CHAN, REQ_STREAM_ID);
}
 
Example 11
Source File: EmbeddedThroughput.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    loadPropertiesFiles(args);

    final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), EmbeddedThroughput::printRate);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final AtomicBoolean running = new AtomicBoolean(true);

    try (MediaDriver ignore = MediaDriver.launch();
        Aeron aeron = Aeron.connect();
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID))
    {
        executor.execute(reporter);
        executor.execute(() -> SamplesUtil.subscriberLoop(
            rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));

        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();

        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++)
            {
                OFFER_BUFFER.putLong(0, i);

                idleStrategy.reset();
                while (publication.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null) < 0)
                {
                    backPressureCount++;
                    idleStrategy.idle();
                }
            }

            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();
    }
}
 
Example 12
Source File: ParameterServerClientPartialTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
    final MediaDriver.Context ctx =
                    new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirsDeleteOnStart(true)
                                    .termBufferSparseFile(false).conductorIdleStrategy(new BusySpinIdleStrategy())
                                    .receiverIdleStrategy(new BusySpinIdleStrategy())
                                    .senderIdleStrategy(new BusySpinIdleStrategy());

    mediaDriver = MediaDriver.launchEmbedded(ctx);
    aeron = Aeron.connect(getContext());
    masterNode = new ParameterServerSubscriber(mediaDriver);
    masterNode.setAeron(aeron);
    int masterPort = 40223 + new java.util.Random().nextInt(13000);
    int masterStatusPort = masterPort - 2000;
    masterNode.run(new String[] {"-m", "true", "-p", String.valueOf(masterPort), "-h", "localhost", "-id", "11",
                    "-md", mediaDriver.aeronDirectoryName(), "-sp", String.valueOf(masterStatusPort), "-s", "2,2",
                    "-u", String.valueOf(1)

    });

    assertTrue(masterNode.isMaster());
    assertEquals(masterPort, masterNode.getPort());
    assertEquals("localhost", masterNode.getHost());
    assertEquals(11, masterNode.getStreamId());
    assertEquals(12, masterNode.getResponder().getStreamId());
    assertEquals(masterNode.getMasterArray(), Nd4j.create(new int[] {2, 2}));

    slaveNode = new ParameterServerSubscriber(mediaDriver);
    slaveNode.setAeron(aeron);
    int slavePort = masterPort + 100;
    int slaveStatusPort = slavePort - 2000;
    slaveNode.run(new String[] {"-p", String.valueOf(slavePort), "-h", "localhost", "-id", "10", "-pm",
                    masterNode.getSubscriber().connectionUrl(), "-md", mediaDriver.aeronDirectoryName(), "-sp",
                    String.valueOf(slaveStatusPort), "-u", String.valueOf(1)

    });

    assertFalse(slaveNode.isMaster());
    assertEquals(slavePort, slaveNode.getPort());
    assertEquals("localhost", slaveNode.getHost());
    assertEquals(10, slaveNode.getStreamId());

    int tries = 10;
    while (!masterNode.subscriberLaunched() && !slaveNode.subscriberLaunched() && tries < 10) {
        Thread.sleep(10000);
        tries++;
    }

    if (!masterNode.subscriberLaunched() && !slaveNode.subscriberLaunched()) {
        throw new IllegalStateException("Failed to start master and slave node");
    }

    log.info("Using media driver directory " + mediaDriver.aeronDirectoryName());
    log.info("Launched media driver");
}
 
Example 13
Source File: DriverLoggingAgentTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
private void testLogMediaDriverEvents(
    final String enabledEvents, final EnumSet<DriverEventCode> expectedEvents) throws InterruptedException
{
    before(enabledEvents, expectedEvents);

    final String aeronDirectoryName = testDir.toPath().resolve("media").toString();

    final MediaDriver.Context driverCtx = new MediaDriver.Context()
        .errorHandler(Tests::onError)
        .publicationLingerTimeoutNs(0)
        .timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(1))
        .aeronDirectoryName(aeronDirectoryName);

    try (MediaDriver ignore = MediaDriver.launchEmbedded(driverCtx))
    {
        final Aeron.Context clientCtx = new Aeron.Context()
            .aeronDirectoryName(driverCtx.aeronDirectoryName());

        try (Aeron aeron = Aeron.connect(clientCtx);
            Subscription subscription = aeron.addSubscription(NETWORK_CHANNEL, STREAM_ID);
            Publication publication = aeron.addPublication(NETWORK_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);
        }

        latch.await();
    }

    assertEquals(expectedEvents.stream().map(DriverEventCode::id).collect(toSet()), LOGGED_EVENTS);
}
 
Example 14
Source File: StreamingPublisher.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    if (MESSAGE_LENGTH < SIZE_OF_LONG)
    {
        throw new IllegalArgumentException("Message length must be at least " + SIZE_OF_LONG + " bytes");
    }

    final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
    final Aeron.Context context = new Aeron.Context();

    if (EMBEDDED_MEDIA_DRIVER)
    {
        context.aeronDirectoryName(driver.aeronDirectoryName());
    }

    final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), StreamingPublisher::printRate);
    final ExecutorService executor = Executors.newFixedThreadPool(1);

    executor.execute(reporter);

    // Connect to media driver and add publication to send messages on the configured channel and stream ID.
    // The Aeron and Publication classes implement AutoCloseable, and will automatically
    // clean up resources when this try block is finished.
    try (Aeron aeron = Aeron.connect(context);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID))
    {
        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();

        do
        {
            printingActive = true;

            System.out.format(
                "%nStreaming %,d messages of%s size %d bytes to %s on stream id %d%n",
                NUMBER_OF_MESSAGES,
                RANDOM_MESSAGE_LENGTH ? " random" : "",
                MESSAGE_LENGTH,
                CHANNEL,
                STREAM_ID);

            long backPressureCount = 0;

            for (long i = 0; i < NUMBER_OF_MESSAGES; i++)
            {
                final int length = LENGTH_GENERATOR.getAsInt();

                OFFER_BUFFER.putLong(0, i);
                idleStrategy.reset();
                while (publication.offer(OFFER_BUFFER, 0, length, null) < 0L)
                {
                    // The offer failed, which is usually due to the publication
                    // being temporarily blocked.  Retry the offer after a short
                    // spin/yield/sleep, depending on the chosen IdleStrategy.
                    backPressureCount++;
                    idleStrategy.idle();
                }

                reporter.onMessage(length);
            }

            System.out.println(
                "Done streaming. Back pressure ratio " + ((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());
    }

    reporter.halt();
    executor.shutdown();
    CloseHelper.quietClose(driver);
}
 
Example 15
Source File: EmbeddedExclusiveSpiedThroughput.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    loadPropertiesFiles(args);

    final RateReporter reporter = new RateReporter(
        TimeUnit.SECONDS.toNanos(1), EmbeddedExclusiveSpiedThroughput::printRate);
    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();
        Subscription subscription = aeron.addSubscription(CommonContext.SPY_PREFIX + CHANNEL, STREAM_ID);
        ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID))
    {
        executor.execute(reporter);
        executor.execute(() -> SamplesUtil.subscriberLoop(
            rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));

        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();

        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++)
            {
                OFFER_BUFFER.putLong(0, i);

                idleStrategy.reset();
                while (publication.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null) < 0)
                {
                    backPressureCount++;
                    idleStrategy.idle();
                }
            }

            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();
    }
}
 
Example 16
Source File: MultipleSubscribersWithFragmentAssembly.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args)
{
    System.out.format("Subscribing to %s on stream ID %d and stream ID %d%n",
        CHANNEL, STREAM_ID_1, STREAM_ID_2);

    final Aeron.Context ctx = new Aeron.Context()
        .availableImageHandler(MultipleSubscribersWithFragmentAssembly::eventAvailableImage)
        .unavailableImageHandler(MultipleSubscribersWithFragmentAssembly::eventUnavailableImage);

    final FragmentAssembler dataHandler1 = new FragmentAssembler(reassembledStringMessage1(STREAM_ID_1));
    final FragmentAssembler dataHandler2 = new FragmentAssembler(reassembledStringMessage2(STREAM_ID_2));

    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));

    try (Aeron aeron = Aeron.connect(ctx);
        Subscription subscription1 = aeron.addSubscription(CHANNEL, STREAM_ID_1);
        Subscription subscription2 = aeron.addSubscription(CHANNEL, STREAM_ID_2))
    {
        final IdleStrategy idleStrategy = new BackoffIdleStrategy(
            100, 10, TimeUnit.MICROSECONDS.toNanos(1), TimeUnit.MICROSECONDS.toNanos(100));

        int idleCount = 0;

        while (running.get())
        {
            final int fragmentsRead1 = subscription1.poll(dataHandler1, FRAGMENT_COUNT_LIMIT);
            final int fragmentsRead2 = subscription2.poll(dataHandler2, FRAGMENT_COUNT_LIMIT);

            if ((fragmentsRead1 + fragmentsRead2) == 0)
            {
                idleStrategy.idle(idleCount++);
            }
            else
            {
                idleCount = 0;
            }
        }

        System.out.println("Shutting down...");
    }
}
 
Example 17
Source File: ParameterServerClientTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
    mediaDriver = MediaDriver.launchEmbedded(AeronUtil.getMediaDriverContext(parameterLength));
    System.setProperty("play.server.dir", "/tmp");
    aeron = Aeron.connect(getContext());
    masterNode = new ParameterServerSubscriber(mediaDriver);
    masterNode.setAeron(aeron);
    int masterPort = 40323 + new java.util.Random().nextInt(3000);
    masterNode.run(new String[] {"-m", "true", "-s", "1," + String.valueOf(parameterLength), "-p",
                    String.valueOf(masterPort), "-h", "localhost", "-id", "11", "-md",
                    mediaDriver.aeronDirectoryName(), "-sp", "33000", "-u", String.valueOf(1)});

    assertTrue(masterNode.isMaster());
    assertEquals(masterPort, masterNode.getPort());
    assertEquals("localhost", masterNode.getHost());
    assertEquals(11, masterNode.getStreamId());
    assertEquals(12, masterNode.getResponder().getStreamId());

    slaveNode = new ParameterServerSubscriber(mediaDriver);
    slaveNode.setAeron(aeron);
    slaveNode.run(new String[] {"-p", String.valueOf(masterPort + 100), "-h", "localhost", "-id", "10", "-pm",
                    masterNode.getSubscriber().connectionUrl(), "-md", mediaDriver.aeronDirectoryName(), "-sp",
                    "31000", "-u", String.valueOf(1)});

    assertFalse(slaveNode.isMaster());
    assertEquals(masterPort + 100, slaveNode.getPort());
    assertEquals("localhost", slaveNode.getHost());
    assertEquals(10, slaveNode.getStreamId());

    int tries = 10;
    while (!masterNode.subscriberLaunched() && !slaveNode.subscriberLaunched() && tries < 10) {
        Thread.sleep(10000);
        tries++;
    }

    if (!masterNode.subscriberLaunched() && !slaveNode.subscriberLaunched()) {
        throw new IllegalStateException("Failed to start master and slave node");
    }

    log.info("Using media driver directory " + mediaDriver.aeronDirectoryName());
    log.info("Launched media driver");
}
 
Example 18
Source File: MultiplePublishersWithFragmentation.java    From aeron with Apache License 2.0 4 votes vote down vote up
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 < 0L)
                    {
                        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 unknown reason");
                        }

                        offerStatus1 = false;
                    }
                    else
                    {
                        j++;
                        offerStatus1 = true;
                        System.out.println("Successfully sent data on stream id " +
                            STREAM_ID_1 + " and data length " + BUFFER_1.capacity() + " at offset " + result1);
                    }
                }

                if (k <= 5000)
                {
                    result2 = publication2.offer(BUFFER_2, 0, BUFFER_2.capacity());
                    if (result2 < 0L)
                    {
                        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 unknown reason");
                        }
                        offerStatus2 = false;
                    }
                    else
                    {
                        k++;
                        offerStatus2 = true;
                        System.out.println("Successfully sent data on stream id " + STREAM_ID_2 +
                            " and data length " + BUFFER_2.capacity() + " at offset " + result2);
                    }
                }
            }
        }

        System.out.println("Done sending total messages for stream id " +
            STREAM_ID_1 + " = " + (j - 1) + " and stream id " + STREAM_ID_2 + " = " + (k - 1));
    }
}
 
Example 19
Source File: SimpleSubscriber.java    From aeron with Apache License 2.0 4 votes vote down vote up
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.println(String.format(
                "Received message (%s) to stream %d from session %x term id %x term offset %d (%d@%d)",
                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();

    // Create an Aeron instance with client-provided context configuration, connect to the
    // media driver, and add a subscription for the given channel and stream using the supplied
    // dataHandler method, which will be called with new messages as they are received.
    // The Aeron and Subscription classes implement AutoCloseable, and will automatically
    // 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...");
    }
}
 
Example 20
Source File: ParameterServerClientPartialTest.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void before() throws Exception {
    final MediaDriver.Context ctx =
                    new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirsDeleteOnStart(true)
                                    .termBufferSparseFile(false).conductorIdleStrategy(new BusySpinIdleStrategy())
                                    .receiverIdleStrategy(new BusySpinIdleStrategy())
                                    .senderIdleStrategy(new BusySpinIdleStrategy());

    mediaDriver = MediaDriver.launchEmbedded(ctx);
    aeron = Aeron.connect(getContext());
    masterNode = new ParameterServerSubscriber(mediaDriver);
    masterNode.setAeron(aeron);
    int masterPort = 40223 + new java.util.Random().nextInt(13000);
    int masterStatusPort = masterPort - 2000;
    masterNode.run(new String[] {"-m", "true", "-p", String.valueOf(masterPort), "-h", "localhost", "-id", "11",
                    "-md", mediaDriver.aeronDirectoryName(), "-sp", String.valueOf(masterStatusPort), "-s", "2,2",
                    "-u", String.valueOf(1)

    });

    assertTrue(masterNode.isMaster());
    assertEquals(masterPort, masterNode.getPort());
    assertEquals("localhost", masterNode.getHost());
    assertEquals(11, masterNode.getStreamId());
    assertEquals(12, masterNode.getResponder().getStreamId());
    assertEquals(masterNode.getMasterArray(), Nd4j.create(new int[] {2, 2}));

    slaveNode = new ParameterServerSubscriber(mediaDriver);
    slaveNode.setAeron(aeron);
    int slavePort = masterPort + 100;
    int slaveStatusPort = slavePort - 2000;
    slaveNode.run(new String[] {"-p", String.valueOf(slavePort), "-h", "localhost", "-id", "10", "-pm",
                    masterNode.getSubscriber().connectionUrl(), "-md", mediaDriver.aeronDirectoryName(), "-sp",
                    String.valueOf(slaveStatusPort), "-u", String.valueOf(1)

    });

    assertFalse(slaveNode.isMaster());
    assertEquals(slavePort, slaveNode.getPort());
    assertEquals("localhost", slaveNode.getHost());
    assertEquals(10, slaveNode.getStreamId());

    int tries = 10;
    while (!masterNode.subscriberLaunched() && !slaveNode.subscriberLaunched() && tries < 10) {
        Thread.sleep(10000);
        tries++;
    }

    if (!masterNode.subscriberLaunched() && !slaveNode.subscriberLaunched()) {
        throw new IllegalStateException("Failed to start master and slave node");
    }

    log.info("Using media driver directory " + mediaDriver.aeronDirectoryName());
    log.info("Launched media driver");
}