Search in sources :

Example 71 with Configuration

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());
    }
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) CliService(com.alipay.sofa.jraft.CliService) HashMap(java.util.HashMap) Map(java.util.Map) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 72 with Configuration

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()));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Configuration(com.alipay.sofa.jraft.conf.Configuration) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId) CliOptions(com.alipay.sofa.jraft.option.CliOptions) Before(org.junit.Before)

Example 73 with Configuration

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));
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 74 with Configuration

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);
    }
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) CliService(com.alipay.sofa.jraft.CliService) HashMap(java.util.HashMap) Map(java.util.Map) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 75 with Configuration

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();
    }
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration)

Aggregations

Configuration (com.alipay.sofa.jraft.conf.Configuration)81 PeerId (com.alipay.sofa.jraft.entity.PeerId)54 Test (org.junit.Test)28 Node (com.alipay.sofa.jraft.Node)20 Endpoint (com.alipay.sofa.jraft.util.Endpoint)20 Status (com.alipay.sofa.jraft.Status)18 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)18 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)9 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)8 File (java.io.File)8 ArrayList (java.util.ArrayList)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)7 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)5 Task (com.alipay.sofa.jraft.entity.Task)5 CliOptions (com.alipay.sofa.jraft.option.CliOptions)5 LogId (com.alipay.sofa.jraft.entity.LogId)4 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)4 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)3 RaftException (com.alipay.sofa.jraft.error.RaftException)3