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;
}
}
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);
}
}
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();
}
}
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...");
}
}
}
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);
}
Aggregations