Search in sources :

Example 6 with MediaDriver

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

the class EmbeddedExclusiveSpiedThroughput method main.

public static void main(final String[] args) throws Exception {
    loadPropertiesFiles(args);
    final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), EmbeddedExclusiveSpiedThroughput::printRate);
    final FragmentHandler rateReporterHandler = rateReporterHandler(reporter);
    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();
        ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID);
        Subscription subscription = aeron.addSubscription(CommonContext.SPY_PREFIX + CHANNEL, STREAM_ID)) {
        executor.execute(reporter);
        executor.execute(() -> SamplesUtil.subscriberLoop(rateReporterHandler, FRAGMENT_COUNT_LIMIT, running).accept(subscription));
        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        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++) {
                ATOMIC_BUFFER.putLong(0, i);
                OFFER_IDLE_STRATEGY.reset();
                while (publication.offer(ATOMIC_BUFFER, 0, ATOMIC_BUFFER.capacity()) < 0) {
                    OFFER_IDLE_STRATEGY.idle();
                    backPressureCount++;
                }
            }
            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();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaDriver(io.aeron.driver.MediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) ExecutorService(java.util.concurrent.ExecutorService) ContinueBarrier(org.agrona.console.ContinueBarrier) ExclusivePublication(io.aeron.ExclusivePublication) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron)

Example 7 with MediaDriver

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

the class EmbeddedExclusiveVectoredIpcThroughput method main.

public static void main(final String[] args) throws Exception {
    loadPropertiesFiles(args);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    final MediaDriver.Context ctx = new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).sharedIdleStrategy(new NoOpIdleStrategy());
    try (MediaDriver ignore = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect();
        ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID);
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
        final Subscriber subscriber = new Subscriber(running, subscription);
        final Thread subscriberThread = new Thread(subscriber);
        subscriberThread.setName("subscriber");
        final Thread publisherThread = new Thread(new Publisher(running, publication));
        publisherThread.setName("publisher");
        final Thread rateReporterThread = new Thread(new RateReporter(running, subscriber));
        rateReporterThread.setName("rate-reporter");
        rateReporterThread.start();
        subscriberThread.start();
        publisherThread.start();
        subscriberThread.join();
        publisherThread.join();
        rateReporterThread.join();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaDriver(io.aeron.driver.MediaDriver)

Example 8 with MediaDriver

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

the class ClusteredMediaDriver method launch.

/**
 * Launch a new {@link ClusteredMediaDriver} with provided contexts.
 *
 * @param driverCtx          for configuring the {@link MediaDriver}.
 * @param archiveCtx         for configuring the {@link Archive}.
 * @param consensusModuleCtx for the configuration of the {@link ConsensusModule}.
 * @return a new {@link ClusteredMediaDriver} with the provided contexts.
 */
public static ClusteredMediaDriver launch(final MediaDriver.Context driverCtx, final Archive.Context archiveCtx, final ConsensusModule.Context consensusModuleCtx) {
    final MediaDriver driver = MediaDriver.launch(driverCtx.spiesSimulateConnection(true));
    final Archive archive = Archive.launch(archiveCtx.mediaDriverAgentInvoker(driver.sharedAgentInvoker()).errorHandler(driverCtx.errorHandler()).errorCounter(driverCtx.systemCounters().get(SystemCounterDescriptor.ERRORS)));
    final ConsensusModule consensusModule = ConsensusModule.launch(consensusModuleCtx);
    return new ClusteredMediaDriver(driver, archive, consensusModule);
}
Also used : Archive(io.aeron.archive.Archive) MediaDriver(io.aeron.driver.MediaDriver)

Example 9 with MediaDriver

use of io.aeron.driver.MediaDriver in project nd4j by deeplearning4j.

the class ParameterServerClientPartialTest method before.

@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");
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) ParameterServerSubscriber(org.nd4j.parameterserver.ParameterServerSubscriber) BusySpinIdleStrategy(org.agrona.concurrent.BusySpinIdleStrategy) BeforeClass(org.junit.BeforeClass)

Example 10 with MediaDriver

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

the class ArchivingMediaDriver method launch.

/**
 * Launch a new {@link ArchivingMediaDriver} with provided contexts.
 *
 * @param driverCtx  for configuring the {@link MediaDriver}.
 * @param archiveCtx for configuring the {@link Archive}.
 * @return a new {@link ArchivingMediaDriver} with the provided contexts.
 */
public static ArchivingMediaDriver launch(final MediaDriver.Context driverCtx, final Archive.Context archiveCtx) {
    MediaDriver driver = null;
    Archive archive = null;
    try {
        driver = MediaDriver.launch(driverCtx);
        final int errorCounterId = SystemCounterDescriptor.ERRORS.id();
        final AtomicCounter errorCounter = null != archiveCtx.errorCounter() ? archiveCtx.errorCounter() : new AtomicCounter(driverCtx.countersValuesBuffer(), errorCounterId);
        final ErrorHandler errorHandler = null != archiveCtx.errorHandler() ? archiveCtx.errorHandler() : driverCtx.errorHandler();
        archive = Archive.launch(archiveCtx.mediaDriverAgentInvoker(driver.sharedAgentInvoker()).aeronDirectoryName(driverCtx.aeronDirectoryName()).errorHandler(errorHandler).errorCounter(errorCounter));
        return new ArchivingMediaDriver(driver, archive);
    } catch (final Exception ex) {
        CloseHelper.quietCloseAll(archive, driver);
        throw ex;
    }
}
Also used : ErrorHandler(org.agrona.ErrorHandler) MediaDriver(io.aeron.driver.MediaDriver) AtomicCounter(org.agrona.concurrent.status.AtomicCounter)

Aggregations

MediaDriver (io.aeron.driver.MediaDriver)59 Aeron (io.aeron.Aeron)22 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)22 Subscription (io.aeron.Subscription)13 FragmentHandler (io.aeron.logbuffer.FragmentHandler)11 ContinueBarrier (org.agrona.console.ContinueBarrier)11 Test (org.junit.jupiter.api.Test)10 Publication (io.aeron.Publication)9 IdleStrategy (org.agrona.concurrent.IdleStrategy)9 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)9 InterruptAfter (io.aeron.test.InterruptAfter)8 ExecutorService (java.util.concurrent.ExecutorService)8 BusySpinIdleStrategy (org.agrona.concurrent.BusySpinIdleStrategy)8 TestMediaDriver (io.aeron.test.driver.TestMediaDriver)7 Archive (io.aeron.archive.Archive)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 Tests (io.aeron.test.Tests)5 DirectBuffer (org.agrona.DirectBuffer)5 CommonContext (io.aeron.CommonContext)4 File (java.io.File)4