use of org.apache.ignite.raft.jraft.core.TestCluster.ELECTION_TIMEOUT_MILLIS 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());
}
use of org.apache.ignite.raft.jraft.core.TestCluster.ELECTION_TIMEOUT_MILLIS in project ignite-3 by apache.
the class ItNodeTest method testElectionTimeoutAutoAdjustWhenBlockedMessages.
private void testElectionTimeoutAutoAdjustWhenBlockedMessages(BiPredicate<Object, String> blockingPredicate) throws Exception {
List<PeerId> peers = TestUtils.generatePeers(4);
int maxElectionRoundsWithoutAdjusting = 3;
cluster = new TestCluster("unittest", dataPath, peers, new LinkedHashSet<>(), ELECTION_TIMEOUT_MILLIS, opts -> opts.setElectionTimeoutStrategy(new ExponentialBackoffTimeoutStrategy(11_000, maxElectionRoundsWithoutAdjusting)), testInfo);
for (PeerId peer : peers) {
assertTrue(cluster.start(peer.getEndpoint()));
}
cluster.waitLeader();
Node leader = cluster.getLeader();
int initElectionTimeout = leader.getOptions().getElectionTimeoutMs();
LOG.warn("Current leader {}, electTimeout={}", leader.getNodeId().getPeerId(), leader.getOptions().getElectionTimeoutMs());
List<Node> followers = cluster.getFollowers();
for (Node follower : followers) {
NodeImpl follower0 = (NodeImpl) follower;
assertEquals(initElectionTimeout, follower0.getOptions().getElectionTimeoutMs());
}
blockMessagesOnFollowers(followers, blockingPredicate);
LOG.warn("Stop leader {}, curTerm={}", leader.getNodeId().getPeerId(), ((NodeImpl) leader).getCurrentTerm());
assertTrue(cluster.stop(leader.getNodeId().getPeerId().getEndpoint()));
assertNull(cluster.getLeader());
assertTrue(waitForCondition(() -> followers.stream().allMatch(f -> f.getOptions().getElectionTimeoutMs() > initElectionTimeout), (long) maxElectionRoundsWithoutAdjusting * // need to multiply to 2 because stepDown happens after voteTimer timeout
(initElectionTimeout + followers.get(0).getOptions().getRaftOptions().getMaxElectionDelayMs()) * 2));
stopBlockingMessagesOnFollowers(followers);
// elect new leader
cluster.waitLeader();
leader = cluster.getLeader();
LOG.info("Elected new leader is {}, curTerm={}", leader.getLeaderId(), ((NodeImpl) leader).getCurrentTerm());
assertTrue(waitForCondition(() -> followers.stream().allMatch(f -> f.getOptions().getElectionTimeoutMs() == initElectionTimeout), 3_000));
}
use of org.apache.ignite.raft.jraft.core.TestCluster.ELECTION_TIMEOUT_MILLIS in project ignite-3 by apache.
the class ItNodeTest method testTripleNodesWithReplicatorStateListener.
@Test
public void testTripleNodesWithReplicatorStateListener() throws Exception {
List<PeerId> peers = TestUtils.generatePeers(3);
// final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
UserReplicatorStateListener listener1 = new UserReplicatorStateListener();
UserReplicatorStateListener listener2 = new UserReplicatorStateListener();
cluster = new TestCluster("unitest", dataPath, peers, new LinkedHashSet<>(), ELECTION_TIMEOUT_MILLIS, opts -> opts.setReplicationStateListeners(List.of(listener1, listener2)), testInfo);
for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint()));
// elect leader
cluster.waitLeader();
cluster.ensureLeader(cluster.getLeader());
for (Node follower : cluster.getFollowers()) waitForCondition(() -> follower.getLeaderId() != null, 5_000);
assertEquals(4, startedCounter.get());
assertEquals(2, cluster.getLeader().getReplicatorStateListeners().size());
assertEquals(2, cluster.getFollowers().get(0).getReplicatorStateListeners().size());
assertEquals(2, cluster.getFollowers().get(1).getReplicatorStateListeners().size());
for (Node node : cluster.getNodes()) node.removeReplicatorStateListener(listener1);
assertEquals(1, cluster.getLeader().getReplicatorStateListeners().size());
assertEquals(1, cluster.getFollowers().get(0).getReplicatorStateListeners().size());
assertEquals(1, cluster.getFollowers().get(1).getReplicatorStateListeners().size());
}
Aggregations