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