Search in sources :

Example 1 with IdGeneratorManager

use of com.alibaba.nacos.core.distributed.id.IdGeneratorManager in project nacos by alibaba.

the class ConfigDerbyRaft_DITCase method test_f_id_generator_leader_transfer.

@Test
public void test_f_id_generator_leader_transfer() throws Exception {
    ConfigurableApplicationContext context7 = applications.get("8847");
    ConfigurableApplicationContext context8 = applications.get("8848");
    ConfigurableApplicationContext context9 = applications.get("8849");
    IdGeneratorManager manager7 = context7.getBean(IdGeneratorManager.class);
    IdGeneratorManager manager8 = context8.getBean(IdGeneratorManager.class);
    IdGeneratorManager manager9 = context9.getBean(IdGeneratorManager.class);
    CPProtocol protocol7 = context7.getBean(CPProtocol.class);
    CPProtocol protocol8 = context8.getBean(CPProtocol.class);
    CPProtocol protocol9 = context9.getBean(CPProtocol.class);
    final String configGroup = com.alibaba.nacos.config.server.constant.Constants.CONFIG_MODEL_RAFT_GROUP;
    long preId = -1L;
    long currentId = -1L;
    if (protocol7.isLeader(configGroup)) {
        preId = manager7.nextId(CONFIG_INFO_ID);
    }
    if (protocol8.isLeader(configGroup)) {
        preId = manager8.nextId(CONFIG_INFO_ID);
    }
    if (protocol9.isLeader(configGroup)) {
        preId = manager9.nextId(CONFIG_INFO_ID);
    }
    // transfer leader to ip:8807
    Map<String, String> transfer = new HashMap<>();
    transfer.put(JRaftConstants.TRANSFER_LEADER, InetUtils.getSelfIP() + ":9847");
    RestResult<String> result = protocol7.execute(transfer);
    System.out.println(result);
    Assert.assertTrue(result.ok());
    TimeUnit.SECONDS.sleep(2);
    Assert.assertTrue(protocol7.isLeader(configGroup));
    currentId = manager7.nextId(CONFIG_INFO_ID);
    Assert.assertNotEquals(preId, currentId);
    preId = currentId;
    // transfer leader to ip:8808
    transfer = new HashMap<>();
    transfer.put(JRaftConstants.TRANSFER_LEADER, InetUtils.getSelfIP() + ":9848");
    result = protocol8.execute(transfer);
    System.out.println(result);
    Assert.assertTrue(result.ok());
    TimeUnit.SECONDS.sleep(2);
    Assert.assertTrue(protocol8.isLeader(configGroup));
    currentId = manager8.nextId(CONFIG_INFO_ID);
    Assert.assertNotEquals(preId, currentId);
    preId = currentId;
    // transfer leader to ip:8809
    transfer = new HashMap<>();
    transfer.put(JRaftConstants.TRANSFER_LEADER, InetUtils.getSelfIP() + ":9849");
    result = protocol9.execute(transfer);
    System.out.println(result);
    Assert.assertTrue(result.ok());
    TimeUnit.SECONDS.sleep(2);
    Assert.assertTrue(protocol9.isLeader(configGroup));
    currentId = manager9.nextId(CONFIG_INFO_ID);
    Assert.assertNotEquals(preId, currentId);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) IdGeneratorManager(com.alibaba.nacos.core.distributed.id.IdGeneratorManager) CPProtocol(com.alibaba.nacos.consistency.cp.CPProtocol) HashMap(java.util.HashMap) Test(org.junit.Test) BaseClusterTest(com.alibaba.nacos.test.base.BaseClusterTest)

Aggregations

CPProtocol (com.alibaba.nacos.consistency.cp.CPProtocol)1 IdGeneratorManager (com.alibaba.nacos.core.distributed.id.IdGeneratorManager)1 BaseClusterTest (com.alibaba.nacos.test.base.BaseClusterTest)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1