use of com.alipay.sofa.jraft.CliService in project sofa-jraft by sofastack.
the class CliServiceTest method testRebalanceOnLeaderFail.
@Test
public void testRebalanceOnLeaderFail() {
final Set<String> groupIds = new TreeSet<>();
groupIds.add("group_1");
groupIds.add("group_2");
groupIds.add("group_3");
groupIds.add("group_4");
final 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));
final Map<String, PeerId> rebalancedLeaderIds = new HashMap<>();
final CliService cliService = new MockLeaderFailCliService();
assertEquals("Fail to get leader", cliService.rebalance(groupIds, conf, rebalancedLeaderIds).getErrorMsg());
}
use of com.alipay.sofa.jraft.CliService in project mmqtt by MrHKing.
the class JRaftMaintainService method execute.
/**
* Execute relevant commands.
*
* @param args {@link Map}
* @return {@link RestResult}
*/
public RestResult<String> execute(Map<String, String> args) {
final CliService cliService = raftServer.getCliService();
if (args.containsKey(JRaftConstants.GROUP_ID)) {
final String groupId = args.get(JRaftConstants.GROUP_ID);
final Node node = raftServer.findNodeByGroup(groupId);
return single(cliService, groupId, node, args);
}
Map<String, JRaftServer.RaftGroupTuple> tupleMap = raftServer.getMultiRaftGroup();
for (Map.Entry<String, JRaftServer.RaftGroupTuple> entry : tupleMap.entrySet()) {
final String group = entry.getKey();
final Node node = entry.getValue().getNode();
RestResult<String> result = single(cliService, group, node, args);
if (!result.ok()) {
return result;
}
}
return RestResultUtils.success();
}
use of com.alipay.sofa.jraft.CliService in project nacos by alibaba.
the class JRaftMaintainService method execute.
/**
* Execute relevant commands.
*
* @param args {@link Map}
* @return {@link RestResult}
*/
public RestResult<String> execute(Map<String, String> args) {
final CliService cliService = raftServer.getCliService();
if (args.containsKey(JRaftConstants.GROUP_ID)) {
final String groupId = args.get(JRaftConstants.GROUP_ID);
final Node node = raftServer.findNodeByGroup(groupId);
return single(cliService, groupId, node, args);
}
Map<String, JRaftServer.RaftGroupTuple> tupleMap = raftServer.getMultiRaftGroup();
for (Map.Entry<String, JRaftServer.RaftGroupTuple> entry : tupleMap.entrySet()) {
final String group = entry.getKey();
final Node node = entry.getValue().getNode();
RestResult<String> result = single(cliService, group, node, args);
if (!result.ok()) {
return result;
}
}
return RestResultUtils.success();
}
use of com.alipay.sofa.jraft.CliService in project sofa-jraft by sofastack.
the class CliServiceTest method testRelalanceOnTransferLeaderFail.
@Test
public void testRelalanceOnTransferLeaderFail() {
final 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");
final 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));
final Map<String, PeerId> rebalancedLeaderIds = new HashMap<>();
final 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());
final 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()) {
System.out.println(entry);
assertEquals(new PeerId("host_1", 8080), entry.getKey());
}
}
use of com.alipay.sofa.jraft.CliService in project sofa-jraft by sofastack.
the class CliServiceTest method testRebalance.
@Test
public void testRebalance() {
final 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");
final 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));
final Map<String, PeerId> rebalancedLeaderIds = new HashMap<>();
final CliService cliService = new MockCliService(rebalancedLeaderIds, new PeerId("host_1", 8080));
assertTrue(cliService.rebalance(groupIds, conf, rebalancedLeaderIds).isOk());
assertEquals(groupIds.size(), rebalancedLeaderIds.size());
final 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);
}
final 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);
}
}
Aggregations