Search in sources :

Example 1 with ClusterLocalConfiguration

use of org.apache.ignite.network.ClusterLocalConfiguration in project ignite-3 by apache.

the class ClusterServiceTestUtils method clusterService.

/**
 * Creates a cluster service and required node configuration manager beneath it. Populates node configuration with specified port.
 * Manages configuration manager lifecycle: on cluster service start starts node configuration manager, on cluster service stop - stops
 * node configuration manager.
 *
 * @param testInfo                 Test info.
 * @param port                     Local port.
 * @param nodeFinder               Node finder.
 * @param clusterSvcFactory        Cluster service factory.
 */
public static ClusterService clusterService(TestInfo testInfo, int port, NodeFinder nodeFinder, TestScaleCubeClusterServiceFactory clusterSvcFactory) {
    var registry = new MessageSerializationRegistryImpl();
    REGISTRY_INITIALIZERS.forEach(c -> {
        try {
            c.invoke(c.getDeclaringClass(), registry);
        } catch (Throwable e) {
            throw new RuntimeException("Failed to invoke registry initializer", e);
        }
    });
    var ctx = new ClusterLocalConfiguration(testNodeName(testInfo, port), registry);
    ConfigurationManager nodeConfigurationMgr = new ConfigurationManager(Collections.singleton(NetworkConfiguration.KEY), Map.of(), new TestConfigurationStorage(ConfigurationType.LOCAL), List.of(), List.of());
    NetworkConfiguration configuration = nodeConfigurationMgr.configurationRegistry().getConfiguration(NetworkConfiguration.KEY);
    var bootstrapFactory = new NettyBootstrapFactory(configuration, ctx.getName());
    var clusterSvc = clusterSvcFactory.createClusterService(ctx, configuration, bootstrapFactory);
    assert nodeFinder instanceof StaticNodeFinder : "Only StaticNodeFinder is supported at the moment";
    return new ClusterService() {

        @Override
        public TopologyService topologyService() {
            return clusterSvc.topologyService();
        }

        @Override
        public MessagingService messagingService() {
            return clusterSvc.messagingService();
        }

        @Override
        public ClusterLocalConfiguration localConfiguration() {
            return clusterSvc.localConfiguration();
        }

        @Override
        public boolean isStopped() {
            return clusterSvc.isStopped();
        }

        @Override
        public void start() {
            nodeConfigurationMgr.start();
            NetworkConfiguration configuration = nodeConfigurationMgr.configurationRegistry().getConfiguration(NetworkConfiguration.KEY);
            configuration.change(netCfg -> netCfg.changePort(port).changeNodeFinder(c -> c.changeType(NodeFinderType.STATIC.toString()).changeNetClusterNodes(nodeFinder.findNodes().stream().map(NetworkAddress::toString).toArray(String[]::new)))).join();
            bootstrapFactory.start();
            clusterSvc.start();
        }

        @Override
        public void stop() {
            try {
                clusterSvc.stop();
                bootstrapFactory.stop();
                nodeConfigurationMgr.stop();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
}
Also used : IntStream(java.util.stream.IntStream) ClassGraph(io.github.classgraph.ClassGraph) NodeFinderType(org.apache.ignite.configuration.schemas.network.NodeFinderType) NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) ConfigurationType(org.apache.ignite.configuration.annotation.ConfigurationType) IgniteTestUtils.testNodeName(org.apache.ignite.internal.testframework.IgniteTestUtils.testNodeName) Collectors.toUnmodifiableList(java.util.stream.Collectors.toUnmodifiableList) ArrayList(java.util.ArrayList) TestScaleCubeClusterServiceFactory(org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory) NetworkConfiguration(org.apache.ignite.configuration.schemas.network.NetworkConfiguration) Map(java.util.Map) NodeFinder(org.apache.ignite.network.NodeFinder) ScanResult(io.github.classgraph.ScanResult) Method(java.lang.reflect.Method) MessagingService(org.apache.ignite.network.MessagingService) MessageSerializationRegistryInitializer(org.apache.ignite.network.serialization.MessageSerializationRegistryInitializer) TopologyService(org.apache.ignite.network.TopologyService) ClusterLocalConfiguration(org.apache.ignite.network.ClusterLocalConfiguration) ClassInfo(io.github.classgraph.ClassInfo) ConfigurationManager(org.apache.ignite.internal.configuration.ConfigurationManager) TestInfo(org.junit.jupiter.api.TestInfo) NetworkAddress(org.apache.ignite.network.NetworkAddress) List(java.util.List) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) MessageSerializationRegistry(org.apache.ignite.network.serialization.MessageSerializationRegistry) TestConfigurationStorage(org.apache.ignite.internal.configuration.storage.TestConfigurationStorage) ClusterService(org.apache.ignite.network.ClusterService) NetworkMessagesSerializationRegistryInitializer(org.apache.ignite.internal.network.NetworkMessagesSerializationRegistryInitializer) MessageSerializationRegistryImpl(org.apache.ignite.network.MessageSerializationRegistryImpl) Collections(java.util.Collections) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) TestConfigurationStorage(org.apache.ignite.internal.configuration.storage.TestConfigurationStorage) NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) ClusterService(org.apache.ignite.network.ClusterService) MessageSerializationRegistryImpl(org.apache.ignite.network.MessageSerializationRegistryImpl) NetworkConfiguration(org.apache.ignite.configuration.schemas.network.NetworkConfiguration) ClusterLocalConfiguration(org.apache.ignite.network.ClusterLocalConfiguration) ConfigurationManager(org.apache.ignite.internal.configuration.ConfigurationManager)

Example 2 with ClusterLocalConfiguration

use of org.apache.ignite.network.ClusterLocalConfiguration in project ignite-3 by apache.

the class MockedStructuresTest method mockManagers.

// todo copy-paste from TableManagerTest will be removed after https://issues.apache.org/jira/browse/IGNITE-16050
/**
 * Instantiates a table and prepares Table manager.
 *
 * @return Table manager.
 */
private TableManager mockManagers() throws NodeStoppingException {
    when(rm.prepareRaftGroup(any(), any(), any())).thenAnswer(mock -> {
        RaftGroupService raftGrpSrvcMock = mock(RaftGroupService.class);
        when(raftGrpSrvcMock.leader()).thenReturn(new Peer(new NetworkAddress("localhost", 47500)));
        return completedFuture(raftGrpSrvcMock);
    });
    when(ts.getByAddress(any(NetworkAddress.class))).thenReturn(new ClusterNode(UUID.randomUUID().toString(), "node0", new NetworkAddress("localhost", 47500)));
    try (MockedStatic<SchemaUtils> schemaServiceMock = mockStatic(SchemaUtils.class)) {
        schemaServiceMock.when(() -> SchemaUtils.prepareSchemaDescriptor(anyInt(), any())).thenReturn(mock(SchemaDescriptor.class));
    }
    when(cs.messagingService()).thenAnswer(invocation -> {
        MessagingService ret = mock(MessagingService.class);
        return ret;
    });
    when(cs.localConfiguration()).thenAnswer(invocation -> {
        ClusterLocalConfiguration ret = mock(ClusterLocalConfiguration.class);
        when(ret.getName()).thenReturn("node1");
        return ret;
    });
    when(cs.topologyService()).thenAnswer(invocation -> {
        TopologyService ret = mock(TopologyService.class);
        when(ret.localMember()).thenReturn(new ClusterNode("1", "node1", null));
        return ret;
    });
    TableManager tableManager = createTableManager();
    return tableManager;
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) SchemaUtils(org.apache.ignite.internal.schema.SchemaUtils) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) NetworkAddress(org.apache.ignite.network.NetworkAddress) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Peer(org.apache.ignite.raft.client.Peer) TableManager(org.apache.ignite.internal.table.distributed.TableManager) ClusterLocalConfiguration(org.apache.ignite.network.ClusterLocalConfiguration) MessagingService(org.apache.ignite.network.MessagingService) TopologyService(org.apache.ignite.network.TopologyService)

Example 3 with ClusterLocalConfiguration

use of org.apache.ignite.network.ClusterLocalConfiguration in project ignite-3 by apache.

the class ScaleCubeClusterServiceFactory method createClusterService.

/**
 * Creates a new {@link ClusterService} using the provided context. The created network will not be in the "started" state.
 *
 * @param context               Cluster context.
 * @param networkConfiguration  Network configuration.
 * @param nettyBootstrapFactory Bootstrap factory.
 * @return New cluster service.
 */
public ClusterService createClusterService(ClusterLocalConfiguration context, NetworkConfiguration networkConfiguration, NettyBootstrapFactory nettyBootstrapFactory) {
    var messageFactory = new NetworkMessagesFactory();
    var topologyService = new ScaleCubeTopologyService();
    UserObjectSerializationContext userObjectSerialization = createUserObjectSerializationContext();
    var messagingService = new DefaultMessagingService(messageFactory, topologyService, userObjectSerialization);
    return new AbstractClusterService(context, topologyService, messagingService) {

        private volatile ClusterImpl cluster;

        private volatile ConnectionManager connectionMgr;

        private volatile CompletableFuture<Void> shutdownFuture;

        /**
         * {@inheritDoc}
         */
        @Override
        public void start() {
            String consistentId = context.getName();
            var serializationService = new SerializationService(context.getSerializationRegistry(), userObjectSerialization);
            UUID launchId = UUID.randomUUID();
            NetworkView configView = networkConfiguration.value();
            connectionMgr = new ConnectionManager(configView, serializationService, consistentId, () -> new RecoveryServerHandshakeManager(launchId, consistentId, messageFactory), () -> new RecoveryClientHandshakeManager(launchId, consistentId, messageFactory), nettyBootstrapFactory);
            connectionMgr.start();
            var transport = new ScaleCubeDirectMarshallerTransport(connectionMgr.getLocalAddress(), messagingService, topologyService, messageFactory);
            NodeFinder finder = NodeFinderFactory.createNodeFinder(configView.nodeFinder());
            cluster = new ClusterImpl(clusterConfig(configView.membership())).handler(cl -> new ClusterMessageHandler() {

                /**
                 * {@inheritDoc}
                 */
                @Override
                public void onMembershipEvent(MembershipEvent event) {
                    topologyService.onMembershipEvent(event);
                }
            }).config(opts -> opts.memberAlias(consistentId)).transport(opts -> opts.transportFactory(transportConfig -> transport)).membership(opts -> opts.seedMembers(parseAddresses(finder.findNodes())));
            shutdownFuture = cluster.onShutdown().toFuture();
            // resolve cyclic dependencies
            topologyService.setCluster(cluster);
            messagingService.setConnectionManager(connectionMgr);
            cluster.startAwait();
            // emit an artificial event as if the local member has joined the topology (ScaleCube doesn't do that)
            var localMembershipEvent = MembershipEvent.createAdded(cluster.member(), null, System.currentTimeMillis());
            topologyService.onMembershipEvent(localMembershipEvent);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void stop() {
            // local member will be null, if cluster has not been started
            if (cluster.member() == null) {
                return;
            }
            cluster.shutdown();
            try {
                shutdownFuture.get(10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new IgniteInternalException("Interrupted while waiting for the ClusterService to stop", e);
            } catch (TimeoutException e) {
                throw new IgniteInternalException("Timeout while waiting for the ClusterService to stop", e);
            } catch (ExecutionException e) {
                throw new IgniteInternalException("Unable to stop the ClusterService", e.getCause());
            }
            connectionMgr.stop();
            // Messaging service checks connection manager's status before sending a message, so connection manager should be
            // stopped before messaging service
            messagingService.stop();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void beforeNodeStop() {
            stop();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isStopped() {
            return shutdownFuture.isDone();
        }
    };
}
Also used : ClusterImpl(io.scalecube.cluster.ClusterImpl) DefaultUserObjectMarshaller(org.apache.ignite.internal.network.serialization.marshal.DefaultUserObjectMarshaller) NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) NetworkMessagesFactory(org.apache.ignite.internal.network.NetworkMessagesFactory) ClassDescriptorRegistry(org.apache.ignite.internal.network.serialization.ClassDescriptorRegistry) RecoveryClientHandshakeManager(org.apache.ignite.internal.network.recovery.RecoveryClientHandshakeManager) ClusterConfig(io.scalecube.cluster.ClusterConfig) ClusterMembershipView(org.apache.ignite.configuration.schemas.network.ClusterMembershipView) RecoveryServerHandshakeManager(org.apache.ignite.internal.network.recovery.RecoveryServerHandshakeManager) NetworkConfiguration(org.apache.ignite.configuration.schemas.network.NetworkConfiguration) ScaleCubeView(org.apache.ignite.configuration.schemas.network.ScaleCubeView) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) ClusterImpl(io.scalecube.cluster.ClusterImpl) DefaultMessagingService(org.apache.ignite.network.DefaultMessagingService) NodeFinder(org.apache.ignite.network.NodeFinder) NetworkView(org.apache.ignite.configuration.schemas.network.NetworkView) Address(io.scalecube.net.Address) NodeFinderFactory(org.apache.ignite.network.NodeFinderFactory) ConnectionManager(org.apache.ignite.internal.network.netty.ConnectionManager) ClassDescriptorFactory(org.apache.ignite.internal.network.serialization.ClassDescriptorFactory) ClusterLocalConfiguration(org.apache.ignite.network.ClusterLocalConfiguration) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) NetworkAddress(org.apache.ignite.network.NetworkAddress) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) MembershipEvent(io.scalecube.cluster.membership.MembershipEvent) AbstractClusterService(org.apache.ignite.network.AbstractClusterService) ClusterService(org.apache.ignite.network.ClusterService) ClusterMessageHandler(io.scalecube.cluster.ClusterMessageHandler) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) MembershipEvent(io.scalecube.cluster.membership.MembershipEvent) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) CompletableFuture(java.util.concurrent.CompletableFuture) ConnectionManager(org.apache.ignite.internal.network.netty.ConnectionManager) NetworkMessagesFactory(org.apache.ignite.internal.network.NetworkMessagesFactory) UUID(java.util.UUID) ExecutionException(java.util.concurrent.ExecutionException) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) TimeoutException(java.util.concurrent.TimeoutException) ClusterMessageHandler(io.scalecube.cluster.ClusterMessageHandler) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) DefaultMessagingService(org.apache.ignite.network.DefaultMessagingService) AbstractClusterService(org.apache.ignite.network.AbstractClusterService) NetworkView(org.apache.ignite.configuration.schemas.network.NetworkView) NodeFinder(org.apache.ignite.network.NodeFinder) RecoveryClientHandshakeManager(org.apache.ignite.internal.network.recovery.RecoveryClientHandshakeManager) RecoveryServerHandshakeManager(org.apache.ignite.internal.network.recovery.RecoveryServerHandshakeManager)

Example 4 with ClusterLocalConfiguration

use of org.apache.ignite.network.ClusterLocalConfiguration in project ignite-3 by apache.

the class LozaTest method testLozaStop.

/**
 * Checks that the all API methods throw the exception ({@link org.apache.ignite.lang.NodeStoppingException})
 * when Loza is closed.
 *
 * @throws Exception If fail.
 */
@Test
public void testLozaStop() throws Exception {
    Mockito.doReturn(new ClusterLocalConfiguration("test_node", null)).when(clusterNetSvc).localConfiguration();
    Mockito.doReturn(Mockito.mock(MessagingService.class)).when(clusterNetSvc).messagingService();
    Mockito.doReturn(Mockito.mock(TopologyService.class)).when(clusterNetSvc).topologyService();
    Loza loza = new Loza(clusterNetSvc, workDir);
    loza.start();
    loza.beforeNodeStop();
    loza.stop();
    String raftGroupId = "test_raft_group";
    List<ClusterNode> nodes = List.of(new ClusterNode(UUID.randomUUID().toString(), UUID.randomUUID().toString(), NetworkAddress.from("127.0.0.1:123")));
    List<ClusterNode> newNodes = List.of(new ClusterNode(UUID.randomUUID().toString(), UUID.randomUUID().toString(), NetworkAddress.from("127.0.0.1:124")), new ClusterNode(UUID.randomUUID().toString(), UUID.randomUUID().toString(), NetworkAddress.from("127.0.0.1:125")));
    Supplier<RaftGroupListener> lsnrSupplier = () -> null;
    assertThrows(NodeStoppingException.class, () -> loza.updateRaftGroup(raftGroupId, nodes, newNodes, lsnrSupplier));
    assertThrows(NodeStoppingException.class, () -> loza.stopRaftGroup(raftGroupId));
    assertThrows(NodeStoppingException.class, () -> loza.prepareRaftGroup(raftGroupId, nodes, lsnrSupplier));
    assertThrows(NodeStoppingException.class, () -> loza.changePeers(raftGroupId, nodes, newNodes));
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) RaftGroupListener(org.apache.ignite.raft.client.service.RaftGroupListener) ClusterLocalConfiguration(org.apache.ignite.network.ClusterLocalConfiguration) MessagingService(org.apache.ignite.network.MessagingService) TopologyService(org.apache.ignite.network.TopologyService) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Aggregations

ClusterLocalConfiguration (org.apache.ignite.network.ClusterLocalConfiguration)4 MessagingService (org.apache.ignite.network.MessagingService)3 NetworkAddress (org.apache.ignite.network.NetworkAddress)3 TopologyService (org.apache.ignite.network.TopologyService)3 List (java.util.List)2 NetworkConfiguration (org.apache.ignite.configuration.schemas.network.NetworkConfiguration)2 ClusterNode (org.apache.ignite.network.ClusterNode)2 ClusterService (org.apache.ignite.network.ClusterService)2 NettyBootstrapFactory (org.apache.ignite.network.NettyBootstrapFactory)2 NodeFinder (org.apache.ignite.network.NodeFinder)2 ClassGraph (io.github.classgraph.ClassGraph)1 ClassInfo (io.github.classgraph.ClassInfo)1 ScanResult (io.github.classgraph.ScanResult)1 ClusterConfig (io.scalecube.cluster.ClusterConfig)1 ClusterImpl (io.scalecube.cluster.ClusterImpl)1 ClusterMessageHandler (io.scalecube.cluster.ClusterMessageHandler)1 MembershipEvent (io.scalecube.cluster.membership.MembershipEvent)1 Address (io.scalecube.net.Address)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1