Search in sources :

Example 76 with Node

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

the class NodeTest method testVoteTimedoutStepDown.

@Test
public void testVoteTimedoutStepDown() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint()));
    }
    // elect leader
    cluster.waitLeader();
    // get leader
    final Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(3, leader.listPeers().size());
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    // Stop all followers
    List<Node> followers = cluster.getFollowers();
    assertFalse(followers.isEmpty());
    for (Node node : followers) {
        assertTrue(cluster.stop(node.getNodeId().getPeerId().getEndpoint()));
    }
    // Wait leader to step down.
    while (leader.isLeader()) {
        Thread.sleep(10);
    }
    // old leader try to elect self, it should fail.
    ((NodeImpl) leader).tryElectSelf();
    Thread.sleep(1500);
    // Start followers
    for (Node node : followers) {
        assertTrue(cluster.start(node.getNodeId().getPeerId().getEndpoint()));
    }
    cluster.ensureSame(-1);
    cluster.stopAll();
}
Also used : Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 77 with Node

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

the class NodeTest method testTripleNodesWithReplicatorStateListener.

@Test
public void testTripleNodesWithReplicatorStateListener() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint()));
    }
    final UserReplicatorStateListener listener1 = new UserReplicatorStateListener();
    final UserReplicatorStateListener listener2 = new UserReplicatorStateListener();
    for (Node node : cluster.getNodes()) {
        node.addReplicatorStateListener(listener1);
        node.addReplicatorStateListener(listener2);
    }
    // elect leader
    cluster.waitLeader();
    assertEquals(4, this.startedCounter.get());
    assertEquals(2, cluster.getLeader().getReplicatorStatueListeners().size());
    assertEquals(2, cluster.getFollowers().get(0).getReplicatorStatueListeners().size());
    assertEquals(2, cluster.getFollowers().get(1).getReplicatorStatueListeners().size());
    for (Node node : cluster.getNodes()) {
        node.removeReplicatorStateListener(listener1);
    }
    assertEquals(1, cluster.getLeader().getReplicatorStatueListeners().size());
    assertEquals(1, cluster.getFollowers().get(0).getReplicatorStatueListeners().size());
    assertEquals(1, 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 78 with Node

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

the class NodeTest method testChangePeersAddMultiNodes.

@Test
public void testChangePeersAddMultiNodes() throws Exception {
    final PeerId peer0 = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    final TestCluster cluster = new TestCluster("testChangePeers", this.dataPath, Collections.singletonList(peer0));
    assertTrue(cluster.start(peer0.getEndpoint()));
    cluster.waitLeader();
    final Node leader = cluster.getLeader();
    this.sendTestTaskAndWait(leader);
    final Configuration conf = new Configuration();
    for (int i = 0; i < 3; i++) {
        final PeerId peer = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + i);
        conf.addPeer(peer);
    }
    PeerId peer = new PeerId(TestUtils.getMyIp(), peer0.getEndpoint().getPort() + 1);
    // fail, because the peers are not started.
    final SynchronizedClosure done = new SynchronizedClosure();
    leader.changePeers(new Configuration(Collections.singletonList(peer)), done);
    Assert.assertEquals(RaftError.ECATCHUP, done.await().getRaftError());
    // start peer1
    assertTrue(cluster.start(peer.getEndpoint()));
    // still fail, because peer2 is not started
    done.reset();
    leader.changePeers(conf, done);
    Assert.assertEquals(RaftError.ECATCHUP, done.await().getRaftError());
    // start peer2
    peer = new PeerId(TestUtils.getMyIp(), peer0.getEndpoint().getPort() + 2);
    assertTrue(cluster.start(peer.getEndpoint()));
    done.reset();
    // works
    leader.changePeers(conf, done);
    assertTrue(done.await().isOk());
    assertTrue(cluster.ensureSame());
    assertEquals(3, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(10, fsm.getLogs().size());
    }
    cluster.stopAll();
}
Also used : SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 79 with Node

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

the class NodeTest method testRemoveFollower.

@Test
public void testRemoveFollower() throws Exception {
    List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint()));
    }
    // elect leader
    cluster.waitLeader();
    // get leader
    final Node leader = cluster.getLeader();
    assertNotNull(leader);
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    cluster.ensureSame();
    List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    final PeerId followerPeer = followers.get(0).getNodeId().getPeerId();
    final Endpoint followerAddr = followerPeer.getEndpoint();
    // stop and clean follower
    LOG.info("Stop and clean follower {}", followerPeer);
    assertTrue(cluster.stop(followerAddr));
    cluster.clean(followerAddr);
    // 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);
    followers = cluster.getFollowers();
    assertEquals(1, followers.size());
    peers = TestUtils.generatePeers(3);
    assertTrue(peers.remove(followerPeer));
    // start follower
    LOG.info("Start and add follower {}", followerPeer);
    assertTrue(cluster.start(followerAddr));
    // re-add follower
    latch = new CountDownLatch(1);
    leader.addPeer(followerPeer, new ExpectClosure(latch));
    waitLatch(latch);
    followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    cluster.ensureSame();
    assertEquals(3, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(20, fsm.getLogs().size());
    }
    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 80 with Node

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

the class NodeTest method testTripleNodesWithStaticLearners.

@Test
public void testTripleNodesWithStaticLearners() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    LinkedHashSet<PeerId> learners = new LinkedHashSet<>();
    PeerId learnerPeer = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + 3);
    learners.add(learnerPeer);
    cluster.setLearners(learners);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint()));
    }
    // elect leader
    cluster.waitLeader();
    final Node leader = cluster.getLeader();
    assertEquals(3, leader.listPeers().size());
    assertEquals(leader.listLearners().size(), 1);
    assertTrue(leader.listLearners().contains(learnerPeer));
    assertTrue(leader.listAliveLearners().isEmpty());
    // start learner after cluster setup.
    assertTrue(cluster.start(learnerPeer.getEndpoint()));
    Thread.sleep(1000);
    assertEquals(3, leader.listPeers().size());
    assertEquals(leader.listLearners().size(), 1);
    assertEquals(leader.listAliveLearners().size(), 1);
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    cluster.ensureSame();
    assertEquals(4, cluster.getFsms().size());
    cluster.stopAll();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Node(com.alipay.sofa.jraft.Node) 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