use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class RouteTableTest method testRefreshConfiguration.
@Test
public void testRefreshConfiguration() throws Exception {
final RouteTable rt = RouteTable.getInstance();
final List<PeerId> partConf = new ArrayList<>();
partConf.add(cluster.getLeader().getLeaderId());
// part of peers conf, only contains leader peer
rt.updateConfiguration(groupId, new Configuration(partConf));
// fetch all conf
final Status st = rt.refreshConfiguration(cliClientService, groupId, 10000);
assertTrue(st.isOk());
final Configuration newCnf = rt.getConfiguration(groupId);
assertArrayEquals(new HashSet<>(cluster.getPeers()).toArray(), new HashSet<>(newCnf.getPeerSet()).toArray());
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class CliServiceTest method testGetPeers.
@Test
public void testGetPeers() throws Exception {
PeerId leader = this.cluster.getLeader().getNodeId().getPeerId();
assertNotNull(leader);
assertArrayEquals(this.conf.getPeerSet().toArray(), new HashSet<>(this.cliService.getPeers(this.groupId, this.conf)).toArray());
// stop one peer
final List<PeerId> peers = this.conf.getPeers();
this.cluster.stop(peers.get(0).getEndpoint());
this.cluster.waitLeader();
leader = this.cluster.getLeader().getNodeId().getPeerId();
assertNotNull(leader);
assertArrayEquals(this.conf.getPeerSet().toArray(), new HashSet<>(this.cliService.getPeers(this.groupId, this.conf)).toArray());
this.cluster.stopAll();
try {
this.cliService.getPeers(this.groupId, this.conf);
fail();
} catch (final IllegalStateException e) {
assertTrue(e.getMessage().contains("Fail to get leader of group " + this.groupId));
}
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class CliServiceTest method testTransferLeader.
@Test
public void testTransferLeader() throws Exception {
final PeerId leader = this.cluster.getLeader().getNodeId().getPeerId().copy();
assertNotNull(leader);
final Set<PeerId> peers = this.conf.getPeerSet();
PeerId targetPeer = null;
for (final PeerId peer : peers) {
if (!peer.equals(leader)) {
targetPeer = peer;
break;
}
}
assertNotNull(targetPeer);
assertTrue(this.cliService.transferLeader(this.groupId, this.conf, targetPeer).isOk());
this.cluster.waitLeader();
assertEquals(targetPeer, this.cluster.getLeader().getNodeId().getPeerId());
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class CliServiceTest method testRebalanceOnLeaderFail.
@Test
public void testRebalanceOnLeaderFail() {
final Set<String> groupIds = new TreeSet<>();
groupIds.add("group_1");
groupIds.add("group_2");
groupIds.add("group_3");
groupIds.add("group_4");
final Configuration conf = new Configuration();
conf.addPeer(new PeerId("host_1", 8080));
conf.addPeer(new PeerId("host_2", 8080));
conf.addPeer(new PeerId("host_3", 8080));
final Map<String, PeerId> rebalancedLeaderIds = new HashMap<>();
final CliService cliService = new MockLeaderFailCliService();
assertEquals("Fail to get leader", cliService.rebalance(groupIds, conf, rebalancedLeaderIds).getErrorMsg());
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class CliServiceTest method testLearnerServices.
@Test
public void testLearnerServices() throws Exception {
final PeerId learner3 = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + LEARNER_PORT_STEP + 3);
assertTrue(this.cluster.startLearner(learner3));
sendTestTaskAndWait(this.cluster.getLeader(), 0);
Thread.sleep(500);
for (final MockStateMachine fsm : this.cluster.getFsms()) {
if (!fsm.getAddress().equals(learner3.getEndpoint())) {
assertEquals(10, fsm.getLogs().size());
}
}
assertEquals(0, this.cluster.getFsmByPeer(learner3).getLogs().size());
List<PeerId> oldLearners = new ArrayList<PeerId>(this.conf.getLearners());
assertEquals(oldLearners, this.cliService.getLearners(this.groupId, this.conf));
assertEquals(oldLearners, this.cliService.getAliveLearners(this.groupId, this.conf));
// Add learner3
this.cliService.addLearners(this.groupId, this.conf, Arrays.asList(learner3));
Thread.sleep(100);
assertEquals(10, this.cluster.getFsmByPeer(learner3).getLogs().size());
sendTestTaskAndWait(this.cluster.getLeader(), 0);
Thread.sleep(500);
for (final MockStateMachine fsm : this.cluster.getFsms()) {
assertEquals(20, fsm.getLogs().size());
}
List<PeerId> newLearners = new ArrayList<>(oldLearners);
newLearners.add(learner3);
assertEquals(newLearners, this.cliService.getLearners(this.groupId, this.conf));
assertEquals(newLearners, this.cliService.getAliveLearners(this.groupId, this.conf));
// Remove 3
this.cliService.removeLearners(this.groupId, this.conf, Arrays.asList(learner3));
sendTestTaskAndWait(this.cluster.getLeader(), 0);
Thread.sleep(500);
for (final MockStateMachine fsm : this.cluster.getFsms()) {
if (!fsm.getAddress().equals(learner3.getEndpoint())) {
assertEquals(30, fsm.getLogs().size());
}
}
// Latest 10 logs are not replicated to learner3, because it's removed.
assertEquals(20, this.cluster.getFsmByPeer(learner3).getLogs().size());
assertEquals(oldLearners, this.cliService.getLearners(this.groupId, this.conf));
assertEquals(oldLearners, this.cliService.getAliveLearners(this.groupId, this.conf));
// Set learners into [learner3]
this.cliService.resetLearners(this.groupId, this.conf, Arrays.asList(learner3));
Thread.sleep(100);
assertEquals(30, this.cluster.getFsmByPeer(learner3).getLogs().size());
sendTestTaskAndWait(this.cluster.getLeader(), 0);
Thread.sleep(500);
// Latest 10 logs are not replicated to learner1 and learner2, because they were removed by resetting learners set.
for (final MockStateMachine fsm : this.cluster.getFsms()) {
if (!oldLearners.contains(new PeerId(fsm.getAddress(), 0))) {
assertEquals(40, fsm.getLogs().size());
} else {
assertEquals(30, fsm.getLogs().size());
}
}
assertEquals(Arrays.asList(learner3), this.cliService.getLearners(this.groupId, this.conf));
assertEquals(Arrays.asList(learner3), this.cliService.getAliveLearners(this.groupId, this.conf));
// Stop learner3
this.cluster.stop(learner3.getEndpoint());
Thread.sleep(1000);
assertEquals(Arrays.asList(learner3), this.cliService.getLearners(this.groupId, this.conf));
assertTrue(this.cliService.getAliveLearners(this.groupId, this.conf).isEmpty());
}
Aggregations