Skip to content

Is there a simpler way for a utility to connect to a local Aeron Archive?

Problem

You have a utility or similar that needs to connect to an Aeron Archive. This utility will have full access to the Aeron Archive folder, and you don't want to specify configuration to connect to the Archive.

Solution

Open the Aeron Archive archive-mark.dat file with io.aeron.archive.ArchiveMarkFile, and read the configuration details from there, and then connect as normal.

Discussion

Aeron Archive writes the active configuration to an SBE based Aeron Mark File, archive-mark.dat. This is stored in the archive folder given to the ArchivingMediaDriver.

To open an Archive Mark File:

private final static TIMEOUT_MS = 10_000;

private void logger(String message)
{
    //do something with the message.
}

private ArchiveMarkFile openMarkFile(File archiveDir, Consumer<String> logger)
{
    return new ArchiveMarkFile(archiveDir, ArchiveMarkFile.FILENAME, SystemEpochClock.INSTANCE, TIMEOUT_MS, logger);
}

To read the connection details from an Archive Mark File:

private AeronArchive connectToLocalArchive(ArchiveMarkFile markFile)
{
    final long lastActivityEpochMs = markFile.activityTimestampVolatile();
    final int localControlStreamId = markFile.decoder().localControlStreamId();

    //the following fields are stored in variable length fields in SBE. 
    //all must be read in this order, or data will be corrupted on read.
    final String controlChannel = markFile.decoder().controlChannel();
    final String localControlChannel = markFile.decoder().localControlChannel();
    final String eventsChannel = markFile.decoder().eventsChannel();
    final String aeronDirectory = markFile.decoder().aeronDirectory();

    return AeronArchive.connect(
        new AeronArchive.Context()
          .aeronDirectoryName(aeronDirectory)
          .controlRequestChannel(localControlChannel)
          .controlRequestStreamId(localControlStreamId)
          .recordingEventsChannel(eventsChannel)
          .controlResponseChannel("aeron:ipc") //change as needed
    );
}

The above examples assume that the ArchivingMediaDriver is running. You can confirm it is still active by checking the lastActivityEpochMs read above against the current system time (e.g. SystemEpochClock.INSTANCE). There are a few additional fields defined in the archive-mark.dat file - see GitHub for the full definition.

See Also

Back to top