Search in sources :

Example 71 with Node

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

the class NodeTest method testReadIndexTimeout.

@Test
public void testReadIndexTimeout() 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(), false, 300, true));
    }
    // elect leader
    cluster.waitLeader();
    // get leader
    final Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(3, leader.listPeers().size());
    // apply tasks to leader
    sendTestTaskAndWait(leader);
    // first call will fail-fast when no connection
    if (!assertReadIndex(leader, 11)) {
        assertTrue(assertReadIndex(leader, 11));
    }
    // read from follower
    for (final Node follower : cluster.getFollowers()) {
        assertNotNull(follower);
        assertReadIndex(follower, 11);
    }
    // read with null request context
    final CountDownLatch latch = new CountDownLatch(1);
    final long start = System.currentTimeMillis();
    leader.readIndex(null, new ReadIndexClosure(0) {

        @Override
        public void run(final Status status, final long index, final byte[] reqCtx) {
            assertNull(reqCtx);
            if (status.isOk()) {
                System.err.println("Read-index so fast: " + (System.currentTimeMillis() - start) + "ms");
            } else {
                assertEquals(status, new Status(RaftError.ETIMEDOUT, "read-index request timeout"));
                assertEquals(index, -1);
            }
            latch.countDown();
        }
    });
    latch.await();
    cluster.stopAll();
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 72 with Node

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

the class NodeTest method testRemoveLeader.

@Test
public void testRemoveLeader() 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
    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 oldLeader = leader.getNodeId().getPeerId().copy();
    final Endpoint oldLeaderAddr = oldLeader.getEndpoint();
    // remove old leader
    LOG.info("Remove old leader {}", oldLeader);
    CountDownLatch latch = new CountDownLatch(1);
    leader.removePeer(oldLeader, new ExpectClosure(latch));
    waitLatch(latch);
    Thread.sleep(100);
    // elect new leader
    cluster.waitLeader();
    leader = cluster.getLeader();
    LOG.info("New leader is {}", leader);
    assertNotNull(leader);
    // apply tasks to new leader
    this.sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
    // stop and clean old leader
    LOG.info("Stop and clean old leader {}", oldLeader);
    assertTrue(cluster.stop(oldLeaderAddr));
    cluster.clean(oldLeaderAddr);
    // Add and start old leader
    LOG.info("Start and add old leader {}", oldLeader);
    assertTrue(cluster.start(oldLeaderAddr));
    peers = TestUtils.generatePeers(3);
    assertTrue(peers.remove(oldLeader));
    latch = new CountDownLatch(1);
    leader.addPeer(oldLeader, 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 73 with Node

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

the class NodeTest method testLeaderFail.

@Test
public void testLeaderFail() 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
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    LOG.info("Current leader is {}", leader.getLeaderId());
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    // stop leader
    LOG.warn("Stop leader {}", leader.getNodeId().getPeerId());
    final PeerId oldLeader = leader.getNodeId().getPeerId();
    assertTrue(cluster.stop(leader.getNodeId().getPeerId().getEndpoint()));
    // apply something when follower
    final List<Node> followers = cluster.getFollowers();
    assertFalse(followers.isEmpty());
    this.sendTestTaskAndWait("follower apply ", followers.get(0), -1);
    // elect new leader
    cluster.waitLeader();
    leader = cluster.getLeader();
    LOG.info("Eelect new leader is {}", leader.getLeaderId());
    // apply tasks to new leader
    CountDownLatch latch = new CountDownLatch(10);
    for (int i = 10; i < 20; i++) {
        final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes());
        final Task task = new Task(data, new ExpectClosure(latch));
        leader.apply(task);
    }
    waitLatch(latch);
    // restart old leader
    LOG.info("restart old leader {}", oldLeader);
    assertTrue(cluster.start(oldLeader.getEndpoint()));
    // apply something
    latch = new CountDownLatch(10);
    for (int i = 20; i < 30; i++) {
        final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes());
        final Task task = new Task(data, new ExpectClosure(latch));
        leader.apply(task);
    }
    waitLatch(latch);
    // stop and clean old leader
    cluster.stop(oldLeader.getEndpoint());
    cluster.clean(oldLeader.getEndpoint());
    // restart old leader
    LOG.info("restart old leader {}", oldLeader);
    assertTrue(cluster.start(oldLeader.getEndpoint()));
    assertTrue(cluster.ensureSame(-1));
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(30, fsm.getLogs().size());
    }
    cluster.stopAll();
}
Also used : Task(com.alipay.sofa.jraft.entity.Task) Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 74 with Node

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

the class NodeTest method testInstallLargeSnapshot.

@Test
public void testInstallLargeSnapshot() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(4);
    final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers.subList(0, 3));
    for (int i = 0; i < peers.size() - 1; i++) {
        final PeerId peer = peers.get(i);
        final boolean started = cluster.start(peer.getEndpoint(), false, 200, false);
        assertTrue(started);
    }
    cluster.waitLeader();
    // get leader
    final Node leader = cluster.getLeader();
    assertNotNull(leader);
    // apply tasks to leader
    sendTestTaskAndWait(leader, 0, RaftError.SUCCESS);
    cluster.ensureSame();
    // apply something more
    for (int i = 1; i < 100; i++) {
        sendTestTaskAndWait(leader, i * 10, RaftError.SUCCESS);
    }
    Thread.sleep(1000);
    // trigger leader snapshot
    triggerLeaderSnapshot(cluster, leader);
    // apply something more
    for (int i = 100; i < 200; i++) {
        sendTestTaskAndWait(leader, i * 10, RaftError.SUCCESS);
    }
    // trigger leader snapshot
    triggerLeaderSnapshot(cluster, leader, 2);
    // wait leader to compact logs
    Thread.sleep(1000);
    // add follower
    final PeerId newPeer = peers.get(3);
    final RaftOptions raftOptions = new RaftOptions();
    raftOptions.setMaxByteCountPerRpc(128);
    final boolean started = cluster.start(newPeer.getEndpoint(), true, 300, false, null, raftOptions);
    assertTrue(started);
    final CountDownLatch latch = new CountDownLatch(1);
    leader.addPeer(newPeer, status -> {
        assertTrue(status.toString(), status.isOk());
        latch.countDown();
    });
    waitLatch(latch);
    cluster.ensureSame();
    assertEquals(4, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(2000, fsm.getLogs().size());
    }
    cluster.stopAll();
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) 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 75 with Node

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

the class NodeTest method testLeaderTransferResumeOnFailure.

@Test
public void testLeaderTransferResumeOnFailure() 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(), false, 1));
    }
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    final List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    final PeerId targetPeer = followers.get(0).getNodeId().getPeerId().copy();
    assertTrue(cluster.stop(targetPeer.getEndpoint()));
    this.sendTestTaskAndWait(leader);
    assertTrue(leader.transferLeadershipTo(targetPeer).isOk());
    final Node savedLeader = leader;
    // try to apply task when transferring leadership
    CountDownLatch latch = new CountDownLatch(1);
    Task task = new Task(ByteBuffer.wrap("aaaaa".getBytes()), new ExpectClosure(RaftError.EBUSY, latch));
    leader.apply(task);
    waitLatch(latch);
    Thread.sleep(100);
    cluster.waitLeader();
    leader = cluster.getLeader();
    assertSame(leader, savedLeader);
    // restart target peer
    assertTrue(cluster.start(targetPeer.getEndpoint()));
    Thread.sleep(100);
    // retry apply task
    latch = new CountDownLatch(1);
    task = new Task(ByteBuffer.wrap("aaaaa".getBytes()), new ExpectClosure(latch));
    leader.apply(task);
    waitLatch(latch);
    assertTrue(cluster.ensureSame(5));
    cluster.stopAll();
}
Also used : Task(com.alipay.sofa.jraft.entity.Task) Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) 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