Search in sources :

Example 71 with Publication

use of io.aeron.Publication in project aeron by real-logic.

the class ManageRecordingHistoryTest method shouldPurgeForLateJoinedStream.

@Test
@InterruptAfter(10)
void shouldPurgeForLateJoinedStream() throws IOException {
    final String messagePrefix = "Message-Prefix-";
    final int initialTermId = 7;
    final long targetPosition = (SEGMENT_LENGTH * 13L) + 1;
    final long startPosition = (SEGMENT_LENGTH * 10L) + 7 * FRAME_ALIGNMENT;
    uriBuilder.initialPosition(startPosition, initialTermId, TERM_LENGTH);
    try (Publication publication = aeronArchive.addRecordedExclusivePublication(uriBuilder.build(), STREAM_ID)) {
        assertEquals(startPosition, publication.position());
        final CountersReader counters = aeron.countersReader();
        final int counterId = awaitRecordingCounterId(counters, publication.sessionId());
        final long recordingId = RecordingPos.getRecordingId(counters, counterId);
        offerToPosition(publication, messagePrefix, targetPosition);
        awaitPosition(counters, counterId, publication.position());
        final File archiveDir = archive.context().archiveDir();
        long position = 0;
        boolean deleted = false;
        while (position < startPosition - SEGMENT_LENGTH) {
            final String segmentFileName = Archive.segmentFileName(recordingId, position);
            if (deleted) {
                assertTrue(new File(archiveDir, segmentFileName + ".del").createNewFile());
            } else {
                assertTrue(new File(archiveDir, segmentFileName).createNewFile());
            }
            deleted = !deleted;
            position += SEGMENT_LENGTH;
        }
        final String fileNamePrefix = recordingId + "-";
        final String[] recordingFiles = archiveDir.list((dir, name) -> name.startsWith(fileNamePrefix));
        assertThat(recordingFiles, arrayWithSize(14));
        final long segmentFileBasePosition = AeronArchive.segmentFileBasePosition(startPosition, startPosition + SEGMENT_LENGTH + TERM_LENGTH + FRAME_ALIGNMENT * 5, TERM_LENGTH, SEGMENT_LENGTH);
        final long purgeSegments = aeronArchive.purgeSegments(recordingId, segmentFileBasePosition);
        assertEquals(11L, purgeSegments);
        assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
        aeronArchive.stopRecording(publication);
        Tests.await(() -> {
            final String[] files = archiveDir.list((dir, name) -> name.startsWith(fileNamePrefix));
            if (null != files && 3 == files.length) {
                assertThat(files, arrayContainingInAnyOrder(Archive.segmentFileName(recordingId, SEGMENT_LENGTH * 13L), Archive.segmentFileName(recordingId, SEGMENT_LENGTH * 12L), Archive.segmentFileName(recordingId, SEGMENT_LENGTH * 11L)));
                return true;
            }
            return false;
        });
    }
}
Also used : Publication(io.aeron.Publication) File(java.io.File) CountersReader(org.agrona.concurrent.status.CountersReader) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Aggregations

Publication (io.aeron.Publication)71 Aeron (io.aeron.Aeron)37 Test (org.junit.jupiter.api.Test)30 CountersReader (org.agrona.concurrent.status.CountersReader)24 InterruptAfter (io.aeron.test.InterruptAfter)22 MediaDriver (io.aeron.driver.MediaDriver)21 Subscription (io.aeron.Subscription)19 File (java.io.File)14 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 ChannelUriStringBuilder (io.aeron.ChannelUriStringBuilder)10 AeronArchive (io.aeron.archive.client.AeronArchive)10 ThreadingMode (io.aeron.driver.ThreadingMode)10 InterruptingTestCallback (io.aeron.test.InterruptingTestCallback)10 Tests (io.aeron.test.Tests)10 ContinueBarrier (org.agrona.console.ContinueBarrier)10 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)10 FragmentAssembler (io.aeron.FragmentAssembler)9 CloseHelper (org.agrona.CloseHelper)9 DirectBuffer (org.agrona.DirectBuffer)9