use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
the class ItNodeTest method testSetPeer2.
@Test
public void testSetPeer2() 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();
assertNotNull(leader);
// apply tasks to leader
sendTestTaskAndWait(leader);
cluster.ensureSame();
List<Node> followers = cluster.getFollowers();
assertEquals(2, followers.size());
PeerId followerPeer1 = followers.get(0).getNodeId().getPeerId();
Endpoint followerAddr1 = followerPeer1.getEndpoint();
PeerId followerPeer2 = followers.get(1).getNodeId().getPeerId();
Endpoint followerAddr2 = followerPeer2.getEndpoint();
LOG.info("Stop and clean follower {}", followerPeer1);
assertTrue(cluster.stop(followerAddr1));
cluster.clean(followerAddr1);
// apply tasks to leader again
sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
// set peer when no quorum die
Endpoint leaderAddr = leader.getLeaderId().getEndpoint().copy();
LOG.info("Set peers to {}", leaderAddr);
LOG.info("Stop and clean follower {}", followerPeer2);
assertTrue(cluster.stop(followerAddr2));
cluster.clean(followerAddr2);
assertTrue(waitForTopology(cluster, leaderAddr, 1, 5_000));
// leader will step-down, become follower
Thread.sleep(2000);
List<PeerId> newPeers = new ArrayList<>();
newPeers.add(new PeerId(leaderAddr, 0));
// new peers equal to current conf
assertTrue(leader.resetPeers(new Configuration(peers)).isOk());
// set peer when quorum die
LOG.warn("Set peers to {}", leaderAddr);
assertTrue(leader.resetPeers(new Configuration(newPeers)).isOk());
cluster.waitLeader();
leader = cluster.getLeader();
assertNotNull(leader);
assertEquals(leaderAddr, leader.getNodeId().getPeerId().getEndpoint());
LOG.info("start follower {}", followerAddr1);
assertTrue(cluster.start(followerAddr1, true, 300));
LOG.info("start follower {}", followerAddr2);
assertTrue(cluster.start(followerAddr2, true, 300));
CountDownLatch latch = new CountDownLatch(1);
LOG.info("Add old follower {}", followerAddr1);
leader.addPeer(followerPeer1, new ExpectClosure(latch));
waitLatch(latch);
latch = new CountDownLatch(1);
LOG.info("Add old follower {}", followerAddr2);
leader.addPeer(followerPeer2, new ExpectClosure(latch));
waitLatch(latch);
newPeers.add(followerPeer1);
newPeers.add(followerPeer2);
cluster.ensureSame();
assertEquals(3, cluster.getFsms().size());
for (MockStateMachine fsm : cluster.getFsms()) assertEquals(20, fsm.getLogs().size());
}
use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
the class ItNodeTest method testShuttingDownLeaderTriggerTimeoutNow.
/**
* 4.2.2 Removing the current leader
*
* @throws Exception If failed.
*/
@Test
public void testShuttingDownLeaderTriggerTimeoutNow() 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);
Node oldLeader = leader;
LOG.info("Shutdown leader {}", leader);
leader.shutdown();
leader.join();
cluster.waitLeader();
leader = cluster.getLeader();
assertNotNull(leader);
assertNotSame(leader, oldLeader);
}
use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
the class ItNodeTest method blockMessagesOnFollowers.
private void blockMessagesOnFollowers(List<Node> followers, BiPredicate<Object, String> blockingPredicate) {
for (Node follower : followers) {
RpcClientEx rpcClientEx = sender(follower);
rpcClientEx.blockMessages(blockingPredicate);
}
}
use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
the class ItNodeTest method testTripleNodes.
@Test
public void testTripleNodes() throws Exception {
List<PeerId> peers = TestUtils.generatePeers(3);
cluster = new TestCluster("unittest", dataPath, peers, testInfo);
for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint()));
// elect leader
cluster.waitLeader();
// get leader
Node leader = cluster.getLeader();
assertNotNull(leader);
cluster.ensureLeader(leader);
assertEquals(3, leader.listPeers().size());
// apply tasks to leader
sendTestTaskAndWait(leader);
{
ByteBuffer data = ByteBuffer.wrap("no closure".getBytes(UTF_8));
Task task = new Task(data, null);
leader.apply(task);
}
{
// task with TaskClosure
ByteBuffer data = ByteBuffer.wrap("task closure".getBytes(UTF_8));
List<String> cbs = synchronizedList(new ArrayList<>());
CountDownLatch latch = new CountDownLatch(1);
Task task = new Task(data, new TaskClosure() {
@Override
public void run(Status status) {
cbs.add("apply");
latch.countDown();
}
@Override
public void onCommitted() {
cbs.add("commit");
}
});
leader.apply(task);
latch.await();
assertEquals(2, cbs.size());
assertEquals("commit", cbs.get(0));
assertEquals("apply", cbs.get(1));
}
cluster.ensureSame();
assertEquals(2, cluster.getFollowers().size());
}
use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
the class ItNodeTest method testLeaderTransferWithReplicatorStateListener.
@Test
public void testLeaderTransferWithReplicatorStateListener() throws Exception {
List<PeerId> peers = TestUtils.generatePeers(3);
cluster = new TestCluster("unitest", dataPath, peers, new LinkedHashSet<>(), ELECTION_TIMEOUT_MILLIS, opts -> opts.setReplicationStateListeners(List.of(new UserReplicatorStateListener())), testInfo);
for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint()));
cluster.waitLeader();
Node leader = cluster.getLeader();
cluster.ensureLeader(leader);
sendTestTaskAndWait(leader);
Thread.sleep(100);
List<Node> followers = cluster.getFollowers();
assertTrue(waitForCondition(() -> startedCounter.get() == 2, 5_000), startedCounter.get() + "");
PeerId targetPeer = followers.get(0).getNodeId().getPeerId().copy();
LOG.info("Transfer leadership from {} to {}", leader, targetPeer);
assertTrue(leader.transferLeadershipTo(targetPeer).isOk());
Thread.sleep(1000);
cluster.waitLeader();
assertTrue(waitForCondition(() -> startedCounter.get() == 4, 5_000), startedCounter.get() + "");
for (Node node : cluster.getNodes()) node.clearReplicatorStateListeners();
assertEquals(0, cluster.getLeader().getReplicatorStateListeners().size());
assertEquals(0, cluster.getFollowers().get(0).getReplicatorStateListeners().size());
assertEquals(0, cluster.getFollowers().get(1).getReplicatorStateListeners().size());
}
Aggregations