Search in sources :

Example 21 with Publication

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

the class ManageRecordingHistoryTest method shouldMigrateSegmentsForStreamJoinedAtTheBeginning.

@Test
@InterruptAfter(10)
void shouldMigrateSegmentsForStreamJoinedAtTheBeginning() throws IOException {
    final long targetPosition = (SEGMENT_LENGTH * 3L) + 1;
    try (Publication publication = aeronArchive.addRecordedPublication(uriBuilder.build(), STREAM_ID)) {
        final CountersReader counters = aeron.countersReader();
        final int dstCounterId = awaitRecordingCounterId(counters, publication.sessionId());
        final long dstRecordingId = RecordingPos.getRecordingId(counters, dstCounterId);
        offerToPosition(publication, "dst-message-", targetPosition);
        awaitPosition(counters, dstCounterId, publication.position());
        aeronArchive.stopRecording(publication);
        final long startPosition = 0L;
        final long migratePosition = AeronArchive.segmentFileBasePosition(startPosition, SEGMENT_LENGTH * 2L, TERM_LENGTH, SEGMENT_LENGTH);
        final long count = aeronArchive.purgeSegments(dstRecordingId, migratePosition);
        assertEquals(2L, count);
        assertEquals(migratePosition, aeronArchive.getStartPosition(dstRecordingId));
        final long srcRecordingId;
        final String migrateChannel = uriBuilder.initialPosition(startPosition, publication.initialTermId(), TERM_LENGTH).endpoint("localhost:4444").build();
        try (Publication migratePub = aeronArchive.addRecordedExclusivePublication(migrateChannel, STREAM_ID)) {
            final int srcCounterId = awaitRecordingCounterId(counters, migratePub.sessionId());
            srcRecordingId = RecordingPos.getRecordingId(counters, srcCounterId);
            offerToPosition(migratePub, "src-message-", SEGMENT_LENGTH * 4 + TERM_LENGTH + FRAME_ALIGNMENT);
            awaitPosition(counters, srcCounterId, migratePub.position());
            aeronArchive.stopRecording(migratePub);
        }
        aeronArchive.truncateRecording(srcRecordingId, migratePosition);
        final File archiveDir = archive.context().archiveDir();
        final String srcPrefix = srcRecordingId + "-";
        Tests.await(() -> {
            final String[] srcFiles = archiveDir.list((dir, name) -> name.startsWith(srcPrefix));
            if (null != srcFiles && 2 == srcFiles.length) {
                assertThat(srcFiles, arrayContainingInAnyOrder(Archive.segmentFileName(srcRecordingId, 0), Archive.segmentFileName(srcRecordingId, SEGMENT_LENGTH)));
                return true;
            }
            return false;
        });
        final Path srcFile = new File(archiveDir, Archive.segmentFileName(srcRecordingId, migratePosition) + ".del").toPath();
        Files.write(srcFile, new byte[] { 0x1, 0x2, 0x3 }, StandardOpenOption.CREATE_NEW);
        final String dstPrefix = dstRecordingId + "-";
        String[] dstFiles = archiveDir.list((dir, name) -> name.startsWith(dstPrefix));
        assertThat(dstFiles, arrayContainingInAnyOrder(Archive.segmentFileName(dstRecordingId, SEGMENT_LENGTH * 2L), Archive.segmentFileName(dstRecordingId, SEGMENT_LENGTH * 3L)));
        final Path dstFile = new File(archiveDir, Archive.segmentFileName(dstRecordingId, migratePosition)).toPath();
        final byte[] dstBytes = Files.readAllBytes(dstFile);
        assertEquals(SEGMENT_LENGTH, dstBytes.length);
        final long migratedSegments = aeronArchive.migrateSegments(srcRecordingId, dstRecordingId);
        assertEquals(2L, migratedSegments);
        assertEquals(startPosition, aeronArchive.getStartPosition(dstRecordingId));
        Tests.await(() -> {
            final String[] srcFiles = archiveDir.list((dir, name) -> name.startsWith(srcPrefix));
            return null != srcFiles && 0 == srcFiles.length;
        });
        dstFiles = archiveDir.list((dir, name) -> name.startsWith(dstPrefix));
        assertThat(dstFiles, arrayContainingInAnyOrder(Archive.segmentFileName(dstRecordingId, 0), Archive.segmentFileName(dstRecordingId, SEGMENT_LENGTH), Archive.segmentFileName(dstRecordingId, SEGMENT_LENGTH * 2L), Archive.segmentFileName(dstRecordingId, SEGMENT_LENGTH * 3L)));
        final byte[] migratedBytes = Files.readAllBytes(dstFile);
        assertArrayEquals(dstBytes, migratedBytes);
    }
}
Also used : Path(java.nio.file.Path) SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) BeforeEach(org.junit.jupiter.api.BeforeEach) FRAME_ALIGNMENT(io.aeron.logbuffer.FrameDescriptor.FRAME_ALIGNMENT) CountersReader(org.agrona.concurrent.status.CountersReader) RecordingSignalAdapter(io.aeron.archive.client.RecordingSignalAdapter) RecordingSignalConsumer(io.aeron.archive.client.RecordingSignalConsumer) MutableReference(org.agrona.collections.MutableReference) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) ChannelUriStringBuilder(io.aeron.ChannelUriStringBuilder) AeronArchive(io.aeron.archive.client.AeronArchive) ArchiveSystemTests(io.aeron.archive.ArchiveSystemTests) RecordingSignal(io.aeron.archive.codecs.RecordingSignal) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publication(io.aeron.Publication) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) Path(java.nio.file.Path) CloseHelper(org.agrona.CloseHelper) RecordingPos(io.aeron.archive.status.RecordingPos) MediaDriver(io.aeron.driver.MediaDriver) Aeron(io.aeron.Aeron) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) File(java.io.File) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter) AfterEach(org.junit.jupiter.api.AfterEach) ThreadingMode(io.aeron.driver.ThreadingMode) TempDir(org.junit.jupiter.api.io.TempDir) Assertions(org.junit.jupiter.api.Assertions) 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)

Example 22 with Publication

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

the class ManageRecordingHistoryTest method shouldMigrateSegmentsForStreamNotSegmentAligned.

@Test
@InterruptAfter(10)
void shouldMigrateSegmentsForStreamNotSegmentAligned() throws IOException {
    final int initialTermId = 7;
    final long targetPosition = (SEGMENT_LENGTH * 3L) + 1;
    final long startPosition = (TERM_LENGTH * 2L) + (FRAME_ALIGNMENT * 2L);
    uriBuilder.initialPosition(startPosition, initialTermId, TERM_LENGTH);
    try (Publication publication = aeronArchive.addRecordedExclusivePublication(uriBuilder.build(), STREAM_ID)) {
        final CountersReader counters = aeron.countersReader();
        final int dstCounterId = awaitRecordingCounterId(counters, publication.sessionId());
        final long dstRecordingId = RecordingPos.getRecordingId(counters, dstCounterId);
        offerToPosition(publication, "dst-message-", targetPosition);
        awaitPosition(counters, dstCounterId, publication.position());
        aeronArchive.stopRecording(publication);
        final long segmentFileBasePosition = AeronArchive.segmentFileBasePosition(startPosition, startPosition + (SEGMENT_LENGTH * 2L), TERM_LENGTH, SEGMENT_LENGTH);
        final long purgedSegments = aeronArchive.purgeSegments(dstRecordingId, segmentFileBasePosition);
        assertEquals(2L, purgedSegments);
        assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(dstRecordingId));
        final long srcRecordingId;
        final String migrateChannel = uriBuilder.initialPosition(startPosition, initialTermId, TERM_LENGTH).endpoint("localhost:4444").build();
        try (Publication migratePub = aeronArchive.addRecordedExclusivePublication(migrateChannel, STREAM_ID)) {
            final int srcCounterId = awaitRecordingCounterId(counters, migratePub.sessionId());
            srcRecordingId = RecordingPos.getRecordingId(counters, srcCounterId);
            offerToPosition(migratePub, "src-message-", segmentFileBasePosition);
            awaitPosition(counters, srcCounterId, migratePub.position());
            aeronArchive.stopRecording(migratePub);
        }
        final File archiveDir = archive.context().archiveDir();
        final String srcPrefix = srcRecordingId + "-";
        final String[] srcFiles = archiveDir.list((dir, name) -> name.startsWith(srcPrefix));
        assertThat(srcFiles, arrayContainingInAnyOrder(Archive.segmentFileName(srcRecordingId, SEGMENT_LENGTH), Archive.segmentFileName(srcRecordingId, SEGMENT_LENGTH * 2L), Archive.segmentFileName(srcRecordingId, SEGMENT_LENGTH * 3L)));
        final String dstPrefix = dstRecordingId + "-";
        String[] dstFiles = archiveDir.list((dir, name) -> name.startsWith(dstPrefix));
        assertThat(dstFiles, arrayContaining(Archive.segmentFileName(dstRecordingId, segmentFileBasePosition)));
        final byte[] srcBytes = Files.readAllBytes(new File(archiveDir, Archive.segmentFileName(srcRecordingId, segmentFileBasePosition)).toPath());
        assertEquals(SEGMENT_LENGTH, srcBytes.length);
        final byte[] dstBytes = Files.readAllBytes(new File(archiveDir, Archive.segmentFileName(dstRecordingId, segmentFileBasePosition)).toPath());
        assertEquals(SEGMENT_LENGTH, dstBytes.length);
        assertThat(srcBytes, not(equalTo(dstBytes)));
        final long migratedSegments = aeronArchive.migrateSegments(srcRecordingId, dstRecordingId);
        assertEquals(2L, migratedSegments);
        assertEquals(startPosition, aeronArchive.getStartPosition(dstRecordingId));
        assertEquals(startPosition, aeronArchive.getStopPosition(srcRecordingId));
        Tests.await(() -> {
            final String[] files = archiveDir.list((dir, name) -> name.startsWith(srcPrefix));
            return null != files && 0 == files.length;
        });
        dstFiles = archiveDir.list((dir, name) -> name.startsWith(dstPrefix));
        assertThat(dstFiles, arrayContainingInAnyOrder(Archive.segmentFileName(dstRecordingId, SEGMENT_LENGTH), Archive.segmentFileName(dstRecordingId, SEGMENT_LENGTH * 2L), Archive.segmentFileName(dstRecordingId, SEGMENT_LENGTH * 3L)));
        final byte[] migratedBytes = Files.readAllBytes(new File(archiveDir, Archive.segmentFileName(dstRecordingId, segmentFileBasePosition)).toPath());
        assertArrayEquals(dstBytes, migratedBytes);
    }
}
Also used : SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) BeforeEach(org.junit.jupiter.api.BeforeEach) FRAME_ALIGNMENT(io.aeron.logbuffer.FrameDescriptor.FRAME_ALIGNMENT) CountersReader(org.agrona.concurrent.status.CountersReader) RecordingSignalAdapter(io.aeron.archive.client.RecordingSignalAdapter) RecordingSignalConsumer(io.aeron.archive.client.RecordingSignalConsumer) MutableReference(org.agrona.collections.MutableReference) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) ChannelUriStringBuilder(io.aeron.ChannelUriStringBuilder) AeronArchive(io.aeron.archive.client.AeronArchive) ArchiveSystemTests(io.aeron.archive.ArchiveSystemTests) RecordingSignal(io.aeron.archive.codecs.RecordingSignal) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publication(io.aeron.Publication) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) Path(java.nio.file.Path) CloseHelper(org.agrona.CloseHelper) RecordingPos(io.aeron.archive.status.RecordingPos) MediaDriver(io.aeron.driver.MediaDriver) Aeron(io.aeron.Aeron) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) File(java.io.File) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter) AfterEach(org.junit.jupiter.api.AfterEach) ThreadingMode(io.aeron.driver.ThreadingMode) TempDir(org.junit.jupiter.api.io.TempDir) Assertions(org.junit.jupiter.api.Assertions) 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)

Example 23 with Publication

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

the class ManageRecordingHistoryTest method shouldDeleteDetachedSegmentsWhenStartNotSegmentAligned.

@Test
@InterruptAfter(10)
void shouldDeleteDetachedSegmentsWhenStartNotSegmentAligned() {
    final String messagePrefix = "Message-Prefix-";
    final int initialTermId = 7;
    final long targetPosition = (SEGMENT_LENGTH * 3L) + 1;
    final long startPosition = (TERM_LENGTH * 2L) + (FRAME_ALIGNMENT * 2L);
    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());
        aeronArchive.stopRecording(publication);
        final String prefix = recordingId + "-";
        final String[] files = archive.context().archiveDir().list((dir, name) -> name.startsWith(prefix));
        assertThat(files, arrayWithSize(3));
        final long segmentFileBasePosition = AeronArchive.segmentFileBasePosition(startPosition, startPosition + (SEGMENT_LENGTH * 2L), TERM_LENGTH, SEGMENT_LENGTH);
        aeronArchive.detachSegments(recordingId, segmentFileBasePosition);
        assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
        final long deletedSegments = aeronArchive.deleteDetachedSegments(recordingId);
        assertEquals(2L, deletedSegments);
        assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
        Tests.await(() -> {
            final String[] updatedFiles = archive.context().archiveDir().list((dir, name) -> name.startsWith(prefix));
            if (null != updatedFiles && 1 == updatedFiles.length) {
                assertThat(updatedFiles, arrayContaining(Archive.segmentFileName(recordingId, segmentFileBasePosition)));
                return true;
            }
            return false;
        });
    }
}
Also used : Publication(io.aeron.Publication) CountersReader(org.agrona.concurrent.status.CountersReader) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 24 with Publication

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

the class ManageRecordingHistoryTest method shouldDetachThenAttachFullSegments.

@Test
@InterruptAfter(10)
void shouldDetachThenAttachFullSegments() {
    final String messagePrefix = "Message-Prefix-";
    final long targetPosition = (SEGMENT_LENGTH * 3L) + 1;
    try (Publication publication = aeronArchive.addRecordedPublication(uriBuilder.build(), STREAM_ID)) {
        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());
        aeronArchive.stopRecording(publication);
        final long startPosition = 0L;
        final long segmentFileBasePosition = AeronArchive.segmentFileBasePosition(startPosition, SEGMENT_LENGTH * 2L, TERM_LENGTH, SEGMENT_LENGTH);
        aeronArchive.detachSegments(recordingId, segmentFileBasePosition);
        assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
        final long attachSegments = aeronArchive.attachSegments(recordingId);
        assertEquals(2L, attachSegments);
        assertEquals(startPosition, aeronArchive.getStartPosition(recordingId));
    }
}
Also used : Publication(io.aeron.Publication) CountersReader(org.agrona.concurrent.status.CountersReader) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 25 with Publication

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

the class ClusterTest method shouldRejectAnInvalidAdminRequest.

@Test
@InterruptAfter(10)
void shouldRejectAnInvalidAdminRequest() {
    final AdminRequestType invalidRequestType = AdminRequestType.NULL_VAL;
    final AtomicBoolean authorisationServiceCalled = new AtomicBoolean();
    cluster = aCluster().withStaticNodes(3).withAuthorisationServiceSupplier(() -> (protocolId, actionId, type, encodedPrincipal) -> {
        authorisationServiceCalled.set(true);
        assertEquals(MessageHeaderDecoder.SCHEMA_ID, protocolId);
        assertEquals(AdminRequestEncoder.TEMPLATE_ID, actionId);
        assertEquals(invalidRequestType, type);
        return true;
    }).start();
    systemTestWatcher.cluster(cluster);
    cluster.awaitLeader();
    final long requestCorrelationId = System.nanoTime();
    final MutableBoolean responseReceived = injectAdminResponseEgressListener(requestCorrelationId, invalidRequestType, AdminResponseCode.ERROR, "Unknown request type: " + invalidRequestType);
    final AeronCluster client = cluster.connectClient();
    final AdminRequestEncoder adminRequestEncoder = new AdminRequestEncoder().wrapAndApplyHeader(cluster.msgBuffer(), 0, new MessageHeaderEncoder()).leadershipTermId(client.leadershipTermId()).clusterSessionId(client.clusterSessionId()).correlationId(requestCorrelationId).requestType(invalidRequestType);
    final Publication ingressPublication = client.ingressPublication();
    while (ingressPublication.offer(adminRequestEncoder.buffer(), 0, MessageHeaderEncoder.ENCODED_LENGTH + adminRequestEncoder.encodedLength()) < 0) {
        Tests.yield();
    }
    Tests.await(authorisationServiceCalled::get);
    while (!responseReceived.get()) {
        client.pollEgress();
        Tests.yield();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MutableBoolean(org.agrona.collections.MutableBoolean) AeronCluster(io.aeron.cluster.client.AeronCluster) Publication(io.aeron.Publication) Test(org.junit.jupiter.api.Test)

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