Search in sources :

Example 41 with PeerId

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

the class NodeTest method testChangePeersStepsDownInJointConsensus.

@Test
public void testChangePeersStepsDownInJointConsensus() throws Exception {
    final List<PeerId> peers = new ArrayList<>();
    final PeerId peer0 = JRaftUtils.getPeerId("127.0.0.1:5006");
    final PeerId peer1 = JRaftUtils.getPeerId("127.0.0.1:5007");
    final PeerId peer2 = JRaftUtils.getPeerId("127.0.0.1:5008");
    final PeerId peer3 = JRaftUtils.getPeerId("127.0.0.1:5009");
    // start single cluster
    peers.add(peer0);
    final TestCluster cluster = new TestCluster("testChangePeersStepsDownInJointConsensus", this.dataPath, peers);
    assertTrue(cluster.start(peer0.getEndpoint()));
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    this.sendTestTaskAndWait(leader);
    // start peer1-3
    assertTrue(cluster.start(peer1.getEndpoint()));
    assertTrue(cluster.start(peer2.getEndpoint()));
    assertTrue(cluster.start(peer3.getEndpoint()));
    final Configuration conf = new Configuration();
    conf.addPeer(peer0);
    conf.addPeer(peer1);
    conf.addPeer(peer2);
    conf.addPeer(peer3);
    // change peers
    final SynchronizedClosure done = new SynchronizedClosure();
    leader.changePeers(conf, done);
    assertTrue(done.await().isOk());
    // stop peer3
    assertTrue(cluster.stop(peer3.getEndpoint()));
    conf.removePeer(peer0);
    conf.removePeer(peer1);
    // Change peers to [peer2, peer3], which must fail since peer3 is stopped
    done.reset();
    leader.changePeers(conf, done);
    Assert.assertEquals(RaftError.EPERM, done.await().getRaftError());
    LOG.info(done.getStatus().toString());
    assertFalse(((NodeImpl) leader).getConf().isStable());
    leader = cluster.getLeader();
    assertNull(leader);
    assertTrue(cluster.start(peer3.getEndpoint()));
    Thread.sleep(1000);
    cluster.waitLeader();
    leader = cluster.getLeader();
    final List<PeerId> thePeers = leader.listPeers();
    assertTrue(thePeers.size() > 0);
    assertEquals(conf.getPeerSet(), new HashSet<>(thePeers));
    cluster.stopAll();
}
Also used : SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 42 with PeerId

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

the class NodeTest method testLeaderTransferWithReplicatorStateListener.

@Test
public void testLeaderTransferWithReplicatorStateListener() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers, 300);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint()));
    }
    cluster.waitLeader();
    final UserReplicatorStateListener listener = new UserReplicatorStateListener();
    for (Node node : cluster.getNodes()) {
        node.addReplicatorStateListener(listener);
    }
    Node leader = cluster.getLeader();
    this.sendTestTaskAndWait(leader);
    Thread.sleep(100);
    final List<Node> followers = cluster.getFollowers();
    final PeerId targetPeer = followers.get(0).getNodeId().getPeerId().copy();
    LOG.info("Transfer leadership from {} to {}", leader, targetPeer);
    assertTrue(leader.transferLeadershipTo(targetPeer).isOk());
    Thread.sleep(1000);
    cluster.waitLeader();
    assertEquals(2, this.startedCounter.get());
    for (Node node : cluster.getNodes()) {
        node.clearReplicatorStateListeners();
    }
    assertEquals(0, cluster.getLeader().getReplicatorStatueListeners().size());
    assertEquals(0, cluster.getFollowers().get(0).getReplicatorStatueListeners().size());
    assertEquals(0, cluster.getFollowers().get(1).getReplicatorStatueListeners().size());
    cluster.stopAll();
}
Also used : Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 43 with PeerId

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

the class NodeTest method testLeaderShouldNotChange.

@Test
public void testLeaderShouldNotChange() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint()));
    }
    cluster.waitLeader();
    // get leader
    final Node leader0 = cluster.getLeader();
    assertNotNull(leader0);
    final long savedTerm = ((NodeImpl) leader0).getCurrentTerm();
    LOG.info("Current leader is {}, term is {}", leader0, savedTerm);
    Thread.sleep(5000);
    cluster.waitLeader();
    final Node leader1 = cluster.getLeader();
    assertNotNull(leader1);
    LOG.info("Current leader is {}", leader1);
    assertEquals(savedTerm, ((NodeImpl) leader1).getCurrentTerm());
    cluster.stopAll();
}
Also used : Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 44 with PeerId

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

the class NodeTest method testLeaderStopAndReElectWithPriority.

@Test
public void testLeaderStopAndReElectWithPriority() throws Exception {
    final List<Integer> priorities = new ArrayList<>();
    priorities.add(100);
    priorities.add(60);
    priorities.add(10);
    final List<PeerId> peers = TestUtils.generatePriorityPeers(3, priorities);
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint(), peer.getPriority()));
    }
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(100, leader.getNodeId().getPeerId().getPriority());
    assertEquals(100, leader.getNodeTargetPriority());
    // apply tasks to leader
    sendTestTaskAndWait(leader);
    // stop leader
    assertTrue(cluster.stop(leader.getNodeId().getPeerId().getEndpoint()));
    // elect new leader
    cluster.waitLeader();
    leader = cluster.getLeader();
    assertNotNull(leader);
    // get current leader priority value
    int leaderPriority = leader.getNodeId().getPeerId().getPriority();
    // get current leader log size
    int peer1LogSize = cluster.getFsmByPeer(peers.get(1)).getLogs().size();
    int peer2LogSize = cluster.getFsmByPeer(peers.get(2)).getLogs().size();
    // if the leader is lower priority value
    if (leaderPriority == 10) {
        // we just compare the two peers' log size value;
        assertTrue(peer2LogSize > peer1LogSize);
    } else {
        assertEquals(60, leader.getNodeId().getPeerId().getPriority());
        assertEquals(100, leader.getNodeTargetPriority());
    }
    cluster.stopAll();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 45 with PeerId

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

the class NodeTest method testNoLeader.

@Test
public void testNoLeader() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    assertTrue(cluster.start(peers.get(0).getEndpoint()));
    final List<Node> followers = cluster.getFollowers();
    assertEquals(1, followers.size());
    final Node follower = followers.get(0);
    sendTestTaskAndWait(follower, 0, RaftError.EPERM);
    // adds a peer3
    final PeerId peer3 = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + 3);
    CountDownLatch latch = new CountDownLatch(1);
    follower.addPeer(peer3, new ExpectClosure(RaftError.EPERM, latch));
    waitLatch(latch);
    // remove the peer0
    final PeerId peer0 = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    latch = new CountDownLatch(1);
    follower.removePeer(peer0, new ExpectClosure(RaftError.EPERM, latch));
    waitLatch(latch);
    cluster.stopAll();
}
Also used : Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) 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