Search in sources :

Example 1 with CPProtocol

use of com.alibaba.nacos.consistency.cp.CPProtocol in project nacos by alibaba.

the class CoreOpsControllerTest method testRaftOps.

@Test
public void testRaftOps() {
    Mockito.when(protocolManager.getCpProtocol()).thenAnswer(invocationOnMock -> {
        CPProtocol cpProtocol = Mockito.mock(CPProtocol.class);
        Mockito.when(cpProtocol.execute(Mockito.anyMap())).thenReturn(RestResultUtils.success("res"));
        return cpProtocol;
    });
    RestResult<String> result = coreOpsController.raftOps(new HashMap<>());
    Assert.assertEquals("res", result.getData());
}
Also used : CPProtocol(com.alibaba.nacos.consistency.cp.CPProtocol) Test(org.junit.Test)

Example 2 with CPProtocol

use of com.alibaba.nacos.consistency.cp.CPProtocol in project nacos by alibaba.

the class CoreOpsV2ControllerTest method testRaftOps.

@Test
public void testRaftOps() {
    Mockito.when(protocolManager.getCpProtocol()).thenAnswer(invocationOnMock -> {
        CPProtocol cpProtocol = Mockito.mock(CPProtocol.class);
        Mockito.when(cpProtocol.execute(Mockito.anyMap())).thenReturn(RestResultUtils.success("res"));
        return cpProtocol;
    });
    RestResult<String> result = coreOpsV2Controller.raftOps(new HashMap<>());
    Assert.assertEquals("res", result.getData());
}
Also used : CPProtocol(com.alibaba.nacos.consistency.cp.CPProtocol) Test(org.junit.Test)

Example 3 with CPProtocol

use of com.alibaba.nacos.consistency.cp.CPProtocol in project nacos by alibaba.

the class BaseClusterTest method run.

private static void run(final int index, final CountDownLatch latch, final Class<?> cls) {
    Runnable runnable = () -> {
        try {
            EnvUtil.setIsStandalone(false);
            final String path = Paths.get(System.getProperty("user.home"), "/nacos-" + index + "/").toString();
            DiskUtils.deleteDirectory(path);
            System.setProperty("nacos.home", path);
            System.out.println("nacos.home is : [" + path + "]");
            Map<String, Object> properties = new HashMap<>();
            properties.put("server.port", "884" + (7 + index));
            properties.put("nacos.home", path);
            properties.put("nacos.logs.path", Paths.get(System.getProperty("user.home"), "nacos-" + index, "/logs/").toString());
            properties.put("spring.jmx.enabled", false);
            properties.put("nacos.core.snowflake.worker-id", index + 1);
            MapPropertySource propertySource = new MapPropertySource("nacos_cluster_test", properties);
            ConfigurableEnvironment environment = new StandardServletEnvironment();
            environment.getPropertySources().addFirst(propertySource);
            SpringApplication cluster = new SpringApplicationBuilder(cls).web(WebApplicationType.SERVLET).environment(environment).properties(clusterInfo).properties("embeddedStorage=true").build();
            ConfigurableApplicationContext context = cluster.run();
            DistributedDatabaseOperateImpl operate = context.getBean(DistributedDatabaseOperateImpl.class);
            CPProtocol protocol = context.getBean(CPProtocol.class);
            protocol.protocolMetaData().subscribe(operate.group(), MetadataKey.LEADER_META_DATA, o -> {
                ProtocolMetaData.ValueItem item = (ProtocolMetaData.ValueItem) o;
                System.out.println("node : 884" + (7 + index) + "-> select leader is : " + item.getData());
                if (FINISHED[index].compareAndSet(false, true)) {
                    latch.countDown();
                }
            });
            new Thread(() -> {
                try {
                    Thread.sleep(5000L);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (FINISHED[index].compareAndSet(false, true)) {
                        latch.countDown();
                    }
                }
            });
            applications.put(String.valueOf(properties.get("server.port")), context);
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            latch.countDown();
        }
    };
    runnable.run();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) NotifyCenter(com.alibaba.nacos.common.notify.NotifyCenter) HttpClientBeanHolder(com.alibaba.nacos.common.http.HttpClientBeanHolder) Subscriber(com.alibaba.nacos.common.notify.listener.Subscriber) BeforeClass(org.junit.BeforeClass) URL(java.net.URL) NacosFactory(com.alibaba.nacos.api.NacosFactory) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) DistributedDatabaseOperateImpl(com.alibaba.nacos.config.server.service.repository.embedded.DistributedDatabaseOperateImpl) ProtocolMetaData(com.alibaba.nacos.consistency.ProtocolMetaData) SpringApplication(org.springframework.boot.SpringApplication) URLClassLoader(java.net.URLClassLoader) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Map(java.util.Map) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) Properties(java.util.Properties) PropertyKeyConst(com.alibaba.nacos.api.PropertyKeyConst) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) RaftDbErrorEvent(com.alibaba.nacos.config.server.model.event.RaftDbErrorEvent) EnvUtil(com.alibaba.nacos.sys.env.EnvUtil) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) InetUtils(com.alibaba.nacos.sys.utils.InetUtils) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) DiskUtils(com.alibaba.nacos.sys.utils.DiskUtils) Event(com.alibaba.nacos.common.notify.Event) Paths(java.nio.file.Paths) MapPropertySource(org.springframework.core.env.MapPropertySource) WebApplicationType(org.springframework.boot.WebApplicationType) MetadataKey(com.alibaba.nacos.consistency.cp.MetadataKey) CPProtocol(com.alibaba.nacos.consistency.cp.CPProtocol) NamingService(com.alibaba.nacos.api.naming.NamingService) DistributedDatabaseOperateImpl(com.alibaba.nacos.config.server.service.repository.embedded.DistributedDatabaseOperateImpl) CPProtocol(com.alibaba.nacos.consistency.cp.CPProtocol) SpringApplication(org.springframework.boot.SpringApplication) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MapPropertySource(org.springframework.core.env.MapPropertySource) ProtocolMetaData(com.alibaba.nacos.consistency.ProtocolMetaData) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with CPProtocol

use of com.alibaba.nacos.consistency.cp.CPProtocol in project nacos by alibaba.

the class EmbeddedDumpService method init.

@PostConstruct
@Override
protected void init() throws Throwable {
    if (EnvUtil.getStandaloneMode()) {
        dumpOperate(processor, dumpAllProcessor, dumpAllBetaProcessor, dumpAllTagProcessor);
        return;
    }
    CPProtocol protocol = protocolManager.getCpProtocol();
    AtomicReference<Throwable> errorReference = new AtomicReference<>(null);
    CountDownLatch waitDumpFinish = new CountDownLatch(1);
    // watch path => /nacos_config/leader/ has value ?
    Observer observer = new Observer() {

        @Override
        public void update(Observable o) {
            if (!(o instanceof ProtocolMetaData.ValueItem)) {
                return;
            }
            final Object arg = ((ProtocolMetaData.ValueItem) o).getData();
            GlobalExecutor.executeByCommon(() -> {
                // must make sure that there is a value here to perform the correct operation that follows
                if (Objects.isNull(arg)) {
                    return;
                }
                // Identify without a timeout mechanism
                EmbeddedStorageContextUtils.putExtendInfo(Constants.EXTEND_NEED_READ_UNTIL_HAVE_DATA, "true");
                // Remove your own listening to avoid task accumulation
                boolean canEnd = false;
                for (; ; ) {
                    try {
                        dumpOperate(processor, dumpAllProcessor, dumpAllBetaProcessor, dumpAllTagProcessor);
                        protocol.protocolMetaData().unSubscribe(Constants.CONFIG_MODEL_RAFT_GROUP, MetadataKey.LEADER_META_DATA, this);
                        canEnd = true;
                    } catch (Throwable ex) {
                        if (!shouldRetry(ex)) {
                            errorReference.set(ex);
                            canEnd = true;
                        }
                    }
                    if (canEnd) {
                        ThreadUtils.countDown(waitDumpFinish);
                        break;
                    }
                    ThreadUtils.sleep(500L);
                }
                EmbeddedStorageContextUtils.cleanAllContext();
            });
        }
    };
    protocol.protocolMetaData().subscribe(Constants.CONFIG_MODEL_RAFT_GROUP, MetadataKey.LEADER_META_DATA, observer);
    // We must wait for the dump task to complete the callback operation before
    // continuing with the initialization
    ThreadUtils.latchAwait(waitDumpFinish);
    // If an exception occurs during the execution of the dump task, the exception
    // needs to be thrown, triggering the node to start the failed process
    final Throwable ex = errorReference.get();
    if (Objects.nonNull(ex)) {
        throw ex;
    }
}
Also used : CPProtocol(com.alibaba.nacos.consistency.cp.CPProtocol) Observer(com.alibaba.nacos.common.utils.Observer) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Observable(com.alibaba.nacos.common.utils.Observable) PostConstruct(javax.annotation.PostConstruct)

Example 5 with CPProtocol

use of com.alibaba.nacos.consistency.cp.CPProtocol 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)5 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)2 NacosFactory (com.alibaba.nacos.api.NacosFactory)1 PropertyKeyConst (com.alibaba.nacos.api.PropertyKeyConst)1 NamingService (com.alibaba.nacos.api.naming.NamingService)1 HttpClientBeanHolder (com.alibaba.nacos.common.http.HttpClientBeanHolder)1 NacosRestTemplate (com.alibaba.nacos.common.http.client.NacosRestTemplate)1 Event (com.alibaba.nacos.common.notify.Event)1 NotifyCenter (com.alibaba.nacos.common.notify.NotifyCenter)1 Subscriber (com.alibaba.nacos.common.notify.listener.Subscriber)1 Observable (com.alibaba.nacos.common.utils.Observable)1 Observer (com.alibaba.nacos.common.utils.Observer)1 RaftDbErrorEvent (com.alibaba.nacos.config.server.model.event.RaftDbErrorEvent)1 DistributedDatabaseOperateImpl (com.alibaba.nacos.config.server.service.repository.embedded.DistributedDatabaseOperateImpl)1 ProtocolMetaData (com.alibaba.nacos.consistency.ProtocolMetaData)1 MetadataKey (com.alibaba.nacos.consistency.cp.MetadataKey)1 IdGeneratorManager (com.alibaba.nacos.core.distributed.id.IdGeneratorManager)1