Search in sources :

Example 6 with ExpandableArrayBuffer

use of org.agrona.ExpandableArrayBuffer in project aeron by real-logic.

the class ConsensusModuleHarness method makeRecordingLog.

public static long makeRecordingLog(final int numMessages, final int maxMessageLength, final Random random, final ConsensusModule.Context context) {
    try (ConsensusModuleHarness harness = new ConsensusModuleHarness(context, new StubClusteredService(), null, true, false)) {
        harness.awaitServiceOnStart();
        final AeronCluster aeronCluster = AeronCluster.connect(new AeronCluster.Context().lock(new NoOpLock()));
        final SessionDecorator sessionDecorator = new SessionDecorator(aeronCluster.clusterSessionId());
        final Publication publication = aeronCluster.ingressPublication();
        final ExpandableArrayBuffer msgBuffer = new ExpandableArrayBuffer(maxMessageLength);
        for (int i = 0; i < numMessages; i++) {
            final long messageCorrelationId = aeronCluster.context().aeron().nextCorrelationId();
            final int length = (null == random) ? maxMessageLength : random.nextInt(maxMessageLength);
            msgBuffer.putInt(0, i);
            while (true) {
                final long result = sessionDecorator.offer(publication, messageCorrelationId, msgBuffer, 0, length);
                if (result > 0) {
                    break;
                }
                checkOfferResult(result);
                TestUtil.checkInterruptedStatus();
                Thread.yield();
            }
        }
        harness.awaitServiceOnMessageCounter(numMessages);
        return publication.position();
    }
}
Also used : NoOpLock(org.agrona.concurrent.NoOpLock) AeronCluster(io.aeron.cluster.client.AeronCluster) SessionDecorator(io.aeron.cluster.client.SessionDecorator) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer)

Example 7 with ExpandableArrayBuffer

use of org.agrona.ExpandableArrayBuffer 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 8 with ExpandableArrayBuffer

use of org.agrona.ExpandableArrayBuffer in project Aeron by real-logic.

the class ClusterNodeTest method shouldEchoMessageViaServiceUsingTryClaim.

@Test
@InterruptAfter(10)
public void shouldEchoMessageViaServiceUsingTryClaim() {
    final ExpandableArrayBuffer msgBuffer = new ExpandableArrayBuffer();
    final String msg = "Hello World!";
    msgBuffer.putStringWithoutLengthAscii(0, msg);
    final MutableInteger messageCount = new MutableInteger();
    final EgressListener listener = (clusterSessionId, timestamp, buffer, offset, length, header) -> {
        assertEquals(msg, buffer.getStringWithoutLengthAscii(offset, length));
        messageCount.value += 1;
    };
    container = launchEchoService();
    aeronCluster = connectToCluster(listener);
    final BufferClaim bufferClaim = new BufferClaim();
    long publicationResult;
    do {
        publicationResult = aeronCluster.tryClaim(msg.length(), bufferClaim);
        if (publicationResult > 0) {
            final int offset = bufferClaim.offset() + AeronCluster.SESSION_HEADER_LENGTH;
            bufferClaim.buffer().putBytes(offset, msgBuffer, 0, msg.length());
            bufferClaim.commit();
        } else {
            Tests.yield();
        }
    } while (publicationResult < 0);
    offerMessage(msgBuffer, msg);
    awaitResponse(messageCount);
    ClusterTests.failOnClusterError();
}
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) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) CLUSTER_MEMBERS(io.aeron.cluster.ClusterTestConstants.CLUSTER_MEMBERS) ClusteredService(io.aeron.cluster.service.ClusteredService) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BufferClaim(io.aeron.logbuffer.BufferClaim) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ArchiveThreadingMode(io.aeron.archive.ArchiveThreadingMode) MutableInteger(org.agrona.collections.MutableInteger) CloseHelper(org.agrona.CloseHelper) MediaDriver(io.aeron.driver.MediaDriver) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) Archive(io.aeron.archive.Archive) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Test(org.junit.jupiter.api.Test) EgressListener(io.aeron.cluster.client.EgressListener) InterruptAfter(io.aeron.test.InterruptAfter) AfterEach(org.junit.jupiter.api.AfterEach) Header(io.aeron.logbuffer.Header) ClientSession(io.aeron.cluster.service.ClientSession) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) INGRESS_ENDPOINTS(io.aeron.cluster.ClusterTestConstants.INGRESS_ENDPOINTS) DirectBuffer(org.agrona.DirectBuffer) MutableInteger(org.agrona.collections.MutableInteger) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) EgressListener(io.aeron.cluster.client.EgressListener) BufferClaim(io.aeron.logbuffer.BufferClaim) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 9 with ExpandableArrayBuffer

use of org.agrona.ExpandableArrayBuffer in project Aeron by real-logic.

the class ClusterNodeTest method shouldEchoMessageViaServiceUsingDirectOffer.

@Test
@InterruptAfter(10)
public void shouldEchoMessageViaServiceUsingDirectOffer() {
    final ExpandableArrayBuffer msgBuffer = new ExpandableArrayBuffer();
    final String msg = "Hello World!";
    msgBuffer.putStringWithoutLengthAscii(0, msg);
    final MutableInteger messageCount = new MutableInteger();
    final EgressListener listener = (clusterSessionId, timestamp, buffer, offset, length, header) -> {
        assertEquals(msg, buffer.getStringWithoutLengthAscii(offset, length));
        messageCount.value += 1;
    };
    container = launchEchoService();
    aeronCluster = connectToCluster(listener);
    offerMessage(msgBuffer, msg);
    awaitResponse(messageCount);
    ClusterTests.failOnClusterError();
}
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) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) CLUSTER_MEMBERS(io.aeron.cluster.ClusterTestConstants.CLUSTER_MEMBERS) ClusteredService(io.aeron.cluster.service.ClusteredService) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BufferClaim(io.aeron.logbuffer.BufferClaim) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ArchiveThreadingMode(io.aeron.archive.ArchiveThreadingMode) MutableInteger(org.agrona.collections.MutableInteger) CloseHelper(org.agrona.CloseHelper) MediaDriver(io.aeron.driver.MediaDriver) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) Archive(io.aeron.archive.Archive) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Test(org.junit.jupiter.api.Test) EgressListener(io.aeron.cluster.client.EgressListener) InterruptAfter(io.aeron.test.InterruptAfter) AfterEach(org.junit.jupiter.api.AfterEach) Header(io.aeron.logbuffer.Header) ClientSession(io.aeron.cluster.service.ClientSession) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) INGRESS_ENDPOINTS(io.aeron.cluster.ClusterTestConstants.INGRESS_ENDPOINTS) DirectBuffer(org.agrona.DirectBuffer) MutableInteger(org.agrona.collections.MutableInteger) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) EgressListener(io.aeron.cluster.client.EgressListener) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 10 with ExpandableArrayBuffer

use of org.agrona.ExpandableArrayBuffer 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)

Aggregations

ExpandableArrayBuffer (org.agrona.ExpandableArrayBuffer)21 Header (io.aeron.logbuffer.Header)11 DirectBuffer (org.agrona.DirectBuffer)11 ClusteredService (io.aeron.cluster.service.ClusteredService)10 AeronCluster (io.aeron.cluster.client.AeronCluster)9 ClientSession (io.aeron.cluster.service.ClientSession)9 StubClusteredService (io.aeron.test.cluster.StubClusteredService)8 Archive (io.aeron.archive.Archive)7 ArchiveThreadingMode (io.aeron.archive.ArchiveThreadingMode)7 CLUSTER_MEMBERS (io.aeron.cluster.ClusterTestConstants.CLUSTER_MEMBERS)7 INGRESS_ENDPOINTS (io.aeron.cluster.ClusterTestConstants.INGRESS_ENDPOINTS)7 ClusteredServiceContainer (io.aeron.cluster.service.ClusteredServiceContainer)7 MediaDriver (io.aeron.driver.MediaDriver)7 ThreadingMode (io.aeron.driver.ThreadingMode)7 ClusterTests (io.aeron.test.cluster.ClusterTests)7 CloseHelper (org.agrona.CloseHelper)7 AfterEach (org.junit.jupiter.api.AfterEach)7 BeforeEach (org.junit.jupiter.api.BeforeEach)7 Test (org.junit.jupiter.api.Test)7 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)7