use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeTest method testVoteTimedoutStepDown.
@Test
public void testVoteTimedoutStepDown() 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);
// Stop all followers
List<Node> followers = cluster.getFollowers();
assertFalse(followers.isEmpty());
for (Node node : followers) {
assertTrue(cluster.stop(node.getNodeId().getPeerId().getEndpoint()));
}
// Wait leader to step down.
while (leader.isLeader()) {
Thread.sleep(10);
}
// old leader try to elect self, it should fail.
((NodeImpl) leader).tryElectSelf();
Thread.sleep(1500);
// Start followers
for (Node node : followers) {
assertTrue(cluster.start(node.getNodeId().getPeerId().getEndpoint()));
}
cluster.ensureSame(-1);
cluster.stopAll();
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeTest method testTripleNodesWithReplicatorStateListener.
@Test
public void testTripleNodesWithReplicatorStateListener() 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()));
}
final UserReplicatorStateListener listener1 = new UserReplicatorStateListener();
final UserReplicatorStateListener listener2 = new UserReplicatorStateListener();
for (Node node : cluster.getNodes()) {
node.addReplicatorStateListener(listener1);
node.addReplicatorStateListener(listener2);
}
// elect leader
cluster.waitLeader();
assertEquals(4, this.startedCounter.get());
assertEquals(2, cluster.getLeader().getReplicatorStatueListeners().size());
assertEquals(2, cluster.getFollowers().get(0).getReplicatorStatueListeners().size());
assertEquals(2, cluster.getFollowers().get(1).getReplicatorStatueListeners().size());
for (Node node : cluster.getNodes()) {
node.removeReplicatorStateListener(listener1);
}
assertEquals(1, cluster.getLeader().getReplicatorStatueListeners().size());
assertEquals(1, cluster.getFollowers().get(0).getReplicatorStatueListeners().size());
assertEquals(1, cluster.getFollowers().get(1).getReplicatorStatueListeners().size());
cluster.stopAll();
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeTest method testChangePeersAddMultiNodes.
@Test
public void testChangePeersAddMultiNodes() 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();
final Node leader = cluster.getLeader();
this.sendTestTaskAndWait(leader);
final Configuration conf = new Configuration();
for (int i = 0; i < 3; i++) {
final PeerId peer = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + i);
conf.addPeer(peer);
}
PeerId peer = new PeerId(TestUtils.getMyIp(), peer0.getEndpoint().getPort() + 1);
// fail, because the peers are not started.
final SynchronizedClosure done = new SynchronizedClosure();
leader.changePeers(new Configuration(Collections.singletonList(peer)), done);
Assert.assertEquals(RaftError.ECATCHUP, done.await().getRaftError());
// start peer1
assertTrue(cluster.start(peer.getEndpoint()));
// still fail, because peer2 is not started
done.reset();
leader.changePeers(conf, done);
Assert.assertEquals(RaftError.ECATCHUP, done.await().getRaftError());
// start peer2
peer = new PeerId(TestUtils.getMyIp(), peer0.getEndpoint().getPort() + 2);
assertTrue(cluster.start(peer.getEndpoint()));
done.reset();
// works
leader.changePeers(conf, done);
assertTrue(done.await().isOk());
assertTrue(cluster.ensureSame());
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 testRemoveFollower.
@Test
public void testRemoveFollower() throws Exception {
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);
// apply tasks to leader
this.sendTestTaskAndWait(leader);
cluster.ensureSame();
List<Node> followers = cluster.getFollowers();
assertEquals(2, followers.size());
final PeerId followerPeer = followers.get(0).getNodeId().getPeerId();
final Endpoint followerAddr = followerPeer.getEndpoint();
// stop and clean follower
LOG.info("Stop and clean follower {}", followerPeer);
assertTrue(cluster.stop(followerAddr));
cluster.clean(followerAddr);
// 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);
followers = cluster.getFollowers();
assertEquals(1, followers.size());
peers = TestUtils.generatePeers(3);
assertTrue(peers.remove(followerPeer));
// start follower
LOG.info("Start and add follower {}", followerPeer);
assertTrue(cluster.start(followerAddr));
// re-add follower
latch = new CountDownLatch(1);
leader.addPeer(followerPeer, new ExpectClosure(latch));
waitLatch(latch);
followers = cluster.getFollowers();
assertEquals(2, followers.size());
cluster.ensureSame();
assertEquals(3, cluster.getFsms().size());
for (final MockStateMachine fsm : cluster.getFsms()) {
assertEquals(20, fsm.getLogs().size());
}
cluster.stopAll();
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeTest method testTripleNodesWithStaticLearners.
@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();
}
Aggregations