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