Search in sources :

Example 16 with AeronArchive

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

the class ArchiveTest method shouldListRegisteredRecordingSubscriptions.

@Test
@InterruptAfter(10)
public void shouldListRegisteredRecordingSubscriptions() {
    final int expectedStreamId = 7;
    final String channelOne = "aeron:ipc";
    final String channelTwo = "aeron:udp?endpoint=localhost:5678";
    final String channelThree = "aeron:udp?endpoint=localhost:4321";
    final ArrayList<SubscriptionDescriptor> descriptors = new ArrayList<>();
    @SuppressWarnings("Indentation") final RecordingSubscriptionDescriptorConsumer consumer = (controlSessionId, correlationId, subscriptionId, streamId, strippedChannel) -> descriptors.add(new SubscriptionDescriptor(controlSessionId, correlationId, subscriptionId, streamId, strippedChannel));
    final MediaDriver.Context driverCtx = new MediaDriver.Context().dirDeleteOnStart(true).threadingMode(ThreadingMode.SHARED);
    final Archive.Context archiveCtx = new Archive.Context().threadingMode(SHARED);
    try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtx, archiveCtx);
        AeronArchive archive = AeronArchive.connect()) {
        final long subIdOne = archive.startRecording(channelOne, expectedStreamId, LOCAL);
        final long subIdTwo = archive.startRecording(channelTwo, expectedStreamId + 1, LOCAL);
        final long subOdThree = archive.startRecording(channelThree, expectedStreamId + 2, LOCAL);
        final int countOne = archive.listRecordingSubscriptions(0, 5, "ipc", expectedStreamId, true, consumer);
        assertEquals(1, descriptors.size());
        assertEquals(1, countOne);
        descriptors.clear();
        final int countTwo = archive.listRecordingSubscriptions(0, 5, "", expectedStreamId, false, consumer);
        assertEquals(3, descriptors.size());
        assertEquals(3, countTwo);
        archive.stopRecording(subIdTwo);
        descriptors.clear();
        final int countThree = archive.listRecordingSubscriptions(0, 5, "", expectedStreamId, false, consumer);
        assertEquals(2, descriptors.size());
        assertEquals(2, countThree);
        assertEquals(1L, descriptors.stream().filter((sd) -> sd.subscriptionId == subIdOne).count());
        assertEquals(1L, descriptors.stream().filter((sd) -> sd.subscriptionId == subOdThree).count());
    } finally {
        archiveCtx.deleteDirectory();
        driverCtx.deleteDirectory();
    }
}
Also used : RecordingSubscriptionDescriptorConsumer(io.aeron.archive.client.RecordingSubscriptionDescriptorConsumer) IntConsumer(java.util.function.IntConsumer) LOCAL(io.aeron.archive.codecs.SourceLocation.LOCAL) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ChannelUriStringBuilder(io.aeron.ChannelUriStringBuilder) AeronArchive(io.aeron.archive.client.AeronArchive) DEDICATED(io.aeron.archive.ArchiveThreadingMode.DEDICATED) Publication(io.aeron.Publication) MediaDriver(io.aeron.driver.MediaDriver) ManyToOneConcurrentLinkedQueue(org.agrona.concurrent.ManyToOneConcurrentLinkedQueue) AuthorisationServiceSupplier(io.aeron.security.AuthorisationServiceSupplier) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter) CountDownLatch(java.util.concurrent.CountDownLatch) TruncateRecordingRequestDecoder(io.aeron.archive.codecs.TruncateRecordingRequestDecoder) EXCLUDE(org.junit.jupiter.params.provider.EnumSource.Mode.EXCLUDE) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) DirectBuffer(org.agrona.DirectBuffer) Mockito.mock(org.mockito.Mockito.mock) Tests(io.aeron.test.Tests) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) CountersReader(org.agrona.concurrent.status.CountersReader) EnumSource(org.junit.jupiter.params.provider.EnumSource) ArrayList(java.util.ArrayList) SystemEpochClock(org.agrona.concurrent.SystemEpochClock) Context(io.aeron.archive.Archive.Context) FrameDescriptor(io.aeron.logbuffer.FrameDescriptor) ValueSource(org.junit.jupiter.params.provider.ValueSource) RecordingPos(io.aeron.archive.status.RecordingPos) Aeron(io.aeron.Aeron) FileStore(java.nio.file.FileStore) SHARED(io.aeron.archive.ArchiveThreadingMode.SHARED) AeronArchive.segmentFileBasePosition(io.aeron.archive.client.AeronArchive.segmentFileBasePosition) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) CommonContext(io.aeron.CommonContext) Checksum(io.aeron.archive.checksum.Checksum) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MIN_CAPACITY(io.aeron.archive.Catalog.MIN_CAPACITY) ArchiveException(io.aeron.archive.client.ArchiveException) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions(org.junit.jupiter.api.Assertions) Context(io.aeron.archive.Archive.Context) CommonContext(io.aeron.CommonContext) AeronArchive(io.aeron.archive.client.AeronArchive) ArrayList(java.util.ArrayList) AeronArchive(io.aeron.archive.client.AeronArchive) RecordingSubscriptionDescriptorConsumer(io.aeron.archive.client.RecordingSubscriptionDescriptorConsumer) MediaDriver(io.aeron.driver.MediaDriver) Context(io.aeron.archive.Archive.Context) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) InterruptAfter(io.aeron.test.InterruptAfter)

Example 17 with AeronArchive

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

the class RecordingLogTest method shouldIgnoreInvalidTermInRecoveryPlan.

@Test
void shouldIgnoreInvalidTermInRecoveryPlan() {
    final int serviceCount = 1;
    final long removedLeadershipTerm = 11L;
    try (RecordingLog recordingLog = new RecordingLog(tempDir, true)) {
        recordingLog.appendTerm(0L, 9L, 444, 0);
        recordingLog.appendTerm(0L, 10L, 666, 0);
        recordingLog.appendSnapshot(1L, 10L, 666, 777L, 0, 0);
        recordingLog.appendSnapshot(2L, 10L, 666, 777L, 0, SERVICE_ID);
        recordingLog.appendSnapshot(3L, 10L, 666, 888L, 0, 0);
        recordingLog.appendSnapshot(4L, 10L, 666, 888L, 0, SERVICE_ID);
        recordingLog.appendTerm(0L, removedLeadershipTerm, 999, 0);
        final RecordingLog.Entry lastTerm = recordingLog.findLastTerm();
        assertNotNull(lastTerm);
        assertEquals(999L, lastTerm.termBaseLogPosition);
        recordingLog.invalidateEntry(removedLeadershipTerm, 6);
    }
    try (RecordingLog recordingLog = new RecordingLog(tempDir, true)) {
        final AeronArchive mockArchive = mock(AeronArchive.class);
        when(mockArchive.listRecording(anyLong(), any())).thenReturn(1);
        final RecordingLog.RecoveryPlan recoveryPlan = recordingLog.createRecoveryPlan(mockArchive, serviceCount, Aeron.NULL_VALUE);
        assertEquals(0L, recoveryPlan.log.recordingId);
        assertEquals(10L, recoveryPlan.log.leadershipTermId);
        assertEquals(666, recoveryPlan.log.termBaseLogPosition);
        final RecordingLog.Entry lastTerm = recordingLog.findLastTerm();
        assertNotNull(lastTerm);
        assertEquals(0L, lastTerm.recordingId);
        assertEquals(0L, recordingLog.findLastTermRecordingId());
        assertTrue(recordingLog.isUnknown(removedLeadershipTerm));
        assertEquals(NULL_VALUE, recordingLog.getTermTimestamp(removedLeadershipTerm));
        assertThrows(ClusterException.class, () -> recordingLog.getTermEntry(removedLeadershipTerm));
        assertThrows(ClusterException.class, () -> recordingLog.commitLogPosition(removedLeadershipTerm, 99L));
    }
}
Also used : AeronArchive(io.aeron.archive.client.AeronArchive) Test(org.junit.jupiter.api.Test)

Example 18 with AeronArchive

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

the class RecordingLogTest method shouldIgnoreIncompleteSnapshotInRecoveryPlan.

@Test
void shouldIgnoreIncompleteSnapshotInRecoveryPlan() {
    final int serviceCount = 1;
    try (RecordingLog recordingLog = new RecordingLog(tempDir, true)) {
        recordingLog.appendSnapshot(1L, 1L, 0, 777L, 0, 0);
        recordingLog.appendSnapshot(2L, 1L, 0, 777L, 0, SERVICE_ID);
        recordingLog.appendSnapshot(3L, 1L, 0, 888L, 0, 0);
    }
    try (RecordingLog recordingLog = new RecordingLog(tempDir, true)) {
        assertEquals(3, recordingLog.entries().size());
        final AeronArchive mockArchive = mock(AeronArchive.class);
        final RecordingLog.RecoveryPlan recoveryPlan = recordingLog.createRecoveryPlan(mockArchive, serviceCount, Aeron.NULL_VALUE);
        assertEquals(2, recoveryPlan.snapshots.size());
        assertEquals(SERVICE_ID, recoveryPlan.snapshots.get(0).serviceId);
        assertEquals(2L, recoveryPlan.snapshots.get(0).recordingId);
        assertEquals(0, recoveryPlan.snapshots.get(1).serviceId);
        assertEquals(1L, recoveryPlan.snapshots.get(1).recordingId);
    }
}
Also used : AeronArchive(io.aeron.archive.client.AeronArchive) Test(org.junit.jupiter.api.Test)

Example 19 with AeronArchive

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

the class RecordedBasicPublisher method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 * @throws InterruptedException if the thread sleep delay is interrupted.
 */
public static void main(final String[] args) throws InterruptedException {
    System.out.println("Publishing to " + CHANNEL + " on stream id " + STREAM_ID);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    // Create a unique response stream id so not to clash with other archive clients.
    final AeronArchive.Context archiveCtx = new AeronArchive.Context().controlResponseStreamId(AeronArchive.Configuration.controlResponseStreamId() + 1);
    try (AeronArchive archive = AeronArchive.connect(archiveCtx)) {
        archive.startRecording(CHANNEL, STREAM_ID, SourceLocation.LOCAL);
        try (Publication publication = archive.context().aeron().addPublication(CHANNEL, STREAM_ID)) {
            final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;
            // Wait for recording to have started before publishing.
            final CountersReader counters = archive.context().aeron().countersReader();
            int counterId = RecordingPos.findCounterIdBySession(counters, publication.sessionId());
            while (CountersReader.NULL_COUNTER_ID == counterId) {
                if (!running.get()) {
                    return;
                }
                idleStrategy.idle();
                counterId = RecordingPos.findCounterIdBySession(counters, publication.sessionId());
            }
            final long recordingId = RecordingPos.getRecordingId(counters, counterId);
            System.out.println("Recording started: recordingId = " + recordingId);
            for (int i = 0; i < NUMBER_OF_MESSAGES && running.get(); i++) {
                final String message = "Hello World! " + i;
                final byte[] messageBytes = message.getBytes();
                BUFFER.putBytes(0, messageBytes);
                System.out.print("Offering " + i + "/" + NUMBER_OF_MESSAGES + " - ");
                final long result = publication.offer(BUFFER, 0, messageBytes.length);
                checkResult(result);
                final String errorMessage = archive.pollForErrorResponse();
                if (null != errorMessage) {
                    throw new IllegalStateException(errorMessage);
                }
                Thread.sleep(TimeUnit.SECONDS.toMillis(1));
            }
            idleStrategy.reset();
            while (counters.getCounterValue(counterId) < publication.position()) {
                if (!RecordingPos.isActive(counters, counterId, recordingId)) {
                    throw new IllegalStateException("recording has stopped unexpectedly: " + recordingId);
                }
                idleStrategy.idle();
            }
        } finally {
            System.out.println("Done sending.");
            archive.stopRecording(CHANNEL, STREAM_ID);
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Publication(io.aeron.Publication) AeronArchive(io.aeron.archive.client.AeronArchive) CountersReader(org.agrona.concurrent.status.CountersReader)

Example 20 with AeronArchive

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

the class BasicArchiveTest method before.

@BeforeEach
public void before() {
    final String aeronDirectoryName = CommonContext.generateRandomDirName();
    final MediaDriver.Context driverCtx = new MediaDriver.Context().aeronDirectoryName(aeronDirectoryName).termBufferSparseFile(true).threadingMode(ThreadingMode.SHARED).spiesSimulateConnection(false).dirDeleteOnStart(true);
    archiveDir = new File(SystemUtil.tmpDirName(), "archive");
    final Archive.Context archiveCtx = new Archive.Context().catalogCapacity(CATALOG_CAPACITY).aeronDirectoryName(aeronDirectoryName).deleteArchiveOnStart(true).archiveDir(archiveDir).fileSyncLevel(0).threadingMode(ArchiveThreadingMode.SHARED);
    try {
        driver = TestMediaDriver.launch(driverCtx, systemTestWatcher);
        archive = Archive.launch(archiveCtx);
    } finally {
        systemTestWatcher.dataCollector().add(driverCtx.aeronDirectory());
        systemTestWatcher.dataCollector().add(archiveCtx.archiveDir());
    }
    aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(aeronDirectoryName));
    aeronArchive = AeronArchive.connect(new AeronArchive.Context().aeron(aeron));
}
Also used : AeronArchive(io.aeron.archive.client.AeronArchive) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) MediaDriver(io.aeron.driver.MediaDriver) File(java.io.File) AeronArchive(io.aeron.archive.client.AeronArchive) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

AeronArchive (io.aeron.archive.client.AeronArchive)54 Test (org.junit.jupiter.api.Test)28 MediaDriver (io.aeron.driver.MediaDriver)26 File (java.io.File)17 Aeron (io.aeron.Aeron)14 CommonContext (io.aeron.CommonContext)12 Context (io.aeron.archive.Archive.Context)12 InterruptAfter (io.aeron.test.InterruptAfter)12 Tests (io.aeron.test.Tests)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 Archive (io.aeron.archive.Archive)10 ArchiveException (io.aeron.archive.client.ArchiveException)10 CountersReader (org.agrona.concurrent.status.CountersReader)10 Publication (io.aeron.Publication)8 FileStore (java.nio.file.FileStore)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)8 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)8 ChannelUriStringBuilder (io.aeron.ChannelUriStringBuilder)7 DEDICATED (io.aeron.archive.ArchiveThreadingMode.DEDICATED)6