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;
}
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());
}
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());
}
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());
}
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());
}
Aggregations