Search in sources :

Example 1 with CliService

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());
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) CliService(com.alipay.sofa.jraft.CliService) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 2 with CliService

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();
}
Also used : Node(com.alipay.sofa.jraft.Node) CliService(com.alipay.sofa.jraft.CliService) Map(java.util.Map)

Example 3 with CliService

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();
}
Also used : Node(com.alipay.sofa.jraft.Node) CliService(com.alipay.sofa.jraft.CliService) Map(java.util.Map)

Example 4 with CliService

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());
    }
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) CliService(com.alipay.sofa.jraft.CliService) HashMap(java.util.HashMap) Map(java.util.Map) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 5 with CliService

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);
    }
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) CliService(com.alipay.sofa.jraft.CliService) HashMap(java.util.HashMap) Map(java.util.Map) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Aggregations

CliService (com.alipay.sofa.jraft.CliService)5 Map (java.util.Map)4 Configuration (com.alipay.sofa.jraft.conf.Configuration)3 PeerId (com.alipay.sofa.jraft.entity.PeerId)3 HashMap (java.util.HashMap)3 TreeSet (java.util.TreeSet)3 Test (org.junit.Test)3 Node (com.alipay.sofa.jraft.Node)2