Search in sources :

Example 91 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class RouteTableTest method testRefreshConfiguration.

@Test
public void testRefreshConfiguration() throws Exception {
    final RouteTable rt = RouteTable.getInstance();
    final List<PeerId> partConf = new ArrayList<>();
    partConf.add(cluster.getLeader().getLeaderId());
    // part of peers conf, only contains leader peer
    rt.updateConfiguration(groupId, new Configuration(partConf));
    // fetch all conf
    final Status st = rt.refreshConfiguration(cliClientService, groupId, 10000);
    assertTrue(st.isOk());
    final Configuration newCnf = rt.getConfiguration(groupId);
    assertArrayEquals(new HashSet<>(cluster.getPeers()).toArray(), new HashSet<>(newCnf.getPeerSet()).toArray());
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) ArrayList(java.util.ArrayList) PeerId(com.alipay.sofa.jraft.entity.PeerId) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 92 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class CliServiceTest method testGetPeers.

@Test
public void testGetPeers() throws Exception {
    PeerId leader = this.cluster.getLeader().getNodeId().getPeerId();
    assertNotNull(leader);
    assertArrayEquals(this.conf.getPeerSet().toArray(), new HashSet<>(this.cliService.getPeers(this.groupId, this.conf)).toArray());
    // stop one peer
    final List<PeerId> peers = this.conf.getPeers();
    this.cluster.stop(peers.get(0).getEndpoint());
    this.cluster.waitLeader();
    leader = this.cluster.getLeader().getNodeId().getPeerId();
    assertNotNull(leader);
    assertArrayEquals(this.conf.getPeerSet().toArray(), new HashSet<>(this.cliService.getPeers(this.groupId, this.conf)).toArray());
    this.cluster.stopAll();
    try {
        this.cliService.getPeers(this.groupId, this.conf);
        fail();
    } catch (final IllegalStateException e) {
        assertTrue(e.getMessage().contains("Fail to get leader of group " + this.groupId));
    }
}
Also used : PeerId(com.alipay.sofa.jraft.entity.PeerId) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 93 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class CliServiceTest method testTransferLeader.

@Test
public void testTransferLeader() throws Exception {
    final PeerId leader = this.cluster.getLeader().getNodeId().getPeerId().copy();
    assertNotNull(leader);
    final Set<PeerId> peers = this.conf.getPeerSet();
    PeerId targetPeer = null;
    for (final PeerId peer : peers) {
        if (!peer.equals(leader)) {
            targetPeer = peer;
            break;
        }
    }
    assertNotNull(targetPeer);
    assertTrue(this.cliService.transferLeader(this.groupId, this.conf, targetPeer).isOk());
    this.cluster.waitLeader();
    assertEquals(targetPeer, this.cluster.getLeader().getNodeId().getPeerId());
}
Also used : PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 94 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId 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 95 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class CliServiceTest method testLearnerServices.

@Test
public void testLearnerServices() throws Exception {
    final PeerId learner3 = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + LEARNER_PORT_STEP + 3);
    assertTrue(this.cluster.startLearner(learner3));
    sendTestTaskAndWait(this.cluster.getLeader(), 0);
    Thread.sleep(500);
    for (final MockStateMachine fsm : this.cluster.getFsms()) {
        if (!fsm.getAddress().equals(learner3.getEndpoint())) {
            assertEquals(10, fsm.getLogs().size());
        }
    }
    assertEquals(0, this.cluster.getFsmByPeer(learner3).getLogs().size());
    List<PeerId> oldLearners = new ArrayList<PeerId>(this.conf.getLearners());
    assertEquals(oldLearners, this.cliService.getLearners(this.groupId, this.conf));
    assertEquals(oldLearners, this.cliService.getAliveLearners(this.groupId, this.conf));
    // Add learner3
    this.cliService.addLearners(this.groupId, this.conf, Arrays.asList(learner3));
    Thread.sleep(100);
    assertEquals(10, this.cluster.getFsmByPeer(learner3).getLogs().size());
    sendTestTaskAndWait(this.cluster.getLeader(), 0);
    Thread.sleep(500);
    for (final MockStateMachine fsm : this.cluster.getFsms()) {
        assertEquals(20, fsm.getLogs().size());
    }
    List<PeerId> newLearners = new ArrayList<>(oldLearners);
    newLearners.add(learner3);
    assertEquals(newLearners, this.cliService.getLearners(this.groupId, this.conf));
    assertEquals(newLearners, this.cliService.getAliveLearners(this.groupId, this.conf));
    // Remove  3
    this.cliService.removeLearners(this.groupId, this.conf, Arrays.asList(learner3));
    sendTestTaskAndWait(this.cluster.getLeader(), 0);
    Thread.sleep(500);
    for (final MockStateMachine fsm : this.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, this.cluster.getFsmByPeer(learner3).getLogs().size());
    assertEquals(oldLearners, this.cliService.getLearners(this.groupId, this.conf));
    assertEquals(oldLearners, this.cliService.getAliveLearners(this.groupId, this.conf));
    // Set learners into [learner3]
    this.cliService.resetLearners(this.groupId, this.conf, Arrays.asList(learner3));
    Thread.sleep(100);
    assertEquals(30, this.cluster.getFsmByPeer(learner3).getLogs().size());
    sendTestTaskAndWait(this.cluster.getLeader(), 0);
    Thread.sleep(500);
    // Latest 10 logs are not replicated to learner1 and learner2, because they were removed by resetting learners set.
    for (final MockStateMachine fsm : this.cluster.getFsms()) {
        if (!oldLearners.contains(new PeerId(fsm.getAddress(), 0))) {
            assertEquals(40, fsm.getLogs().size());
        } else {
            assertEquals(30, fsm.getLogs().size());
        }
    }
    assertEquals(Arrays.asList(learner3), this.cliService.getLearners(this.groupId, this.conf));
    assertEquals(Arrays.asList(learner3), this.cliService.getAliveLearners(this.groupId, this.conf));
    // Stop learner3
    this.cluster.stop(learner3.getEndpoint());
    Thread.sleep(1000);
    assertEquals(Arrays.asList(learner3), this.cliService.getLearners(this.groupId, this.conf));
    assertTrue(this.cliService.getAliveLearners(this.groupId, this.conf).isEmpty());
}
Also used : ArrayList(java.util.ArrayList) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Aggregations

PeerId (com.alipay.sofa.jraft.entity.PeerId)236 Test (org.junit.Test)107 Node (com.alipay.sofa.jraft.Node)70 Configuration (com.alipay.sofa.jraft.conf.Configuration)54 Status (com.alipay.sofa.jraft.Status)49 Endpoint (com.alipay.sofa.jraft.util.Endpoint)43 ArrayList (java.util.ArrayList)32 CountDownLatch (java.util.concurrent.CountDownLatch)28 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)24 LogId (com.alipay.sofa.jraft.entity.LogId)17 Message (com.google.protobuf.Message)15 ByteBuffer (java.nio.ByteBuffer)15 Task (com.alipay.sofa.jraft.entity.Task)13 File (java.io.File)12 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)11 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)11 JRaftException (com.alipay.sofa.jraft.error.JRaftException)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)9 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)8