Search in sources :

Example 11 with Node

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

the class ItNodeTest method testSetPeer1.

@Test
public void testSetPeer1() throws Exception {
    cluster = new TestCluster("testSetPeer1", dataPath, new ArrayList<>(), testInfo);
    PeerId bootPeer = new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT);
    assertTrue(cluster.start(bootPeer.getEndpoint()));
    List<Node> nodes = cluster.getFollowers();
    assertEquals(1, nodes.size());
    List<PeerId> peers = new ArrayList<>();
    peers.add(bootPeer);
    // reset peers from empty
    assertTrue(nodes.get(0).resetPeers(new Configuration(peers)).isOk());
    cluster.waitLeader();
    assertNotNull(cluster.getLeader());
}
Also used : Configuration(org.apache.ignite.raft.jraft.conf.Configuration) Node(org.apache.ignite.raft.jraft.Node) ArrayList(java.util.ArrayList) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 12 with Node

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

the class ItNodeTest method testLeaderTransfer.

@Test
public void testLeaderTransfer() throws Exception {
    List<PeerId> peers = TestUtils.generatePeers(3);
    cluster = new TestCluster("unitest", dataPath, peers, ELECTION_TIMEOUT_MILLIS, testInfo);
    for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint()));
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    sendTestTaskAndWait(leader);
    Thread.sleep(100);
    List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    PeerId targetPeer = followers.get(0).getNodeId().getPeerId().copy();
    LOG.info("Transfer leadership from {} to {}", leader, targetPeer);
    assertTrue(leader.transferLeadershipTo(targetPeer).isOk());
    cluster.waitLeader();
    leader = cluster.getLeader();
    assertEquals(leader.getNodeId().getPeerId(), targetPeer);
}
Also used : Node(org.apache.ignite.raft.jraft.Node) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 13 with Node

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

the class ItNodeTest method testRestoreSnapshot.

/**
 * @throws Exception
 */
@Test
public void testRestoreSnapshot() throws Exception {
    List<PeerId> peers = TestUtils.generatePeers(3);
    cluster = new TestCluster("unitest", dataPath, peers, testInfo);
    for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint()));
    cluster.waitLeader();
    // get leader
    Node leader = cluster.getLeader();
    LOG.info("Leader: " + leader);
    assertNotNull(leader);
    // apply tasks to leader
    sendTestTaskAndWait(leader);
    cluster.ensureSame();
    triggerLeaderSnapshot(cluster, leader);
    // stop leader
    Endpoint leaderAddr = leader.getNodeId().getPeerId().getEndpoint().copy();
    assertTrue(cluster.stop(leaderAddr));
    // restart leader
    cluster.waitLeader();
    assertEquals(0, cluster.getLeaderFsm().getLoadSnapshotTimes());
    assertTrue(cluster.start(leaderAddr));
    cluster.ensureSame();
    assertEquals(0, cluster.getLeaderFsm().getLoadSnapshotTimes());
}
Also used : Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) Node(org.apache.ignite.raft.jraft.Node) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 14 with Node

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

the class ItNodeTest method testLeaderShouldNotChange.

@Test
public void testLeaderShouldNotChange() throws Exception {
    List<PeerId> peers = TestUtils.generatePeers(3);
    cluster = new TestCluster("unitest", dataPath, peers, testInfo);
    for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint()));
    cluster.waitLeader();
    // get leader
    Node leader0 = cluster.getLeader();
    assertNotNull(leader0);
    long savedTerm = ((NodeImpl) leader0).getCurrentTerm();
    LOG.info("Current leader is {}, term is {}", leader0, savedTerm);
    Thread.sleep(5000);
    cluster.waitLeader();
    Node leader1 = cluster.getLeader();
    assertNotNull(leader1);
    LOG.info("Current leader is {}", leader1);
    assertEquals(savedTerm, ((NodeImpl) leader1).getCurrentTerm());
}
Also used : Node(org.apache.ignite.raft.jraft.Node) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 15 with Node

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

the class ItNodeTest method testBlockedElection.

@Test
public void testBlockedElection() throws Exception {
    List<PeerId> peers = TestUtils.generatePeers(3);
    cluster = new TestCluster("unittest", dataPath, peers, testInfo);
    for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint()));
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    LOG.warn("Current leader {}, electTimeout={}", leader.getNodeId().getPeerId(), leader.getOptions().getElectionTimeoutMs());
    List<Node> followers = cluster.getFollowers();
    blockMessagesOnFollowers(followers, (msg, nodeId) -> {
        if (msg instanceof RpcRequests.RequestVoteRequest) {
            RpcRequests.RequestVoteRequest msg0 = (RpcRequests.RequestVoteRequest) msg;
            return !msg0.preVote();
        }
        return false;
    });
    LOG.warn("Stop leader {}, curTerm={}", leader.getNodeId().getPeerId(), ((NodeImpl) leader).getCurrentTerm());
    assertTrue(cluster.stop(leader.getNodeId().getPeerId().getEndpoint()));
    assertNull(cluster.getLeader());
    Thread.sleep(2000);
    assertNull(cluster.getLeader());
    stopBlockingMessagesOnFollowers(followers);
    // elect new leader
    cluster.waitLeader();
    leader = cluster.getLeader();
    LOG.info("Elect new leader is {}, curTerm={}", leader.getLeaderId(), ((NodeImpl) leader).getCurrentTerm());
}
Also used : Node(org.apache.ignite.raft.jraft.Node) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) 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