Search in sources :

Example 56 with Publication

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

the class ManageRecordingHistoryTest method shouldPurgeRecording.

@Test
@InterruptAfter(10)
void shouldPurgeRecording() throws IOException {
    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 newRecordingId = RecordingPos.getRecordingId(counters, counterId);
        offerToPosition(publication, messagePrefix, targetPosition);
        awaitPosition(counters, counterId, publication.position());
        final MutableReference<RecordingSignal> signalRef = new MutableReference<>();
        final RecordingSignalConsumer consumer = (controlSessionId, correlationId, recordingId, subscriptionId, position, transitionType) -> {
            if (newRecordingId == recordingId) {
                signalRef.set(transitionType);
            }
        };
        final RecordingSignalAdapter adapter = new RecordingSignalAdapter(aeronArchive.controlSessionId(), ERROR_CONTROL_LISTENER, consumer, aeronArchive.controlResponsePoller().subscription(), FRAGMENT_LIMIT);
        aeronArchive.stopRecording(publication);
        assertEquals(RecordingSignal.STOP, awaitSignal(signalRef, adapter));
        final String prefix = newRecordingId + "-";
        final File archiveDir = archive.context().archiveDir();
        assertTrue(new File(archiveDir, prefix + (SEGMENT_LENGTH * 4L) + ".rec").createNewFile());
        assertTrue(new File(archiveDir, prefix + (SEGMENT_LENGTH * 5L) + ".rec.del").createNewFile());
        aeronArchive.purgeRecording(newRecordingId);
        assertEquals(RecordingSignal.DELETE, awaitSignal(signalRef, adapter));
        assertThat(archiveDir.list(((dir, name) -> name.startsWith(prefix))), arrayWithSize(0));
    }
}
Also used : RecordingSignalConsumer(io.aeron.archive.client.RecordingSignalConsumer) 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) RecordingSignal(io.aeron.archive.codecs.RecordingSignal) Publication(io.aeron.Publication) MutableReference(org.agrona.collections.MutableReference) RecordingSignalAdapter(io.aeron.archive.client.RecordingSignalAdapter) File(java.io.File) CountersReader(org.agrona.concurrent.status.CountersReader) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 57 with Publication

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

the class ManageRecordingHistoryTest method shouldPurgeForStreamJoinedAtTheBeginning.

@Test
@InterruptAfter(10)
void shouldPurgeForStreamJoinedAtTheBeginning() {
    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());
        final long startPosition = 0L;
        final long segmentFileBasePosition = AeronArchive.segmentFileBasePosition(startPosition, SEGMENT_LENGTH * 2L, TERM_LENGTH, SEGMENT_LENGTH);
        final long count = aeronArchive.purgeSegments(recordingId, segmentFileBasePosition);
        assertEquals(2L, count);
        assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
        aeronArchive.stopRecording(publication);
    }
}
Also used : Publication(io.aeron.Publication) CountersReader(org.agrona.concurrent.status.CountersReader) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 58 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 59 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)

Example 60 with Publication

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

the class ClusterTest method shouldRejectAnAdminRequestIfLeadershipTermIsInvalid.

@Test
@InterruptAfter(10)
void shouldRejectAnAdminRequestIfLeadershipTermIsInvalid() {
    cluster = aCluster().withStaticNodes(3).start();
    systemTestWatcher.cluster(cluster);
    cluster.awaitLeader();
    AeronCluster client = cluster.connectClient();
    final long requestCorrelationId = System.nanoTime();
    final long expectedLeadershipTermId = client.leadershipTermId();
    final long invalidLeadershipTermId = expectedLeadershipTermId - 1000;
    final AdminRequestType requestType = AdminRequestType.NULL_VAL;
    final MutableBoolean responseReceived = injectAdminResponseEgressListener(requestCorrelationId, requestType, AdminResponseCode.ERROR, "Invalid leadership term: expected " + expectedLeadershipTermId + ", got " + invalidLeadershipTermId);
    client = cluster.connectClient();
    final AdminRequestEncoder adminRequestEncoder = new AdminRequestEncoder().wrapAndApplyHeader(cluster.msgBuffer(), 0, new MessageHeaderEncoder()).leadershipTermId(invalidLeadershipTermId).clusterSessionId(client.clusterSessionId()).correlationId(requestCorrelationId).requestType(requestType);
    final Publication ingressPublication = client.ingressPublication();
    while (ingressPublication.offer(adminRequestEncoder.buffer(), 0, MessageHeaderEncoder.ENCODED_LENGTH + adminRequestEncoder.encodedLength()) < 0) {
        Tests.yield();
    }
    while (!responseReceived.get()) {
        client.pollEgress();
        Tests.yield();
    }
}
Also used : AeronCluster(io.aeron.cluster.client.AeronCluster) MutableBoolean(org.agrona.collections.MutableBoolean) 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