Search in sources :

Example 11 with ClusteredService

use of io.aeron.cluster.service.ClusteredService in project aeron by real-logic.

the class ClusterNodeRestartTest method launchService.

private void launchService(final boolean initialLaunch, final AtomicLong msgCounter) {
    final ClusteredService service = new StubClusteredService() {

        private int counterValue = 0;

        public void onSessionMessage(final long clusterSessionId, final long correlationId, final long timestampMs, final DirectBuffer buffer, final int offset, final int length, final Header header) {
            final int sentValue = buffer.getInt(offset);
            assertThat(sentValue, is(counterValue));
            counterValue++;
            serviceState.set(Integer.toString(counterValue));
            msgCounter.getAndIncrement();
        }

        public void onTakeSnapshot(final Publication snapshotPublication) {
            final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();
            int length = 0;
            buffer.putInt(length, counterValue);
            length += SIZE_OF_INT;
            length += buffer.putIntAscii(length, counterValue);
            snapshotPublication.offer(buffer, 0, length);
        }

        public void onLoadSnapshot(final Image snapshotImage) {
            while (true) {
                final int fragments = snapshotImage.poll((buffer, offset, length, header) -> {
                    counterValue = buffer.getInt(offset);
                    final String s = buffer.getStringWithoutLengthAscii(offset + SIZE_OF_INT, length - SIZE_OF_INT);
                    serviceState.set(s);
                }, 1);
                if (fragments == 1) {
                    break;
                }
                TestUtil.checkInterruptedStatus();
                Thread.yield();
            }
        }
    };
    container = null;
    container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(() -> {
    }).errorHandler(Throwable::printStackTrace).deleteDirOnStart(initialLaunch));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) Publication(io.aeron.Publication) ClusteredService(io.aeron.cluster.service.ClusteredService) Image(io.aeron.Image) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer)

Example 12 with ClusteredService

use of io.aeron.cluster.service.ClusteredService in project aeron by real-logic.

the class MultiNodeTest method shouldBecomeFollowerStaticThreeNodeConfigWithElection.

@Test(timeout = 10_000L)
public void shouldBecomeFollowerStaticThreeNodeConfigWithElection() {
    final ClusteredService mockService = mock(ClusteredService.class);
    final ConsensusModule.Context context = new ConsensusModule.Context().clusterMembers(THREE_NODE_MEMBERS).memberStatusChannel("aeron:udp?endpoint=localhost:9020").appointedLeaderId(1);
    try (ConsensusModuleHarness harness = new ConsensusModuleHarness(context, mockService, mockMemberStatusListeners, true, true)) {
        harness.memberStatusPublisher().requestVote(harness.memberStatusPublication(1), 0, 0, 0, 1);
        harness.awaitMemberStatusMessage(1);
        verify(mockMemberStatusListeners[1]).onVote(0, 0, 0, 1, 0, true);
        final int logSessionId = 123456;
        final ChannelUri channelUri = ChannelUri.parse(context.logChannel());
        channelUri.put(CommonContext.ENDPOINT_PARAM_NAME, harness.member(0).logEndpoint());
        channelUri.put(CommonContext.SESSION_ID_PARAM_NAME, Integer.toString(logSessionId));
        final Publication logPublication = harness.aeron().addExclusivePublication(channelUri.toString(), context.logStreamId());
        harness.memberStatusPublisher().commitPosition(harness.memberStatusPublication(1), 0, 0, 1, logSessionId);
        harness.awaitMemberStatusMessage(1);
        verify(mockMemberStatusListeners[1]).onAppendedPosition(0, 0, 0);
        harness.awaitServiceOnStart();
    }
}
Also used : ChannelUri(io.aeron.ChannelUri) Publication(io.aeron.Publication) ClusteredService(io.aeron.cluster.service.ClusteredService) Test(org.junit.Test)

Example 13 with ClusteredService

use of io.aeron.cluster.service.ClusteredService in project Aeron by real-logic.

the class ClusterLoggingAgentTest method testClusterEventsLogging.

private void testClusterEventsLogging(final String enabledEvents, final EnumSet<ClusterEventCode> expectedEvents) {
    before(enabledEvents, expectedEvents);
    final Context mediaDriverCtx = new Context().errorHandler(Tests::onError).dirDeleteOnStart(true).threadingMode(ThreadingMode.SHARED);
    final AeronArchive.Context aeronArchiveContext = new AeronArchive.Context().controlRequestChannel("aeron:ipc?term-length=64k").controlRequestStreamId(AeronArchive.Configuration.localControlStreamId()).controlResponseChannel("aeron:ipc?term-length=64k").controlResponseStreamId(AeronArchive.Configuration.localControlStreamId() + 1).controlResponseStreamId(101);
    final Archive.Context archiveCtx = new Archive.Context().errorHandler(Tests::onError).archiveDir(new File(testDir, "archive")).deleteArchiveOnStart(true).recordingEventsEnabled(false).threadingMode(ArchiveThreadingMode.SHARED);
    final ConsensusModule.Context consensusModuleCtx = new ConsensusModule.Context().errorHandler(ClusterTests.errorHandler(0)).clusterDir(new File(testDir, "consensus-module")).archiveContext(aeronArchiveContext.clone()).clusterMemberId(0).clusterMembers("0,localhost:20110,localhost:20220,localhost:20330,localhost:20440,localhost:8010").logChannel("aeron:udp?term-length=256k|control-mode=manual|control=localhost:20550").ingressChannel("aeron:udp?term-length=64k").replicationChannel("aeron:udp?endpoint=localhost:0");
    final ClusteredService clusteredService = mock(ClusteredService.class);
    final ClusteredServiceContainer.Context clusteredServiceCtx = new ClusteredServiceContainer.Context().errorHandler(ClusterTests.errorHandler(0)).archiveContext(aeronArchiveContext.clone()).clusterDir(new File(testDir, "service")).clusteredService(clusteredService);
    clusteredMediaDriver = ClusteredMediaDriver.launch(mediaDriverCtx, archiveCtx, consensusModuleCtx);
    container = ClusteredServiceContainer.launch(clusteredServiceCtx);
    Tests.await(WAIT_LIST::isEmpty);
    final Counter state = clusteredMediaDriver.consensusModule().context().electionStateCounter();
    final Supplier<String> message = () -> ElectionState.get(state).toString();
    while (ElectionState.CLOSED != ElectionState.get(state)) {
        Tests.sleep(1, message);
    }
}
Also used : Context(io.aeron.driver.MediaDriver.Context) AeronArchive(io.aeron.archive.client.AeronArchive) Archive(io.aeron.archive.Archive) ClusteredService(io.aeron.cluster.service.ClusteredService) AeronArchive(io.aeron.archive.client.AeronArchive) ClusterTests(io.aeron.test.cluster.ClusterTests) Tests(io.aeron.test.Tests) Counter(io.aeron.Counter) ConsensusModule(io.aeron.cluster.ConsensusModule) File(java.io.File) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer)

Example 14 with ClusteredService

use of io.aeron.cluster.service.ClusteredService in project Aeron by real-logic.

the class AuthenticationTest method launchService.

private void launchService(final MutableLong sessionId, final MutableReference<byte[]> encodedPrincipal, final AtomicLong msgCounter) {
    final ClusteredService service = new StubClusteredService() {

        private int counterValue = 0;

        public void onSessionOpen(final ClientSession session, final long timestamp) {
            sessionId.value = session.id();
            encodedPrincipal.set(session.encodedPrincipal());
        }

        public void onSessionMessage(final ClientSession session, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
            assertEquals(counterValue, buffer.getInt(offset));
            msgCounter.getAndIncrement();
            counterValue++;
        }
    };
    container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) ClientSession(io.aeron.cluster.service.ClientSession) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredService(io.aeron.cluster.service.ClusteredService) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer)

Example 15 with ClusteredService

use of io.aeron.cluster.service.ClusteredService in project Aeron by real-logic.

the class ClusterNodeRestartTest method launchService.

private void launchService(final AtomicLong msgCounter) {
    final ClusteredService service = new StubClusteredService() {

        private int nextCorrelationId = 0;

        private int counterValue = 0;

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

        public void onSessionMessage(final ClientSession session, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
            final int sentValue = buffer.getInt(offset + MESSAGE_VALUE_OFFSET);
            assertEquals(counterValue, sentValue);
            counterValue++;
            serviceState.set(Integer.toString(counterValue));
            msgCounter.getAndIncrement();
            if (TIMER_MESSAGE_LENGTH == length) {
                final long correlationId = serviceCorrelationId(nextCorrelationId++);
                final long deadlineMs = timestamp + buffer.getLong(offset + TIMER_MESSAGE_DELAY_OFFSET);
                while (!cluster.scheduleTimer(correlationId, deadlineMs)) {
                    idleStrategy.idle();
                }
            }
        }

        public void onTakeSnapshot(final ExclusivePublication snapshotPublication) {
            final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();
            int length = 0;
            buffer.putInt(length, nextCorrelationId);
            length += SIZE_OF_INT;
            buffer.putInt(length, counterValue);
            length += SIZE_OF_INT;
            length += buffer.putStringAscii(length, Integer.toString(counterValue));
            snapshotPublication.offer(buffer, 0, length);
        }
    };
    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)

Aggregations

ClusteredService (io.aeron.cluster.service.ClusteredService)21 ClientSession (io.aeron.cluster.service.ClientSession)13 ClusteredServiceContainer (io.aeron.cluster.service.ClusteredServiceContainer)12 StubClusteredService (io.aeron.test.cluster.StubClusteredService)12 Header (io.aeron.logbuffer.Header)10 DirectBuffer (org.agrona.DirectBuffer)10 ExpandableArrayBuffer (org.agrona.ExpandableArrayBuffer)9 Archive (io.aeron.archive.Archive)8 ClusterTests (io.aeron.test.cluster.ClusterTests)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 Test (org.junit.jupiter.api.Test)8 Image (io.aeron.Image)7 Publication (io.aeron.Publication)7 ExclusivePublication (io.aeron.ExclusivePublication)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 Cluster (io.aeron.cluster.service.Cluster)6 MediaDriver (io.aeron.driver.MediaDriver)6