use of org.apache.ignite.raft.jraft.entity.PeerId in project ignite-3 by apache.
the class ItCliServiceTest method setup.
/**
* Executes before each test.
*/
@BeforeEach
public void setup(TestInfo testInfo, @WorkDirectory Path dataPath) throws Exception {
LOG.info(">>>>>>>>>>>>>>> Start test method: " + testInfo.getDisplayName());
List<PeerId> peers = TestUtils.generatePeers(3);
LinkedHashSet<PeerId> learners = new LinkedHashSet<>();
// 2 learners
for (int i = 0; i < 2; i++) {
learners.add(new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT + LEARNER_PORT_STEP + i));
}
cluster = new TestCluster(groupId, dataPath.toString(), peers, learners, ELECTION_TIMEOUT_MILLIS, testInfo);
for (PeerId peer : peers) {
cluster.start(peer.getEndpoint());
}
for (PeerId peer : learners) {
cluster.startLearner(peer);
}
cluster.waitLeader();
cluster.ensureLeader(cluster.getLeader());
cliService = new CliServiceImpl();
conf = new Configuration(peers, learners);
CliOptions opts = new CliOptions();
clientExecutor = JRaftUtils.createClientExecutor(opts, "client");
opts.setClientExecutor(clientExecutor);
List<NetworkAddress> addressList = peers.stream().map(PeerId::getEndpoint).map(JRaftUtils::addressFromEndpoint).collect(toList());
ClusterService clientSvc = ClusterServiceTestUtils.clusterService(testInfo, TestUtils.INIT_PORT - 1, new StaticNodeFinder(addressList), new TestScaleCubeClusterServiceFactory());
clientSvc.start();
IgniteRpcClient rpcClient = new IgniteRpcClient(clientSvc) {
@Override
public void shutdown() {
super.shutdown();
clientSvc.stop();
}
};
opts.setRpcClient(rpcClient);
assertTrue(cliService.init(opts));
}
use of org.apache.ignite.raft.jraft.entity.PeerId in project ignite-3 by apache.
the class ItCliServiceTest method testRebalance.
@Test
public void testRebalance() {
Set<String> groupIds = new TreeSet<>();
groupIds.add("group_1");
groupIds.add("group_2");
groupIds.add("group_3");
groupIds.add("group_4");
groupIds.add("group_5");
groupIds.add("group_6");
groupIds.add("group_7");
groupIds.add("group_8");
Configuration conf = new Configuration();
conf.addPeer(new PeerId("host_1", 8080));
conf.addPeer(new PeerId("host_2", 8080));
conf.addPeer(new PeerId("host_3", 8080));
Map<String, PeerId> rebalancedLeaderIds = new HashMap<>();
CliService cliService = new MockCliService(rebalancedLeaderIds, new PeerId("host_1", 8080));
assertTrue(cliService.rebalance(groupIds, conf, rebalancedLeaderIds).isOk());
assertEquals(groupIds.size(), rebalancedLeaderIds.size());
Map<PeerId, Integer> ret = new HashMap<>();
for (Map.Entry<String, PeerId> entry : rebalancedLeaderIds.entrySet()) {
ret.compute(entry.getValue(), (ignored, num) -> num == null ? 1 : num + 1);
}
int expectedAvgLeaderNum = (int) Math.ceil((double) groupIds.size() / conf.size());
for (Map.Entry<PeerId, Integer> entry : ret.entrySet()) {
System.out.println(entry);
assertTrue(entry.getValue() <= expectedAvgLeaderNum);
}
}
use of org.apache.ignite.raft.jraft.entity.PeerId in project ignite-3 by apache.
the class ItCliServiceTest method testLearnerServices.
@Test
public void testLearnerServices() throws Exception {
PeerId learner3 = new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT + LEARNER_PORT_STEP + 3);
assertTrue(cluster.startLearner(learner3));
sendTestTaskAndWait(cluster.getLeader(), 0);
cluster.ensureSame(addr -> addr.equals(learner3.getEndpoint()));
for (MockStateMachine fsm : cluster.getFsms()) {
if (!fsm.getAddress().equals(learner3.getEndpoint())) {
assertEquals(10, fsm.getLogs().size());
}
}
assertEquals(0, cluster.getFsmByPeer(learner3).getLogs().size());
List<PeerId> oldLearners = new ArrayList<PeerId>(conf.getLearners());
assertEquals(oldLearners, cliService.getLearners(groupId, conf));
assertEquals(oldLearners, cliService.getAliveLearners(groupId, conf));
// Add learner3
cliService.addLearners(groupId, conf, Collections.singletonList(learner3));
assertTrue(waitForCondition(() -> cluster.getFsmByPeer(learner3).getLogs().size() == 10, 5_000));
sendTestTaskAndWait(cluster.getLeader(), 0);
cluster.ensureSame();
for (MockStateMachine fsm : cluster.getFsms()) {
assertEquals(20, fsm.getLogs().size());
}
List<PeerId> newLearners = new ArrayList<>(oldLearners);
newLearners.add(learner3);
assertEquals(newLearners, cliService.getLearners(groupId, conf));
assertEquals(newLearners, cliService.getAliveLearners(groupId, conf));
// Remove 3
cliService.removeLearners(groupId, conf, Collections.singletonList(learner3));
sendTestTaskAndWait(cluster.getLeader(), 0);
cluster.ensureSame(addr -> addr.equals(learner3.getEndpoint()));
for (MockStateMachine fsm : cluster.getFsms()) {
if (!fsm.getAddress().equals(learner3.getEndpoint())) {
assertEquals(30, fsm.getLogs().size());
}
}
// Latest 10 logs are not replicated to learner3, because it's removed.
assertEquals(20, cluster.getFsmByPeer(learner3).getLogs().size());
assertEquals(oldLearners, cliService.getLearners(groupId, conf));
assertEquals(oldLearners, cliService.getAliveLearners(groupId, conf));
// Set learners into [learner3]
cliService.resetLearners(groupId, conf, Collections.singletonList(learner3));
assertTrue(waitForCondition(() -> cluster.getFsmByPeer(learner3).getLogs().size() == 30, 5_000));
sendTestTaskAndWait(cluster.getLeader(), 0);
cluster.ensureSame(addr -> oldLearners.contains(new PeerId(addr, 0)));
// Latest 10 logs are not replicated to learner1 and learner2, because they were removed by resetting learners set.
for (MockStateMachine fsm : cluster.getFsms()) {
if (!oldLearners.contains(new PeerId(fsm.getAddress(), 0))) {
assertEquals(40, fsm.getLogs().size());
} else {
assertEquals(30, fsm.getLogs().size());
}
}
assertEquals(Collections.singletonList(learner3), cliService.getLearners(groupId, conf));
assertEquals(Collections.singletonList(learner3), cliService.getAliveLearners(groupId, conf));
// Stop learner3
cluster.stop(learner3.getEndpoint());
sleep(1000);
assertEquals(Collections.singletonList(learner3), cliService.getLearners(groupId, conf));
assertTrue(cliService.getAliveLearners(groupId, conf).isEmpty());
}
use of org.apache.ignite.raft.jraft.entity.PeerId in project ignite-3 by apache.
the class ItCliServiceTest method testChangePeers.
@Test
public void testChangePeers() throws Exception {
List<PeerId> newPeers = TestUtils.generatePeers(6);
newPeers.removeAll(conf.getPeerSet());
assertEquals(3, newPeers.size());
for (PeerId peer : newPeers) {
assertTrue(cluster.start(peer.getEndpoint()));
}
cluster.waitLeader();
Node oldLeaderNode = cluster.getLeader();
assertNotNull(oldLeaderNode);
PeerId oldLeader = oldLeaderNode.getNodeId().getPeerId();
assertNotNull(oldLeader);
Status status = cliService.changePeers(groupId, conf, new Configuration(newPeers));
assertTrue(status.isOk(), status.getErrorMsg());
cluster.waitLeader();
PeerId newLeader = cluster.getLeader().getNodeId().getPeerId();
assertNotEquals(oldLeader, newLeader);
assertTrue(newPeers.contains(newLeader));
}
use of org.apache.ignite.raft.jraft.entity.PeerId in project ignite-3 by apache.
the class ItCliServiceTest method testRelalanceOnTransferLeaderFail.
@Test
public void testRelalanceOnTransferLeaderFail() {
Set<String> groupIds = new TreeSet<>();
groupIds.add("group_1");
groupIds.add("group_2");
groupIds.add("group_3");
groupIds.add("group_4");
groupIds.add("group_5");
groupIds.add("group_6");
groupIds.add("group_7");
Configuration conf = new Configuration();
conf.addPeer(new PeerId("host_1", 8080));
conf.addPeer(new PeerId("host_2", 8080));
conf.addPeer(new PeerId("host_3", 8080));
Map<String, PeerId> rebalancedLeaderIds = new HashMap<>();
CliService cliService = new MockTransferLeaderFailCliService(rebalancedLeaderIds, new PeerId("host_1", 8080));
assertEquals("Fail to transfer leader", cliService.rebalance(groupIds, conf, rebalancedLeaderIds).getErrorMsg());
assertTrue(groupIds.size() >= rebalancedLeaderIds.size());
Map<PeerId, Integer> ret = new HashMap<>();
for (Map.Entry<String, PeerId> entry : rebalancedLeaderIds.entrySet()) {
ret.compute(entry.getValue(), (ignored, num) -> num == null ? 1 : num + 1);
}
for (Map.Entry<PeerId, Integer> entry : ret.entrySet()) {
LOG.info(entry.toString());
assertEquals(new PeerId("host_1", 8080), entry.getKey());
}
}
Aggregations