Search in sources :

Example 1 with Configuration

use of org.apache.ignite.raft.jraft.conf.Configuration 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));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) IgniteRpcClient(org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient) CliOptions(org.apache.ignite.raft.jraft.option.CliOptions) ClusterService(org.apache.ignite.network.ClusterService) TestScaleCubeClusterServiceFactory(org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory) NetworkAddress(org.apache.ignite.network.NetworkAddress) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with Configuration

use of org.apache.ignite.raft.jraft.conf.Configuration 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);
    }
}
Also used : Configuration(org.apache.ignite.raft.jraft.conf.Configuration) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) CliService(org.apache.ignite.raft.jraft.CliService) Map(java.util.Map) HashMap(java.util.HashMap) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 3 with Configuration

use of org.apache.ignite.raft.jraft.conf.Configuration 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));
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) Node(org.apache.ignite.raft.jraft.Node) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 4 with Configuration

use of org.apache.ignite.raft.jraft.conf.Configuration 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());
    }
}
Also used : Configuration(org.apache.ignite.raft.jraft.conf.Configuration) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) CliService(org.apache.ignite.raft.jraft.CliService) Map(java.util.Map) HashMap(java.util.HashMap) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 5 with Configuration

use of org.apache.ignite.raft.jraft.conf.Configuration in project ignite-3 by apache.

the class ItCliServiceTest method testRebalanceOnLeaderFail.

@Test
public void testRebalanceOnLeaderFail() {
    Set<String> groupIds = new TreeSet<>();
    groupIds.add("group_1");
    groupIds.add("group_2");
    groupIds.add("group_3");
    groupIds.add("group_4");
    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 MockLeaderFailCliService();
    assertEquals("Fail to get leader", cliService.rebalance(groupIds, conf, rebalancedLeaderIds).getErrorMsg());
}
Also used : Configuration(org.apache.ignite.raft.jraft.conf.Configuration) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) CliService(org.apache.ignite.raft.jraft.CliService) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Aggregations

Configuration (org.apache.ignite.raft.jraft.conf.Configuration)45 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)37 Test (org.junit.jupiter.api.Test)20 Node (org.apache.ignite.raft.jraft.Node)17 Status (org.apache.ignite.raft.jraft.Status)16 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)13 ArrayList (java.util.ArrayList)9 RaftGroupService (org.apache.ignite.raft.jraft.RaftGroupService)9 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)9 SynchronizedClosure (org.apache.ignite.raft.jraft.closure.SynchronizedClosure)8 CountDownLatch (java.util.concurrent.CountDownLatch)5 ExecutorService (java.util.concurrent.ExecutorService)5 NetworkAddress (org.apache.ignite.network.NetworkAddress)5 ConfigurationEntry (org.apache.ignite.raft.jraft.conf.ConfigurationEntry)5 LogId (org.apache.ignite.raft.jraft.entity.LogId)5 Task (org.apache.ignite.raft.jraft.entity.Task)5 IgniteRpcClient (org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient)5 HashMap (java.util.HashMap)4 ClusterService (org.apache.ignite.network.ClusterService)4 StaticNodeFinder (org.apache.ignite.network.StaticNodeFinder)4