Java Code Examples for io.aeron.archive.client.AeronArchive#Context

The following examples show how to use io.aeron.archive.client.AeronArchive#Context . 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: ArchiveLoggingAgentTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void testArchiveLogging(final String enabledEvents, final EnumSet<ArchiveEventCode> expectedEvents)
    throws InterruptedException
{
    before(enabledEvents, expectedEvents);

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

    final MediaDriver.Context mediaDriverCtx = new MediaDriver.Context()
        .errorHandler(Tests::onError)
        .aeronDirectoryName(aeronDirectoryName)
        .dirDeleteOnStart(true)
        .threadingMode(ThreadingMode.SHARED);

    final AeronArchive.Context aeronArchiveContext = new AeronArchive.Context()
        .aeronDirectoryName(aeronDirectoryName)
        .controlRequestChannel("aeron:udp?term-length=64k|endpoint=localhost:8010")
        .controlRequestStreamId(100)
        .controlResponseChannel("aeron:udp?term-length=64k|endpoint=localhost:8020")
        .controlResponseStreamId(101)
        .recordingEventsChannel("aeron:udp?control-mode=dynamic|control=localhost:8030");

    final Archive.Context archiveCtx = new Archive.Context()
        .errorHandler(Tests::onError)
        .archiveDir(new File(testDir, "archive"))
        .deleteArchiveOnStart(true)
        .controlChannel(aeronArchiveContext.controlRequestChannel())
        .controlStreamId(aeronArchiveContext.controlRequestStreamId())
        .localControlStreamId(aeronArchiveContext.controlRequestStreamId())
        .recordingEventsChannel(aeronArchiveContext.recordingEventsChannel())
        .threadingMode(ArchiveThreadingMode.SHARED);

    try (ArchivingMediaDriver ignore1 = ArchivingMediaDriver.launch(mediaDriverCtx, archiveCtx))
    {
        try (AeronArchive ignore2 = AeronArchive.connect(aeronArchiveContext))
        {
            latch.await();
        }
    }
}
 
Example 2
Source File: LiveRecordingMessageTransceiver.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
public void init(final Configuration configuration)
{
    final AeronArchive.Context context = aeronArchive.context();
    final Aeron aeron = context.aeron();

    fragmentLimit = fragmentLimit();

    subscription = aeron.addSubscription(receiveChannel(), receiveStreamId());

    final String sendChannel = sendChannel();
    final int sendStreamId = sendStreamId();
    publication = aeron.addExclusivePublication(sendChannel, sendStreamId);

    recordingEventsSubscription = aeron.addSubscription(
        context.recordingEventsChannel(), context.recordingEventsStreamId());

    recordingEventsAdapter = new RecordingEventsAdapter(
        new LiveRecordingEventsListener(this), recordingEventsSubscription, fragmentLimit);

    while (!recordingEventsSubscription.isConnected() || !subscription.isConnected() || !publication.isConnected())
    {
        yieldUninterruptedly();
    }

    final int publicationSessionId = publication.sessionId();
    final String channel = addSessionId(sendChannel, publicationSessionId);
    aeronArchive.startRecording(channel, sendStreamId, LOCAL, true);
    recordingId = awaitRecordingStart(aeron, publicationSessionId);

    offerBuffer = new UnsafeBuffer(allocateDirectAligned(configuration.messageLength(), CACHE_LINE_LENGTH));
    image = subscription.imageAtIndex(0);
}
 
Example 3
Source File: ReplayedBasicSubscriber.java    From aeron with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args)
{
    System.out.println("Subscribing to " + CHANNEL + " on stream id " + STREAM_ID);

    final FragmentHandler fragmentHandler = SamplesUtil.printStringMessage(STREAM_ID);
    final AtomicBoolean running = new AtomicBoolean(true);

    SigInt.register(() -> running.set(false));

    // Create a unique response stream id so not to clash with other archive clients.
    final AeronArchive.Context archiveCtx = new AeronArchive.Context()
        .controlResponseStreamId(AeronArchive.Configuration.controlResponseStreamId() + 2);

    try (AeronArchive archive = AeronArchive.connect(archiveCtx))
    {
        final long recordingId = findLatestRecording(archive);
        final long position = 0L;
        final long length = Long.MAX_VALUE;

        final long sessionId = archive.startReplay(recordingId, position, length, CHANNEL, REPLAY_STREAM_ID);
        final String channel = ChannelUri.addSessionId(CHANNEL, (int)sessionId);

        try (Subscription subscription = archive.context().aeron().addSubscription(channel, REPLAY_STREAM_ID))
        {
            SamplesUtil.subscriberLoop(fragmentHandler, FRAGMENT_COUNT_LIMIT, running).accept(subscription);
            System.out.println("Shutting down...");
        }
    }
}
 
Example 4
Source File: ResetArchiveState.java    From artio with Apache License 2.0 4 votes vote down vote up
private void createArchiver()
{
    aeron = Aeron.connect(configuration.aeronContextClone());
    final AeronArchive.Context archiveContext = configuration.archiveContextClone();
    archive = AeronArchive.connect(archiveContext.aeron(aeron));
}
 
Example 5
Source File: ClusterLoggingAgentTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
private void testClusterEventsLogging(
    final String enabledEvents, final EnumSet<ClusterEventCode> expectedEvents) throws InterruptedException
{
    before(enabledEvents, expectedEvents.size());

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

    final Context mediaDriverCtx = new Context()
        .errorHandler(Tests::onError)
        .aeronDirectoryName(aeronDirectoryName)
        .dirDeleteOnStart(true)
        .threadingMode(ThreadingMode.SHARED);

    final AeronArchive.Context aeronArchiveContext = new AeronArchive.Context()
        .aeronDirectoryName(aeronDirectoryName)
        .controlRequestChannel("aeron:udp?term-length=64k|endpoint=localhost:8010")
        .controlRequestStreamId(100)
        .controlResponseChannel("aeron:udp?term-length=64k|endpoint=localhost:8020")
        .controlResponseStreamId(101)
        .recordingEventsChannel("aeron:udp?control-mode=dynamic|control=localhost:8030");

    final Archive.Context archiveCtx = new Archive.Context()
        .errorHandler(Tests::onError)
        .archiveDir(new File(testDir, "archive"))
        .deleteArchiveOnStart(true)
        .controlChannel(aeronArchiveContext.controlRequestChannel())
        .controlStreamId(aeronArchiveContext.controlRequestStreamId())
        .localControlStreamId(aeronArchiveContext.controlRequestStreamId())
        .recordingEventsChannel(aeronArchiveContext.recordingEventsChannel())
        .threadingMode(ArchiveThreadingMode.SHARED);

    final ConsensusModule.Context consensusModuleCtx = new ConsensusModule.Context()
        .errorHandler(Tests::onError)
        .clusterDir(new File(testDir, "consensus-module"))
        .archiveContext(aeronArchiveContext.clone())
        .clusterMemberId(0)
        .clusterMembers("0,localhost:20110,localhost:20220,localhost:20330,localhost:20440,localhost:8010")
        .logChannel("aeron:udp?term-length=256k|control-mode=manual|control=localhost:20550");

    final ClusteredService clusteredService = mock(ClusteredService.class);
    final ClusteredServiceContainer.Context clusteredServiceCtx = new ClusteredServiceContainer.Context()
        .aeronDirectoryName(aeronDirectoryName)
        .errorHandler(Tests::onError)
        .archiveContext(aeronArchiveContext.clone())
        .clusterDir(new File(testDir, "service"))
        .clusteredService(clusteredService);

    clusteredMediaDriver = ClusteredMediaDriver.launch(mediaDriverCtx, archiveCtx, consensusModuleCtx);
    container = ClusteredServiceContainer.launch(clusteredServiceCtx);

    latch.await();
    verify(clusteredService, timeout(5000)).onRoleChange(eq(Cluster.Role.LEADER));

    final Set<Integer> expected = expectedEvents
        .stream()
        .map(ClusterEventLogger::toEventCodeId)
        .collect(toSet());

    assertEquals(expected, LOGGED_EVENTS);
}
 
Example 6
Source File: BasicAuctionClusteredServiceNode.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args)
{
    final int nodeId = parseInt(System.getProperty("aeron.cluster.tutorial.nodeId"));                // <1>

    final List<String> hostnames = Arrays.asList("localhost", "localhost", "localhost");             // <2>
    final String hostname = hostnames.get(nodeId);

    final File baseDir = new File(System.getProperty("user.dir"), "node" + nodeId);            // <3>
    final String aeronDirName = CommonContext.getAeronDirectoryName() + "-" + nodeId + "-driver";

    final ShutdownSignalBarrier barrier = new ShutdownSignalBarrier();                               // <4>
    // end::main[]

    // tag::media_driver[]
    final MediaDriver.Context mediaDriverContext = new MediaDriver.Context()
        .aeronDirectoryName(aeronDirName)
        .threadingMode(ThreadingMode.SHARED)
        .termBufferSparseFile(true)
        .multicastFlowControlSupplier(new MinMulticastFlowControlSupplier())
        .terminationHook(barrier::signal)
        .errorHandler(BasicAuctionClusteredServiceNode.errorHandler("Media Driver"));
    // end::media_driver[]

    // tag::archive[]
    final Archive.Context archiveContext = new Archive.Context()
        .archiveDir(new File(baseDir, "archive"))
        .controlChannel(udpChannel(nodeId, "localhost", ARCHIVE_CONTROL_REQUEST_PORT_OFFSET))
        .localControlChannel("aeron:ipc?term-length=64k")
        .recordingEventsEnabled(false)
        .threadingMode(ArchiveThreadingMode.SHARED);
    // end::archive[]

    // tag::archive_client[]
    final AeronArchive.Context aeronArchiveContext = new AeronArchive.Context()
        .controlRequestChannel(archiveContext.controlChannel())
        .controlRequestStreamId(archiveContext.controlStreamId())
        .controlResponseChannel(udpChannel(nodeId, "localhost", ARCHIVE_CONTROL_RESPONSE_PORT_OFFSET))
        .aeronDirectoryName(aeronDirName);
    // end::archive_client[]

    // tag::consensus_module[]
    final ConsensusModule.Context consensusModuleContext = new ConsensusModule.Context()
        .errorHandler(errorHandler("Consensus Module"))
        .clusterMemberId(nodeId)                                                                     // <1>
        .clusterMembers(clusterMembers(hostnames))                                                   // <2>
        .clusterDir(new File(baseDir, "consensus-module"))                                     // <3>
        .ingressChannel("aeron:udp?term-length=64k")                                                 // <4>
        .logChannel(logControlChannel(nodeId, hostname, LOG_CONTROL_PORT_OFFSET))                    // <5>
        .archiveContext(aeronArchiveContext.clone());                                                // <6>
    // end::consensus_module[]

    // tag::clustered_service[]
    final ClusteredServiceContainer.Context clusteredServiceContext =
        new ClusteredServiceContainer.Context()
        .aeronDirectoryName(aeronDirName)                                                            // <1>
        .archiveContext(aeronArchiveContext.clone())                                                 // <2>
        .clusterDir(new File(baseDir, "service"))
        .clusteredService(new BasicAuctionClusteredService())                                        // <3>
        .errorHandler(errorHandler("Clustered Service"));
    // end::clustered_service[]

    // tag::running[]
    try (
        ClusteredMediaDriver clusteredMediaDriver = ClusteredMediaDriver.launch(
            mediaDriverContext, archiveContext, consensusModuleContext);                             // <1>
        ClusteredServiceContainer container = ClusteredServiceContainer.launch(
            clusteredServiceContext))                                                                // <2>
    {
        System.out.println("[" + nodeId + "] Started Cluster Node on " + hostname + "...");
        barrier.await();                                                                             // <3>
        System.out.println("[" + nodeId + "] Exiting");
    }
    // end::running[]
}
 
Example 7
Source File: RecordedBasicPublisher.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    System.out.println("Publishing to " + CHANNEL + " on stream id " + STREAM_ID);

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

    // Create a unique response stream id so not to clash with other archive clients.
    final AeronArchive.Context archiveCtx = new AeronArchive.Context()
        .controlResponseStreamId(AeronArchive.Configuration.controlResponseStreamId() + 1);

    try (AeronArchive archive = AeronArchive.connect(archiveCtx))
    {
        archive.startRecording(CHANNEL, STREAM_ID, SourceLocation.LOCAL);

        try (Publication publication = archive.context().aeron().addPublication(CHANNEL, STREAM_ID))
        {
            final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;
            // Wait for recording to have started before publishing.
            final CountersReader counters = archive.context().aeron().countersReader();
            int counterId = RecordingPos.findCounterIdBySession(counters, publication.sessionId());
            while (CountersReader.NULL_COUNTER_ID == counterId)
            {
                if (!running.get())
                {
                    return;
                }

                idleStrategy.idle();
                counterId = RecordingPos.findCounterIdBySession(counters, publication.sessionId());
            }

            final long recordingId = RecordingPos.getRecordingId(counters, counterId);
            System.out.println("Recording started: recordingId = " + recordingId);

            for (int i = 0; i < NUMBER_OF_MESSAGES && running.get(); i++)
            {
                final String message = "Hello World! " + i;
                final byte[] messageBytes = message.getBytes();
                BUFFER.putBytes(0, messageBytes);

                System.out.print("Offering " + i + "/" + NUMBER_OF_MESSAGES + " - ");

                final long result = publication.offer(BUFFER, 0, messageBytes.length);
                checkResult(result);

                final String errorMessage = archive.pollForErrorResponse();
                if (null != errorMessage)
                {
                    throw new IllegalStateException(errorMessage);
                }

                Thread.sleep(TimeUnit.SECONDS.toMillis(1));
            }

            idleStrategy.reset();
            while (counters.getCounterValue(counterId) < publication.position())
            {
                if (!RecordingPos.isActive(counters, counterId, recordingId))
                {
                    throw new IllegalStateException("recording has stopped unexpectedly: " + recordingId);
                }

                idleStrategy.idle();
            }
        }
        finally
        {
            System.out.println("Done sending.");
            archive.stopRecording(CHANNEL, STREAM_ID);
        }
    }
}
 
Example 8
Source File: StartFromTruncatedRecordingLogTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
private void startNode(final int index, final boolean cleanStart)
{
    final String baseDirName = baseDirName(index);
    final String aeronDirName = aeronDirName(index);

    final AeronArchive.Context archiveCtx = new AeronArchive.Context()
        .controlRequestChannel(memberSpecificPort(ARCHIVE_CONTROL_REQUEST_CHANNEL, index))
        .controlRequestStreamId(100 + index)
        .controlResponseChannel(memberSpecificPort(ARCHIVE_CONTROL_RESPONSE_CHANNEL, index))
        .controlResponseStreamId(110 + index)
        .aeronDirectoryName(baseDirName);

    clusteredMediaDrivers[index] = ClusteredMediaDriver.launch(
        new MediaDriver.Context()
            .aeronDirectoryName(aeronDirName)
            .warnIfDirectoryExists(false)
            .threadingMode(ThreadingMode.SHARED)
            .termBufferSparseFile(true)
            .errorHandler(ClusterTests.errorHandler(index))
            .dirDeleteOnShutdown(false)
            .dirDeleteOnStart(true),
        new Archive.Context()
            .maxCatalogEntries(MAX_CATALOG_ENTRIES)
            .archiveDir(new File(baseDirName, "archive"))
            .controlChannel(archiveCtx.controlRequestChannel())
            .controlStreamId(archiveCtx.controlRequestStreamId())
            .localControlChannel("aeron:ipc?term-length=64k")
            .localControlStreamId(archiveCtx.controlRequestStreamId())
            .recordingEventsEnabled(false)
            .threadingMode(ArchiveThreadingMode.SHARED)
            .errorHandler(Tests::onError)
            .deleteArchiveOnStart(cleanStart),
        new ConsensusModule.Context()
            .errorHandler(ClusterTests.errorHandler(index))
            .clusterMemberId(index)
            .clusterMembers(CLUSTER_MEMBERS)
            .clusterDir(new File(baseDirName, "consensus-module"))
            .ingressChannel("aeron:udp?term-length=64k")
            .logChannel(memberSpecificPort(LOG_CHANNEL, index))
            .archiveContext(archiveCtx.clone())
            .shouldTerminateWhenClosed(false)
            .deleteDirOnStart(cleanStart));

    containers[index] = ClusteredServiceContainer.launch(
        new ClusteredServiceContainer.Context()
            .aeronDirectoryName(aeronDirName)
            .archiveContext(archiveCtx.clone())
            .clusterDir(new File(baseDirName, "service"))
            .clusteredService(echoServices[index])
            .errorHandler(ClusterTests.errorHandler(index)));
}
 
Example 9
Source File: ArchiveConductor.java    From aeron with Apache License 2.0 4 votes vote down vote up
void replicate(
    final long correlationId,
    final long srcRecordingId,
    final long dstRecordingId,
    final long channelTagId,
    final long subscriptionTagId,
    final int srcControlStreamId,
    final String srcControlChannel,
    final String liveDestination,
    final ControlSession controlSession)
{
    final boolean hasRecording = catalog.hasRecording(dstRecordingId);
    if (NULL_VALUE != dstRecordingId && !hasRecording)
    {
        final String msg = "unknown destination recording id " + dstRecordingId;
        controlSession.sendErrorResponse(correlationId, UNKNOWN_RECORDING, msg, controlResponseProxy);
        return;
    }

    if (hasRecording)
    {
        catalog.recordingSummary(dstRecordingId, recordingSummary);
    }

    final AeronArchive.Context remoteArchiveContext = ctx.archiveClientContext().clone()
        .controlRequestChannel(srcControlChannel)
        .controlRequestStreamId(srcControlStreamId);

    final long replicationId = aeron.nextCorrelationId();
    final ReplicationSession replicationSession = new ReplicationSession(
        srcRecordingId,
        dstRecordingId,
        channelTagId,
        subscriptionTagId,
        replicationId,
        liveDestination,
        ctx.replicationChannel(),
        hasRecording ? recordingSummary : null,
        remoteArchiveContext,
        cachedEpochClock,
        catalog,
        controlResponseProxy,
        controlSession);

    replicationSessionByIdMap.put(replicationId, replicationSession);
    addSession(replicationSession);

    controlSession.sendOkResponse(correlationId, replicationId, controlResponseProxy);
}
 
Example 10
Source File: FixEngine.java    From artio with Apache License 2.0 4 votes vote down vote up
private FixEngine(final EngineConfiguration configuration)
{
    try
    {
        this.configuration = configuration;

        timers = new EngineTimers(configuration.clock());
        scheduler = configuration.scheduler();
        scheduler.configure(configuration.aeronContext());
        init(configuration);
        final AeronArchive.Context archiveContext = configuration.aeronArchiveContext();
        final AeronArchive aeronArchive =
            configuration.logAnyMessages() ? AeronArchive.connect(archiveContext.aeron(aeron)) : null;
        recordingCoordinator = new RecordingCoordinator(
            aeron,
            aeronArchive,
            configuration,
            configuration.archiverIdleStrategy(),
            errorHandler);

        final ExclusivePublication replayPublication = replayPublication();
        engineContext = new EngineContext(
            configuration,
            errorHandler,
            replayPublication,
            fixCounters,
            aeron,
            aeronArchive,
            recordingCoordinator);
        initFramer(configuration, fixCounters, replayPublication.sessionId());
        initMonitoringAgent(timers.all(), configuration, aeronArchive);
    }
    catch (final Exception e)
    {
        if (engineContext != null)
        {
            engineContext.completeDuringStartup();
        }

        suppressingClose(this, e);

        throw e;
    }
}
 
Example 11
Source File: EngineConfiguration.java    From artio with Apache License 2.0 4 votes vote down vote up
public AeronArchive.Context archiveContextClone()
{
    return archiveContextClone;
}
 
Example 12
Source File: EngineConfiguration.java    From artio with Apache License 2.0 4 votes vote down vote up
public AeronArchive.Context aeronArchiveContext()
{
    return archiveContext;
}
 
Example 13
Source File: ArchivePruneSystemTest.java    From artio with Apache License 2.0 4 votes vote down vote up
private AeronArchive newArchive()
{
    final AeronArchive.Context archiveContext = acceptingEngine.configuration().archiveContextClone();
    return AeronArchive.connect(archiveContext);
}
 
Example 14
Source File: Archive.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the {@link io.aeron.archive.client.AeronArchive.Context} that should be used for communicating
 * with a remote archive for replication.
 *
 * @return the {@link io.aeron.archive.client.AeronArchive.Context} that should be used for communicating
 * with a remote archive for replication.
 */
public AeronArchive.Context archiveClientContext()
{
    return archiveClientContext;
}
 
Example 15
Source File: ClusterBackup.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the {@link io.aeron.archive.client.AeronArchive.Context} that should be used for communicating with
 * the local Archive.
 *
 * @return the {@link io.aeron.archive.client.AeronArchive.Context} that should be used for communicating
 * with the local Archive.
 */
public AeronArchive.Context archiveContext()
{
    return archiveContext;
}
 
Example 16
Source File: ClusteredServiceContainer.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the context that should be used for communicating with the local Archive.
 *
 * @return the context that should be used for communicating with the local Archive.
 */
public AeronArchive.Context archiveContext()
{
    return archiveContext;
}
 
Example 17
Source File: ConsensusModule.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the {@link io.aeron.archive.client.AeronArchive.Context} that should be used for communicating with
 * the local Archive.
 *
 * @return the {@link io.aeron.archive.client.AeronArchive.Context} that should be used for communicating
 * with the local Archive.
 */
public AeronArchive.Context archiveContext()
{
    return archiveContext;
}
 
Example 18
Source File: Archive.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Set the {@link io.aeron.archive.client.AeronArchive.Context} that should be used for communicating
 * with a remote archive for replication.
 *
 * @param archiveContext that should be used for communicating with a remote Archive.
 * @return this for a fluent API.
 */
public Context archiveClientContext(final AeronArchive.Context archiveContext)
{
    this.archiveClientContext = archiveContext;
    return this;
}