Search in sources :

Example 6 with AeronArchive

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

the class ClusteredServiceAgent method onTakeSnapshot.

private long onTakeSnapshot(final long logPosition, final long leadershipTermId) {
    try (AeronArchive archive = AeronArchive.connect(ctx.archiveContext().clone());
        ExclusivePublication publication = aeron.addExclusivePublication(ctx.snapshotChannel(), ctx.snapshotStreamId())) {
        final String channel = ChannelUri.addSessionId(ctx.snapshotChannel(), publication.sessionId());
        archive.startRecording(channel, ctx.snapshotStreamId(), LOCAL, true);
        final CountersReader counters = aeron.countersReader();
        final int counterId = awaitRecordingCounter(publication.sessionId(), counters, archive);
        final long recordingId = RecordingPos.getRecordingId(counters, counterId);
        snapshotState(publication, logPosition, leadershipTermId);
        checkForClockTick();
        archive.checkForErrorResponse();
        service.onTakeSnapshot(publication);
        awaitRecordingComplete(recordingId, publication.position(), counters, counterId, archive);
        return recordingId;
    } catch (final ArchiveException ex) {
        if (ex.errorCode() == ArchiveException.STORAGE_SPACE) {
            throw new AgentTerminationException(ex);
        }
        throw ex;
    }
}
Also used : ArchiveException(io.aeron.archive.client.ArchiveException) AeronArchive(io.aeron.archive.client.AeronArchive) CountersReader(org.agrona.concurrent.status.CountersReader)

Example 7 with AeronArchive

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

the class RecordingLogTest method shouldIgnoreInvalidMidSnapshotInRecoveryPlan.

@Test
void shouldIgnoreInvalidMidSnapshotInRecoveryPlan() {
    final int serviceCount = 1;
    try (RecordingLog recordingLog = new RecordingLog(tempDir, true)) {
        recordingLog.appendSnapshot(1, 1L, 0, 777L, 0, 0);
        recordingLog.appendSnapshot(2, 1L, 0, 777L, 0, SERVICE_ID);
        recordingLog.appendSnapshot(3, 1L, 0, 888L, 0, 0);
        recordingLog.appendSnapshot(4, 1L, 0, 888L, 0, SERVICE_ID);
        recordingLog.appendSnapshot(5, 1L, 0, 999L, 0, 0);
        recordingLog.appendSnapshot(6, 1L, 0, 999L, 0, SERVICE_ID);
        recordingLog.invalidateEntry(1L, 2);
        recordingLog.invalidateEntry(1L, 3);
    }
    try (RecordingLog recordingLog = new RecordingLog(tempDir, true)) {
        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(6L, recoveryPlan.snapshots.get(0).recordingId);
        assertEquals(0, recoveryPlan.snapshots.get(1).serviceId);
        assertEquals(5L, recoveryPlan.snapshots.get(1).recordingId);
    }
}
Also used : AeronArchive(io.aeron.archive.client.AeronArchive) Test(org.junit.jupiter.api.Test)

Example 8 with AeronArchive

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

the class ArchiveCreator method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
@SuppressWarnings("try")
public static void main(final String[] args) {
    final String archiveDirName = Archive.Configuration.archiveDirName();
    final File archiveDir = ARCHIVE_DIR_DEFAULT.equals(archiveDirName) ? new File("archive") : new File(archiveDirName);
    final MediaDriver.Context driverContext = new MediaDriver.Context().publicationTermBufferLength(TERM_LENGTH).termBufferSparseFile(true).threadingMode(ThreadingMode.SHARED).errorHandler(Throwable::printStackTrace).spiesSimulateConnection(true).dirDeleteOnStart(true);
    final Archive.Context archiveContext = new Archive.Context().catalogCapacity(CATALOG_CAPACITY).segmentFileLength(SEGMENT_LENGTH).deleteArchiveOnStart(true).archiveDir(archiveDir).fileSyncLevel(0).threadingMode(ArchiveThreadingMode.SHARED);
    System.out.println("Creating basic archive at " + archiveContext.archiveDir());
    try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverContext, archiveContext);
        Aeron aeron = Aeron.connect();
        AeronArchive aeronArchive = AeronArchive.connect(new AeronArchive.Context().aeron(aeron))) {
        createRecording(aeron, aeronArchive, 0, (SEGMENT_LENGTH * 5L) + 1);
        createRecording(aeron, aeronArchive, (long) TERM_LENGTH + (FrameDescriptor.FRAME_ALIGNMENT * 2), (SEGMENT_LENGTH * 3L) + 1);
    } catch (final Exception ex) {
        ex.printStackTrace();
    }
}
Also used : Archive(io.aeron.archive.Archive) AeronArchive(io.aeron.archive.client.AeronArchive) MediaDriver(io.aeron.driver.MediaDriver) ArchivingMediaDriver(io.aeron.archive.ArchivingMediaDriver) File(java.io.File) ArchivingMediaDriver(io.aeron.archive.ArchivingMediaDriver) AeronArchive(io.aeron.archive.client.AeronArchive) Aeron(io.aeron.Aeron)

Example 9 with AeronArchive

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

the class ReplayedBasicSubscriber method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
public static void main(final String[] args) {
    System.out.println("Subscribing to " + CHANNEL + " on stream id " + STREAM_ID);
    final FragmentHandler fragmentHandler = SamplesUtil.printAsciiMessage(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() + 2);
    try (AeronArchive archive = AeronArchive.connect(archiveCtx)) {
        final long recordingId = findLatestRecording(archive);
        final long position = 0L;
        final long length = Long.MAX_VALUE;
        final long sessionId = archive.startReplay(recordingId, position, length, CHANNEL, REPLAY_STREAM_ID);
        final String channel = ChannelUri.addSessionId(CHANNEL, (int) sessionId);
        try (Subscription subscription = archive.context().aeron().addSubscription(channel, REPLAY_STREAM_ID)) {
            SamplesUtil.subscriberLoop(fragmentHandler, FRAGMENT_COUNT_LIMIT, running).accept(subscription);
            System.out.println("Shutting down...");
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FragmentHandler(io.aeron.logbuffer.FragmentHandler) Subscription(io.aeron.Subscription) AeronArchive(io.aeron.archive.client.AeronArchive)

Example 10 with AeronArchive

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

the class ArchiveDeleteAndRestartTest method recordAndReplayExclusivePublication.

@InterruptAfter(10)
@Test
public void recordAndReplayExclusivePublication() {
    final UnsafeBuffer buffer = new UnsafeBuffer(new byte[1024]);
    buffer.setMemory(0, buffer.capacity(), (byte) 'z');
    AeronArchive aeronArchive = AeronArchive.connect(new AeronArchive.Context().aeron(client));
    final String uri = "aeron:ipc?term-length=16m|init-term-id=502090867|term-offset=0|term-id=502090867";
    final ExclusivePublication recordedPublication1 = client.addExclusivePublication(uri, STREAM_ID);
    final long subscriptionId = aeronArchive.startRecording(uri, STREAM_ID, SourceLocation.LOCAL);
    for (int i = 0; i < 10; i++) {
        while (recordedPublication1.offer(buffer, 0, 1024) < 0) {
            Tests.yieldingIdle("Failed to offer data");
        }
    }
    final long position1 = recordedPublication1.position();
    final RecordingDescriptorCollector collector = new RecordingDescriptorCollector(10);
    while (aeronArchive.listRecordings(0, Integer.MAX_VALUE, collector.reset()) < 1) {
        Tests.yieldingIdle("Didn't find recording");
    }
    while (position1 != aeronArchive.getRecordingPosition(collector.descriptors().get(0).recordingId())) {
        Tests.yieldingIdle("Failed to record data");
    }
    recordedPublication1.close();
    aeronArchive.stopRecording(subscriptionId);
    while (position1 != aeronArchive.getStopPosition(collector.descriptors().get(0).recordingId())) {
        Tests.yieldingIdle("Failed to stop recording");
    }
    aeronArchive.close();
    archive.close();
    archive.context().deleteDirectory();
    archive = Archive.launch(archiveContext.clone());
    aeronArchive = AeronArchive.connect(new AeronArchive.Context().aeron(client));
    final ExclusivePublication recordedPublication2 = client.addExclusivePublication(uri, STREAM_ID);
    aeronArchive.startRecording(uri, STREAM_ID, SourceLocation.LOCAL);
    for (int i = 0; i < 10; i++) {
        while (recordedPublication2.offer(buffer, 0, 1024) < 0) {
            Tests.yieldingIdle("Failed to offer data");
        }
    }
    while (aeronArchive.listRecordings(0, Integer.MAX_VALUE, collector.reset()) < 1) {
        Tests.yieldingIdle("Didn't find recording");
    }
    assertEquals(1, aeronArchive.listRecordings(0, Integer.MAX_VALUE, collector.reset()), collector.descriptors()::toString);
}
Also used : RecordingDescriptorCollector(io.aeron.samples.archive.RecordingDescriptorCollector) ExclusivePublication(io.aeron.ExclusivePublication) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) AeronArchive(io.aeron.archive.client.AeronArchive) InterruptAfter(io.aeron.test.InterruptAfter) Test(org.junit.jupiter.api.Test)

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