Search in sources :

Example 1 with IgniteRpcClient

use of org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient in project ignite-3 by apache.

the class ItCliServiceTest method setup.

/**
 * Executes before each test.
 */
@BeforeEach
public void setup(TestInfo testInfo, @WorkDirectory Path dataPath) throws Exception {
    LOG.info(">>>>>>>>>>>>>>> Start test method: " + testInfo.getDisplayName());
    List<PeerId> peers = TestUtils.generatePeers(3);
    LinkedHashSet<PeerId> learners = new LinkedHashSet<>();
    // 2 learners
    for (int i = 0; i < 2; i++) {
        learners.add(new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT + LEARNER_PORT_STEP + i));
    }
    cluster = new TestCluster(groupId, dataPath.toString(), peers, learners, ELECTION_TIMEOUT_MILLIS, testInfo);
    for (PeerId peer : peers) {
        cluster.start(peer.getEndpoint());
    }
    for (PeerId peer : learners) {
        cluster.startLearner(peer);
    }
    cluster.waitLeader();
    cluster.ensureLeader(cluster.getLeader());
    cliService = new CliServiceImpl();
    conf = new Configuration(peers, learners);
    CliOptions opts = new CliOptions();
    clientExecutor = JRaftUtils.createClientExecutor(opts, "client");
    opts.setClientExecutor(clientExecutor);
    List<NetworkAddress> addressList = peers.stream().map(PeerId::getEndpoint).map(JRaftUtils::addressFromEndpoint).collect(toList());
    ClusterService clientSvc = ClusterServiceTestUtils.clusterService(testInfo, TestUtils.INIT_PORT - 1, new StaticNodeFinder(addressList), new TestScaleCubeClusterServiceFactory());
    clientSvc.start();
    IgniteRpcClient rpcClient = new IgniteRpcClient(clientSvc) {

        @Override
        public void shutdown() {
            super.shutdown();
            clientSvc.stop();
        }
    };
    opts.setRpcClient(rpcClient);
    assertTrue(cliService.init(opts));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) IgniteRpcClient(org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient) CliOptions(org.apache.ignite.raft.jraft.option.CliOptions) ClusterService(org.apache.ignite.network.ClusterService) TestScaleCubeClusterServiceFactory(org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory) NetworkAddress(org.apache.ignite.network.NetworkAddress) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with IgniteRpcClient

use of org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient in project ignite-3 by apache.

the class ItNodeTest method createService.

/**
 * @param groupId Group id.
 * @param peerId Peer id.
 * @param nodeOptions Node options.
 * @return Raft group service.
 */
private RaftGroupService createService(String groupId, PeerId peerId, NodeOptions nodeOptions) {
    Configuration initialConf = nodeOptions.getInitialConf();
    nodeOptions.setStripes(1);
    Stream<PeerId> peers = initialConf == null ? Stream.empty() : Stream.concat(initialConf.getPeers().stream(), initialConf.getLearners().stream());
    List<NetworkAddress> addressList = peers.map(PeerId::getEndpoint).map(JRaftUtils::addressFromEndpoint).collect(toList());
    var nodeManager = new NodeManager();
    ClusterService clusterService = ClusterServiceTestUtils.clusterService(testInfo, peerId.getEndpoint().getPort(), new StaticNodeFinder(addressList), new TestScaleCubeClusterServiceFactory());
    ExecutorService requestExecutor = JRaftUtils.createRequestExecutor(nodeOptions);
    executors.add(requestExecutor);
    IgniteRpcServer rpcServer = new TestIgniteRpcServer(clusterService, nodeManager, nodeOptions, requestExecutor);
    nodeOptions.setRpcClient(new IgniteRpcClient(clusterService));
    clusterService.start();
    var service = new RaftGroupService(groupId, peerId, nodeOptions, rpcServer, nodeManager) {

        @Override
        public synchronized void shutdown() {
            rpcServer.shutdown();
            super.shutdown();
            clusterService.stop();
        }
    };
    services.add(service);
    return service;
}
Also used : TestIgniteRpcServer(org.apache.ignite.raft.jraft.rpc.TestIgniteRpcServer) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) IgniteRpcClient(org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) IgniteRpcServer(org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcServer) TestIgniteRpcServer(org.apache.ignite.raft.jraft.rpc.TestIgniteRpcServer) NodeManager(org.apache.ignite.raft.jraft.NodeManager) ClusterService(org.apache.ignite.network.ClusterService) TestScaleCubeClusterServiceFactory(org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory) NetworkAddress(org.apache.ignite.network.NetworkAddress) ExecutorService(java.util.concurrent.ExecutorService) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 3 with IgniteRpcClient

use of org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient in project ignite-3 by apache.

the class JraftServerImpl method startRaftGroup.

/**
 * {@inheritDoc}
 */
@Override
public synchronized boolean startRaftGroup(String groupId, RaftGroupListener lsnr, @Nullable List<Peer> initialConf) {
    if (groups.containsKey(groupId)) {
        return false;
    }
    // Thread pools are shared by all raft groups.
    NodeOptions nodeOptions = opts.copy();
    Path serverDataPath = getServerDataPath(groupId);
    try {
        Files.createDirectories(serverDataPath);
    } catch (IOException e) {
        throw new IgniteInternalException(e);
    }
    nodeOptions.setLogUri(serverDataPath.resolve("logs").toString());
    nodeOptions.setRaftMetaUri(serverDataPath.resolve("meta").toString());
    nodeOptions.setSnapshotUri(serverDataPath.resolve("snapshot").toString());
    nodeOptions.setFsm(new DelegatingStateMachine(lsnr));
    if (initialConf != null) {
        List<PeerId> mapped = initialConf.stream().map(PeerId::fromPeer).collect(Collectors.toList());
        nodeOptions.setInitialConf(new Configuration(mapped, null));
    }
    IgniteRpcClient client = new IgniteRpcClient(service);
    nodeOptions.setRpcClient(client);
    NetworkAddress addr = service.topologyService().localMember().address();
    var peerId = new PeerId(addr.host(), addr.port(), 0, ElectionPriority.DISABLED);
    var server = new RaftGroupService(groupId, peerId, nodeOptions, rpcServer, nodeManager);
    server.start();
    groups.put(groupId, server);
    return true;
}
Also used : Path(java.nio.file.Path) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) NetworkAddress(org.apache.ignite.network.NetworkAddress) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) IgniteRpcClient(org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) IOException(java.io.IOException) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 4 with IgniteRpcClient

use of org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient in project ignite-3 by apache.

the class TestCluster method start.

public boolean start(Endpoint listenAddr, boolean emptyPeers, int snapshotIntervalSecs, boolean enableMetrics, SnapshotThrottle snapshotThrottle, RaftOptions raftOptions, int priority) throws IOException {
    this.lock.lock();
    try {
        if (this.serverMap.get(listenAddr) != null) {
            return true;
        }
        // Start node in non shared pools mode. Pools will be managed by node itself.
        NodeOptions nodeOptions = new NodeOptions();
        nodeOptions.setServerName(listenAddr.toString());
        nodeOptions.setElectionTimeoutMs(this.electionTimeoutMs);
        nodeOptions.setEnableMetrics(enableMetrics);
        nodeOptions.setSnapshotThrottle(snapshotThrottle);
        nodeOptions.setSnapshotIntervalSecs(snapshotIntervalSecs);
        nodeOptions.setServiceFactory(this.raftServiceFactory);
        if (raftOptions != null) {
            nodeOptions.setRaftOptions(raftOptions);
        }
        String serverDataPath = this.dataPath + File.separator + listenAddr.toString().replace(':', '_');
        new File(serverDataPath).mkdirs();
        nodeOptions.setLogUri(serverDataPath + File.separator + "logs");
        nodeOptions.setRaftMetaUri(serverDataPath + File.separator + "meta");
        nodeOptions.setSnapshotUri(serverDataPath + File.separator + "snapshot");
        nodeOptions.setElectionPriority(priority);
        // Align rpc options with election timeout.
        nodeOptions.setRpcConnectTimeoutMs(this.electionTimeoutMs / 3);
        nodeOptions.setRpcDefaultTimeout(this.electionTimeoutMs / 2);
        // Reduce default threads count per test node.
        nodeOptions.setRaftRpcThreadPoolSize(Utils.cpus());
        nodeOptions.setTimerPoolSize(Utils.cpus() * 2);
        nodeOptions.setRpcProcessorThreadPoolSize(Utils.cpus() * 3);
        nodeOptions.setElectionTimeoutStrategy(new ExponentialBackoffTimeoutStrategy());
        MockStateMachine fsm = new MockStateMachine(listenAddr);
        nodeOptions.setFsm(fsm);
        if (!emptyPeers)
            nodeOptions.setInitialConf(new Configuration(this.peers, this.learners));
        List<NetworkAddress> addressList = (emptyPeers ? Stream.<PeerId>empty() : peers.stream()).map(PeerId::getEndpoint).map(JRaftUtils::addressFromEndpoint).collect(toList());
        NodeManager nodeManager = new NodeManager();
        ClusterService clusterService = ClusterServiceTestUtils.clusterService(testInfo, listenAddr.getPort(), new StaticNodeFinder(addressList), new TestScaleCubeClusterServiceFactory());
        var rpcClient = new IgniteRpcClient(clusterService);
        nodeOptions.setRpcClient(rpcClient);
        ExecutorService requestExecutor = JRaftUtils.createRequestExecutor(nodeOptions);
        var rpcServer = new TestIgniteRpcServer(clusterService, nodeManager, nodeOptions, requestExecutor);
        clusterService.start();
        if (optsClo != null)
            optsClo.accept(nodeOptions);
        RaftGroupService server = new RaftGroupService(this.name, new PeerId(listenAddr, 0, priority), nodeOptions, rpcServer, nodeManager) {

            @Override
            public synchronized void shutdown() {
                // This stop order is consistent with JRaftServerImpl
                rpcServer.shutdown();
                ExecutorServiceHelper.shutdownAndAwaitTermination(requestExecutor);
                super.shutdown();
                // Network service must be stopped after a node because raft initiates timeoutnowrequest on stop for faster
                // leader election.
                clusterService.stop();
            }
        };
        this.serverMap.put(listenAddr, server);
        Node node = server.start();
        this.fsms.put(new PeerId(listenAddr, 0), fsm);
        this.nodes.add((NodeImpl) node);
        return true;
    } finally {
        this.lock.unlock();
    }
}
Also used : TestIgniteRpcServer(org.apache.ignite.raft.jraft.rpc.TestIgniteRpcServer) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) IgniteRpcClient(org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) Node(org.apache.ignite.raft.jraft.Node) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) NodeManager(org.apache.ignite.raft.jraft.NodeManager) ClusterService(org.apache.ignite.network.ClusterService) TestScaleCubeClusterServiceFactory(org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory) NetworkAddress(org.apache.ignite.network.NetworkAddress) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File) ExponentialBackoffTimeoutStrategy(org.apache.ignite.raft.jraft.util.ExponentialBackoffTimeoutStrategy) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 5 with IgniteRpcClient

use of org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient in project ignite-3 by apache.

the class IgniteRpcTest method waitForTopology.

/**
 * {@inheritDoc}
 */
@Override
protected boolean waitForTopology(RpcClient client, int expected, long timeout) {
    IgniteRpcClient client0 = (IgniteRpcClient) client;
    ClusterService service = client0.clusterService();
    return waitForTopology(service, expected, timeout);
}
Also used : ClusterService(org.apache.ignite.network.ClusterService) IgniteRpcClient(org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient)

Aggregations

IgniteRpcClient (org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient)6 ClusterService (org.apache.ignite.network.ClusterService)5 NetworkAddress (org.apache.ignite.network.NetworkAddress)4 StaticNodeFinder (org.apache.ignite.network.StaticNodeFinder)4 TestScaleCubeClusterServiceFactory (org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory)4 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)4 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)4 RaftGroupService (org.apache.ignite.raft.jraft.RaftGroupService)3 ExecutorService (java.util.concurrent.ExecutorService)2 NodeManager (org.apache.ignite.raft.jraft.NodeManager)2 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)2 TestIgniteRpcServer (org.apache.ignite.raft.jraft.rpc.TestIgniteRpcServer)2 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 LinkedHashSet (java.util.LinkedHashSet)1 IgniteInternalException (org.apache.ignite.lang.IgniteInternalException)1 JRaftUtils.addressFromEndpoint (org.apache.ignite.raft.jraft.JRaftUtils.addressFromEndpoint)1 Node (org.apache.ignite.raft.jraft.Node)1 CliOptions (org.apache.ignite.raft.jraft.option.CliOptions)1