Search in sources :

Example 6 with Archive

use of io.aeron.archive.Archive in project Aeron by real-logic.

the class ArchiveCreator method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
@SuppressWarnings("try")
public static void main(final String[] args) {
    final String archiveDirName = Archive.Configuration.archiveDirName();
    final File archiveDir = ARCHIVE_DIR_DEFAULT.equals(archiveDirName) ? new File("archive") : new File(archiveDirName);
    final MediaDriver.Context driverContext = new MediaDriver.Context().publicationTermBufferLength(TERM_LENGTH).termBufferSparseFile(true).threadingMode(ThreadingMode.SHARED).errorHandler(Throwable::printStackTrace).spiesSimulateConnection(true).dirDeleteOnStart(true);
    final Archive.Context archiveContext = new Archive.Context().catalogCapacity(CATALOG_CAPACITY).segmentFileLength(SEGMENT_LENGTH).deleteArchiveOnStart(true).archiveDir(archiveDir).fileSyncLevel(0).threadingMode(ArchiveThreadingMode.SHARED);
    System.out.println("Creating basic archive at " + archiveContext.archiveDir());
    try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverContext, archiveContext);
        Aeron aeron = Aeron.connect();
        AeronArchive aeronArchive = AeronArchive.connect(new AeronArchive.Context().aeron(aeron))) {
        createRecording(aeron, aeronArchive, 0, (SEGMENT_LENGTH * 5L) + 1);
        createRecording(aeron, aeronArchive, (long) TERM_LENGTH + (FrameDescriptor.FRAME_ALIGNMENT * 2), (SEGMENT_LENGTH * 3L) + 1);
    } catch (final Exception ex) {
        ex.printStackTrace();
    }
}
Also used : Archive(io.aeron.archive.Archive) AeronArchive(io.aeron.archive.client.AeronArchive) MediaDriver(io.aeron.driver.MediaDriver) ArchivingMediaDriver(io.aeron.archive.ArchivingMediaDriver) File(java.io.File) ArchivingMediaDriver(io.aeron.archive.ArchivingMediaDriver) AeronArchive(io.aeron.archive.client.AeronArchive) Aeron(io.aeron.Aeron)

Example 7 with Archive

use of io.aeron.archive.Archive 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)

Example 8 with Archive

use of io.aeron.archive.Archive in project aeron by real-logic.

the class StartFromTruncatedRecordingLogTest method truncateRecordingLogAndDeleteMarkFiles.

private void truncateRecordingLogAndDeleteMarkFiles(final int index) throws IOException {
    final String baseDirName = baseDirName(index);
    final File consensusModuleDataDir = new File(baseDirName, "consensus-module");
    final File archiveDataDir = new File(baseDirName, "archive");
    final File tmpRecordingFile = new File(baseDirName, RECORDING_LOG_FILE_NAME);
    deleteFile(tmpRecordingFile);
    deleteFile(new File(archiveDataDir, ArchiveMarkFile.FILENAME));
    deleteFile(new File(consensusModuleDataDir, ClusterMarkFile.FILENAME));
    try (RecordingLog recordingLog = new RecordingLog(consensusModuleDataDir, false)) {
        final RecordingLog.Entry lastTermEntry = recordingLog.findLastTerm();
        if (null == lastTermEntry) {
            throw new IllegalStateException("no term found in recording log");
        }
        try (RecordingLog newRecordingLog = new RecordingLog(new File(baseDirName), true)) {
            newRecordingLog.appendTerm(lastTermEntry.recordingId, lastTermEntry.leadershipTermId, lastTermEntry.termBaseLogPosition, lastTermEntry.timestamp);
            newRecordingLog.commitLogPosition(lastTermEntry.leadershipTermId, lastTermEntry.logPosition);
            appendServiceSnapshot(recordingLog, newRecordingLog, 0);
            appendServiceSnapshot(recordingLog, newRecordingLog, ConsensusModule.Configuration.SERVICE_ID);
        }
    }
    Files.copy(new File(baseDirName).toPath().resolve(RECORDING_LOG_FILE_NAME), consensusModuleDataDir.toPath().resolve(RECORDING_LOG_FILE_NAME), StandardCopyOption.REPLACE_EXISTING);
    try (RecordingLog copiedRecordingLog = new RecordingLog(consensusModuleDataDir, false)) {
        final LongHashSet recordingIds = new LongHashSet();
        copiedRecordingLog.entries().stream().mapToLong((e) -> e.recordingId).forEach(recordingIds::add);
        final Predicate<Path> filterPredicate = (p) -> p.getFileName().toString().endsWith(".rec");
        try (Stream<Path> segments = Files.list(archiveDataDir.toPath()).filter(filterPredicate)) {
            segments.filter((p) -> {
                final String fileName = p.getFileName().toString();
                final long recordingId = Long.parseLong(fileName.split("-")[0]);
                return !recordingIds.contains(recordingId);
            }).map(Path::toFile).forEach(StartFromTruncatedRecordingLogTest::deleteFile);
        }
        assertTrue(copiedRecordingLog.entries().size() <= 3);
    }
}
Also used : ClusterTests(io.aeron.test.cluster.ClusterTests) BeforeEach(org.junit.jupiter.api.BeforeEach) StubClusteredService(io.aeron.test.cluster.StubClusteredService) AtomicCounter(org.agrona.concurrent.status.AtomicCounter) SlowTest(io.aeron.test.SlowTest) org.agrona(org.agrona) LongHashSet(org.agrona.collections.LongHashSet) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AeronArchive(io.aeron.archive.client.AeronArchive) ArchiveMarkFile(io.aeron.archive.ArchiveMarkFile) Publication(io.aeron.Publication) Path(java.nio.file.Path) MediaDriver(io.aeron.driver.MediaDriver) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) Archive(io.aeron.archive.Archive) Predicate(java.util.function.Predicate) ClusterException(io.aeron.cluster.client.ClusterException) TimeoutException(io.aeron.exceptions.TimeoutException) Test(org.junit.jupiter.api.Test) RECORDING_LOG_FILE_NAME(io.aeron.cluster.RecordingLog.RECORDING_LOG_FILE_NAME) EgressListener(io.aeron.cluster.client.EgressListener) InterruptAfter(io.aeron.test.InterruptAfter) Header(io.aeron.logbuffer.Header) Stream(java.util.stream.Stream) ClientSession(io.aeron.cluster.service.ClientSession) Counter(io.aeron.Counter) Mockito.mock(org.mockito.Mockito.mock) AeronCluster(io.aeron.cluster.client.AeronCluster) Tests(io.aeron.test.Tests) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) CountersReader(org.agrona.concurrent.status.CountersReader) StandardCopyOption(java.nio.file.StandardCopyOption) ArchiveThreadingMode(io.aeron.archive.ArchiveThreadingMode) MutableInteger(org.agrona.collections.MutableInteger) Files(java.nio.file.Files) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) CommonContext(io.aeron.CommonContext) NULL_VALUE(io.aeron.Aeron.NULL_VALUE) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Cluster(io.aeron.cluster.service.Cluster) AfterEach(org.junit.jupiter.api.AfterEach) ThreadingMode(io.aeron.driver.ThreadingMode) NoOpLock(org.agrona.concurrent.NoOpLock) ClusterMarkFile(io.aeron.cluster.service.ClusterMarkFile) Assertions(org.junit.jupiter.api.Assertions) EventCode(io.aeron.cluster.codecs.EventCode) Path(java.nio.file.Path) LongHashSet(org.agrona.collections.LongHashSet) ArchiveMarkFile(io.aeron.archive.ArchiveMarkFile) File(java.io.File) ClusterMarkFile(io.aeron.cluster.service.ClusterMarkFile)

Example 9 with Archive

use of io.aeron.archive.Archive in project aeron by real-logic.

the class ClusterLoggingAgentTest method testClusterEventsLogging.

private void testClusterEventsLogging(final String enabledEvents, final EnumSet<ClusterEventCode> expectedEvents) {
    before(enabledEvents, expectedEvents);
    final Context mediaDriverCtx = new Context().errorHandler(Tests::onError).dirDeleteOnStart(true).threadingMode(ThreadingMode.SHARED);
    final AeronArchive.Context aeronArchiveContext = new AeronArchive.Context().controlRequestChannel("aeron:ipc?term-length=64k").controlRequestStreamId(AeronArchive.Configuration.localControlStreamId()).controlResponseChannel("aeron:ipc?term-length=64k").controlResponseStreamId(AeronArchive.Configuration.localControlStreamId() + 1).controlResponseStreamId(101);
    final Archive.Context archiveCtx = new Archive.Context().errorHandler(Tests::onError).archiveDir(new File(testDir, "archive")).deleteArchiveOnStart(true).recordingEventsEnabled(false).threadingMode(ArchiveThreadingMode.SHARED);
    final ConsensusModule.Context consensusModuleCtx = new ConsensusModule.Context().errorHandler(ClusterTests.errorHandler(0)).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").ingressChannel("aeron:udp?term-length=64k").replicationChannel("aeron:udp?endpoint=localhost:0");
    final ClusteredService clusteredService = mock(ClusteredService.class);
    final ClusteredServiceContainer.Context clusteredServiceCtx = new ClusteredServiceContainer.Context().errorHandler(ClusterTests.errorHandler(0)).archiveContext(aeronArchiveContext.clone()).clusterDir(new File(testDir, "service")).clusteredService(clusteredService);
    clusteredMediaDriver = ClusteredMediaDriver.launch(mediaDriverCtx, archiveCtx, consensusModuleCtx);
    container = ClusteredServiceContainer.launch(clusteredServiceCtx);
    Tests.await(WAIT_LIST::isEmpty);
    final Counter state = clusteredMediaDriver.consensusModule().context().electionStateCounter();
    final Supplier<String> message = () -> ElectionState.get(state).toString();
    while (ElectionState.CLOSED != ElectionState.get(state)) {
        Tests.sleep(1, message);
    }
}
Also used : Context(io.aeron.driver.MediaDriver.Context) AeronArchive(io.aeron.archive.client.AeronArchive) Archive(io.aeron.archive.Archive) ClusteredService(io.aeron.cluster.service.ClusteredService) AeronArchive(io.aeron.archive.client.AeronArchive) ClusterTests(io.aeron.test.cluster.ClusterTests) Tests(io.aeron.test.Tests) Counter(io.aeron.Counter) ConsensusModule(io.aeron.cluster.ConsensusModule) File(java.io.File) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer)

Example 10 with Archive

use of io.aeron.archive.Archive in project aeron by real-logic.

the class ClusteredArchive method launch.

/**
 * Launch a new {@link ClusteredArchive} with provided contexts.
 *
 * @param aeronDirectoryName for connecting to the {@link MediaDriver}.
 * @param archiveCtx         for configuring the {@link Archive}.
 * @param consensusModuleCtx for the configuration of the {@link ConsensusModule}.
 * @return a new {@link ClusteredArchive} with the provided contexts.
 */
public static ClusteredArchive launch(final String aeronDirectoryName, final Archive.Context archiveCtx, final ConsensusModule.Context consensusModuleCtx) {
    Archive archive = null;
    ConsensusModule consensusModule = null;
    try {
        archive = Archive.launch(archiveCtx.aeronDirectoryName(aeronDirectoryName));
        consensusModule = ConsensusModule.launch(consensusModuleCtx.aeronDirectoryName(aeronDirectoryName));
        return new ClusteredArchive(archive, consensusModule);
    } catch (final Exception ex) {
        CloseHelper.quietCloseAll(consensusModule, archive);
        throw ex;
    }
}
Also used : Archive(io.aeron.archive.Archive)

Aggregations

Archive (io.aeron.archive.Archive)25 MediaDriver (io.aeron.driver.MediaDriver)20 AeronArchive (io.aeron.archive.client.AeronArchive)17 File (java.io.File)13 Test (org.junit.jupiter.api.Test)10 Aeron (io.aeron.Aeron)8 ClusteredServiceContainer (io.aeron.cluster.service.ClusteredServiceContainer)8 SlowTest (io.aeron.test.SlowTest)8 CommonContext (io.aeron.CommonContext)7 ArchivingMediaDriver (io.aeron.archive.ArchivingMediaDriver)6 Tests (io.aeron.test.Tests)6 AtomicCounter (org.agrona.concurrent.status.AtomicCounter)6 Counter (io.aeron.Counter)4 ArchiveThreadingMode (io.aeron.archive.ArchiveThreadingMode)4 AeronCluster (io.aeron.cluster.client.AeronCluster)4 EgressListener (io.aeron.cluster.client.EgressListener)4 ClientSession (io.aeron.cluster.service.ClientSession)4 ThreadingMode (io.aeron.driver.ThreadingMode)4 Header (io.aeron.logbuffer.Header)4 InterruptAfter (io.aeron.test.InterruptAfter)4