Search in sources :

Example 66 with Node

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

the class NodeTest method testChangePeers.

@Test
public void testChangePeers() throws Exception {
    final PeerId peer0 = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    final TestCluster cluster = new TestCluster("testChangePeers", this.dataPath, Collections.singletonList(peer0));
    assertTrue(cluster.start(peer0.getEndpoint()));
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    this.sendTestTaskAndWait(leader);
    for (int i = 1; i < 10; i++) {
        final PeerId peer = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + i);
        assertTrue(cluster.start(peer.getEndpoint(), true, 300));
    }
    for (int i = 0; i < 9; i++) {
        cluster.waitLeader();
        leader = cluster.getLeader();
        assertNotNull(leader);
        PeerId peer = new PeerId(TestUtils.getMyIp(), peer0.getEndpoint().getPort() + i);
        Assert.assertEquals(peer, leader.getNodeId().getPeerId());
        peer = new PeerId(TestUtils.getMyIp(), peer0.getEndpoint().getPort() + i + 1);
        final SynchronizedClosure done = new SynchronizedClosure();
        leader.changePeers(new Configuration(Collections.singletonList(peer)), done);
        assertTrue(done.await().isOk());
    }
    assertTrue(cluster.ensureSame());
    cluster.stopAll();
}
Also used : SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) Configuration(com.alipay.sofa.jraft.conf.Configuration) 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 67 with Node

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

the class NodeTest method testNodeMetrics.

@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());
}
Also used : Task(com.alipay.sofa.jraft.entity.Task) ConsoleReporter(com.codahale.metrics.ConsoleReporter) Node(com.alipay.sofa.jraft.Node) ByteBuffer(java.nio.ByteBuffer) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 68 with Node

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

the class NodeTest method testRemovingLeaderTriggerTimeoutNow.

@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();
}
Also used : Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 69 with Node

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

the class NodeTest method testResetLearners.

@Test
public void testResetLearners() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);
    final LinkedHashSet<PeerId> learners = new LinkedHashSet<>();
    for (int i = 0; i < 3; i++) {
        learners.add(new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + 3 + i));
    }
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers, learners, 300);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint()));
    }
    for (final PeerId peer : learners) {
        assertTrue(cluster.startLearner(peer));
    }
    // elect leader
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    assertEquals(3, leader.listAlivePeers().size());
    assertEquals(3, leader.listAliveLearners().size());
    this.sendTestTaskAndWait(leader);
    Thread.sleep(500);
    List<MockStateMachine> fsms = cluster.getFsms();
    assertEquals(6, fsms.size());
    cluster.ensureSame();
    {
        // Reset learners to 2 nodes
        PeerId learnerPeer = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + 3);
        learners.remove(learnerPeer);
        assertEquals(2, learners.size());
        SynchronizedClosure done = new SynchronizedClosure();
        leader.resetLearners(new ArrayList<>(learners), done);
        assertTrue(done.await().isOk());
        assertEquals(2, leader.listAliveLearners().size());
        assertEquals(2, leader.listLearners().size());
        this.sendTestTaskAndWait(leader);
        Thread.sleep(500);
        assertEquals(6, fsms.size());
        // get the removed learner's fsm
        MockStateMachine fsm = fsms.remove(3);
        assertEquals(fsm.getAddress(), learnerPeer.getEndpoint());
        // Ensure no more logs replicated to the removed learner.
        assertTrue(cluster.getLeaderFsm().getLogs().size() > fsm.getLogs().size());
        assertEquals(cluster.getLeaderFsm().getLogs().size(), 2 * fsm.getLogs().size());
    }
    {
        // remove another learner
        PeerId learnerPeer = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + 4);
        SynchronizedClosure done = new SynchronizedClosure();
        leader.removeLearners(Arrays.asList(learnerPeer), done);
        assertTrue(done.await().isOk());
        this.sendTestTaskAndWait(leader);
        Thread.sleep(500);
        // get the removed learner's fsm
        MockStateMachine fsm = fsms.remove(3);
        assertEquals(fsm.getAddress(), learnerPeer.getEndpoint());
        // Ensure no more logs replicated to the removed learner.
        assertTrue(cluster.getLeaderFsm().getLogs().size() > fsm.getLogs().size());
        assertEquals(cluster.getLeaderFsm().getLogs().size(), fsm.getLogs().size() / 2 * 3);
    }
    assertEquals(3, leader.listAlivePeers().size());
    assertEquals(1, leader.listAliveLearners().size());
    assertEquals(1, leader.listLearners().size());
    cluster.stopAll();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) 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 70 with Node

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

the class NodeTest method testNodesWithSpecialPriorityElection.

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