Search in sources :

Example 21 with Node

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

the class NodeTest method testInstallSnapshotWithThrottle.

@Test
public void testInstallSnapshotWithThrottle() 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(), false, 200, false, new ThroughputSnapshotThrottle(1024, 1)));
    }
    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));
    cluster.waitLeader();
    // apply something more
    this.sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
    Thread.sleep(1000);
    // trigger leader snapshot
    triggerLeaderSnapshot(cluster, leader);
    // apply something more
    this.sendTestTaskAndWait(leader, 20, RaftError.SUCCESS);
    // trigger leader snapshot
    triggerLeaderSnapshot(cluster, leader, 2);
    // wait leader to compact logs
    Thread.sleep(1000);
    // restart follower.
    cluster.clean(followerAddr);
    assertTrue(cluster.start(followerAddr, true, 300, false, new ThroughputSnapshotThrottle(1024, 1)));
    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) ThroughputSnapshotThrottle(com.alipay.sofa.jraft.storage.snapshot.ThroughputSnapshotThrottle) Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 22 with Node

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

the class NodeTest method testChangePeersChaosApplyTasks.

@Test
public void testChangePeersChaosApplyTasks() throws Exception {
    // start cluster
    final List<PeerId> peers = new ArrayList<>();
    peers.add(new PeerId("127.0.0.1", TestUtils.INIT_PORT));
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers, 1000);
    assertTrue(cluster.start(peers.get(0).getEndpoint(), false, 100000));
    // start other peers
    for (int i = 1; i < 10; i++) {
        final PeerId peer = new PeerId("127.0.0.1", TestUtils.INIT_PORT + i);
        peers.add(peer);
        assertTrue(cluster.start(peer.getEndpoint(), true, 100000));
    }
    final int threads = 3;
    final List<ChangeArg> args = new ArrayList<>();
    final List<Future<?>> futures = new ArrayList<>();
    final CountDownLatch latch = new CountDownLatch(threads);
    for (int t = 0; t < threads; t++) {
        final ChangeArg arg = new ChangeArg(cluster, peers, false, true);
        args.add(arg);
        futures.add(startChangePeersThread(arg));
        Utils.runInThread(() -> {
            try {
                for (int i = 0; i < 5000; ) {
                    cluster.waitLeader();
                    final Node leader = cluster.getLeader();
                    if (leader == null) {
                        continue;
                    }
                    final SynchronizedClosure done = new SynchronizedClosure();
                    final Task task = new Task(ByteBuffer.wrap(("hello" + i).getBytes()), done);
                    leader.apply(task);
                    final Status status = done.await();
                    if (status.isOk()) {
                        if (++i % 100 == 0) {
                            System.out.println("Progress:" + i);
                        }
                    } else {
                        assertEquals(RaftError.EPERM, status.getRaftError());
                    }
                }
            } catch (final Exception e) {
                e.printStackTrace();
            } finally {
                latch.countDown();
            }
        });
    }
    latch.await();
    for (final ChangeArg arg : args) {
        arg.stop = true;
    }
    for (final Future<?> future : futures) {
        future.get();
    }
    cluster.waitLeader();
    final SynchronizedClosure done = new SynchronizedClosure();
    final Node leader = cluster.getLeader();
    leader.changePeers(new Configuration(peers), done);
    assertTrue(done.await().isOk());
    cluster.ensureSame();
    assertEquals(10, cluster.getFsms().size());
    try {
        for (final MockStateMachine fsm : cluster.getFsms()) {
            final int logSize = fsm.getLogs().size();
            assertTrue("logSize= " + logSize, logSize >= 5000 * threads);
            assertTrue("logSize= " + logSize, logSize - 5000 * threads < 100);
        }
    } finally {
        cluster.stopAll();
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) Task(com.alipay.sofa.jraft.entity.Task) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(com.alipay.sofa.jraft.util.Endpoint) RaftException(com.alipay.sofa.jraft.error.RaftException) LogIndexOutOfBoundsException(com.alipay.sofa.jraft.error.LogIndexOutOfBoundsException) LogNotFoundException(com.alipay.sofa.jraft.error.LogNotFoundException) Future(java.util.concurrent.Future) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 23 with Node

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

the class NodeTest method testChangePeersChaosWithoutSnapshot.

@Test
public void testChangePeersChaosWithoutSnapshot() throws Exception {
    // start cluster
    final List<PeerId> peers = new ArrayList<>();
    peers.add(new PeerId("127.0.0.1", TestUtils.INIT_PORT));
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers, 1000);
    assertTrue(cluster.start(peers.get(0).getEndpoint(), false, 100000));
    // start other peers
    for (int i = 1; i < 10; i++) {
        final PeerId peer = new PeerId("127.0.0.1", TestUtils.INIT_PORT + i);
        peers.add(peer);
        assertTrue(cluster.start(peer.getEndpoint(), true, 10000));
    }
    final ChangeArg arg = new ChangeArg(cluster, peers, false, true);
    final Future<?> future = startChangePeersThread(arg);
    final int tasks = 5000;
    for (int i = 0; i < tasks; ) {
        cluster.waitLeader();
        final Node leader = cluster.getLeader();
        if (leader == null) {
            continue;
        }
        final SynchronizedClosure done = new SynchronizedClosure();
        final Task task = new Task(ByteBuffer.wrap(("hello" + i).getBytes()), done);
        leader.apply(task);
        final Status status = done.await();
        if (status.isOk()) {
            if (++i % 100 == 0) {
                System.out.println("Progress:" + i);
            }
        } else {
            assertEquals(RaftError.EPERM, status.getRaftError());
        }
    }
    arg.stop = true;
    future.get();
    cluster.waitLeader();
    final SynchronizedClosure done = new SynchronizedClosure();
    final Node leader = cluster.getLeader();
    leader.changePeers(new Configuration(peers), done);
    assertTrue(done.await().isOk());
    cluster.ensureSame();
    assertEquals(10, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertTrue(fsm.getLogs().size() >= tasks);
        assertTrue(fsm.getLogs().size() - tasks < 100);
    }
    cluster.stopAll();
}
Also used : Status(com.alipay.sofa.jraft.Status) SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) Task(com.alipay.sofa.jraft.entity.Task) Configuration(com.alipay.sofa.jraft.conf.Configuration) 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 24 with Node

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

the class NodeTest method testRestoreSnasphot.

@Test
public void testRestoreSnasphot() 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();
    triggerLeaderSnapshot(cluster, leader);
    // stop leader
    final Endpoint leaderAddr = leader.getNodeId().getPeerId().getEndpoint().copy();
    assertTrue(cluster.stop(leaderAddr));
    Thread.sleep(2000);
    // restart leader
    cluster.waitLeader();
    assertEquals(0, cluster.getLeaderFsm().getLoadSnapshotTimes());
    assertTrue(cluster.start(leaderAddr));
    cluster.ensureSame();
    assertEquals(0, cluster.getLeaderFsm().getLoadSnapshotTimes());
    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 25 with Node

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

the class NodeTest method testNoLeaderWithZeroValPriorityElection.

@Test
public void testNoLeaderWithZeroValPriorityElection() throws Exception {
    List<Integer> priorities = new ArrayList<>();
    priorities.add(0);
    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()));
    }
    Thread.sleep(200);
    final List<Node> followers = cluster.getFollowers();
    assertEquals(3, followers.size());
    for (Node follower : followers) {
        assertEquals(0, follower.getNodeId().getPeerId().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