Search in sources :

Example 1 with SnapshotThrottle

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

the class NodeTest method testInstallLargeSnapshotWithThrottle.

@Test
public void testInstallLargeSnapshotWithThrottle() 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 SnapshotThrottle snapshotThrottle = new ThroughputSnapshotThrottle(128, 1);
    final boolean started = cluster.start(newPeer.getEndpoint(), true, 300, false, snapshotThrottle);
    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 : ThroughputSnapshotThrottle(com.alipay.sofa.jraft.storage.snapshot.ThroughputSnapshotThrottle) Node(com.alipay.sofa.jraft.Node) SnapshotThrottle(com.alipay.sofa.jraft.storage.SnapshotThrottle) ThroughputSnapshotThrottle(com.alipay.sofa.jraft.storage.snapshot.ThroughputSnapshotThrottle) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Aggregations

Node (com.alipay.sofa.jraft.Node)1 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 SnapshotThrottle (com.alipay.sofa.jraft.storage.SnapshotThrottle)1 ThroughputSnapshotThrottle (com.alipay.sofa.jraft.storage.snapshot.ThroughputSnapshotThrottle)1 Endpoint (com.alipay.sofa.jraft.util.Endpoint)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1