Search in sources :

Example 1 with ExclusivePublication

use of io.aeron.ExclusivePublication in project Aeron by real-logic.

the class ClusterNodeRestartTest method launchReschedulingService.

private void launchReschedulingService(final AtomicLong triggeredTimersCounter) {
    final ClusteredService service = new StubClusteredService() {

        public void onSessionMessage(final ClientSession session, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
            scheduleNext(serviceCorrelationId(7), timestamp + 200);
        }

        public void onTimerEvent(final long correlationId, final long timestamp) {
            triggeredTimersCounter.getAndIncrement();
            scheduleNext(correlationId, timestamp + 200);
        }

        public void onStart(final Cluster cluster, final Image snapshotImage) {
            super.onStart(cluster, snapshotImage);
            if (null != snapshotImage) {
                final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> triggeredTimersCounter.set(buffer.getLong(offset));
                while (true) {
                    final int fragments = snapshotImage.poll(fragmentHandler, 1);
                    if (fragments == 1 || snapshotImage.isEndOfStream()) {
                        break;
                    }
                    idleStrategy.idle();
                }
            }
        }

        public void onTakeSnapshot(final ExclusivePublication snapshotPublication) {
            final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();
            buffer.putLong(0, triggeredTimersCounter.get());
            while (snapshotPublication.offer(buffer, 0, SIZE_OF_INT) < 0) {
                idleStrategy.idle();
            }
        }

        private void scheduleNext(final long correlationId, final long deadline) {
            idleStrategy.reset();
            while (!cluster.scheduleTimer(correlationId, deadline)) {
                idleStrategy.idle();
            }
        }
    };
    container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
}
Also used : ClusterTests(io.aeron.test.cluster.ClusterTests) AeronCluster(io.aeron.cluster.client.AeronCluster) BeforeEach(org.junit.jupiter.api.BeforeEach) StubClusteredService(io.aeron.test.cluster.StubClusteredService) AtomicCounter(org.agrona.concurrent.status.AtomicCounter) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) io.aeron.test(io.aeron.test) CountersReader(org.agrona.concurrent.status.CountersReader) CLUSTER_MEMBERS(io.aeron.cluster.ClusterTestConstants.CLUSTER_MEMBERS) ClusteredService(io.aeron.cluster.service.ClusteredService) SIZE_OF_INT(org.agrona.BitUtil.SIZE_OF_INT) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) SIZE_OF_LONG(org.agrona.BitUtil.SIZE_OF_LONG) Publication(io.aeron.Publication) ArchiveThreadingMode(io.aeron.archive.ArchiveThreadingMode) CloseHelper(org.agrona.CloseHelper) ExclusivePublication(io.aeron.ExclusivePublication) PrintStream(java.io.PrintStream) MediaDriver(io.aeron.driver.MediaDriver) Image(io.aeron.Image) Archive(io.aeron.archive.Archive) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Cluster(io.aeron.cluster.service.Cluster) AfterEach(org.junit.jupiter.api.AfterEach) Header(io.aeron.logbuffer.Header) ClientSession(io.aeron.cluster.service.ClientSession) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions(org.junit.jupiter.api.Assertions) INGRESS_ENDPOINTS(io.aeron.cluster.ClusterTestConstants.INGRESS_ENDPOINTS) FragmentHandler(io.aeron.logbuffer.FragmentHandler) DirectBuffer(org.agrona.DirectBuffer) Mockito.mock(org.mockito.Mockito.mock) AeronCluster(io.aeron.cluster.client.AeronCluster) Cluster(io.aeron.cluster.service.Cluster) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredService(io.aeron.cluster.service.ClusteredService) ExclusivePublication(io.aeron.ExclusivePublication) Image(io.aeron.Image) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) FragmentHandler(io.aeron.logbuffer.FragmentHandler) ClientSession(io.aeron.cluster.service.ClientSession) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer)

Example 2 with ExclusivePublication

use of io.aeron.ExclusivePublication in project Aeron by real-logic.

the class ClusterTimerTest method launchReschedulingService.

private void launchReschedulingService(final AtomicLong triggeredTimersCounter) {
    final ClusteredService service = new StubClusteredService() {

        private int timerId = 1;

        public void onTimerEvent(final long correlationId, final long timestamp) {
            triggeredTimersCounter.getAndIncrement();
            scheduleNext(serviceCorrelationId(timerId++), timestamp + INTERVAL_MS);
        }

        public void onStart(final Cluster cluster, final Image snapshotImage) {
            super.onStart(cluster, snapshotImage);
            this.cluster = cluster;
            if (null != snapshotImage) {
                final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> timerId = buffer.getInt(offset);
                while (true) {
                    final int fragments = snapshotImage.poll(fragmentHandler, 1);
                    if (fragments == 1 || snapshotImage.isEndOfStream()) {
                        break;
                    }
                    idleStrategy.idle();
                }
            }
        }

        public void onTakeSnapshot(final ExclusivePublication snapshotPublication) {
            final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer(SIZE_OF_INT);
            buffer.putInt(0, timerId);
            idleStrategy.reset();
            while (snapshotPublication.offer(buffer, 0, SIZE_OF_INT) < 0) {
                idleStrategy.idle();
            }
        }

        public void onNewLeadershipTermEvent(final long leadershipTermId, final long logPosition, final long timestamp, final long termBaseLogPosition, final int leaderMemberId, final int logSessionId, final TimeUnit timeUnit, final int appVersion) {
            scheduleNext(serviceCorrelationId(timerId++), timestamp + INTERVAL_MS);
        }

        private void scheduleNext(final long correlationId, final long deadlineMs) {
            idleStrategy.reset();
            while (!cluster.scheduleTimer(correlationId, deadlineMs)) {
                idleStrategy.idle();
            }
        }
    };
    container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
}
Also used : ClusterTests(io.aeron.test.cluster.ClusterTests) AeronCluster(io.aeron.cluster.client.AeronCluster) Tests(io.aeron.test.Tests) BeforeEach(org.junit.jupiter.api.BeforeEach) StubClusteredService(io.aeron.test.cluster.StubClusteredService) AtomicCounter(org.agrona.concurrent.status.AtomicCounter) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) CountersReader(org.agrona.concurrent.status.CountersReader) CLUSTER_MEMBERS(io.aeron.cluster.ClusterTestConstants.CLUSTER_MEMBERS) ClusteredService(io.aeron.cluster.service.ClusteredService) SIZE_OF_INT(org.agrona.BitUtil.SIZE_OF_INT) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ArchiveThreadingMode(io.aeron.archive.ArchiveThreadingMode) CloseHelper(org.agrona.CloseHelper) ExclusivePublication(io.aeron.ExclusivePublication) MediaDriver(io.aeron.driver.MediaDriver) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) Image(io.aeron.Image) Archive(io.aeron.archive.Archive) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) InterruptAfter(io.aeron.test.InterruptAfter) AtomicLong(java.util.concurrent.atomic.AtomicLong) Cluster(io.aeron.cluster.service.Cluster) AfterEach(org.junit.jupiter.api.AfterEach) ClientSession(io.aeron.cluster.service.ClientSession) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions(org.junit.jupiter.api.Assertions) INGRESS_ENDPOINTS(io.aeron.cluster.ClusterTestConstants.INGRESS_ENDPOINTS) FragmentHandler(io.aeron.logbuffer.FragmentHandler) FragmentHandler(io.aeron.logbuffer.FragmentHandler) AeronCluster(io.aeron.cluster.client.AeronCluster) Cluster(io.aeron.cluster.service.Cluster) TimeUnit(java.util.concurrent.TimeUnit) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredService(io.aeron.cluster.service.ClusteredService) ExclusivePublication(io.aeron.ExclusivePublication) StubClusteredService(io.aeron.test.cluster.StubClusteredService) Image(io.aeron.Image) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer)

Example 3 with ExclusivePublication

use of io.aeron.ExclusivePublication 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);
}
Also used : RecordingDescriptorCollector(io.aeron.samples.archive.RecordingDescriptorCollector) ExclusivePublication(io.aeron.ExclusivePublication) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) AeronArchive(io.aeron.archive.client.AeronArchive) InterruptAfter(io.aeron.test.InterruptAfter) Test(org.junit.jupiter.api.Test)

Example 4 with ExclusivePublication

use of io.aeron.ExclusivePublication in project aeron by real-logic.

the class EmbeddedExclusiveSpiedThroughput method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 * @throws InterruptedException if interrupted during linger.
 */
public static void main(final String[] args) throws InterruptedException {
    loadPropertiesFiles(args);
    final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), EmbeddedExclusiveSpiedThroughput::printRate);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final AtomicBoolean running = new AtomicBoolean(true);
    final MediaDriver.Context ctx = new MediaDriver.Context().spiesSimulateConnection(true);
    try (MediaDriver mediaDriver = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
        Subscription subscription = aeron.addSubscription(CommonContext.SPY_PREFIX + CHANNEL, STREAM_ID);
        ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID)) {
        executor.execute(reporter);
        executor.execute(() -> SamplesUtil.subscriberLoop(rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));
        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();
        do {
            System.out.format("%nStreaming %,d messages of payload length %d bytes to %s on stream id %d%n", NUMBER_OF_MESSAGES, MESSAGE_LENGTH, CHANNEL, STREAM_ID);
            printingActive = true;
            long backPressureCount = 0;
            for (long i = 0; i < NUMBER_OF_MESSAGES; i++) {
                OFFER_BUFFER.putLong(0, i);
                idleStrategy.reset();
                while (publication.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null) < 0) {
                    backPressureCount++;
                    idleStrategy.idle();
                }
            }
            System.out.println("Done streaming. backPressureRatio=" + ((double) backPressureCount / NUMBER_OF_MESSAGES));
            if (LINGER_TIMEOUT_MS > 0) {
                System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
                Thread.sleep(LINGER_TIMEOUT_MS);
            }
            printingActive = false;
        } while (barrier.await());
        running.set(false);
        reporter.halt();
        executor.shutdown();
    }
}
Also used : CommonContext(io.aeron.CommonContext) IdleStrategy(org.agrona.concurrent.IdleStrategy) ContinueBarrier(org.agrona.console.ContinueBarrier) ExclusivePublication(io.aeron.ExclusivePublication) Aeron(io.aeron.Aeron) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaDriver(io.aeron.driver.MediaDriver) ExecutorService(java.util.concurrent.ExecutorService) Subscription(io.aeron.Subscription)

Example 5 with ExclusivePublication

use of io.aeron.ExclusivePublication in project aeron by real-logic.

the class EmbeddedRecordingThroughput method streamMessagesForRecording.

private long streamMessagesForRecording() {
    try (ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID)) {
        final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;
        while (!publication.isConnected()) {
            idleStrategy.idle();
        }
        final long startNs = System.nanoTime();
        final UnsafeBuffer buffer = this.buffer;
        for (long i = 0; i < NUMBER_OF_MESSAGES; i++) {
            buffer.putLong(0, i);
            idleStrategy.reset();
            while (publication.offer(buffer, 0, MESSAGE_LENGTH) < 0) {
                idleStrategy.idle();
            }
        }
        final long stopPosition = publication.position();
        final CountersReader counters = aeron.countersReader();
        final int counterId = RecordingPos.findCounterIdBySession(counters, publication.sessionId());
        idleStrategy.reset();
        while (counters.getCounterValue(counterId) < stopPosition) {
            idleStrategy.idle();
        }
        final long durationMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
        final double dataRate = (stopPosition * 1000.0d / durationMs) / MEGABYTE;
        final double recordingMb = stopPosition / MEGABYTE;
        final long msgRate = (NUMBER_OF_MESSAGES / durationMs) * 1000L;
        System.out.printf("Recorded %.02f MB @ %.02f MB/s - %,d msg/sec - %d byte payload + 32 byte header%n", recordingMb, dataRate, msgRate, MESSAGE_LENGTH);
        return RecordingPos.getRecordingId(counters, counterId);
    }
}
Also used : YieldingIdleStrategy(org.agrona.concurrent.YieldingIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) ExclusivePublication(io.aeron.ExclusivePublication) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) CountersReader(org.agrona.concurrent.status.CountersReader)

Aggregations

ExclusivePublication (io.aeron.ExclusivePublication)12 MediaDriver (io.aeron.driver.MediaDriver)8 CountersReader (org.agrona.concurrent.status.CountersReader)8 Test (org.junit.jupiter.api.Test)8 Image (io.aeron.Image)6 Archive (io.aeron.archive.Archive)6 ArchiveThreadingMode (io.aeron.archive.ArchiveThreadingMode)6 CLUSTER_MEMBERS (io.aeron.cluster.ClusterTestConstants.CLUSTER_MEMBERS)6 INGRESS_ENDPOINTS (io.aeron.cluster.ClusterTestConstants.INGRESS_ENDPOINTS)6 AeronCluster (io.aeron.cluster.client.AeronCluster)6 ClientSession (io.aeron.cluster.service.ClientSession)6 Cluster (io.aeron.cluster.service.Cluster)6 ClusteredService (io.aeron.cluster.service.ClusteredService)6 ClusteredServiceContainer (io.aeron.cluster.service.ClusteredServiceContainer)6 ThreadingMode (io.aeron.driver.ThreadingMode)6 FragmentHandler (io.aeron.logbuffer.FragmentHandler)6 ClusterTests (io.aeron.test.cluster.ClusterTests)6 StubClusteredService (io.aeron.test.cluster.StubClusteredService)6 TimeUnit (java.util.concurrent.TimeUnit)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6