Search in sources :

Example 61 with MediaDriver

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

the class BasicPublisher method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 * @throws InterruptedException if the thread sleep delay is interrupted.
 */
public static void main(final String[] args) throws InterruptedException {
    System.out.println("Publishing to " + CHANNEL + " on stream id " + STREAM_ID);
    // If configured to do so, create an embedded media driver within this application rather
    // than relying on an external one.
    final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
    final Aeron.Context ctx = new Aeron.Context();
    if (EMBEDDED_MEDIA_DRIVER) {
        ctx.aeronDirectoryName(driver.aeronDirectoryName());
    }
    // clean up resources when this try block is finished
    try (Aeron aeron = Aeron.connect(ctx);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
        final UnsafeBuffer buffer = new UnsafeBuffer(BufferUtil.allocateDirectAligned(256, 64));
        for (long i = 0; i < NUMBER_OF_MESSAGES; i++) {
            System.out.print("Offering " + i + "/" + NUMBER_OF_MESSAGES + " - ");
            final int length = buffer.putStringWithoutLengthAscii(0, "Hello World! " + i);
            final long result = publication.offer(buffer, 0, length);
            if (result > 0) {
                System.out.println("yay!");
            } else if (result == Publication.BACK_PRESSURED) {
                System.out.println("Offer failed due to back pressure");
            } else if (result == Publication.NOT_CONNECTED) {
                System.out.println("Offer failed because publisher is not connected to a subscriber");
            } else if (result == Publication.ADMIN_ACTION) {
                System.out.println("Offer failed because of an administration action in the system");
            } else if (result == Publication.CLOSED) {
                System.out.println("Offer failed because publication is closed");
                break;
            } else if (result == Publication.MAX_POSITION_EXCEEDED) {
                System.out.println("Offer failed due to publication reaching its max position");
                break;
            } else {
                System.out.println("Offer failed due to unknown reason: " + result);
            }
            if (!publication.isConnected()) {
                System.out.println("No active subscribers detected");
            }
            Thread.sleep(TimeUnit.SECONDS.toMillis(1));
        }
        System.out.println("Done sending.");
        if (LINGER_TIMEOUT_MS > 0) {
            System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
            Thread.sleep(LINGER_TIMEOUT_MS);
        }
    }
    CloseHelper.close(driver);
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) Publication(io.aeron.Publication) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Aeron(io.aeron.Aeron)

Example 62 with MediaDriver

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

the class EmbeddedExclusiveBufferClaimIpcThroughput method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 * @throws InterruptedException if the join on the created threads is interrupted.
 */
public static void main(final String[] args) throws InterruptedException {
    loadPropertiesFiles(args);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    final MediaDriver.Context ctx = new MediaDriver.Context().threadingMode(ThreadingMode.SHARED);
    try (MediaDriver mediaDriver = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID);
        Publication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID)) {
        final ImageRateSubscriber subscriber = new ImageRateSubscriber(FRAGMENT_COUNT_LIMIT, 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 ImageRateReporter(MESSAGE_LENGTH, 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 63 with MediaDriver

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

the class ClusterBackupMediaDriver method launch.

/**
 * Launch a new {@link ClusterBackupMediaDriver} with provided contexts.
 *
 * @param driverCtx        for configuring the {@link MediaDriver}.
 * @param archiveCtx       for configuring the {@link Archive}.
 * @param clusterBackupCtx for the configuration of the {@link ClusterBackup}.
 * @return a new {@link ClusterBackupMediaDriver} with the provided contexts.
 */
public static ClusterBackupMediaDriver launch(final MediaDriver.Context driverCtx, final Archive.Context archiveCtx, final ClusterBackup.Context clusterBackupCtx) {
    MediaDriver driver = null;
    Archive archive = null;
    ClusterBackup clusterBackup = null;
    try {
        driver = MediaDriver.launch(driverCtx.spiesSimulateConnection(true));
        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.aeronDirectoryName(driverCtx.aeronDirectoryName()).mediaDriverAgentInvoker(driver.sharedAgentInvoker()).errorHandler(errorHandler).errorCounter(errorCounter));
        clusterBackup = ClusterBackup.launch(clusterBackupCtx.aeronDirectoryName(driverCtx.aeronDirectoryName()));
        return new ClusterBackupMediaDriver(driver, archive, clusterBackup);
    } catch (final Exception ex) {
        CloseHelper.quietCloseAll(clusterBackup, archive, driver);
        throw ex;
    }
}
Also used : ErrorHandler(org.agrona.ErrorHandler) Archive(io.aeron.archive.Archive) MediaDriver(io.aeron.driver.MediaDriver) AtomicCounter(org.agrona.concurrent.status.AtomicCounter)

Example 64 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)

Example 65 with MediaDriver

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

the class RecordingDescriptorCollectorTest method shouldAllowUserToRetainDescriptorsToPreventReuse.

@Test
void shouldAllowUserToRetainDescriptorsToPreventReuse(@TempDir final Path tempDir) {
    try (MediaDriver mediaDriver = MediaDriver.launch(new MediaDriver.Context().dirDeleteOnStart(true));
        Archive ignore = Archive.launch(new Archive.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()).archiveDir(tempDir.resolve("archive").toFile()).deleteArchiveOnStart(true));
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
        AeronArchive aeronArchive = AeronArchive.connect(new AeronArchive.Context().aeron(aeron).ownsAeronClient(false))) {
        createRecordings(aeronArchive, 3);
        final RecordingDescriptorCollector collector = new RecordingDescriptorCollector(1);
        long fromRecordingId = 0;
        int count = aeronArchive.listRecordings(fromRecordingId, collector.poolSize(), collector.reset());
        assertEquals(1, count);
        final RecordingDescriptor desc0 = collector.descriptors().get(0);
        fromRecordingId += count;
        count = aeronArchive.listRecordings(fromRecordingId, collector.poolSize(), collector.reset());
        assertEquals(1, count);
        final RecordingDescriptor desc1 = collector.descriptors().get(0);
        assertEquals(desc0.recordingId(), desc1.recordingId());
        desc1.retain();
        fromRecordingId += count;
        count = aeronArchive.listRecordings(fromRecordingId, collector.poolSize(), collector.reset());
        assertEquals(1, count);
        final RecordingDescriptor desc2 = collector.descriptors().get(0);
        assertNotEquals(desc1.recordingId(), desc2.recordingId());
    }
}
Also used : Archive(io.aeron.archive.Archive) AeronArchive(io.aeron.archive.client.AeronArchive) MediaDriver(io.aeron.driver.MediaDriver) AeronArchive(io.aeron.archive.client.AeronArchive) Aeron(io.aeron.Aeron) SlowTest(io.aeron.test.SlowTest) Test(org.junit.jupiter.api.Test)

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