Search in sources :

Example 1 with ClusteredServiceContainer

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

the class EchoServiceNode method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
@SuppressWarnings("try")
public static void main(final String[] args) {
    final int nodeId = parseInt(System.getProperty("aeron.cluster.tutorial.nodeId"));
    final String hostnamesStr = System.getProperty("aeron.cluster.tutorial.hostnames", "localhost,localhost,localhost");
    final String internalHostnamesStr = System.getProperty("aeron.cluster.tutorial.hostnames.internal", hostnamesStr);
    final List<String> hostnames = Arrays.asList(hostnamesStr.split(","));
    final List<String> internalHostnames = Arrays.asList(internalHostnamesStr.split(","));
    final String baseDir = System.getProperty("aeron.cluster.tutorial.baseDir");
    final ShutdownSignalBarrier barrier = new ShutdownSignalBarrier();
    final ClusterConfig clusterConfig = ClusterConfig.create(nodeId, hostnames, internalHostnames, PORT_BASE, new EchoService());
    clusterConfig.mediaDriverContext().errorHandler(EchoServiceNode.errorHandler("Media Driver"));
    clusterConfig.archiveContext().errorHandler(EchoServiceNode.errorHandler("Archive"));
    clusterConfig.aeronArchiveContext().errorHandler(EchoServiceNode.errorHandler("Aeron Archive"));
    clusterConfig.consensusModuleContext().errorHandler(errorHandler("Consensus Module"));
    clusterConfig.clusteredServiceContext().errorHandler(errorHandler("Clustered Service"));
    try (ClusteredMediaDriver ignore = ClusteredMediaDriver.launch(clusterConfig.mediaDriverContext(), clusterConfig.archiveContext(), clusterConfig.consensusModuleContext());
        ClusteredServiceContainer ignore2 = ClusteredServiceContainer.launch(clusterConfig.clusteredServiceContext())) {
        System.out.println("[" + nodeId + "] Started Cluster Node on " + hostnames.get(nodeId) + "...");
        barrier.await();
        System.out.println("[" + nodeId + "] Exiting");
    }
}
Also used : ShutdownSignalBarrier(org.agrona.concurrent.ShutdownSignalBarrier) ClusteredMediaDriver(io.aeron.cluster.ClusteredMediaDriver) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer)

Example 2 with ClusteredServiceContainer

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

the class MultiClusteredServicesTest method shouldSupportMultipleServicesPerNode.

@Test
@InterruptAfter(20)
public void shouldSupportMultipleServicesPerNode() {
    final List<TestCluster.NodeContext> nodeContexts = new ArrayList<>();
    final List<TestCluster.ServiceContext> serviceContexts = new ArrayList<>();
    final List<ClusteredMediaDriver> clusteredMediaDrivers = new ArrayList<>();
    final List<ClusteredServiceContainer> clusteredServiceContainers = new ArrayList<>();
    nodeContexts.add(TestCluster.nodeContext(0, true));
    nodeContexts.add(TestCluster.nodeContext(1, true));
    nodeContexts.add(TestCluster.nodeContext(2, true));
    serviceContexts.add(TestCluster.serviceContext(0, 0, nodeContexts.get(0), ServiceA::new));
    serviceContexts.add(TestCluster.serviceContext(0, 1, nodeContexts.get(0), ServiceB::new));
    serviceContexts.add(TestCluster.serviceContext(1, 0, nodeContexts.get(1), ServiceA::new));
    serviceContexts.add(TestCluster.serviceContext(1, 1, nodeContexts.get(1), ServiceB::new));
    serviceContexts.add(TestCluster.serviceContext(2, 0, nodeContexts.get(2), ServiceA::new));
    serviceContexts.add(TestCluster.serviceContext(2, 1, nodeContexts.get(2), ServiceB::new));
    nodeContexts.forEach((context) -> {
        try {
            clusteredMediaDrivers.add(ClusteredMediaDriver.launch(context.mediaDriverCtx, context.archiveCtx, context.consensusModuleCtx));
        } finally {
            systemTestWatcher.dataCollector().add(context.mediaDriverCtx.aeronDirectory());
            systemTestWatcher.dataCollector().add(context.archiveCtx.archiveDir());
            systemTestWatcher.dataCollector().add(context.consensusModuleCtx.clusterDir());
        }
    });
    serviceContexts.forEach((context) -> {
        context.serviceContainerCtx.aeronDirectoryName(context.aeronCtx.aeronDirectoryName());
        try {
            clusteredServiceContainers.add(ClusteredServiceContainer.launch(context.serviceContainerCtx));
        } finally {
            systemTestWatcher.dataCollector().add(context.serviceContainerCtx.clusterDir());
        }
    });
    final String aeronDirName = CommonContext.getAeronDirectoryName();
    final MediaDriver clientMediaDriver = MediaDriver.launch(new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirDeleteOnStart(true).aeronDirectoryName(aeronDirName).nameResolver(new RedirectingNameResolver(TestCluster.DEFAULT_NODE_MAPPINGS)));
    final AeronCluster client = AeronCluster.connect(new AeronCluster.Context().aeronDirectoryName(aeronDirName).ingressChannel(CommonContext.UDP_CHANNEL).ingressEndpoints(TestCluster.ingressEndpoints(0, 3)));
    try {
        final DirectBuffer buffer = new UnsafeBuffer(new byte[100]);
        while (client.offer(buffer, 0, 100) < 0) {
            Tests.yield();
        }
        Tests.awaitValue(serviceAMessageCount, 3);
        Tests.awaitValue(serviceBMessageCount, 3);
    } finally {
        CloseHelper.closeAll(client, clientMediaDriver);
        clusteredMediaDrivers.forEach((clusteredMediaDriver) -> clusteredMediaDriver.consensusModule().close());
        CloseHelper.closeAll(clusteredServiceContainers);
        CloseHelper.closeAll(clusteredMediaDrivers);
        clientMediaDriver.context().deleteDirectory();
    }
}
Also used : CommonContext(io.aeron.CommonContext) AeronCluster(io.aeron.cluster.client.AeronCluster) ArrayList(java.util.ArrayList) DirectBuffer(org.agrona.DirectBuffer) MediaDriver(io.aeron.driver.MediaDriver) RedirectingNameResolver(io.aeron.test.driver.RedirectingNameResolver) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) Test(org.junit.jupiter.api.Test)

Example 3 with ClusteredServiceContainer

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

the class MultiClusteredServicesTest method shouldSupportMultipleServicesPerNode.

@Test
@InterruptAfter(20)
public void shouldSupportMultipleServicesPerNode() {
    final List<TestCluster.NodeContext> nodeContexts = new ArrayList<>();
    final List<TestCluster.ServiceContext> serviceContexts = new ArrayList<>();
    final List<ClusteredMediaDriver> clusteredMediaDrivers = new ArrayList<>();
    final List<ClusteredServiceContainer> clusteredServiceContainers = new ArrayList<>();
    nodeContexts.add(TestCluster.nodeContext(0, true));
    nodeContexts.add(TestCluster.nodeContext(1, true));
    nodeContexts.add(TestCluster.nodeContext(2, true));
    serviceContexts.add(TestCluster.serviceContext(0, 0, nodeContexts.get(0), ServiceA::new));
    serviceContexts.add(TestCluster.serviceContext(0, 1, nodeContexts.get(0), ServiceB::new));
    serviceContexts.add(TestCluster.serviceContext(1, 0, nodeContexts.get(1), ServiceA::new));
    serviceContexts.add(TestCluster.serviceContext(1, 1, nodeContexts.get(1), ServiceB::new));
    serviceContexts.add(TestCluster.serviceContext(2, 0, nodeContexts.get(2), ServiceA::new));
    serviceContexts.add(TestCluster.serviceContext(2, 1, nodeContexts.get(2), ServiceB::new));
    nodeContexts.forEach((context) -> {
        try {
            clusteredMediaDrivers.add(ClusteredMediaDriver.launch(context.mediaDriverCtx, context.archiveCtx, context.consensusModuleCtx));
        } finally {
            systemTestWatcher.dataCollector().add(context.mediaDriverCtx.aeronDirectory());
            systemTestWatcher.dataCollector().add(context.archiveCtx.archiveDir());
            systemTestWatcher.dataCollector().add(context.consensusModuleCtx.clusterDir());
        }
    });
    serviceContexts.forEach((context) -> {
        context.serviceContainerCtx.aeronDirectoryName(context.aeronCtx.aeronDirectoryName());
        try {
            clusteredServiceContainers.add(ClusteredServiceContainer.launch(context.serviceContainerCtx));
        } finally {
            systemTestWatcher.dataCollector().add(context.serviceContainerCtx.clusterDir());
        }
    });
    final String aeronDirName = CommonContext.getAeronDirectoryName();
    final MediaDriver clientMediaDriver = MediaDriver.launch(new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirDeleteOnStart(true).aeronDirectoryName(aeronDirName).nameResolver(new RedirectingNameResolver(TestCluster.DEFAULT_NODE_MAPPINGS)));
    final AeronCluster client = AeronCluster.connect(new AeronCluster.Context().aeronDirectoryName(aeronDirName).ingressChannel(CommonContext.UDP_CHANNEL).ingressEndpoints(TestCluster.ingressEndpoints(0, 3)));
    try {
        final DirectBuffer buffer = new UnsafeBuffer(new byte[100]);
        while (client.offer(buffer, 0, 100) < 0) {
            Tests.yield();
        }
        Tests.awaitValue(serviceAMessageCount, 3);
        Tests.awaitValue(serviceBMessageCount, 3);
    } finally {
        CloseHelper.closeAll(client, clientMediaDriver);
        clusteredMediaDrivers.forEach((clusteredMediaDriver) -> clusteredMediaDriver.consensusModule().close());
        CloseHelper.closeAll(clusteredServiceContainers);
        CloseHelper.closeAll(clusteredMediaDrivers);
        clientMediaDriver.context().deleteDirectory();
    }
}
Also used : CommonContext(io.aeron.CommonContext) AeronCluster(io.aeron.cluster.client.AeronCluster) ArrayList(java.util.ArrayList) DirectBuffer(org.agrona.DirectBuffer) MediaDriver(io.aeron.driver.MediaDriver) RedirectingNameResolver(io.aeron.test.driver.RedirectingNameResolver) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) Test(org.junit.jupiter.api.Test)

Example 4 with ClusteredServiceContainer

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

the class StartFromTruncatedRecordingLogTest method after.

@AfterEach
public void after() {
    CloseHelper.closeAll(client, clientMediaDriver);
    for (final ClusteredMediaDriver driver : clusteredMediaDrivers) {
        if (null != driver) {
            driver.consensusModule().close();
        }
    }
    CloseHelper.closeAll(containers);
    CloseHelper.closeAll(clusteredMediaDrivers);
    clientMediaDriver.context().deleteDirectory();
    for (final ClusteredServiceContainer container : containers) {
        if (null != container) {
            container.context().deleteDirectory();
        }
    }
    for (final ClusteredMediaDriver driver : clusteredMediaDrivers) {
        if (null != driver) {
            driver.consensusModule().context().deleteDirectory();
            driver.archive().context().deleteDirectory();
            driver.mediaDriver().context().deleteDirectory();
        }
    }
}
Also used : ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) AfterEach(org.junit.jupiter.api.AfterEach)

Example 5 with ClusteredServiceContainer

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

the class MultiModuleSharedDriverTest method shouldSupportTwoSingleNodeClusters.

@Test
@InterruptAfter(20)
@SuppressWarnings({ "try", "methodlength" })
public void shouldSupportTwoSingleNodeClusters() {
    final MediaDriver.Context driverCtx = new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).nameResolver(new RedirectingNameResolver(TestCluster.DEFAULT_NODE_MAPPINGS)).dirDeleteOnShutdown(false).dirDeleteOnStart(true);
    final Archive.Context archiveCtx = new Archive.Context().threadingMode(ArchiveThreadingMode.SHARED).archiveDir(new File(SystemUtil.tmpDirName(), "archive")).recordingEventsEnabled(false).deleteArchiveOnStart(true);
    try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtx, archiveCtx)) {
        final ConsensusModule.Context moduleCtx0 = new ConsensusModule.Context().clusterId(0).deleteDirOnStart(true).clusterDir(new File(SystemUtil.tmpDirName(), "cluster-0-0")).logChannel("aeron:ipc?term-length=64k").logStreamId(100).serviceStreamId(104).consensusModuleStreamId(105).ingressChannel("aeron:udp?endpoint=localhost:9020").replicationChannel("aeron:udp?endpoint=localhost:0");
        final ClusteredServiceContainer.Context containerCtx0 = new ClusteredServiceContainer.Context().clusterId(moduleCtx0.clusterId()).clusteredService(new EchoService()).clusterDir(moduleCtx0.clusterDir()).serviceStreamId(moduleCtx0.serviceStreamId()).consensusModuleStreamId(moduleCtx0.consensusModuleStreamId());
        final ConsensusModule.Context moduleCtx1 = new ConsensusModule.Context().clusterId(1).deleteDirOnStart(true).clusterDir(new File(SystemUtil.tmpDirName(), "cluster-0-1")).logChannel("aeron:ipc?term-length=64k").logStreamId(200).serviceStreamId(204).consensusModuleStreamId(205).ingressChannel("aeron:udp?endpoint=localhost:9021").replicationChannel("aeron:udp?endpoint=localhost:0");
        final ClusteredServiceContainer.Context containerCtx1 = new ClusteredServiceContainer.Context().clusteredService(new EchoService()).clusterDir(moduleCtx1.clusterDir()).serviceStreamId(moduleCtx1.serviceStreamId()).consensusModuleStreamId(moduleCtx1.consensusModuleStreamId()).clusterId(moduleCtx1.clusterId());
        ConsensusModule consensusModule0 = null;
        ClusteredServiceContainer container0 = null;
        ConsensusModule consensusModule1 = null;
        ClusteredServiceContainer container1 = null;
        AeronCluster client0 = null;
        AeronCluster client1 = null;
        try {
            consensusModule0 = ConsensusModule.launch(moduleCtx0);
            consensusModule1 = ConsensusModule.launch(moduleCtx1);
            container0 = ClusteredServiceContainer.launch(containerCtx0);
            container1 = ClusteredServiceContainer.launch(containerCtx1);
            final MutableReference<String> egress = new MutableReference<>();
            final EgressListener egressListener = (clusterSessionId, timestamp, buffer, offset, length, header) -> egress.set(buffer.getStringWithoutLengthAscii(offset, length));
            client0 = AeronCluster.connect(new AeronCluster.Context().egressListener(egressListener).ingressChannel(moduleCtx0.ingressChannel()).egressChannel("aeron:udp?endpoint=localhost:0"));
            client1 = AeronCluster.connect(new AeronCluster.Context().egressListener(egressListener).ingressChannel(moduleCtx1.ingressChannel()).egressChannel("aeron:udp?endpoint=localhost:0"));
            echoMessage(client0, "Message 0", egress);
            echoMessage(client1, "Message 1", egress);
        } finally {
            systemTestWatcher.dataCollector().add(moduleCtx0.clusterDir());
            systemTestWatcher.dataCollector().add(moduleCtx1.clusterDir());
            CloseHelper.closeAll(client0, client1, consensusModule0, consensusModule1, container0, container1);
        }
    } finally {
        systemTestWatcher.dataCollector().add(driverCtx.aeronDirectory());
        systemTestWatcher.dataCollector().add(archiveCtx.archiveDir());
    }
}
Also used : CommonContext(io.aeron.CommonContext) AeronCluster(io.aeron.cluster.client.AeronCluster) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) io.aeron.test(io.aeron.test) SystemUtil(org.agrona.SystemUtil) RedirectingNameResolver(io.aeron.test.driver.RedirectingNameResolver) MutableReference(org.agrona.collections.MutableReference) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ArchivingMediaDriver(io.aeron.archive.ArchivingMediaDriver) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ArchiveThreadingMode(io.aeron.archive.ArchiveThreadingMode) CloseHelper(org.agrona.CloseHelper) MediaDriver(io.aeron.driver.MediaDriver) Archive(io.aeron.archive.Archive) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) CommonContext(io.aeron.CommonContext) File(java.io.File) Test(org.junit.jupiter.api.Test) EgressListener(io.aeron.cluster.client.EgressListener) Header(io.aeron.logbuffer.Header) ClientSession(io.aeron.cluster.service.ClientSession) ThreadingMode(io.aeron.driver.ThreadingMode) TestCluster(io.aeron.test.cluster.TestCluster) DirectBuffer(org.agrona.DirectBuffer) Archive(io.aeron.archive.Archive) AeronCluster(io.aeron.cluster.client.AeronCluster) MutableReference(org.agrona.collections.MutableReference) EgressListener(io.aeron.cluster.client.EgressListener) ArchivingMediaDriver(io.aeron.archive.ArchivingMediaDriver) MediaDriver(io.aeron.driver.MediaDriver) RedirectingNameResolver(io.aeron.test.driver.RedirectingNameResolver) File(java.io.File) ArchivingMediaDriver(io.aeron.archive.ArchivingMediaDriver) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) Test(org.junit.jupiter.api.Test)

Aggregations

ClusteredServiceContainer (io.aeron.cluster.service.ClusteredServiceContainer)10 CommonContext (io.aeron.CommonContext)6 MediaDriver (io.aeron.driver.MediaDriver)6 Archive (io.aeron.archive.Archive)4 ClusteredMediaDriver (io.aeron.cluster.ClusteredMediaDriver)4 AeronCluster (io.aeron.cluster.client.AeronCluster)4 RedirectingNameResolver (io.aeron.test.driver.RedirectingNameResolver)4 File (java.io.File)4 DirectBuffer (org.agrona.DirectBuffer)4 ShutdownSignalBarrier (org.agrona.concurrent.ShutdownSignalBarrier)4 Test (org.junit.jupiter.api.Test)4 ArchiveThreadingMode (io.aeron.archive.ArchiveThreadingMode)2 ArchivingMediaDriver (io.aeron.archive.ArchivingMediaDriver)2 AeronArchive (io.aeron.archive.client.AeronArchive)2 ConsensusModule (io.aeron.cluster.ConsensusModule)2 EgressListener (io.aeron.cluster.client.EgressListener)2 ClientSession (io.aeron.cluster.service.ClientSession)2 MinMulticastFlowControlSupplier (io.aeron.driver.MinMulticastFlowControlSupplier)2 ThreadingMode (io.aeron.driver.ThreadingMode)2 Header (io.aeron.logbuffer.Header)2