Search in sources :

Example 1 with RECORDING_LOG_FILE_NAME

use of io.aeron.cluster.RecordingLog.RECORDING_LOG_FILE_NAME 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 2 with RECORDING_LOG_FILE_NAME

use of io.aeron.cluster.RecordingLog.RECORDING_LOG_FILE_NAME 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)

Aggregations

NULL_VALUE (io.aeron.Aeron.NULL_VALUE)2 CommonContext (io.aeron.CommonContext)2 Counter (io.aeron.Counter)2 Publication (io.aeron.Publication)2 Archive (io.aeron.archive.Archive)2 ArchiveMarkFile (io.aeron.archive.ArchiveMarkFile)2 ArchiveThreadingMode (io.aeron.archive.ArchiveThreadingMode)2 AeronArchive (io.aeron.archive.client.AeronArchive)2 RECORDING_LOG_FILE_NAME (io.aeron.cluster.RecordingLog.RECORDING_LOG_FILE_NAME)2 AeronCluster (io.aeron.cluster.client.AeronCluster)2 ClusterException (io.aeron.cluster.client.ClusterException)2 EgressListener (io.aeron.cluster.client.EgressListener)2 EventCode (io.aeron.cluster.codecs.EventCode)2 ClientSession (io.aeron.cluster.service.ClientSession)2 Cluster (io.aeron.cluster.service.Cluster)2 ClusterMarkFile (io.aeron.cluster.service.ClusterMarkFile)2 ClusteredServiceContainer (io.aeron.cluster.service.ClusteredServiceContainer)2 MediaDriver (io.aeron.driver.MediaDriver)2 ThreadingMode (io.aeron.driver.ThreadingMode)2 TimeoutException (io.aeron.exceptions.TimeoutException)2