use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class CliServiceTest method testRelalanceOnTransferLeaderFail.
@Test
public void testRelalanceOnTransferLeaderFail() {
final Set<String> groupIds = new TreeSet<>();
groupIds.add("group_1");
groupIds.add("group_2");
groupIds.add("group_3");
groupIds.add("group_4");
groupIds.add("group_5");
groupIds.add("group_6");
groupIds.add("group_7");
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 MockTransferLeaderFailCliService(rebalancedLeaderIds, new PeerId("host_1", 8080));
assertEquals("Fail to transfer leader", cliService.rebalance(groupIds, conf, rebalancedLeaderIds).getErrorMsg());
assertTrue(groupIds.size() >= rebalancedLeaderIds.size());
final Map<PeerId, Integer> ret = new HashMap<>();
for (Map.Entry<String, PeerId> entry : rebalancedLeaderIds.entrySet()) {
ret.compute(entry.getValue(), (ignored, num) -> num == null ? 1 : num + 1);
}
for (Map.Entry<PeerId, Integer> entry : ret.entrySet()) {
System.out.println(entry);
assertEquals(new PeerId("host_1", 8080), entry.getKey());
}
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class CliServiceTest method setup.
@Before
public void setup() throws Exception {
System.out.println(">>>>>>>>>>>>>>> Start test method: " + this.testName.getMethodName());
this.dataPath = TestUtils.mkTempDir();
FileUtils.forceMkdir(new File(this.dataPath));
assertEquals(NodeImpl.GLOBAL_NUM_NODES.get(), 0);
final List<PeerId> peers = TestUtils.generatePeers(3);
final LinkedHashSet<PeerId> learners = new LinkedHashSet<>();
// 2 learners
for (int i = 0; i < 2; i++) {
learners.add(new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + LEARNER_PORT_STEP + i));
}
this.cluster = new TestCluster(this.groupId, this.dataPath, peers, learners, 300);
for (final PeerId peer : peers) {
this.cluster.start(peer.getEndpoint());
}
for (final PeerId peer : learners) {
this.cluster.startLearner(peer);
}
this.cluster.waitLeader();
this.cliService = new CliServiceImpl();
this.conf = new Configuration(peers, learners);
assertTrue(this.cliService.init(new CliOptions()));
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class CliServiceTest method testChangePeers.
@Test
public void testChangePeers() throws Exception {
final List<PeerId> newPeers = TestUtils.generatePeers(10);
newPeers.removeAll(this.conf.getPeerSet());
for (final PeerId peer : newPeers) {
assertTrue(this.cluster.start(peer.getEndpoint()));
}
this.cluster.waitLeader();
final Node oldLeaderNode = this.cluster.getLeader();
assertNotNull(oldLeaderNode);
final PeerId oldLeader = oldLeaderNode.getNodeId().getPeerId();
assertNotNull(oldLeader);
assertTrue(this.cliService.changePeers(this.groupId, this.conf, new Configuration(newPeers)).isOk());
this.cluster.waitLeader();
final PeerId newLeader = this.cluster.getLeader().getNodeId().getPeerId();
assertNotEquals(oldLeader, newLeader);
assertTrue(newPeers.contains(newLeader));
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class CliServiceTest method testRebalance.
@Test
public void testRebalance() {
final Set<String> groupIds = new TreeSet<>();
groupIds.add("group_1");
groupIds.add("group_2");
groupIds.add("group_3");
groupIds.add("group_4");
groupIds.add("group_5");
groupIds.add("group_6");
groupIds.add("group_7");
groupIds.add("group_8");
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 MockCliService(rebalancedLeaderIds, new PeerId("host_1", 8080));
assertTrue(cliService.rebalance(groupIds, conf, rebalancedLeaderIds).isOk());
assertEquals(groupIds.size(), rebalancedLeaderIds.size());
final Map<PeerId, Integer> ret = new HashMap<>();
for (Map.Entry<String, PeerId> entry : rebalancedLeaderIds.entrySet()) {
ret.compute(entry.getValue(), (ignored, num) -> num == null ? 1 : num + 1);
}
final int expectedAvgLeaderNum = (int) Math.ceil((double) groupIds.size() / conf.size());
for (Map.Entry<PeerId, Integer> entry : ret.entrySet()) {
System.out.println(entry);
assertTrue(entry.getValue() <= expectedAvgLeaderNum);
}
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class NodeImpl method resetLearners.
@Override
public void resetLearners(final List<PeerId> learners, final Closure done) {
checkPeers(learners);
this.writeLock.lock();
try {
final Configuration newConf = new Configuration(this.conf.getConf());
newConf.setLearners(new LinkedHashSet<>(learners));
unsafeRegisterConfChange(this.conf.getConf(), newConf, done);
} finally {
this.writeLock.unlock();
}
}
Aggregations