Search in sources :

Example 6 with Task

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

the class NodeTest method sendTestTaskAndWait.

private void sendTestTaskAndWait(final Node node, final int start, final RaftError err) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(10);
    for (int i = start; i < start + 10; i++) {
        final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes());
        final Task task = new Task(data, new ExpectClosure(err, latch));
        node.apply(task);
    }
    waitLatch(latch);
}
Also used : Task(com.alipay.sofa.jraft.entity.Task) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Endpoint(com.alipay.sofa.jraft.util.Endpoint)

Example 7 with Task

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

the class NodeTest method testChangePeersChaosWithSnapshot.

@Test
public void testChangePeersChaosWithSnapshot() 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, 2));
    // 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()));
    }
    final ChangeArg arg = new ChangeArg(cluster, peers, false, false);
    final Future<?> future = startChangePeersThread(arg);
    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());
        }
    }
    arg.stop = true;
    future.get();
    cluster.waitLeader();
    final SynchronizedClosure done = new SynchronizedClosure();
    final Node leader = cluster.getLeader();
    leader.changePeers(new Configuration(peers), done);
    final Status st = done.await();
    assertTrue(st.getErrorMsg(), st.isOk());
    cluster.ensureSame();
    assertEquals(10, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertTrue(fsm.getLogs().size() >= 5000);
    }
    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 8 with Task

use of com.alipay.sofa.jraft.entity.Task 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 9 with Task

use of com.alipay.sofa.jraft.entity.Task 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 10 with Task

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

the class NodeTest method testTripleNodesV1V2Codec.

@Test
public void testTripleNodesV1V2Codec() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    for (int i = 0; i < peers.size(); i++) {
        // Peer3 use codec v1
        if (i == 2) {
            cluster.setRaftServiceFactory(new V1JRaftServiceFactory());
        }
        assertTrue(cluster.start(peers.get(i).getEndpoint()));
    }
    // elect leader
    cluster.waitLeader();
    // get leader
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(3, leader.listPeers().size());
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    {
        final ByteBuffer data = ByteBuffer.wrap("no closure".getBytes());
        final Task task = new Task(data, null);
        leader.apply(task);
    }
    {
        // task with TaskClosure
        final ByteBuffer data = ByteBuffer.wrap("task closure".getBytes());
        final Vector<String> cbs = new Vector<>();
        final CountDownLatch latch = new CountDownLatch(1);
        final Task task = new Task(data, new TaskClosure() {

            @Override
            public void run(final Status status) {
                cbs.add("apply");
                latch.countDown();
            }

            @Override
            public void onCommitted() {
                cbs.add("commit");
            }
        });
        leader.apply(task);
        latch.await();
        assertEquals(2, cbs.size());
        assertEquals("commit", cbs.get(0));
        assertEquals("apply", cbs.get(1));
    }
    cluster.ensureSame(-1);
    assertEquals(2, cluster.getFollowers().size());
    // transfer the leader to v1 codec peer
    assertTrue(leader.transferLeadershipTo(peers.get(2)).isOk());
    cluster.waitLeader();
    leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(leader.getLeaderId(), peers.get(2));
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    cluster.ensureSame();
    cluster.stopAll();
    // start the cluster with v2 codec, should work
    final TestCluster newCluster = new TestCluster("unittest", this.dataPath, peers);
    for (int i = 0; i < peers.size(); i++) {
        assertTrue(newCluster.start(peers.get(i).getEndpoint()));
    }
    // elect leader
    newCluster.waitLeader();
    newCluster.ensureSame();
    leader = newCluster.getLeader();
    assertNotNull(leader);
    // apply new tasks
    this.sendTestTaskAndWait(leader);
    newCluster.ensureSame();
    newCluster.stopAll();
}
Also used : Status(com.alipay.sofa.jraft.Status) 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) TaskClosure(com.alipay.sofa.jraft.closure.TaskClosure) Vector(java.util.Vector) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Aggregations

Task (com.alipay.sofa.jraft.entity.Task)27 PeerId (com.alipay.sofa.jraft.entity.PeerId)18 ByteBuffer (java.nio.ByteBuffer)17 Node (com.alipay.sofa.jraft.Node)16 Status (com.alipay.sofa.jraft.Status)15 CountDownLatch (java.util.concurrent.CountDownLatch)14 Test (org.junit.Test)13 Endpoint (com.alipay.sofa.jraft.util.Endpoint)12 Configuration (com.alipay.sofa.jraft.conf.Configuration)10 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)7 ArrayList (java.util.ArrayList)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)6 RaftError (com.alipay.sofa.jraft.error.RaftError)6 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)6 List (java.util.List)5 JRaftUtils (com.alipay.sofa.jraft.JRaftUtils)4 TaskClosure (com.alipay.sofa.jraft.closure.TaskClosure)4 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)4 TimeUnit (java.util.concurrent.TimeUnit)4