use of io.aeron.Publication in project Aeron by real-logic.
the class ManageRecordingHistoryTest method shouldDeleteDetachedFullSegments.
@Test
@InterruptAfter(10)
void shouldDeleteDetachedFullSegments() {
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 recordingId = RecordingPos.getRecordingId(counters, counterId);
offerToPosition(publication, messagePrefix, targetPosition);
awaitPosition(counters, counterId, publication.position());
aeronArchive.stopRecording(publication);
final String prefix = recordingId + "-";
final File archiveDir = archive.context().archiveDir();
final String[] files = archiveDir.list((dir, name) -> name.startsWith(prefix));
assertThat(files, arrayWithSize(4));
final long startPosition = 0L;
final long segmentFileBasePosition = AeronArchive.segmentFileBasePosition(startPosition, SEGMENT_LENGTH * 2L, TERM_LENGTH, SEGMENT_LENGTH);
aeronArchive.detachSegments(recordingId, segmentFileBasePosition);
assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
final long deletedSegments = aeronArchive.deleteDetachedSegments(recordingId);
assertEquals(2L, deletedSegments);
assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
Tests.await(() -> {
final String[] updatedFiles = archiveDir.list((dir, name) -> name.startsWith(prefix));
if (null != updatedFiles && 2 == updatedFiles.length) {
assertThat(updatedFiles, arrayContainingInAnyOrder(Archive.segmentFileName(recordingId, segmentFileBasePosition), Archive.segmentFileName(recordingId, segmentFileBasePosition + SEGMENT_LENGTH)));
return true;
}
return false;
});
}
}
use of io.aeron.Publication 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.Publication in project aeron by real-logic.
the class DriverLoggingAgentTest method testLogMediaDriverEvents.
private void testLogMediaDriverEvents(final String channel, final String enabledEvents, final EnumSet<DriverEventCode> expectedEvents) {
before(enabledEvents, expectedEvents);
final MediaDriver.Context driverCtx = new MediaDriver.Context().errorHandler(Tests::onError).publicationLingerTimeoutNs(0).timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(1));
try (MediaDriver mediaDriver = MediaDriver.launch(driverCtx)) {
try (Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
Publication publication = aeron.addPublication(channel, STREAM_ID)) {
final UnsafeBuffer offerBuffer = new UnsafeBuffer(new byte[32]);
while (publication.offer(offerBuffer) < 0) {
Tests.yield();
}
final MutableInteger counter = new MutableInteger();
final FragmentHandler handler = (buffer, offset, length, header) -> counter.value++;
while (0 == subscription.poll(handler, 1)) {
Tests.yield();
}
assertEquals(counter.get(), 1);
}
final Supplier<String> errorMessage = () -> "Pending events: " + WAIT_LIST;
while (!WAIT_LIST.isEmpty()) {
Tests.yieldingIdle(errorMessage);
}
}
}
use of io.aeron.Publication in project aeron by real-logic.
the class ManageRecordingHistoryTest method shouldPurgeForStreamJoinedAtTheBeginning.
@Test
@InterruptAfter(10)
void shouldPurgeForStreamJoinedAtTheBeginning() {
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 recordingId = RecordingPos.getRecordingId(counters, counterId);
offerToPosition(publication, messagePrefix, targetPosition);
awaitPosition(counters, counterId, publication.position());
final long startPosition = 0L;
final long segmentFileBasePosition = AeronArchive.segmentFileBasePosition(startPosition, SEGMENT_LENGTH * 2L, TERM_LENGTH, SEGMENT_LENGTH);
final long count = aeronArchive.purgeSegments(recordingId, segmentFileBasePosition);
assertEquals(2L, count);
assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
aeronArchive.stopRecording(publication);
}
}
use of io.aeron.Publication in project aeron by real-logic.
the class ManageRecordingHistoryTest method shouldDeleteDetachedFullSegments.
@Test
@InterruptAfter(10)
void shouldDeleteDetachedFullSegments() {
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 recordingId = RecordingPos.getRecordingId(counters, counterId);
offerToPosition(publication, messagePrefix, targetPosition);
awaitPosition(counters, counterId, publication.position());
aeronArchive.stopRecording(publication);
final String prefix = recordingId + "-";
final File archiveDir = archive.context().archiveDir();
final String[] files = archiveDir.list((dir, name) -> name.startsWith(prefix));
assertThat(files, arrayWithSize(4));
final long startPosition = 0L;
final long segmentFileBasePosition = AeronArchive.segmentFileBasePosition(startPosition, SEGMENT_LENGTH * 2L, TERM_LENGTH, SEGMENT_LENGTH);
aeronArchive.detachSegments(recordingId, segmentFileBasePosition);
assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
final long deletedSegments = aeronArchive.deleteDetachedSegments(recordingId);
assertEquals(2L, deletedSegments);
assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
Tests.await(() -> {
final String[] updatedFiles = archiveDir.list((dir, name) -> name.startsWith(prefix));
if (null != updatedFiles && 2 == updatedFiles.length) {
assertThat(updatedFiles, arrayContainingInAnyOrder(Archive.segmentFileName(recordingId, segmentFileBasePosition), Archive.segmentFileName(recordingId, segmentFileBasePosition + SEGMENT_LENGTH)));
return true;
}
return false;
});
}
}
Aggregations