Search in sources :

Example 26 with ClusterService

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

the class ItSimpleCounterServerTest method before.

/**
 * Before each.
 */
@BeforeEach
void before() throws Exception {
    LOG.info(">>>> Starting test {}", testInfo.getTestMethod().orElseThrow().getName());
    var addr = new NetworkAddress("localhost", PORT);
    ClusterService service = clusterService(PORT, List.of(addr), true);
    server = new JraftServerImpl(service, dataPath) {

        @Override
        public synchronized void stop() {
            super.stop();
            service.stop();
        }
    };
    server.start();
    ClusterNode serverNode = server.clusterService().topologyService().localMember();
    assertTrue(server.startRaftGroup(COUNTER_GROUP_ID_0, new CounterListener(), List.of(new Peer(serverNode.address()))));
    assertTrue(server.startRaftGroup(COUNTER_GROUP_ID_1, new CounterListener(), List.of(new Peer(serverNode.address()))));
    ClusterService clientNode1 = clusterService(PORT + 1, List.of(addr), true);
    executor = new ScheduledThreadPoolExecutor(20, new NamedThreadFactory(Loza.CLIENT_POOL_NAME));
    client1 = RaftGroupServiceImpl.start(COUNTER_GROUP_ID_0, clientNode1, FACTORY, 1000, List.of(new Peer(serverNode.address())), false, 200, executor).get(3, TimeUnit.SECONDS);
    ClusterService clientNode2 = clusterService(PORT + 2, List.of(addr), true);
    client2 = RaftGroupServiceImpl.start(COUNTER_GROUP_ID_1, clientNode2, FACTORY, 1000, List.of(new Peer(serverNode.address())), false, 200, executor).get(3, TimeUnit.SECONDS);
    assertTrue(waitForTopology(service, 3, 1000));
    assertTrue(waitForTopology(clientNode1, 3, 1000));
    assertTrue(waitForTopology(clientNode2, 3, 1000));
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) ClusterService(org.apache.ignite.network.ClusterService) NetworkAddress(org.apache.ignite.network.NetworkAddress) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) NamedThreadFactory(org.apache.ignite.internal.thread.NamedThreadFactory) Peer(org.apache.ignite.raft.client.Peer) JraftServerImpl(org.apache.ignite.internal.raft.server.impl.JraftServerImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 27 with ClusterService

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

the class ItAbstractListenerSnapshotTest method afterTest.

/**
 * Shutdown raft server, executor for raft group services and stop all cluster nodes.
 *
 * @throws Exception If failed to shutdown raft server,
 */
@AfterEach
public void afterTest() throws Exception {
    for (JraftServerImpl server : servers) {
        server.stopRaftGroup(raftGroupId());
    }
    for (RaftGroupService client : clients) {
        client.shutdown();
    }
    IgniteUtils.shutdownAndAwaitTermination(executor, 10, TimeUnit.SECONDS);
    for (JraftServerImpl server : servers) {
        server.beforeNodeStop();
        server.stop();
    }
    for (ClusterService service : cluster) {
        service.stop();
    }
}
Also used : ClusterService(org.apache.ignite.network.ClusterService) JraftServerImpl(org.apache.ignite.internal.raft.server.impl.JraftServerImpl) AfterEach(org.junit.jupiter.api.AfterEach)

Example 28 with ClusterService

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

the class ItAbstractListenerSnapshotTest method startServer.

/**
 * Starts a raft server.
 *
 * @param testInfo Test info.
 * @param idx      Server index (affects port of the server).
 * @return Server.
 */
private JraftServerImpl startServer(TestInfo testInfo, int idx) {
    var addr = new NetworkAddress(getLocalAddress(), PORT);
    ClusterService service = clusterService(testInfo, PORT + idx, addr);
    Path jraft = workDir.resolve("jraft" + idx);
    JraftServerImpl server = new JraftServerImpl(service, jraft) {

        @Override
        public void stop() {
            super.stop();
            service.stop();
        }
    };
    server.start();
    Path listenerPersistencePath = workDir.resolve("db" + idx);
    servers.add(server);
    server.startRaftGroup(raftGroupId(), createListener(service, listenerPersistencePath), INITIAL_CONF);
    return server;
}
Also used : Path(java.nio.file.Path) ClusterService(org.apache.ignite.network.ClusterService) NetworkAddress(org.apache.ignite.network.NetworkAddress) JraftServerImpl(org.apache.ignite.internal.raft.server.impl.JraftServerImpl)

Example 29 with ClusterService

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

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

the class KeyValueViewOperationsSimpleSchemaTest method createTable.

@NotNull
private TableImpl createTable(SchemaDescriptor schema) {
    ClusterService clusterService = Mockito.mock(ClusterService.class, RETURNS_DEEP_STUBS);
    Mockito.when(clusterService.topologyService().localMember().address()).thenReturn(DummyInternalTableImpl.ADDR);
    LockManager lockManager = new HeapLockManager();
    TxManager txManager = new TxManagerImpl(clusterService, lockManager);
    MessagingService messagingService = MessagingServiceTestUtils.mockMessagingService(txManager);
    Mockito.when(clusterService.messagingService()).thenReturn(messagingService);
    DummyInternalTableImpl table = new DummyInternalTableImpl(new VersionedRowStore(new ConcurrentHashMapPartitionStorage(), txManager), txManager);
    return new TableImpl(table, new DummySchemaManagerImpl(schema));
}
Also used : VersionedRowStore(org.apache.ignite.internal.table.distributed.storage.VersionedRowStore) HeapLockManager(org.apache.ignite.internal.tx.impl.HeapLockManager) LockManager(org.apache.ignite.internal.tx.LockManager) HeapLockManager(org.apache.ignite.internal.tx.impl.HeapLockManager) ClusterService(org.apache.ignite.network.ClusterService) TxManagerImpl(org.apache.ignite.internal.tx.impl.TxManagerImpl) ConcurrentHashMapPartitionStorage(org.apache.ignite.internal.storage.basic.ConcurrentHashMapPartitionStorage) DummyInternalTableImpl(org.apache.ignite.internal.table.impl.DummyInternalTableImpl) TxManager(org.apache.ignite.internal.tx.TxManager) DummyInternalTableImpl(org.apache.ignite.internal.table.impl.DummyInternalTableImpl) DummySchemaManagerImpl(org.apache.ignite.internal.table.impl.DummySchemaManagerImpl) MessagingService(org.apache.ignite.network.MessagingService) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ClusterService (org.apache.ignite.network.ClusterService)42 NetworkAddress (org.apache.ignite.network.NetworkAddress)18 StaticNodeFinder (org.apache.ignite.network.StaticNodeFinder)12 Test (org.junit.jupiter.api.Test)12 ConcurrentHashMapPartitionStorage (org.apache.ignite.internal.storage.basic.ConcurrentHashMapPartitionStorage)11 VersionedRowStore (org.apache.ignite.internal.table.distributed.storage.VersionedRowStore)11 HeapLockManager (org.apache.ignite.internal.tx.impl.HeapLockManager)11 TxManagerImpl (org.apache.ignite.internal.tx.impl.TxManagerImpl)11 List (java.util.List)10 TestScaleCubeClusterServiceFactory (org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory)10 ClusterNode (org.apache.ignite.network.ClusterNode)9 MessagingService (org.apache.ignite.network.MessagingService)9 BeforeEach (org.junit.jupiter.api.BeforeEach)8 DummyInternalTableImpl (org.apache.ignite.internal.table.impl.DummyInternalTableImpl)7 DummySchemaManagerImpl (org.apache.ignite.internal.table.impl.DummySchemaManagerImpl)7 TxManager (org.apache.ignite.internal.tx.TxManager)7 RaftGroupService (org.apache.ignite.raft.client.service.RaftGroupService)7 AfterEach (org.junit.jupiter.api.AfterEach)7 JraftServerImpl (org.apache.ignite.internal.raft.server.impl.JraftServerImpl)6 Peer (org.apache.ignite.raft.client.Peer)6