Search in sources :

Example 1 with NodeFinder

use of org.apache.ignite.network.NodeFinder 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 NodeFinder

use of org.apache.ignite.network.NodeFinder 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 3 with NodeFinder

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

the class ItTxDistributedTestSingleNode method before.

/**
 * Initialize the test state.
 */
@Override
@BeforeEach
public void before() throws Exception {
    int nodes = nodes();
    int replicas = replicas();
    assertTrue(nodes > 0);
    assertTrue(replicas > 0);
    List<NetworkAddress> localAddresses = findLocalAddresses(NODE_PORT_BASE, NODE_PORT_BASE + nodes);
    var nodeFinder = new StaticNodeFinder(localAddresses);
    nodeFinder.findNodes().parallelStream().map(addr -> startNode(testInfo, addr.toString(), addr.port(), nodeFinder)).forEach(cluster::add);
    for (ClusterService node : cluster) {
        assertTrue(waitForTopology(node, nodes, 1000));
    }
    log.info("The cluster has been started");
    if (startClient()) {
        client = startNode(testInfo, "client", NODE_PORT_BASE - 1, nodeFinder);
        assertTrue(waitForTopology(client, nodes + 1, 1000));
        log.info("The client has been started");
    }
    // Start raft servers. Each raft server can hold multiple groups.
    raftServers = new HashMap<>(nodes);
    txManagers = new HashMap<>(nodes);
    executor = new ScheduledThreadPoolExecutor(20, new NamedThreadFactory(Loza.CLIENT_POOL_NAME));
    for (int i = 0; i < nodes; i++) {
        var raftSrv = new Loza(cluster.get(i), workDir);
        raftSrv.start();
        ClusterNode node = cluster.get(i).topologyService().localMember();
        raftServers.put(node, raftSrv);
        TableTxManagerImpl txMgr = new TableTxManagerImpl(cluster.get(i), new HeapLockManager());
        txMgr.start();
        txManagers.put(node, txMgr);
    }
    log.info("Raft servers have been started");
    final String accountsName = "accounts";
    final String customersName = "customers";
    UUID accTblId = UUID.randomUUID();
    UUID custTblId = UUID.randomUUID();
    accRaftClients = startTable(accountsName, accTblId);
    custRaftClients = startTable(customersName, custTblId);
    log.info("Partition groups have been started");
    TxManager txMgr;
    if (startClient()) {
        txMgr = new TxManagerImpl(client, new HeapLockManager());
    } else {
        // Collocated mode.
        txMgr = txManagers.get(accRaftClients.get(0).clusterService().topologyService().localMember());
    }
    assertNotNull(txMgr);
    igniteTransactions = new IgniteTransactionsImpl(txMgr);
    this.accounts = new TableImpl(new InternalTableImpl(accountsName, accTblId, accRaftClients, 1, NetworkAddress::toString, txMgr, Mockito.mock(TableStorage.class)), new DummySchemaManagerImpl(ACCOUNTS_SCHEMA));
    this.customers = new TableImpl(new InternalTableImpl(customersName, custTblId, custRaftClients, 1, NetworkAddress::toString, txMgr, Mockito.mock(TableStorage.class)), new DummySchemaManagerImpl(CUSTOMERS_SCHEMA));
    log.info("Tables have been started");
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) TxAbstractTest(org.apache.ignite.internal.table.TxAbstractTest) RaftMessagesFactory(org.apache.ignite.raft.jraft.RaftMessagesFactory) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Map(java.util.Map) JraftServerImpl(org.apache.ignite.internal.raft.server.impl.JraftServerImpl) NodeFinder(org.apache.ignite.network.NodeFinder) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) InternalTableImpl(org.apache.ignite.internal.table.distributed.storage.InternalTableImpl) Set(java.util.Set) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TxManager(org.apache.ignite.internal.tx.TxManager) TestInfo(org.junit.jupiter.api.TestInfo) ClusterNode(org.apache.ignite.network.ClusterNode) List(java.util.List) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RaftGroupServiceImpl(org.apache.ignite.raft.jraft.rpc.impl.RaftGroupServiceImpl) ClusterServiceTestUtils.findLocalAddresses(org.apache.ignite.utils.ClusterServiceTestUtils.findLocalAddresses) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ConcurrentHashMapPartitionStorage(org.apache.ignite.internal.storage.basic.ConcurrentHashMapPartitionStorage) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) RendezvousAffinityFunction(org.apache.ignite.internal.affinity.RendezvousAffinityFunction) ClusterServiceTestUtils(org.apache.ignite.utils.ClusterServiceTestUtils) Loza(org.apache.ignite.internal.raft.Loza) HashMap(java.util.HashMap) VersionedRowStore(org.apache.ignite.internal.table.distributed.storage.VersionedRowStore) HeapLockManager(org.apache.ignite.internal.tx.impl.HeapLockManager) TestScaleCubeClusterServiceFactory(org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory) TestUtils.waitForTopology(org.apache.ignite.raft.jraft.test.TestUtils.waitForTopology) TableImpl(org.apache.ignite.internal.table.TableImpl) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) DummySchemaManagerImpl(org.apache.ignite.internal.table.impl.DummySchemaManagerImpl) TxManagerImpl(org.apache.ignite.internal.tx.impl.TxManagerImpl) TableTxManagerImpl(org.apache.ignite.internal.table.distributed.TableTxManagerImpl) TableStorage(org.apache.ignite.internal.storage.engine.TableStorage) NetworkAddress(org.apache.ignite.network.NetworkAddress) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) Peer(org.apache.ignite.raft.client.Peer) AfterEach(org.junit.jupiter.api.AfterEach) ClusterService(org.apache.ignite.network.ClusterService) PartitionListener(org.apache.ignite.internal.table.distributed.raft.PartitionListener) IgniteTransactionsImpl(org.apache.ignite.internal.tx.impl.IgniteTransactionsImpl) NamedThreadFactory(org.apache.ignite.internal.thread.NamedThreadFactory) Table(org.apache.ignite.table.Table) ClusterNode(org.apache.ignite.network.ClusterNode) TableStorage(org.apache.ignite.internal.storage.engine.TableStorage) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) IgniteTransactionsImpl(org.apache.ignite.internal.tx.impl.IgniteTransactionsImpl) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) NamedThreadFactory(org.apache.ignite.internal.thread.NamedThreadFactory) InternalTableImpl(org.apache.ignite.internal.table.distributed.storage.InternalTableImpl) TableImpl(org.apache.ignite.internal.table.TableImpl) TxManager(org.apache.ignite.internal.tx.TxManager) Loza(org.apache.ignite.internal.raft.Loza) DummySchemaManagerImpl(org.apache.ignite.internal.table.impl.DummySchemaManagerImpl) HeapLockManager(org.apache.ignite.internal.tx.impl.HeapLockManager) ClusterService(org.apache.ignite.network.ClusterService) InternalTableImpl(org.apache.ignite.internal.table.distributed.storage.InternalTableImpl) NetworkAddress(org.apache.ignite.network.NetworkAddress) TxManagerImpl(org.apache.ignite.internal.tx.impl.TxManagerImpl) TableTxManagerImpl(org.apache.ignite.internal.table.distributed.TableTxManagerImpl) TableTxManagerImpl(org.apache.ignite.internal.table.distributed.TableTxManagerImpl) UUID(java.util.UUID) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

List (java.util.List)3 ClusterService (org.apache.ignite.network.ClusterService)3 NetworkAddress (org.apache.ignite.network.NetworkAddress)3 NodeFinder (org.apache.ignite.network.NodeFinder)3 Map (java.util.Map)2 UUID (java.util.UUID)2 TimeUnit (java.util.concurrent.TimeUnit)2 Collectors (java.util.stream.Collectors)2 NetworkConfiguration (org.apache.ignite.configuration.schemas.network.NetworkConfiguration)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 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1