Search in sources :

Example 51 with Node

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

the class KVStateMachineTest method failApplyTest.

@Test
public void failApplyTest() throws Exception {
    final CountDownLatch latch = new CountDownLatch(APPLY_COUNT);
    final List<KVStoreClosure> closures = new ArrayList<>();
    final BlockingQueue<Status> successQueue = new ArrayBlockingQueue<>(APPLY_COUNT);
    final BlockingQueue<Status> failQueue = new ArrayBlockingQueue<>(APPLY_COUNT);
    assertTrue(this.raftGroupService.getRaftNode().isLeader());
    for (int i = 0; i < SUCCESS_COUNT; i++) {
        final KVStoreClosure c = new BaseKVStoreClosure() {

            @Override
            public void run(Status status) {
                successQueue.add(status);
                latch.countDown();
            }
        };
        closures.add(c);
    }
    for (int i = SUCCESS_COUNT; i < APPLY_COUNT; i++) {
        final KVStoreClosure c = new BaseKVStoreClosure() {

            @Override
            public void run(Status status) {
                failQueue.add(status);
                latch.countDown();
            }
        };
        closures.add(c);
    }
    for (int i = 0; i < SUCCESS_COUNT; i++) {
        final byte[] bytes = BytesUtil.writeUtf8(String.valueOf(i));
        this.raftRawKVStore.put(bytes, bytes, closures.get(i));
    }
    for (int i = SUCCESS_COUNT; i < APPLY_COUNT; i++) {
        final byte[] bytes = BytesUtil.writeUtf8(String.valueOf(i));
        this.raftRawKVStore.merge(bytes, bytes, closures.get(i));
    }
    latch.await();
    final Node node = this.raftGroupService.getRaftNode();
    assertFalse(node.isLeader());
    final Field field = node.getClass().getDeclaredField("state");
    field.setAccessible(true);
    assertEquals(field.get(node), STATE_ERROR);
    assertEquals(SUCCESS_COUNT, successQueue.size());
    assertEquals(APPLY_COUNT - SUCCESS_COUNT, failQueue.size());
    while (true) {
        final Status st = successQueue.poll();
        if (st == null) {
            break;
        }
        assertTrue(st.isOk());
    }
    while (true) {
        final Status st = failQueue.poll();
        if (st == null) {
            break;
        }
        assertFalse(st.isOk());
        assertTrue(st.getRaftError() == RaftError.ESTATEMACHINE || st.getRaftError() == RaftError.EPERM);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Field(java.lang.reflect.Field) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Test(org.junit.Test)

Example 52 with Node

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

the class NodeTest method testShutdownAndJoinWorkAfterInitFails.

@Test
public void testShutdownAndJoinWorkAfterInitFails() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    NodeManager.getInstance().addAddress(addr);
    {
        final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
        final MockStateMachine fsm = new MockStateMachine(addr);
        nodeOptions.setFsm(fsm);
        nodeOptions.setLogUri(this.dataPath + File.separator + "log");
        nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
        nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
        nodeOptions.setSnapshotIntervalSecs(10);
        nodeOptions.setInitialConf(new Configuration(Collections.singletonList(new PeerId(addr, 0))));
        final Node node = new NodeImpl("unittest", new PeerId(addr, 0));
        assertTrue(node.init(nodeOptions));
        Thread.sleep(1000);
        this.sendTestTaskAndWait(node);
        // save snapshot
        final CountDownLatch latch = new CountDownLatch(1);
        node.snapshot(new ExpectClosure(latch));
        waitLatch(latch);
        node.shutdown();
        node.join();
    }
    {
        final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
        final MockStateMachine fsm = new MockFSM1(addr);
        nodeOptions.setFsm(fsm);
        nodeOptions.setLogUri(this.dataPath + File.separator + "log");
        nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
        nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
        nodeOptions.setSnapshotIntervalSecs(10);
        nodeOptions.setInitialConf(new Configuration(Collections.singletonList(new PeerId(addr, 0))));
        final Node node = new NodeImpl("unittest", new PeerId(addr, 0));
        assertFalse(node.init(nodeOptions));
        node.shutdown();
        node.join();
    }
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 53 with Node

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

the class NodeTest method testAutoSnapshot.

@Test
public void testAutoSnapshot() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    NodeManager.getInstance().addAddress(addr);
    final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
    final MockStateMachine fsm = new MockStateMachine(addr);
    nodeOptions.setFsm(fsm);
    nodeOptions.setLogUri(this.dataPath + File.separator + "log");
    nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOptions.setSnapshotIntervalSecs(10);
    nodeOptions.setInitialConf(new Configuration(Collections.singletonList(new PeerId(addr, 0))));
    final Node node = new NodeImpl("unittest", new PeerId(addr, 0));
    assertTrue(node.init(nodeOptions));
    // wait node elect self as leader
    Thread.sleep(2000);
    sendTestTaskAndWait(node);
    // wait for auto snapshot
    Thread.sleep(10000);
    // first snapshot will be triggered randomly
    final int times = fsm.getSaveSnapshotTimes();
    assertTrue("snapshotTimes=" + times, times >= 1);
    assertTrue(fsm.getSnapshotIndex() > 0);
    final CountDownLatch latch = new CountDownLatch(1);
    node.shutdown(new ExpectClosure(latch));
    node.join();
    waitLatch(latch);
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 54 with Node

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

the class NodeTest method testFollowerStartStopFollowing.

@Test
public void testFollowerStartStopFollowing() throws Exception {
    // start five nodes
    final List<PeerId> peers = TestUtils.generatePeers(5);
    final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers, 1000);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint()));
    }
    cluster.waitLeader();
    final Node firstLeader = cluster.getLeader();
    assertNotNull(firstLeader);
    // apply something
    this.sendTestTaskAndWait(firstLeader);
    // assert follow times
    final List<Node> firstFollowers = cluster.getFollowers();
    assertEquals(4, firstFollowers.size());
    for (final Node node : firstFollowers) {
        assertEquals(1, ((MockStateMachine) node.getOptions().getFsm()).getOnStartFollowingTimes());
        assertEquals(0, ((MockStateMachine) node.getOptions().getFsm()).getOnStopFollowingTimes());
    }
    // stop leader and elect new one
    final Endpoint fstLeaderAddr = firstLeader.getNodeId().getPeerId().getEndpoint();
    assertTrue(cluster.stop(fstLeaderAddr));
    cluster.waitLeader();
    final Node secondLeader = cluster.getLeader();
    assertNotNull(secondLeader);
    this.sendTestTaskAndWait(secondLeader, 10, RaftError.SUCCESS);
    // ensure start/stop following times
    final List<Node> secondFollowers = cluster.getFollowers();
    assertEquals(3, secondFollowers.size());
    for (final Node node : secondFollowers) {
        assertEquals(2, ((MockStateMachine) node.getOptions().getFsm()).getOnStartFollowingTimes());
        assertEquals(1, ((MockStateMachine) node.getOptions().getFsm()).getOnStopFollowingTimes());
    }
    // transfer leadership to a follower
    final PeerId targetPeer = secondFollowers.get(0).getNodeId().getPeerId().copy();
    assertTrue(secondLeader.transferLeadershipTo(targetPeer).isOk());
    Thread.sleep(100);
    cluster.waitLeader();
    final Node thirdLeader = cluster.getLeader();
    Assert.assertEquals(targetPeer, thirdLeader.getNodeId().getPeerId());
    this.sendTestTaskAndWait(thirdLeader, 20, RaftError.SUCCESS);
    final List<Node> thirdFollowers = cluster.getFollowers();
    assertEquals(3, thirdFollowers.size());
    for (int i = 0; i < 3; i++) {
        if (thirdFollowers.get(i).getNodeId().getPeerId().equals(secondLeader.getNodeId().getPeerId())) {
            assertEquals(2, ((MockStateMachine) thirdFollowers.get(i).getOptions().getFsm()).getOnStartFollowingTimes());
            assertEquals(1, ((MockStateMachine) thirdFollowers.get(i).getOptions().getFsm()).getOnStopFollowingTimes());
            continue;
        }
        assertEquals(3, ((MockStateMachine) thirdFollowers.get(i).getOptions().getFsm()).getOnStartFollowingTimes());
        assertEquals(2, ((MockStateMachine) thirdFollowers.get(i).getOptions().getFsm()).getOnStopFollowingTimes());
    }
    cluster.ensureSame();
    cluster.stopAll();
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Node(com.alipay.sofa.jraft.Node) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 55 with Node

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

the class NodeTest method testInitShutdown.

@Test
public void testInitShutdown() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    NodeManager.getInstance().addAddress(addr);
    final NodeOptions nodeOptions = new NodeOptions();
    nodeOptions.setFsm(new MockStateMachine(addr));
    nodeOptions.setLogUri(this.dataPath + File.separator + "log");
    nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    final Node node = new NodeImpl("unittest", new PeerId(addr, 0));
    assertTrue(node.init(nodeOptions));
    node.shutdown();
    node.join();
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Node(com.alipay.sofa.jraft.Node) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) 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