Search in sources :

Example 61 with Node

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

the class NodeTest method testInstallSnapshot.

@Test
public void testInstallSnapshot() 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 leader = cluster.getLeader();
    assertNotNull(leader);
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    cluster.ensureSame();
    // stop follower1
    final List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    final Endpoint followerAddr = followers.get(0).getNodeId().getPeerId().getEndpoint();
    assertTrue(cluster.stop(followerAddr));
    // apply something more
    this.sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
    // trigger leader snapshot
    triggerLeaderSnapshot(cluster, leader);
    // apply something more
    this.sendTestTaskAndWait(leader, 20, RaftError.SUCCESS);
    triggerLeaderSnapshot(cluster, leader, 2);
    // wait leader to compact logs
    Thread.sleep(50);
    // restart follower.
    cluster.clean(followerAddr);
    assertTrue(cluster.start(followerAddr, true, 300));
    Thread.sleep(2000);
    cluster.ensureSame();
    assertEquals(3, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(30, fsm.getLogs().size());
    }
    cluster.stopAll();
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 62 with Node

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

the class NodeTest method testLeaderTransfer.

@Test
public void testLeaderTransfer() 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();
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    this.sendTestTaskAndWait(leader);
    Thread.sleep(100);
    final List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    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();
    leader = cluster.getLeader();
    Assert.assertEquals(leader.getNodeId().getPeerId(), targetPeer);
    cluster.stopAll();
}
Also used : Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 63 with Node

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

the class NodeTest method testTransferShouldWorkAfterInstallSnapshot.

@Test
public void testTransferShouldWorkAfterInstallSnapshot() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers, 1000);
    for (int i = 0; i < peers.size() - 1; i++) {
        assertTrue(cluster.start(peers.get(i).getEndpoint()));
    }
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    this.sendTestTaskAndWait(leader);
    final List<Node> followers = cluster.getFollowers();
    assertEquals(1, followers.size());
    final PeerId follower = followers.get(0).getNodeId().getPeerId();
    assertTrue(leader.transferLeadershipTo(follower).isOk());
    Thread.sleep(2000);
    leader = cluster.getLeader();
    Assert.assertEquals(follower, leader.getNodeId().getPeerId());
    CountDownLatch latch = new CountDownLatch(1);
    leader.snapshot(new ExpectClosure(latch));
    waitLatch(latch);
    latch = new CountDownLatch(1);
    leader.snapshot(new ExpectClosure(latch));
    waitLatch(latch);
    // start the last peer which should be recover with snapshot.
    final PeerId lastPeer = peers.get(2);
    assertTrue(cluster.start(lastPeer.getEndpoint()));
    Thread.sleep(5000);
    assertTrue(leader.transferLeadershipTo(lastPeer).isOk());
    Thread.sleep(2000);
    leader = cluster.getLeader();
    Assert.assertEquals(lastPeer, leader.getNodeId().getPeerId());
    assertEquals(3, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(10, fsm.getLogs().size());
    }
    cluster.stopAll();
}
Also used : Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 64 with Node

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

the class NodeTest method testPreVote.

@Test
public void testPreVote() throws Exception {
    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
    Node leader = cluster.getLeader();
    final long savedTerm = ((NodeImpl) leader).getCurrentTerm();
    assertNotNull(leader);
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    cluster.ensureSame();
    final List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    final PeerId followerPeer = followers.get(0).getNodeId().getPeerId();
    final Endpoint followerAddr = followerPeer.getEndpoint();
    // remove follower
    LOG.info("Remove follower {}", followerPeer);
    CountDownLatch latch = new CountDownLatch(1);
    leader.removePeer(followerPeer, new ExpectClosure(latch));
    waitLatch(latch);
    this.sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
    Thread.sleep(2000);
    // add follower
    LOG.info("Add follower {}", followerAddr);
    peers = TestUtils.generatePeers(3);
    assertTrue(peers.remove(followerPeer));
    latch = new CountDownLatch(1);
    leader.addPeer(followerPeer, new ExpectClosure(latch));
    waitLatch(latch);
    leader = cluster.getLeader();
    assertNotNull(leader);
    // leader term should not be changed.
    assertEquals(savedTerm, ((NodeImpl) leader).getCurrentTerm());
    cluster.stopAll();
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 65 with Node

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

the class NodeTest method testNodesWithZeroValPriorityElection.

@Test
public void testNodesWithZeroValPriorityElection() throws Exception {
    List<Integer> priorities = new ArrayList<Integer>();
    priorities.add(50);
    priorities.add(0);
    priorities.add(0);
    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()));
    }
    // elect leader
    cluster.waitLeader();
    // get leader
    final Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(3, leader.listPeers().size());
    assertEquals(2, cluster.getFollowers().size());
    assertEquals(50, leader.getNodeTargetPriority());
    assertEquals(50, leader.getLeaderId().getPriority());
    cluster.stopAll();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Aggregations

Node (com.alipay.sofa.jraft.Node)90 PeerId (com.alipay.sofa.jraft.entity.PeerId)74 Test (org.junit.Test)64 Endpoint (com.alipay.sofa.jraft.util.Endpoint)41 CountDownLatch (java.util.concurrent.CountDownLatch)32 Configuration (com.alipay.sofa.jraft.conf.Configuration)25 ArrayList (java.util.ArrayList)22 Status (com.alipay.sofa.jraft.Status)21 Task (com.alipay.sofa.jraft.entity.Task)17 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)17 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)14 ByteBuffer (java.nio.ByteBuffer)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)10 RaftException (com.alipay.sofa.jraft.error.RaftException)8 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)7 LinkedHashSet (java.util.LinkedHashSet)7 NodeId (com.alipay.sofa.jraft.entity.NodeId)6 LogIndexOutOfBoundsException (com.alipay.sofa.jraft.error.LogIndexOutOfBoundsException)6 LogNotFoundException (com.alipay.sofa.jraft.error.LogNotFoundException)6