Search in sources :

Example 1 with PeerId

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));
}
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 PeerId

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);
    }
}
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 PeerId

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());
}
Also used : ArrayList(java.util.ArrayList) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 4 with PeerId

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));
}
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 5 with PeerId

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());
    }
}
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)

Aggregations

PeerId (org.apache.ignite.raft.jraft.entity.PeerId)170 Test (org.junit.jupiter.api.Test)95 Node (org.apache.ignite.raft.jraft.Node)72 Status (org.apache.ignite.raft.jraft.Status)41 ArrayList (java.util.ArrayList)40 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)39 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)38 CountDownLatch (java.util.concurrent.CountDownLatch)27 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)20 ByteBuffer (java.nio.ByteBuffer)16 RaftGroupService (org.apache.ignite.raft.jraft.RaftGroupService)16 SynchronizedClosure (org.apache.ignite.raft.jraft.closure.SynchronizedClosure)14 RpcRequests (org.apache.ignite.raft.jraft.rpc.RpcRequests)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 LogId (org.apache.ignite.raft.jraft.entity.LogId)13 Task (org.apache.ignite.raft.jraft.entity.Task)13 Message (org.apache.ignite.raft.jraft.rpc.Message)12 NetworkAddress (org.apache.ignite.network.NetworkAddress)11 LinkedHashSet (java.util.LinkedHashSet)9 CompletableFuture (java.util.concurrent.CompletableFuture)9