use of org.apache.ignite.raft.jraft.util.ExponentialBackoffTimeoutStrategy 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.util.ExponentialBackoffTimeoutStrategy in project ignite-3 by apache.
the class TestCluster method start.
public boolean start(Endpoint listenAddr, boolean emptyPeers, int snapshotIntervalSecs, boolean enableMetrics, SnapshotThrottle snapshotThrottle, RaftOptions raftOptions, int priority) throws IOException {
this.lock.lock();
try {
if (this.serverMap.get(listenAddr) != null) {
return true;
}
// Start node in non shared pools mode. Pools will be managed by node itself.
NodeOptions nodeOptions = new NodeOptions();
nodeOptions.setServerName(listenAddr.toString());
nodeOptions.setElectionTimeoutMs(this.electionTimeoutMs);
nodeOptions.setEnableMetrics(enableMetrics);
nodeOptions.setSnapshotThrottle(snapshotThrottle);
nodeOptions.setSnapshotIntervalSecs(snapshotIntervalSecs);
nodeOptions.setServiceFactory(this.raftServiceFactory);
if (raftOptions != null) {
nodeOptions.setRaftOptions(raftOptions);
}
String serverDataPath = this.dataPath + File.separator + listenAddr.toString().replace(':', '_');
new File(serverDataPath).mkdirs();
nodeOptions.setLogUri(serverDataPath + File.separator + "logs");
nodeOptions.setRaftMetaUri(serverDataPath + File.separator + "meta");
nodeOptions.setSnapshotUri(serverDataPath + File.separator + "snapshot");
nodeOptions.setElectionPriority(priority);
// Align rpc options with election timeout.
nodeOptions.setRpcConnectTimeoutMs(this.electionTimeoutMs / 3);
nodeOptions.setRpcDefaultTimeout(this.electionTimeoutMs / 2);
// Reduce default threads count per test node.
nodeOptions.setRaftRpcThreadPoolSize(Utils.cpus());
nodeOptions.setTimerPoolSize(Utils.cpus() * 2);
nodeOptions.setRpcProcessorThreadPoolSize(Utils.cpus() * 3);
nodeOptions.setElectionTimeoutStrategy(new ExponentialBackoffTimeoutStrategy());
MockStateMachine fsm = new MockStateMachine(listenAddr);
nodeOptions.setFsm(fsm);
if (!emptyPeers)
nodeOptions.setInitialConf(new Configuration(this.peers, this.learners));
List<NetworkAddress> addressList = (emptyPeers ? Stream.<PeerId>empty() : peers.stream()).map(PeerId::getEndpoint).map(JRaftUtils::addressFromEndpoint).collect(toList());
NodeManager nodeManager = new NodeManager();
ClusterService clusterService = ClusterServiceTestUtils.clusterService(testInfo, listenAddr.getPort(), new StaticNodeFinder(addressList), new TestScaleCubeClusterServiceFactory());
var rpcClient = new IgniteRpcClient(clusterService);
nodeOptions.setRpcClient(rpcClient);
ExecutorService requestExecutor = JRaftUtils.createRequestExecutor(nodeOptions);
var rpcServer = new TestIgniteRpcServer(clusterService, nodeManager, nodeOptions, requestExecutor);
clusterService.start();
if (optsClo != null)
optsClo.accept(nodeOptions);
RaftGroupService server = new RaftGroupService(this.name, new PeerId(listenAddr, 0, priority), nodeOptions, rpcServer, nodeManager) {
@Override
public synchronized void shutdown() {
// This stop order is consistent with JRaftServerImpl
rpcServer.shutdown();
ExecutorServiceHelper.shutdownAndAwaitTermination(requestExecutor);
super.shutdown();
// Network service must be stopped after a node because raft initiates timeoutnowrequest on stop for faster
// leader election.
clusterService.stop();
}
};
this.serverMap.put(listenAddr, server);
Node node = server.start();
this.fsms.put(new PeerId(listenAddr, 0), fsm);
this.nodes.add((NodeImpl) node);
return true;
} finally {
this.lock.unlock();
}
}
Aggregations