com.alipay.sofa.jraft.Node Java Examples

The following examples show how to use com.alipay.sofa.jraft.Node. 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: NodeTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Test
public void testNodesWithPartPriorityElection() throws Exception {

    List<Integer> priorities = new ArrayList<>();
    priorities.add(100);
    priorities.add(40);
    priorities.add(-1);

    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()));
    }

    // elect leader
    cluster.waitLeader();

    // get leader
    final Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(3, leader.listPeers().size());
    assertEquals(2, cluster.getFollowers().size());
    cluster.stopAll();
}
 
Example #2
Source File: AppendEntriesRequestProcessor.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Override
public Executor select(final String reqClass, final Object reqHeader) {
    final AppendEntriesRequestHeader header = (AppendEntriesRequestHeader) reqHeader;
    final String groupId = header.getGroupId();
    final String peerId = header.getPeerId();

    final PeerId peer = new PeerId();

    if (!peer.parse(peerId)) {
        return executor();
    }

    final Node node = NodeManager.getInstance().get(groupId, peer);

    if (node == null || !node.getRaftOptions().isReplicatorPipeline()) {
        return executor();
    }

    // The node enable pipeline, we should ensure bolt support it.
    RpcFactoryHelper.rpcFactory().ensurePipeline();

    final PeerRequestContext ctx = getPeerRequestContext(groupId, peerId, null);

    return ctx.executor;
}
 
Example #3
Source File: BaseCliRequestProcessor.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
protected Node getNode(String groupId, PeerId peerId, Status st) {
    Node node = null;

    if (peerId != null) {
        node = NodeManager.getInstance().get(groupId, peerId);
        if (node == null) {
            st.setError(RaftError.ENOENT, "Fail to find node %s in group %s", peerId, groupId);
        }
    } else {
        List<Node> nodes = NodeManager.getInstance().getNodesByGroupId(groupId);
        if (nodes == null || nodes.isEmpty()) {
            st.setError(RaftError.ENOENT, "Empty nodes in group %s", groupId);
        } else if (nodes.size() > 1) {
            st.setError(RaftError.EINVAL, "Peer must be specified since there're %d nodes in group %s",
                nodes.size(), groupId);
        } else {
            node = nodes.get(0);
        }

    }
    if (node != null && node.getOptions().isDisableCli()) {
        st.setError(RaftError.EACCES, "Cli service is not allowed to access node %s", node.getNodeId());
    }
    return node;
}
 
Example #4
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #5
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #6
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Test
public void testNodesWithSpecialPriorityElection() throws Exception {

    List<Integer> priorities = new ArrayList<Integer>();
    priorities.add(0);
    priorities.add(0);
    priorities.add(-1);

    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()));
    }

    // elect leader
    cluster.waitLeader();

    // get leader
    final Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(3, leader.listPeers().size());
    assertEquals(2, cluster.getFollowers().size());
    cluster.stopAll();
}
 
Example #7
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Test
public void testLeaderShouldNotChange() 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 leader0 = cluster.getLeader();
    assertNotNull(leader0);
    final long savedTerm = ((NodeImpl) leader0).getCurrentTerm();
    LOG.info("Current leader is {}, term is {}", leader0, savedTerm);
    Thread.sleep(5000);
    cluster.waitLeader();
    final Node leader1 = cluster.getLeader();
    assertNotNull(leader1);
    LOG.info("Current leader is {}", leader1);
    assertEquals(savedTerm, ((NodeImpl) leader1).getCurrentTerm());
    cluster.stopAll();
}
 
Example #8
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "unused", "SameParameterValue" })
private void assertReadIndex(final Node node, final int index) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final byte[] requestContext = TestUtils.getRandomBytes();
    node.readIndex(requestContext, new ReadIndexClosure() {

        @Override
        public void run(final Status status, final long theIndex, final byte[] reqCtx) {
            assertTrue(status.getErrorMsg(), status.isOk());
            assertEquals(index, theIndex);
            assertArrayEquals(requestContext, reqCtx);
            latch.countDown();
        }
    });
    latch.await();
}
 
Example #9
Source File: BaseCliRequestProcessorTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Test
public void testManyNodes() {
    Node node1 = Mockito.mock(Node.class);
    Mockito.when(node1.getGroupId()).thenReturn("test");
    Mockito.when(node1.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost", 8081)));
    NodeOptions opts = new NodeOptions();
    Mockito.when(node1.getOptions()).thenReturn(opts);
    NodeManager.getInstance().addAddress(new Endpoint("localhost", 8081));
    NodeManager.getInstance().add(node1);

    Node node2 = Mockito.mock(Node.class);
    Mockito.when(node2.getGroupId()).thenReturn("test");
    Mockito.when(node2.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost", 8082)));
    Mockito.when(node2.getOptions()).thenReturn(opts);
    NodeManager.getInstance().addAddress(new Endpoint("localhost", 8082));
    NodeManager.getInstance().add(node2);

    this.processor = new MockCliRequestProcessor(null, "test");
    this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
    ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
    assertNotNull(resp);
    assertEquals(RaftError.EINVAL.getNumber(), resp.getErrorCode());
    assertEquals("Peer must be specified since there're 2 nodes in group test", resp.getErrorMsg());
}
 
Example #10
Source File: CliServiceTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Test
public void testChangePeers() throws Exception {
    final List<PeerId> newPeers = TestUtils.generatePeers(10);
    newPeers.removeAll(this.conf.getPeerSet());
    for (final PeerId peer : newPeers) {
        assertTrue(this.cluster.start(peer.getEndpoint()));
    }
    this.cluster.waitLeader();
    final Node oldLeaderNode = this.cluster.getLeader();
    assertNotNull(oldLeaderNode);
    final PeerId oldLeader = oldLeaderNode.getNodeId().getPeerId();
    assertNotNull(oldLeader);
    assertTrue(this.cliService.changePeers(this.groupId, this.conf, new Configuration(newPeers)).isOk());
    this.cluster.waitLeader();
    final PeerId newLeader = this.cluster.getLeader().getNodeId().getPeerId();
    assertNotEquals(oldLeader, newLeader);
    assertTrue(newPeers.contains(newLeader));
}
 
Example #11
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetPeer1() throws Exception {
    final TestCluster cluster = new TestCluster("testSetPeer1", this.dataPath, new ArrayList<>());

    final PeerId bootPeer = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    assertTrue(cluster.start(bootPeer.getEndpoint()));
    final List<Node> nodes = cluster.getFollowers();
    assertEquals(1, nodes.size());

    final List<PeerId> peers = new ArrayList<>();
    peers.add(bootPeer);
    // reset peers from empty
    assertTrue(nodes.get(0).resetPeers(new Configuration(peers)).isOk());
    cluster.waitLeader();
    assertNotNull(cluster.getLeader());

    cluster.stopAll();
}
 
Example #12
Source File: TestCluster.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
public Node removeNode(final Endpoint addr) {
    Node ret = null;
    this.lock.lock();
    try {
        for (int i = 0; i < this.nodes.size(); i++) {
            if (this.nodes.get(i).getNodeId().getPeerId().getEndpoint().equals(addr)) {
                ret = this.nodes.remove(i);
                this.fsms.remove(ret.getNodeId().getPeerId());
                break;
            }
        }
    } finally {
        this.lock.unlock();
    }
    return ret;
}
 
Example #13
Source File: NodeRequestProcessor.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Override
public Message processRequest(final T request, final RpcRequestClosure done) {
    final PeerId peer = new PeerId();
    final String peerIdStr = getPeerId(request);
    if (peer.parse(peerIdStr)) {
        final String groupId = getGroupId(request);
        final Node node = NodeManager.getInstance().get(groupId, peer);
        if (node != null) {
            return processRequest0((RaftServerService) node, request, done);
        } else {
            return RpcFactoryHelper //
                .responseFactory() //
                .newResponse(defaultResp(), RaftError.ENOENT, "Peer id not found: %s, group: %s", peerIdStr,
                    groupId);
        }
    } else {
        return RpcFactoryHelper //
            .responseFactory() //
            .newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peerId: %s", peerIdStr);
    }
}
 
Example #14
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testLeaderTransfer() 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()));
    }

    cluster.waitLeader();

    Node leader = cluster.getLeader();
    assertNotNull(leader);
    this.sendTestTaskAndWait(leader);

    Thread.sleep(100);

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

    final PeerId targetPeer = followers.get(0).getNodeId().getPeerId().copy();
    LOG.info("Transfer leadership from {} to {}", leader, targetPeer);
    assertTrue(leader.transferLeadershipTo(targetPeer).isOk());
    Thread.sleep(1000);
    cluster.waitLeader();
    leader = cluster.getLeader();
    Assert.assertEquals(leader.getNodeId().getPeerId(), targetPeer);

    cluster.stopAll();
}
 
Example #15
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #16
Source File: TestCluster.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
public Node getLeader() {
    this.lock.lock();
    try {
        for (int i = 0; i < this.nodes.size(); i++) {
            final NodeImpl node = this.nodes.get(i);
            if (node.isLeader() && this.fsms.get(node.getServerId()).getLeaderTerm() == node.getCurrentTerm()) {
                return node;
            }
        }
        return null;
    } finally {
        this.lock.unlock();
    }
}
 
Example #17
Source File: TestCluster.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
public boolean stop(final Endpoint listenAddr) throws InterruptedException {
    final Node node = removeNode(listenAddr);
    final CountDownLatch latch = new CountDownLatch(1);
    if (node != null) {
        node.shutdown(new ExpectClosure(latch));
        node.join();
        latch.await();
    }
    final RaftGroupService raftGroupService = this.serverMap.remove(listenAddr.toString());
    raftGroupService.shutdown();
    raftGroupService.join();
    return node != null;
}
 
Example #18
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemovingLeaderTriggerTimeoutNow() 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()));
    }

    cluster.waitLeader();

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

    final CountDownLatch latch = new CountDownLatch(1);
    oldLeader.removePeer(oldLeader.getNodeId().getPeerId(), new ExpectClosure(latch));
    waitLatch(latch);

    Thread.sleep(100);
    leader = cluster.getLeader();
    assertNotNull(leader);
    assertNotSame(leader, oldLeader);

    cluster.stopAll();
}
 
Example #19
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testShuttingDownLeaderTriggerTimeoutNow() 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()));
    }

    cluster.waitLeader();

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

    LOG.info("Shutdown leader {}", leader);
    leader.shutdown();
    leader.join();

    Thread.sleep(100);
    leader = cluster.getLeader();
    assertNotNull(leader);
    assertNotSame(leader, oldLeader);

    cluster.stopAll();
}
 
Example #20
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 #21
Source File: RemovePeerRequestProcessorTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void verify(String interest, Node node, ArgumentCaptor<Closure> doneArg) {
    assertEquals(interest, RemovePeerRequest.class.getName());
    Mockito.verify(node).removePeer(eq(new PeerId("localhost", 8082)), doneArg.capture());
    Closure done = doneArg.getValue();
    assertNotNull(done);
    done.run(Status.OK());
    assertNotNull(this.asyncContext.getResponseObject());
    assertEquals("[localhost:8081, localhost:8082, localhost:8083]", this.asyncContext.as(RemovePeerResponse.class)
        .getOldPeersList().toString());
    assertEquals("[localhost:8081, localhost:8083]", this.asyncContext.as(RemovePeerResponse.class)
        .getNewPeersList().toString());
}
 
Example #22
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testNodesWithPriorityElection() throws Exception {

    List<Integer> priorities = new ArrayList<>();
    priorities.add(100);
    priorities.add(40);
    priorities.add(40);

    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()));
    }

    // elect leader
    cluster.waitLeader();

    // get leader
    final Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(3, leader.listPeers().size());
    assertEquals(100, leader.getNodeTargetPriority());
    assertEquals(100, leader.getLeaderId().getPriority());
    assertEquals(2, cluster.getFollowers().size());
    cluster.stopAll();
}
 
Example #23
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testTripleNodesWithStaticLearners() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);

    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    LinkedHashSet<PeerId> learners = new LinkedHashSet<>();
    PeerId learnerPeer = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + 3);
    learners.add(learnerPeer);
    cluster.setLearners(learners);

    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint()));
    }

    // elect leader
    cluster.waitLeader();
    final Node leader = cluster.getLeader();

    assertEquals(3, leader.listPeers().size());
    assertEquals(leader.listLearners().size(), 1);
    assertTrue(leader.listLearners().contains(learnerPeer));
    assertTrue(leader.listAliveLearners().isEmpty());

    // start learner after cluster setup.
    assertTrue(cluster.start(learnerPeer.getEndpoint()));

    Thread.sleep(1000);

    assertEquals(3, leader.listPeers().size());
    assertEquals(leader.listLearners().size(), 1);
    assertEquals(leader.listAliveLearners().size(), 1);

    // apply tasks to leader
    this.sendTestTaskAndWait(leader);

    cluster.ensureSame();
    assertEquals(4, cluster.getFsms().size());
    cluster.stopAll();
}
 
Example #24
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 #25
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 #26
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleNode() 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 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()) {
        ;
    }

    sendTestTaskAndWait(node);
    assertEquals(10, fsm.getLogs().size());
    int i = 0;
    for (final ByteBuffer data : fsm.getLogs()) {
        assertEquals("hello" + i++, new String(data.array()));
    }
    node.shutdown();
    node.join();
}
 
Example #27
Source File: NodeRequestProcessorTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testOK() {
    Node node = Mockito.mock(Node.class, withSettings().extraInterfaces(RaftServerService.class));
    Mockito.when(node.getGroupId()).thenReturn("test");
    PeerId peerId = new PeerId("localhost", 8081);
    Mockito.when(node.getNodeId()).thenReturn(new NodeId("test", peerId));
    NodeManager.getInstance().addAddress(peerId.getEndpoint());
    NodeManager.getInstance().add(node);

    this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
    ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
    assertNotNull(resp);
    assertEquals(0, resp.getErrorCode());
}
 
Example #28
Source File: ResetLearnersRequestProcessorTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void verify(final String interest, final Node node, final ArgumentCaptor<Closure> doneArg) {
    assertEquals(interest, ResetLearnersRequest.class.getName());
    Mockito.verify(node).resetLearners(
        eq(Arrays.asList(new PeerId("learner", 8082), new PeerId("test", 8182), new PeerId("test", 8183))),
        doneArg.capture());
    Closure done = doneArg.getValue();
    assertNotNull(done);
    done.run(Status.OK());
    assertNotNull(this.asyncContext.getResponseObject());
    assertEquals("[learner:8081, learner:8082, learner:8083]", this.asyncContext.as(LearnersOpResponse.class)
        .getOldLearnersList().toString());
    assertEquals("[learner:8082, test:8182, test:8183]", this.asyncContext.as(LearnersOpResponse.class)
        .getNewLearnersList().toString());
}
 
Example #29
Source File: RemoveLearnersRequestProcessorTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void verify(final String interest, final Node node, final ArgumentCaptor<Closure> doneArg) {
    assertEquals(interest, RemoveLearnersRequest.class.getName());
    Mockito.verify(node).removeLearners(eq(Arrays.asList(new PeerId("learner", 8082), new PeerId("test", 8183))),
        doneArg.capture());
    Closure done = doneArg.getValue();
    assertNotNull(done);
    done.run(Status.OK());
    assertNotNull(this.asyncContext.getResponseObject());
    assertEquals("[learner:8081, learner:8082, learner:8083]", this.asyncContext.as(LearnersOpResponse.class)
        .getOldLearnersList().toString());
    assertEquals("[learner:8081, learner:8083]", this.asyncContext.as(LearnersOpResponse.class)
        .getNewLearnersList().toString());
}
 
Example #30
Source File: TestCluster.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
public MockStateMachine getLeaderFsm() {
    final Node leader = getLeader();
    if (leader != null) {
        return (MockStateMachine) leader.getOptions().getFsm();
    }
    return null;
}