use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeTest method testInstallSnapshot.
@Test
public void testInstallSnapshot() 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();
// stop follower1
final List<Node> followers = cluster.getFollowers();
assertEquals(2, followers.size());
final Endpoint followerAddr = followers.get(0).getNodeId().getPeerId().getEndpoint();
assertTrue(cluster.stop(followerAddr));
// apply something more
this.sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
// trigger leader snapshot
triggerLeaderSnapshot(cluster, leader);
// apply something more
this.sendTestTaskAndWait(leader, 20, RaftError.SUCCESS);
triggerLeaderSnapshot(cluster, leader, 2);
// wait leader to compact logs
Thread.sleep(50);
// restart follower.
cluster.clean(followerAddr);
assertTrue(cluster.start(followerAddr, true, 300));
Thread.sleep(2000);
cluster.ensureSame();
assertEquals(3, cluster.getFsms().size());
for (final MockStateMachine fsm : cluster.getFsms()) {
assertEquals(30, fsm.getLogs().size());
}
cluster.stopAll();
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeTest method testLeaderTransfer.
@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();
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeTest method testTransferShouldWorkAfterInstallSnapshot.
@Test
public void testTransferShouldWorkAfterInstallSnapshot() throws Exception {
final List<PeerId> peers = TestUtils.generatePeers(3);
final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers, 1000);
for (int i = 0; i < peers.size() - 1; i++) {
assertTrue(cluster.start(peers.get(i).getEndpoint()));
}
cluster.waitLeader();
Node leader = cluster.getLeader();
assertNotNull(leader);
this.sendTestTaskAndWait(leader);
final List<Node> followers = cluster.getFollowers();
assertEquals(1, followers.size());
final PeerId follower = followers.get(0).getNodeId().getPeerId();
assertTrue(leader.transferLeadershipTo(follower).isOk());
Thread.sleep(2000);
leader = cluster.getLeader();
Assert.assertEquals(follower, leader.getNodeId().getPeerId());
CountDownLatch latch = new CountDownLatch(1);
leader.snapshot(new ExpectClosure(latch));
waitLatch(latch);
latch = new CountDownLatch(1);
leader.snapshot(new ExpectClosure(latch));
waitLatch(latch);
// start the last peer which should be recover with snapshot.
final PeerId lastPeer = peers.get(2);
assertTrue(cluster.start(lastPeer.getEndpoint()));
Thread.sleep(5000);
assertTrue(leader.transferLeadershipTo(lastPeer).isOk());
Thread.sleep(2000);
leader = cluster.getLeader();
Assert.assertEquals(lastPeer, leader.getNodeId().getPeerId());
assertEquals(3, cluster.getFsms().size());
for (final MockStateMachine fsm : cluster.getFsms()) {
assertEquals(10, fsm.getLogs().size());
}
cluster.stopAll();
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeTest method testPreVote.
@Test
public void testPreVote() throws Exception {
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
Node leader = cluster.getLeader();
final long savedTerm = ((NodeImpl) leader).getCurrentTerm();
assertNotNull(leader);
// apply tasks to leader
this.sendTestTaskAndWait(leader);
cluster.ensureSame();
final List<Node> followers = cluster.getFollowers();
assertEquals(2, followers.size());
final PeerId followerPeer = followers.get(0).getNodeId().getPeerId();
final Endpoint followerAddr = followerPeer.getEndpoint();
// remove follower
LOG.info("Remove follower {}", followerPeer);
CountDownLatch latch = new CountDownLatch(1);
leader.removePeer(followerPeer, new ExpectClosure(latch));
waitLatch(latch);
this.sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
Thread.sleep(2000);
// add follower
LOG.info("Add follower {}", followerAddr);
peers = TestUtils.generatePeers(3);
assertTrue(peers.remove(followerPeer));
latch = new CountDownLatch(1);
leader.addPeer(followerPeer, new ExpectClosure(latch));
waitLatch(latch);
leader = cluster.getLeader();
assertNotNull(leader);
// leader term should not be changed.
assertEquals(savedTerm, ((NodeImpl) leader).getCurrentTerm());
cluster.stopAll();
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeTest method testNodesWithZeroValPriorityElection.
@Test
public void testNodesWithZeroValPriorityElection() throws Exception {
List<Integer> priorities = new ArrayList<Integer>();
priorities.add(50);
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()));
}
// elect leader
cluster.waitLeader();
// get leader
final Node leader = cluster.getLeader();
assertNotNull(leader);
assertEquals(3, leader.listPeers().size());
assertEquals(2, cluster.getFollowers().size());
assertEquals(50, leader.getNodeTargetPriority());
assertEquals(50, leader.getLeaderId().getPriority());
cluster.stopAll();
}
Aggregations