Search in sources :

Example 21 with Node

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

the class TestCluster method removeNode.

public Node removeNode(Endpoint addr) {
    Node ret = null;
    this.lock.lock();
    try {
        for (int i = 0; i < this.nodes.size(); i++) {
            if (this.nodes.get(i).getNodeId().getPeerId().getEndpoint().equals(addr)) {
                ret = this.nodes.remove(i);
                this.fsms.remove(ret.getNodeId().getPeerId());
                break;
            }
        }
    } finally {
        this.lock.unlock();
    }
    return ret;
}
Also used : Node(org.apache.ignite.raft.jraft.Node) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint)

Example 22 with Node

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

the class ItNodeTest method testNodesWithSpecialPriorityElection.

@Test
public void testNodesWithSpecialPriorityElection() throws Exception {
    List<Integer> priorities = new ArrayList<>();
    priorities.add(0);
    priorities.add(0);
    priorities.add(-1);
    List<PeerId> peers = TestUtils.generatePriorityPeers(3, priorities);
    cluster = new TestCluster("unittest", dataPath, peers, testInfo);
    for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint(), peer.getPriority()));
    // elect leader
    cluster.waitLeader();
    // get leader
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    cluster.ensureLeader(leader);
    assertEquals(3, leader.listPeers().size());
    assertEquals(2, cluster.getFollowers().size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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 23 with Node

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

the class ItNodeTest method testBootStrapWithoutSnapshot.

@Test
public void testBootStrapWithoutSnapshot() throws Exception {
    Endpoint addr = new Endpoint("127.0.0.1", 5006);
    MockStateMachine fsm = new MockStateMachine(addr);
    BootstrapOptions opts = new BootstrapOptions();
    opts.setServiceFactory(new DefaultJRaftServiceFactory());
    opts.setLastLogIndex(0);
    opts.setRaftMetaUri(dataPath + File.separator + "meta");
    opts.setLogUri(dataPath + File.separator + "log");
    opts.setSnapshotUri(dataPath + File.separator + "snapshot");
    opts.setGroupConf(JRaftUtils.getConfiguration("127.0.0.1:5006"));
    opts.setFsm(fsm);
    NodeOptions nodeOpts = createNodeOptions();
    opts.setNodeOptions(nodeOpts);
    assertTrue(JRaftUtils.bootstrap(opts));
    nodeOpts.setRaftMetaUri(dataPath + File.separator + "meta");
    nodeOpts.setLogUri(dataPath + File.separator + "log");
    nodeOpts.setSnapshotUri(dataPath + File.separator + "snapshot");
    nodeOpts.setFsm(fsm);
    RaftGroupService service = createService("test", new PeerId(addr, 0), nodeOpts);
    Node node = service.start();
    while (!node.isLeader()) Thread.sleep(20);
    sendTestTaskAndWait(node);
    assertEquals(10, fsm.getLogs().size());
}
Also used : Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) Node(org.apache.ignite.raft.jraft.Node) BootstrapOptions(org.apache.ignite.raft.jraft.option.BootstrapOptions) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 24 with Node

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

the class ItNodeTest method testInstallSnapshotWithThrottle.

@Test
public void testInstallSnapshotWithThrottle() throws Exception {
    List<PeerId> peers = TestUtils.generatePeers(3);
    cluster = new TestCluster("unitest", dataPath, peers, testInfo);
    for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint(), false, 200, false, new ThroughputSnapshotThrottle(1024, 1)));
    cluster.waitLeader();
    // get leader
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    cluster.ensureLeader(leader);
    // apply tasks to leader
    sendTestTaskAndWait(leader);
    cluster.ensureSame();
    // stop follower1
    List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    Endpoint followerAddr = followers.get(0).getNodeId().getPeerId().getEndpoint();
    assertTrue(cluster.stop(followerAddr));
    cluster.waitLeader();
    // apply something more
    sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
    Thread.sleep(1000);
    // trigger leader snapshot
    triggerLeaderSnapshot(cluster, leader);
    // apply something more
    sendTestTaskAndWait(leader, 20, RaftError.SUCCESS);
    // trigger leader snapshot
    triggerLeaderSnapshot(cluster, leader, 2);
    // wait leader to compact logs
    Thread.sleep(1000);
    // restart follower.
    cluster.clean(followerAddr);
    assertTrue(cluster.start(followerAddr, true, 300, false, new ThroughputSnapshotThrottle(1024, 1)));
    Thread.sleep(2000);
    cluster.ensureSame();
    assertEquals(3, cluster.getFsms().size());
    for (MockStateMachine fsm : cluster.getFsms()) assertEquals(30, fsm.getLogs().size());
}
Also used : Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) ThroughputSnapshotThrottle(org.apache.ignite.raft.jraft.storage.snapshot.ThroughputSnapshotThrottle) Node(org.apache.ignite.raft.jraft.Node) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 25 with Node

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

the class ItNodeTest method testPreVote.

@Test
public void testPreVote() 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();
    long savedTerm = ((NodeImpl) leader).getCurrentTerm();
    assertNotNull(leader);
    // apply tasks to leader
    sendTestTaskAndWait(leader);
    cluster.ensureSame();
    List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    PeerId followerPeer = followers.get(0).getNodeId().getPeerId();
    Endpoint followerAddr = followerPeer.getEndpoint();
    // remove follower
    LOG.info("Remove follower {}", followerPeer);
    CountDownLatch latch = new CountDownLatch(1);
    leader.removePeer(followerPeer, new ExpectClosure(latch));
    waitLatch(latch);
    sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
    Thread.sleep(2000);
    // add follower
    LOG.info("Add follower {}", followerAddr);
    peers = TestUtils.generatePeers(3);
    assertTrue(peers.remove(followerPeer));
    latch = new CountDownLatch(1);
    leader.addPeer(followerPeer, new ExpectClosure(latch));
    waitLatch(latch);
    leader = cluster.getLeader();
    assertNotNull(leader);
    // leader term should not be changed.
    assertEquals(savedTerm, ((NodeImpl) leader).getCurrentTerm());
}
Also used : Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) Node(org.apache.ignite.raft.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) 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