Search in sources :

Example 6 with Node

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

the class TestCluster method ensureLeader.

/**
 * Ensure all peers leader is expectAddr
 * @param expectAddr expected address
 * @throws InterruptedException if interrupted
 */
public void ensureLeader(final Endpoint expectAddr) throws InterruptedException {
    while (true) {
        this.lock.lock();
        for (final Node node : this.nodes) {
            final PeerId leaderId = node.getLeaderId();
            if (!leaderId.getEndpoint().equals(expectAddr)) {
                this.lock.unlock();
                Thread.sleep(10);
                continue;
            }
        }
        // all is ready
        this.lock.unlock();
        return;
    }
}
Also used : Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 7 with Node

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

the class TestCluster method stopAll.

public void stopAll() throws InterruptedException {
    final List<Endpoint> addrs = getAllNodes();
    final List<Node> nodes = new ArrayList<>();
    for (final Endpoint addr : addrs) {
        final Node node = removeNode(addr);
        node.shutdown();
        nodes.add(node);
        this.serverMap.remove(addr.toString()).shutdown();
    }
    for (final Node node : nodes) {
        node.join();
    }
    CLUSTERS.remove(this);
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList)

Example 8 with Node

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

the class TestCluster method removeNode.

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;
}
Also used : Node(com.alipay.sofa.jraft.Node) Endpoint(com.alipay.sofa.jraft.util.Endpoint)

Example 9 with Node

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

the class NodeTest method testTripleNodes.

@Test
public void testTripleNodes() 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()));
    }
    // 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);
    }
    {
        // 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());
    cluster.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) Vector(java.util.Vector) PeerId(com.alipay.sofa.jraft.entity.PeerId) TaskClosure(com.alipay.sofa.jraft.closure.TaskClosure) Test(org.junit.Test)

Example 10 with Node

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

the class NodeTest method testNodesWithPartPriorityElection.

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