Search in sources :

Example 81 with Node

use of org.apache.ignite.raft.jraft.Node 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 82 with Node

use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.

the class TestCluster method ensureSame.

/**
 * @param filter The node to exclude filter.
 * @return {@code True} if all FSM state are the same.
 * @throws InterruptedException
 */
public void ensureSame(Predicate<Endpoint> filter) throws InterruptedException {
    this.lock.lock();
    List<MockStateMachine> fsmList = new ArrayList<>(this.fsms.values());
    if (fsmList.size() <= 1) {
        LOG.warn("ensureSame is skipped because only one node in the group");
        this.lock.unlock();
        return;
    }
    Node leader = getLeader();
    assertNotNull(leader);
    MockStateMachine first = fsms.get(leader.getNodeId().getPeerId());
    LOG.info("Start ensureSame, leader={}", leader);
    try {
        assertTrue(TestUtils.waitForCondition(() -> {
            first.lock();
            try {
                for (int i = 0; i < fsmList.size(); i++) {
                    MockStateMachine fsm = fsmList.get(i);
                    if (fsm == first || filter.test(fsm.getAddress()))
                        continue;
                    fsm.lock();
                    try {
                        int size0 = first.getLogs().size();
                        int size1 = fsm.getLogs().size();
                        if (size0 == 0 || size0 != size1)
                            return false;
                        for (int j = 0; j < size0; j++) {
                            ByteBuffer data0 = first.getLogs().get(j);
                            ByteBuffer data1 = fsm.getLogs().get(j);
                            if (!data0.equals(data1))
                                return false;
                        }
                    } finally {
                        fsm.unlock();
                    }
                }
            } finally {
                first.unlock();
            }
            return true;
        }, 20_000));
    } finally {
        this.lock.unlock();
        Node leader1 = getLeader();
        LOG.info("End ensureSame, leader={}", leader1);
        assertSame(leader, leader1, "Leader shouldn't change while comparing fsms");
    }
}
Also used : Node(org.apache.ignite.raft.jraft.Node) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer)

Example 83 with Node

use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.

the class BaseCliRequestProcessorTest method mockNode.

private Node mockNode(boolean disableCli) {
    Node node = Mockito.mock(Node.class);
    Mockito.when(node.getGroupId()).thenReturn("test");
    Mockito.when(node.getNodeId()).thenReturn(new NodeId("test", this.peer.copy()));
    NodeOptions opts = new NodeOptions();
    opts.setDisableCli(disableCli);
    Mockito.when(node.getOptions()).thenReturn(opts);
    this.asyncContext.getNodeManager().add(node);
    return node;
}
Also used : Node(org.apache.ignite.raft.jraft.Node) NodeId(org.apache.ignite.raft.jraft.entity.NodeId) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions)

Example 84 with Node

use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.

the class BaseCliRequestProcessorTest method testSingleNode.

@Test
public void testSingleNode() {
    Node node = this.mockNode(false);
    this.processor = new MockCliRequestProcessor(null, "test");
    this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
    ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
    assertNotNull(resp);
    assertSame(this.processor.ctx.node, node);
    assertNotNull(resp);
    assertEquals(0, resp.errorCode());
}
Also used : Node(org.apache.ignite.raft.jraft.Node) ErrorResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Node (org.apache.ignite.raft.jraft.Node)84 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)75 Test (org.junit.jupiter.api.Test)66 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)38 CountDownLatch (java.util.concurrent.CountDownLatch)30 ArrayList (java.util.ArrayList)26 Status (org.apache.ignite.raft.jraft.Status)23 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)23 Task (org.apache.ignite.raft.jraft.entity.Task)18 SynchronizedClosure (org.apache.ignite.raft.jraft.closure.SynchronizedClosure)17 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)16 ByteBuffer (java.nio.ByteBuffer)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 RaftGroupService (org.apache.ignite.raft.jraft.RaftGroupService)12 RaftException (org.apache.ignite.raft.jraft.error.RaftException)11 LinkedHashSet (java.util.LinkedHashSet)10 ReadIndexClosure (org.apache.ignite.raft.jraft.closure.ReadIndexClosure)10 List (java.util.List)9 LogIndexOutOfBoundsException (org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException)9 LogNotFoundException (org.apache.ignite.raft.jraft.error.LogNotFoundException)9