Java Code Examples for com.alipay.sofa.jraft.Node#apply()

The following examples show how to use com.alipay.sofa.jraft.Node#apply() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CliServiceTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("SameParameterValue")
private void sendTestTaskAndWait(final Node node, final int code) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(10);
    for (int i = 0; i < 10; i++) {
        final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes());
        final Task task = new Task(data, new ExpectClosure(code, null, latch));
        node.apply(task);
    }
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
 
Example 2
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 3
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("SameParameterValue")
private void sendTestTaskAndWait(final String prefix, final Node node, final int code) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(10);
    for (int i = 0; i < 10; i++) {
        final ByteBuffer data = ByteBuffer.wrap((prefix + i).getBytes());
        final Task task = new Task(data, new ExpectClosure(code, null, latch));
        node.apply(task);
    }
    waitLatch(latch);
}
 
Example 4
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testNodeMetrics() 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
    this.sendTestTaskAndWait(leader);

    {
        final ByteBuffer data = ByteBuffer.wrap("no closure".getBytes());
        final Task task = new Task(data, null);
        leader.apply(task);
    }

    cluster.ensureSame(-1);
    for (final Node node : cluster.getNodes()) {
        System.out.println("-------------" + node.getNodeId() + "-------------");
        final ConsoleReporter reporter = ConsoleReporter.forRegistry(node.getNodeMetrics().getMetricRegistry())
            .build();
        reporter.report();
        reporter.close();
        System.out.println();
    }
    // TODO check http status
    assertEquals(2, cluster.getFollowers().size());
    cluster.stopAll();
    //   System.out.println(node.getNodeMetrics().getMetrics());
}
 
Example 5
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testLeaderTransferBeforeLogIsCompleted() 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);

    Thread.sleep(100);

    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);
    LOG.info("Transfer leadership from {} to {}", leader, targetPeer);
    assertTrue(leader.transferLeadershipTo(targetPeer).isOk());
    final CountDownLatch latch = new CountDownLatch(1);
    final Task task = new Task(ByteBuffer.wrap("aaaaa".getBytes()), new ExpectClosure(RaftError.EBUSY, latch));
    leader.apply(task);
    waitLatch(latch);

    assertTrue(cluster.start(targetPeer.getEndpoint()));
    Thread.sleep(5000);
    cluster.waitLeader();
    leader = cluster.getLeader();
    Assert.assertEquals(targetPeer, leader.getNodeId().getPeerId());
    assertTrue(cluster.ensureSame(5));

    cluster.stopAll();
}
 
Example 6
Source File: CliServiceTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("SameParameterValue")
private void sendTestTaskAndWait(final Node node, final int code) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(10);
    for (int i = 0; i < 10; i++) {
        final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes());
        final Task task = new Task(data, new ExpectClosure(code, null, latch));
        node.apply(task);
    }
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
 
Example 7
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Test
public void testNodeTaskOverload() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    final PeerId peer = new PeerId(addr, 0);

    NodeManager.getInstance().addAddress(addr);
    final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
    final RaftOptions raftOptions = new RaftOptions();
    raftOptions.setDisruptorBufferSize(2);
    nodeOptions.setRaftOptions(raftOptions);
    final MockStateMachine fsm = new MockStateMachine(addr);
    nodeOptions.setFsm(fsm);
    nodeOptions.setLogUri(this.dataPath + File.separator + "log");
    nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer)));
    final Node node = new NodeImpl("unittest", peer);
    assertTrue(node.init(nodeOptions));

    assertEquals(1, node.listPeers().size());
    assertTrue(node.listPeers().contains(peer));

    while (!node.isLeader()) {
        ;
    }

    final List<Task> tasks = new ArrayList<>();
    final AtomicInteger c = new AtomicInteger(0);
    for (int i = 0; i < 10; i++) {
        final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes());
        final Task task = new Task(data, new JoinableClosure(status -> {
            System.out.println(status);
            if (!status.isOk()) {
                assertTrue(
                        status.getRaftError() == RaftError.EBUSY || status.getRaftError() == RaftError.EPERM);
            }
            c.incrementAndGet();
        }));
        node.apply(task);
        tasks.add(task);
    }
    try {
        Task.joinAll(tasks, TimeUnit.SECONDS.toMillis(30));
        assertEquals(10, c.get());
    } finally {
        node.shutdown();
        node.join();
    }
}
 
Example 8
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Test
public void testRecoverFollower() 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();

    final Node leader = cluster.getLeader();
    assertNotNull(leader);

    Thread.sleep(100);

    final List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());

    final Endpoint followerAddr = followers.get(0).getNodeId().getPeerId().getEndpoint().copy();
    assertTrue(cluster.stop(followerAddr));

    this.sendTestTaskAndWait(leader);

    for (int i = 10; i < 30; i++) {
        final ByteBuffer data = ByteBuffer.wrap(("no clusre" + i).getBytes());
        final Task task = new Task(data, null);
        leader.apply(task);
    }
    // wait leader to compact logs
    Thread.sleep(5000);
    // restart follower
    assertTrue(cluster.start(followerAddr));
    assertTrue(cluster.ensureSame(30));
    assertEquals(3, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(30, fsm.getLogs().size());
    }
    cluster.stopAll();
}