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