Search in sources :

Example 16 with 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;
        });
    }
}
Also used : Publication(io.aeron.Publication) File(java.io.File) CountersReader(org.agrona.concurrent.status.CountersReader) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 17 with Publication

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);
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Publication(io.aeron.Publication) AeronArchive(io.aeron.archive.client.AeronArchive) CountersReader(org.agrona.concurrent.status.CountersReader)

Example 18 with Publication

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);
        }
    }
}
Also used : Tests(io.aeron.test.Tests) DriverEventCode(io.aeron.agent.DriverEventCode) Subscription(io.aeron.Subscription) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) EnumSource(org.junit.jupiter.params.provider.EnumSource) Supplier(java.util.function.Supplier) Collections.synchronizedSet(java.util.Collections.synchronizedSet) MessageHandler(org.agrona.concurrent.MessageHandler) EVENT_READER_FRAME_LIMIT(io.aeron.agent.EventConfiguration.EVENT_READER_FRAME_LIMIT) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) EVENT_RING_BUFFER(io.aeron.agent.EventConfiguration.EVENT_RING_BUFFER) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Publication(io.aeron.Publication) Agent(org.agrona.concurrent.Agent) MutableInteger(org.agrona.collections.MutableInteger) EnumSet(java.util.EnumSet) MediaDriver(io.aeron.driver.MediaDriver) Aeron(io.aeron.Aeron) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) EnumMap(java.util.EnumMap) Set(java.util.Set) IPC_CHANNEL(io.aeron.CommonContext.IPC_CHANNEL) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) InterruptAfter(io.aeron.test.InterruptAfter) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) INCLUDE(org.junit.jupiter.params.provider.EnumSource.Mode.INCLUDE) MutableDirectBuffer(org.agrona.MutableDirectBuffer) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) Publication(io.aeron.Publication) Tests(io.aeron.test.Tests) Aeron(io.aeron.Aeron) MediaDriver(io.aeron.driver.MediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) Subscription(io.aeron.Subscription) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Example 19 with Publication

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);
    }
}
Also used : Publication(io.aeron.Publication) CountersReader(org.agrona.concurrent.status.CountersReader) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 20 with 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;
        });
    }
}
Also used : Publication(io.aeron.Publication) File(java.io.File) CountersReader(org.agrona.concurrent.status.CountersReader) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Aggregations

Publication (io.aeron.Publication)71 Aeron (io.aeron.Aeron)37 Test (org.junit.jupiter.api.Test)30 CountersReader (org.agrona.concurrent.status.CountersReader)24 InterruptAfter (io.aeron.test.InterruptAfter)22 MediaDriver (io.aeron.driver.MediaDriver)21 Subscription (io.aeron.Subscription)19 File (java.io.File)14 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 ChannelUriStringBuilder (io.aeron.ChannelUriStringBuilder)10 AeronArchive (io.aeron.archive.client.AeronArchive)10 ThreadingMode (io.aeron.driver.ThreadingMode)10 InterruptingTestCallback (io.aeron.test.InterruptingTestCallback)10 Tests (io.aeron.test.Tests)10 ContinueBarrier (org.agrona.console.ContinueBarrier)10 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)10 FragmentAssembler (io.aeron.FragmentAssembler)9 CloseHelper (org.agrona.CloseHelper)9 DirectBuffer (org.agrona.DirectBuffer)9