use of io.aeron.Publication in project aeron by real-logic.
the class ArchiveTest method shouldRecoverRecordingWithNonZeroStartPosition.
@Test
public void shouldRecoverRecordingWithNonZeroStartPosition() {
final MediaDriver.Context driverCtx = new MediaDriver.Context().dirDeleteOnStart(true).threadingMode(ThreadingMode.SHARED);
final Archive.Context archiveCtx = new Archive.Context().threadingMode(SHARED);
long resultingPosition;
final int initialPosition = DataHeaderFlyweight.HEADER_LENGTH * 9;
final long recordingId;
try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtx.clone(), archiveCtx.clone());
AeronArchive archive = AeronArchive.connect()) {
final int termLength = 128 * 1024;
final int initialTermId = 29;
final String channel = new ChannelUriStringBuilder().media(CommonContext.IPC_MEDIA).initialPosition(initialPosition, initialTermId, termLength).build();
final Publication publication = archive.addRecordedExclusivePublication(channel, 1);
final DirectBuffer buffer = new UnsafeBuffer("Hello World".getBytes(StandardCharsets.US_ASCII));
while ((resultingPosition = publication.offer(buffer)) <= 0) {
Tests.yield();
}
final Aeron aeron = archive.context().aeron();
int counterId;
final int sessionId = publication.sessionId();
final CountersReader countersReader = aeron.countersReader();
while (Aeron.NULL_VALUE == (counterId = RecordingPos.findCounterIdBySession(countersReader, sessionId))) {
Tests.yield();
}
recordingId = RecordingPos.getRecordingId(countersReader, counterId);
while (countersReader.getCounterValue(counterId) < resultingPosition) {
Tests.yield();
}
}
try (Catalog catalog = openCatalog(archiveCtx)) {
final Catalog.CatalogEntryProcessor catalogEntryProcessor = (recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> descriptorEncoder.stopPosition(Aeron.NULL_VALUE);
assertTrue(catalog.forEntry(recordingId, catalogEntryProcessor));
}
final Archive.Context archiveCtxClone = archiveCtx.clone();
final MediaDriver.Context driverCtxClone = driverCtx.clone();
try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtxClone, archiveCtxClone);
AeronArchive archive = AeronArchive.connect()) {
assertEquals(initialPosition, archive.getStartPosition(recordingId));
assertEquals(resultingPosition, archive.getStopPosition(recordingId));
} finally {
archiveCtxClone.deleteDirectory();
driverCtxClone.deleteDirectory();
}
}
use of io.aeron.Publication in project aeron by real-logic.
the class RecordingDescriptorCollectorTest method createRecordings.
private void createRecordings(final AeronArchive aeronArchive, final int numRecordings) {
final UnsafeBuffer message = new UnsafeBuffer("this is some data".getBytes());
for (int i = 0; i < numRecordings; i++) {
try (Publication publication = aeronArchive.addRecordedPublication("aeron:ipc?ssc=true", 10000 + i)) {
long expectedPosition;
while ((expectedPosition = publication.offer(message, 0, message.capacity())) < 0) {
Tests.yield();
}
long recordingId;
while ((recordingId = aeronArchive.findLastMatchingRecording(0, "aeron:ipc", publication.streamId(), publication.sessionId())) == Aeron.NULL_VALUE) {
Tests.yield();
}
while (expectedPosition < aeronArchive.getRecordingPosition(recordingId)) {
Tests.yield();
}
}
}
}
use of io.aeron.Publication in project aeron by real-logic.
the class FileSender method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
* @throws InterruptedException if the thread is interrupted when sleeping.
*/
public static void main(final String[] args) throws InterruptedException {
if (args.length != 1) {
System.out.println("Filename to be sent must be supplied as a command line argument");
System.exit(1);
}
try (Aeron aeron = Aeron.connect();
Publication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID)) {
while (!publication.isConnected()) {
Thread.sleep(1);
}
final File file = new File(args[0]);
final UnsafeBuffer buffer = new UnsafeBuffer(IoUtil.mapExistingFile(file, "sending"));
final long correlationId = aeron.nextCorrelationId();
sendFileCreate(publication, correlationId, buffer.capacity(), file.getName());
streamChunks(publication, correlationId, buffer);
}
}
use of io.aeron.Publication in project aeron by real-logic.
the class ManageRecordingHistoryTest method shouldDetachThenAttachWhenStartNotSegmentAligned.
@Test
@InterruptAfter(10)
void shouldDetachThenAttachWhenStartNotSegmentAligned() {
final String messagePrefix = "Message-Prefix-";
final int initialTermId = 7;
final long targetPosition = (SEGMENT_LENGTH * 3L) + 1;
final long startPosition = (TERM_LENGTH * 2L) + (FRAME_ALIGNMENT * 2L);
uriBuilder.initialPosition(startPosition, initialTermId, TERM_LENGTH);
try (Publication publication = aeronArchive.addRecordedExclusivePublication(uriBuilder.build(), STREAM_ID)) {
assertEquals(startPosition, publication.position());
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());
aeronArchive.stopRecording(publication);
final long segmentFileBasePosition = AeronArchive.segmentFileBasePosition(startPosition, startPosition + (SEGMENT_LENGTH * 2L), TERM_LENGTH, SEGMENT_LENGTH);
aeronArchive.detachSegments(recordingId, segmentFileBasePosition);
assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
final long attachSegments = aeronArchive.attachSegments(recordingId);
assertEquals(2L, attachSegments);
assertEquals(startPosition, aeronArchive.getStartPosition(recordingId));
}
}
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));
}
}
Aggregations