From 34fd36b1159ae89d2b1e1a89666d43aa1c37bb5a Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Sun, 12 Jul 2020 13:52:29 +0800 Subject: [PATCH] [ISSUE#3179]fix import formate issue. --- .../controller/ConfigOpsController.java | 2 +- .../config/server/filter/CurcuitFilter.java | 8 +- .../server/model/event/ConfigDumpEvent.java | 2 +- .../service/dump/DumpConfigHandler.java | 7 +- .../embedded/DerbySnapshotOperation.java | 2 +- .../DistributedDatabaseOperateImpl.java | 4 +- .../EmbeddedStoragePersistServiceImpl.java | 2 +- .../test/config/ConfigDerbyRaft_DITCase.java | 709 +++++++++--------- .../nacos/test/core/BaseClusterTest.java | 354 +++++---- 9 files changed, 538 insertions(+), 552 deletions(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigOpsController.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigOpsController.java index 584a671626e..cb7728c0606 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigOpsController.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigOpsController.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.model.RestResultUtils; import com.alibaba.nacos.common.utils.Objects; +import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.model.event.DerbyImportEvent; import com.alibaba.nacos.config.server.service.datasource.DynamicDataSource; @@ -30,7 +31,6 @@ import com.alibaba.nacos.config.server.utils.PropertyUtil; import com.alibaba.nacos.core.auth.ActionTypes; import com.alibaba.nacos.core.auth.Secured; -import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.core.utils.ApplicationUtils; import com.alibaba.nacos.core.utils.WebUtils; import org.apache.commons.lang3.StringUtils; diff --git a/config/src/main/java/com/alibaba/nacos/config/server/filter/CurcuitFilter.java b/config/src/main/java/com/alibaba/nacos/config/server/filter/CurcuitFilter.java index dcba2fd82d1..4c71d4012bb 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/filter/CurcuitFilter.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/filter/CurcuitFilter.java @@ -17,6 +17,9 @@ package com.alibaba.nacos.config.server.filter; import com.alibaba.nacos.common.utils.ExceptionUtil; +import com.alibaba.nacos.common.notify.Event; +import com.alibaba.nacos.common.notify.NotifyCenter; +import com.alibaba.nacos.common.notify.listener.SmartSubscriber; import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.model.event.RaftDbErrorEvent; import com.alibaba.nacos.config.server.model.event.RaftDbErrorRecoverEvent; @@ -26,9 +29,6 @@ import com.alibaba.nacos.core.cluster.MemberMetaDataConstants; import com.alibaba.nacos.core.cluster.ServerMemberManager; import com.alibaba.nacos.core.code.ControllerMethodsCache; -import com.alibaba.nacos.common.notify.Event; -import com.alibaba.nacos.common.notify.NotifyCenter; -import com.alibaba.nacos.common.notify.listener.SmartSubscriber; import org.springframework.beans.factory.annotation.Autowired; import javax.annotation.PostConstruct; @@ -121,7 +121,7 @@ private void listenerSelfInCluster() { private void registerSubscribe() { NotifyCenter.registerSubscriber(new SmartSubscriber() { - + @Override public void onEvent(Event event) { // @JustForTest diff --git a/config/src/main/java/com/alibaba/nacos/config/server/model/event/ConfigDumpEvent.java b/config/src/main/java/com/alibaba/nacos/config/server/model/event/ConfigDumpEvent.java index 76cbbb7e545..ae046ef8a48 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/model/event/ConfigDumpEvent.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/model/event/ConfigDumpEvent.java @@ -222,7 +222,7 @@ public ConfigDumpEventBuilder lastModifiedTs(long lastModifiedTs) { this.lastModifiedTs = lastModifiedTs; return this; } - + /** * Build a configDumpEvent. * diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpConfigHandler.java b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpConfigHandler.java index c70f496caab..97e205f8264 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpConfigHandler.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpConfigHandler.java @@ -17,14 +17,14 @@ package com.alibaba.nacos.config.server.service.dump; import com.alibaba.nacos.common.utils.StringUtils; +import com.alibaba.nacos.common.notify.Event; +import com.alibaba.nacos.common.notify.listener.Subscriber; import com.alibaba.nacos.config.server.model.event.ConfigDumpEvent; import com.alibaba.nacos.config.server.service.AggrWhitelist; import com.alibaba.nacos.config.server.service.ClientIpWhiteList; import com.alibaba.nacos.config.server.service.ConfigCacheService; import com.alibaba.nacos.config.server.service.SwitchService; import com.alibaba.nacos.config.server.service.trace.ConfigTraceService; -import com.alibaba.nacos.common.notify.Event; -import com.alibaba.nacos.common.notify.listener.Subscriber; /** * Dump config subscriber. @@ -37,8 +37,7 @@ public class DumpConfigHandler extends Subscriber { * trigger config dump event. * * @param event {@link ConfigDumpEvent} - * @return {@code true} if the config dump task success , else - * {@code false} + * @return {@code true} if the config dump task success , else {@code false} */ public static boolean configDump(ConfigDumpEvent event) { final String dataId = event.getDataId(); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/DerbySnapshotOperation.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/DerbySnapshotOperation.java index c9f45411a8e..8fa6c7b101d 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/DerbySnapshotOperation.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/DerbySnapshotOperation.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.config.server.service.repository.embedded; +import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.config.server.model.event.DerbyLoadEvent; import com.alibaba.nacos.config.server.service.datasource.DataSourceService; import com.alibaba.nacos.config.server.service.datasource.DynamicDataSource; @@ -27,7 +28,6 @@ import com.alibaba.nacos.consistency.snapshot.Writer; import com.alibaba.nacos.core.distributed.raft.utils.RaftExecutor; import com.alibaba.nacos.core.utils.DiskUtils; -import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.core.utils.ApplicationUtils; import com.alibaba.nacos.core.utils.TimerContext; import com.alipay.sofa.jraft.util.CRC64; diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/DistributedDatabaseOperateImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/DistributedDatabaseOperateImpl.java index de23641a4b9..081a7e5dd00 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/DistributedDatabaseOperateImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/DistributedDatabaseOperateImpl.java @@ -21,6 +21,8 @@ import com.alibaba.nacos.common.JustForTest; import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.model.RestResultUtils; +import com.alibaba.nacos.common.notify.Event; +import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.common.notify.listener.Subscriber; import com.alibaba.nacos.common.utils.ExceptionUtil; import com.alibaba.nacos.common.utils.JacksonUtils; @@ -54,8 +56,6 @@ import com.alibaba.nacos.consistency.snapshot.SnapshotOperation; import com.alibaba.nacos.core.cluster.ServerMemberManager; import com.alibaba.nacos.core.distributed.ProtocolManager; -import com.alibaba.nacos.common.notify.Event; -import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.core.utils.ClassUtils; import com.alibaba.nacos.core.utils.DiskUtils; import com.alibaba.nacos.core.utils.GenericType; diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedStoragePersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedStoragePersistServiceImpl.java index b7a596b158a..e510740bdb7 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedStoragePersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedStoragePersistServiceImpl.java @@ -18,6 +18,7 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.common.utils.MD5Utils; +import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.config.server.configuration.ConditionOnEmbeddedStorage; import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.enums.FileTypeEnum; @@ -48,7 +49,6 @@ import com.alibaba.nacos.config.server.utils.LogUtil; import com.alibaba.nacos.config.server.utils.ParamUtils; import com.alibaba.nacos.core.distributed.id.IdGeneratorManager; -import com.alibaba.nacos.common.notify.NotifyCenter; import com.google.common.base.Joiner; import com.google.common.collect.Lists; import org.apache.commons.collections.CollectionUtils; diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigDerbyRaft_DITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigDerbyRaft_DITCase.java index 1ceaa0d5855..be489522c92 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigDerbyRaft_DITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigDerbyRaft_DITCase.java @@ -17,6 +17,10 @@ package com.alibaba.nacos.test.config; import com.alibaba.nacos.api.config.listener.AbstractListener; +import com.alibaba.nacos.common.notify.Event; +import com.alibaba.nacos.common.notify.NotifyCenter; +import com.alibaba.nacos.common.notify.listener.Subscriber; +import com.alibaba.nacos.common.utils.ThreadUtils; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; import com.alibaba.nacos.common.model.RestResult; @@ -27,13 +31,10 @@ import com.alibaba.nacos.consistency.cp.CPProtocol; import com.alibaba.nacos.core.distributed.id.IdGeneratorManager; import com.alibaba.nacos.core.distributed.raft.utils.JRaftConstants; -import com.alibaba.nacos.common.notify.Event; -import com.alibaba.nacos.common.notify.NotifyCenter; -import com.alibaba.nacos.common.notify.listener.Subscriber; import com.alibaba.nacos.core.utils.GenericType; import com.alibaba.nacos.core.utils.InetUtils; -import com.alibaba.nacos.common.utils.ThreadUtils; import com.alibaba.nacos.test.core.BaseClusterTest; + import org.junit.Assert; import org.junit.FixMethodOrder; import org.junit.Ignore; @@ -55,360 +56,348 @@ @SuppressWarnings("all") @Ignore @FixMethodOrder(value = MethodSorters.NAME_ASCENDING) -public class ConfigDerbyRaft_DITCase - extends BaseClusterTest { - - @Test - public void test_a_publish_config() throws Exception { - boolean result = iconfig7.publishConfig("raft_test", "cluster_test_1", - "this.is.raft_cluster=lessspring_7"); - Assert.assertTrue(result); - - ThreadUtils.sleep(5000); - - ConfigurableApplicationContext context7 = applications.get("8847"); - ConfigurableApplicationContext context8 = applications.get("8848"); - ConfigurableApplicationContext context9 = applications.get("8849"); - - PersistService operate7 = context7.getBean(EmbeddedStoragePersistServiceImpl.class); - PersistService operate8 = context8.getBean(EmbeddedStoragePersistServiceImpl.class); - PersistService operate9 = context9.getBean(EmbeddedStoragePersistServiceImpl.class); - - String s7 = operate7.findConfigInfo("raft_test", "cluster_test_1", "").getContent(); - String s8 = operate8.findConfigInfo("raft_test", "cluster_test_1", "").getContent(); - String s9 = operate9.findConfigInfo("raft_test", "cluster_test_1", "").getContent(); - - Assert.assertArrayEquals("The three nodes must have consistent data", - new String[] { s7, s8, s9 }, - new String[] { "this.is.raft_cluster=lessspring_7", - "this.is.raft_cluster=lessspring_7", - "this.is.raft_cluster=lessspring_7" }); - } - - @Test - public void test_b_publish_config() throws Exception { - ThreadUtils.sleep(5000); - - boolean result = iconfig8.publishConfig("raft_test", "cluster_test_2", - "this.is.raft_cluster=lessspring_8"); - Assert.assertTrue(result); - - ThreadUtils.sleep(5000); - - ConfigurableApplicationContext context7 = applications.get("8847"); - ConfigurableApplicationContext context8 = applications.get("8848"); - ConfigurableApplicationContext context9 = applications.get("8849"); - - PersistService operate7 = context7.getBean(EmbeddedStoragePersistServiceImpl.class); - PersistService operate8 = context8.getBean(EmbeddedStoragePersistServiceImpl.class); - PersistService operate9 = context9.getBean(EmbeddedStoragePersistServiceImpl.class); - - String s7 = operate7.findConfigInfo("raft_test", "cluster_test_2", "").getContent(); - String s8 = operate8.findConfigInfo("raft_test", "cluster_test_2", "").getContent(); - String s9 = operate9.findConfigInfo("raft_test", "cluster_test_2", "").getContent(); - - Assert.assertArrayEquals("The three nodes must have consistent data", - new String[] { s7, s8, s9 }, - new String[] { "this.is.raft_cluster=lessspring_8", - "this.is.raft_cluster=lessspring_8", - "this.is.raft_cluster=lessspring_8" }); - } - - @Test - public void test_c_publish_config() throws Exception { - ThreadUtils.sleep(5000); - boolean result = iconfig9.publishConfig("raft_test", "cluster_test_2", - "this.is.raft_cluster=lessspring_9"); - Assert.assertTrue(result); - - ThreadUtils.sleep(5000); - - ConfigurableApplicationContext context7 = applications.get("8847"); - ConfigurableApplicationContext context8 = applications.get("8848"); - ConfigurableApplicationContext context9 = applications.get("8849"); - - PersistService operate7 = context7.getBean(EmbeddedStoragePersistServiceImpl.class); - PersistService operate8 = context8.getBean(EmbeddedStoragePersistServiceImpl.class); - PersistService operate9 = context9.getBean(EmbeddedStoragePersistServiceImpl.class); - - String s7 = operate7.findConfigInfo("raft_test", "cluster_test_2", "").getContent(); - String s8 = operate8.findConfigInfo("raft_test", "cluster_test_2", "").getContent(); - String s9 = operate9.findConfigInfo("raft_test", "cluster_test_2", "").getContent(); - - Assert.assertArrayEquals("The three nodes must have consistent data", - new String[] { s7, s8, s9 }, - new String[] { "this.is.raft_cluster=lessspring_9", - "this.is.raft_cluster=lessspring_9", - "this.is.raft_cluster=lessspring_9" }); - } - - @Test - public void test_d_modify_config() throws Exception { - boolean result = iconfig7.publishConfig("raft_test", "cluster_test_1", - "this.is.raft_cluster=lessspring_7_it_is_for_modify"); - Assert.assertTrue(result); - - ThreadUtils.sleep(5000); - - ConfigurableApplicationContext context7 = applications.get("8847"); - ConfigurableApplicationContext context8 = applications.get("8848"); - ConfigurableApplicationContext context9 = applications.get("8849"); - - PersistService operate7 = context7.getBean(EmbeddedStoragePersistServiceImpl.class); - PersistService operate8 = context8.getBean(EmbeddedStoragePersistServiceImpl.class); - PersistService operate9 = context9.getBean(EmbeddedStoragePersistServiceImpl.class); - - String s7 = operate7.findConfigInfo("raft_test", "cluster_test_1", "").getContent(); - String s8 = operate8.findConfigInfo("raft_test", "cluster_test_1", "").getContent(); - String s9 = operate9.findConfigInfo("raft_test", "cluster_test_1", "").getContent(); - - Assert.assertArrayEquals("The three nodes must have consistent data", - new String[] { s7, s8, s9 }, - new String[] { "this.is.raft_cluster=lessspring_7_it_is_for_modify", - "this.is.raft_cluster=lessspring_7_it_is_for_modify", - "this.is.raft_cluster=lessspring_7_it_is_for_modify" }); - } - - @Test - public void test_l_client_operation() throws Exception { - final String dataId = "test_l_client_operation"; - final String groupId = "test_l_client_operation"; - String content = "test_l_client_operation" + System.currentTimeMillis(); - - // publish by 8847 - boolean result = iconfig7.publishConfig(dataId, groupId, content); - Assert.assertTrue(result); - ThreadUtils.sleep(5000); - - String v1_7 = iconfig7.getConfig(dataId, groupId, 5000L); - String v1_8 = iconfig8.getConfig(dataId, groupId, 5000L); - String v1_9 = iconfig9.getConfig(dataId, groupId, 5000L); - - Assert.assertEquals(content, v1_7); - Assert.assertEquals(content, v1_8); - Assert.assertEquals(content, v1_9); - - // publish by 8848 - content = "test_l_client_operation" + System.currentTimeMillis(); - result = iconfig8.publishConfig(dataId, groupId, content); - Assert.assertTrue(result); - ThreadUtils.sleep(5000); - - String v2_7 = iconfig7.getConfig(dataId, groupId, 5000L); - String v2_8 = iconfig8.getConfig(dataId, groupId, 5000L); - String v2_9 = iconfig9.getConfig(dataId, groupId, 5000L); - - Assert.assertEquals(content, v2_7); - Assert.assertEquals(content, v2_8); - Assert.assertEquals(content, v2_9); - - // publish by 8849 - content = "test_l_client_operation" + System.currentTimeMillis(); - result = iconfig9.publishConfig(dataId, groupId, content); - Assert.assertTrue(result); - ThreadUtils.sleep(5000); - - String v3_7 = iconfig7.getConfig(dataId, groupId, 5000L); - String v3_8 = iconfig8.getConfig(dataId, groupId, 5000L); - String v3_9 = iconfig9.getConfig(dataId, groupId, 5000L); - - Assert.assertEquals(content, v3_7); - Assert.assertEquals(content, v3_8); - Assert.assertEquals(content, v3_9); - - // delete by 8849 - result = iconfig9.removeConfig(dataId, groupId); - Assert.assertTrue(result); - ThreadUtils.sleep(5000); - - String v4_7 = iconfig7.getConfig(dataId, groupId, 5000L); - String v4_8 = iconfig8.getConfig(dataId, groupId, 5000L); - String v4_9 = iconfig9.getConfig(dataId, groupId, 5000L); - - Assert.assertNull(v4_7); - Assert.assertNull(v4_8); - Assert.assertNull(v4_9); - } - - @Test - public void test_k_config_listener() throws Exception { - String dataId = "test_h_config_listener"; - String group = "test_h_config_listener"; - String content = "test_h_config_listener"; - CountDownLatch[] latch = new CountDownLatch[]{new CountDownLatch(1), new CountDownLatch(0)}; - AtomicReference r = new AtomicReference<>(); - AtomicInteger i = new AtomicInteger(0); - iconfig7.addListener(dataId, group, new AbstractListener() { - @Override - public void receiveConfigInfo(String configInfo) { - System.out.println(configInfo); - r.set(configInfo); - latch[i.getAndIncrement()].countDown(); - } - }); - - iconfig7.publishConfig(dataId, group, content); - - ThreadUtils.sleep(10_000L); - latch[0].await(10_000L, TimeUnit.MILLISECONDS); - Assert.assertEquals(content, r.get()); - Assert.assertEquals(content, iconfig7.getConfig(dataId, group, 2_000L)); - - content = content + System.currentTimeMillis(); - iconfig7.publishConfig(dataId, group, content); - - ThreadUtils.sleep(10_000L); - latch[1].await(10_000L, TimeUnit.MILLISECONDS); - Assert.assertEquals(content, r.get()); - Assert.assertEquals(content, iconfig7.getConfig(dataId, group, 2_000L)); - } - - @Test - public void test_e_derby_ops() throws Exception { - String url = "http://127.0.0.1:8848/nacos/v1/cs/ops/derby"; - Query query = Query.newInstance() - .addParam("sql", "select * from users"); - RestResult>> result = httpClient.get(url, Header.EMPTY, query, new GenericType>>>(){}.getType()); - System.out.println(result.getData()); - Assert.assertTrue(result.ok()); - List> list = result.getData(); - Assert.assertEquals(1, list.size()); - Assert.assertEquals("nacos", list.get(0).get("USERNAME")); - } - - @Test - public void test_g_derby_ops_no_select() throws Exception { - String url = "http://127.0.0.1:8848/nacos/v1/cs/ops/derby"; - Query query = Query.newInstance() - .addParam("sql", "update users set username='nacos'"); - RestResult result = httpClient.get(url, Header.EMPTY, query, new GenericType>(){}.getType()); - System.out.println(result); - Assert.assertFalse(result.ok()); - Assert.assertEquals("Only query statements are allowed to be executed", result.getMessage()); - } - - @Test - public void test_h_derby_has_error() throws Exception { - - ThreadUtils.sleep(5000); - - boolean result = iconfig7.publishConfig("raft_test_raft_error", "cluster_test_1", - "this.is.raft_cluster=lessspring_7"); - Assert.assertTrue(result); - - NotifyCenter.registerToPublisher(RaftDbErrorRecoverEvent.class, 8); - - CountDownLatch latch1 = new CountDownLatch(1); - NotifyCenter.registerSubscriber(new Subscriber() { - @Override - public void onEvent(RaftDbErrorEvent event) { - latch1.countDown(); - } - - @Override - public Class subscribeType() { - return RaftDbErrorEvent.class; - } - }); - NotifyCenter.publishEvent(new RaftDbErrorEvent()); - latch1.await(10_000L, TimeUnit.MILLISECONDS); - - result = iconfig7.publishConfig("raft_test_raft_error", "cluster_test_1", - "this.is.raft_cluster=lessspring_7"); - Assert.assertFalse(result); - - CountDownLatch latch2 = new CountDownLatch(1); - NotifyCenter.registerSubscriber(new Subscriber() { - - @Override - public void onEvent(RaftDbErrorRecoverEvent event) { - latch2.countDown(); - } - - @Override - public Class subscribeType() { - return RaftDbErrorRecoverEvent.class; - } - }); - NotifyCenter.publishEvent(new RaftDbErrorRecoverEvent()); - latch2.await(10_000L, TimeUnit.MILLISECONDS); - - result = iconfig7.publishConfig("raft_test_raft_error", "cluster_test_1", - "this.is.raft_cluster=lessspring_7"); - Assert.assertTrue(result); - } - - @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 transfer = new HashMap<>(); - transfer.put(JRaftConstants.TRANSFER_LEADER, InetUtils.getSelfIp() + ":9847"); - RestResult 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); - - } - +public class ConfigDerbyRaft_DITCase extends BaseClusterTest { + + @Test + public void test_a_publish_config() throws Exception { + boolean result = iconfig7.publishConfig("raft_test", "cluster_test_1", "this.is.raft_cluster=lessspring_7"); + Assert.assertTrue(result); + + ThreadUtils.sleep(5000); + + ConfigurableApplicationContext context7 = applications.get("8847"); + ConfigurableApplicationContext context8 = applications.get("8848"); + ConfigurableApplicationContext context9 = applications.get("8849"); + + PersistService operate7 = context7.getBean(EmbeddedStoragePersistServiceImpl.class); + PersistService operate8 = context8.getBean(EmbeddedStoragePersistServiceImpl.class); + PersistService operate9 = context9.getBean(EmbeddedStoragePersistServiceImpl.class); + + String s7 = operate7.findConfigInfo("raft_test", "cluster_test_1", "").getContent(); + String s8 = operate8.findConfigInfo("raft_test", "cluster_test_1", "").getContent(); + String s9 = operate9.findConfigInfo("raft_test", "cluster_test_1", "").getContent(); + + Assert.assertArrayEquals("The three nodes must have consistent data", new String[] {s7, s8, s9}, + new String[] {"this.is.raft_cluster=lessspring_7", "this.is.raft_cluster=lessspring_7", + "this.is.raft_cluster=lessspring_7"}); + } + + @Test + public void test_b_publish_config() throws Exception { + ThreadUtils.sleep(5000); + + boolean result = iconfig8.publishConfig("raft_test", "cluster_test_2", "this.is.raft_cluster=lessspring_8"); + Assert.assertTrue(result); + + ThreadUtils.sleep(5000); + + ConfigurableApplicationContext context7 = applications.get("8847"); + ConfigurableApplicationContext context8 = applications.get("8848"); + ConfigurableApplicationContext context9 = applications.get("8849"); + + PersistService operate7 = context7.getBean(EmbeddedStoragePersistServiceImpl.class); + PersistService operate8 = context8.getBean(EmbeddedStoragePersistServiceImpl.class); + PersistService operate9 = context9.getBean(EmbeddedStoragePersistServiceImpl.class); + + String s7 = operate7.findConfigInfo("raft_test", "cluster_test_2", "").getContent(); + String s8 = operate8.findConfigInfo("raft_test", "cluster_test_2", "").getContent(); + String s9 = operate9.findConfigInfo("raft_test", "cluster_test_2", "").getContent(); + + Assert.assertArrayEquals("The three nodes must have consistent data", new String[] {s7, s8, s9}, + new String[] {"this.is.raft_cluster=lessspring_8", "this.is.raft_cluster=lessspring_8", + "this.is.raft_cluster=lessspring_8"}); + } + + @Test + public void test_c_publish_config() throws Exception { + ThreadUtils.sleep(5000); + boolean result = iconfig9.publishConfig("raft_test", "cluster_test_2", "this.is.raft_cluster=lessspring_9"); + Assert.assertTrue(result); + + ThreadUtils.sleep(5000); + + ConfigurableApplicationContext context7 = applications.get("8847"); + ConfigurableApplicationContext context8 = applications.get("8848"); + ConfigurableApplicationContext context9 = applications.get("8849"); + + PersistService operate7 = context7.getBean(EmbeddedStoragePersistServiceImpl.class); + PersistService operate8 = context8.getBean(EmbeddedStoragePersistServiceImpl.class); + PersistService operate9 = context9.getBean(EmbeddedStoragePersistServiceImpl.class); + + String s7 = operate7.findConfigInfo("raft_test", "cluster_test_2", "").getContent(); + String s8 = operate8.findConfigInfo("raft_test", "cluster_test_2", "").getContent(); + String s9 = operate9.findConfigInfo("raft_test", "cluster_test_2", "").getContent(); + + Assert.assertArrayEquals("The three nodes must have consistent data", new String[] {s7, s8, s9}, + new String[] {"this.is.raft_cluster=lessspring_9", "this.is.raft_cluster=lessspring_9", + "this.is.raft_cluster=lessspring_9"}); + } + + @Test + public void test_d_modify_config() throws Exception { + boolean result = iconfig7 + .publishConfig("raft_test", "cluster_test_1", "this.is.raft_cluster=lessspring_7_it_is_for_modify"); + Assert.assertTrue(result); + + ThreadUtils.sleep(5000); + + ConfigurableApplicationContext context7 = applications.get("8847"); + ConfigurableApplicationContext context8 = applications.get("8848"); + ConfigurableApplicationContext context9 = applications.get("8849"); + + PersistService operate7 = context7.getBean(EmbeddedStoragePersistServiceImpl.class); + PersistService operate8 = context8.getBean(EmbeddedStoragePersistServiceImpl.class); + PersistService operate9 = context9.getBean(EmbeddedStoragePersistServiceImpl.class); + + String s7 = operate7.findConfigInfo("raft_test", "cluster_test_1", "").getContent(); + String s8 = operate8.findConfigInfo("raft_test", "cluster_test_1", "").getContent(); + String s9 = operate9.findConfigInfo("raft_test", "cluster_test_1", "").getContent(); + + Assert.assertArrayEquals("The three nodes must have consistent data", new String[] {s7, s8, s9}, + new String[] {"this.is.raft_cluster=lessspring_7_it_is_for_modify", + "this.is.raft_cluster=lessspring_7_it_is_for_modify", + "this.is.raft_cluster=lessspring_7_it_is_for_modify"}); + } + + @Test + public void test_l_client_operation() throws Exception { + final String dataId = "test_l_client_operation"; + final String groupId = "test_l_client_operation"; + String content = "test_l_client_operation" + System.currentTimeMillis(); + + // publish by 8847 + boolean result = iconfig7.publishConfig(dataId, groupId, content); + Assert.assertTrue(result); + ThreadUtils.sleep(5000); + + String v1_7 = iconfig7.getConfig(dataId, groupId, 5000L); + String v1_8 = iconfig8.getConfig(dataId, groupId, 5000L); + String v1_9 = iconfig9.getConfig(dataId, groupId, 5000L); + + Assert.assertEquals(content, v1_7); + Assert.assertEquals(content, v1_8); + Assert.assertEquals(content, v1_9); + + // publish by 8848 + content = "test_l_client_operation" + System.currentTimeMillis(); + result = iconfig8.publishConfig(dataId, groupId, content); + Assert.assertTrue(result); + ThreadUtils.sleep(5000); + + String v2_7 = iconfig7.getConfig(dataId, groupId, 5000L); + String v2_8 = iconfig8.getConfig(dataId, groupId, 5000L); + String v2_9 = iconfig9.getConfig(dataId, groupId, 5000L); + + Assert.assertEquals(content, v2_7); + Assert.assertEquals(content, v2_8); + Assert.assertEquals(content, v2_9); + + // publish by 8849 + content = "test_l_client_operation" + System.currentTimeMillis(); + result = iconfig9.publishConfig(dataId, groupId, content); + Assert.assertTrue(result); + ThreadUtils.sleep(5000); + + String v3_7 = iconfig7.getConfig(dataId, groupId, 5000L); + String v3_8 = iconfig8.getConfig(dataId, groupId, 5000L); + String v3_9 = iconfig9.getConfig(dataId, groupId, 5000L); + + Assert.assertEquals(content, v3_7); + Assert.assertEquals(content, v3_8); + Assert.assertEquals(content, v3_9); + + // delete by 8849 + result = iconfig9.removeConfig(dataId, groupId); + Assert.assertTrue(result); + ThreadUtils.sleep(5000); + + String v4_7 = iconfig7.getConfig(dataId, groupId, 5000L); + String v4_8 = iconfig8.getConfig(dataId, groupId, 5000L); + String v4_9 = iconfig9.getConfig(dataId, groupId, 5000L); + + Assert.assertNull(v4_7); + Assert.assertNull(v4_8); + Assert.assertNull(v4_9); + } + + @Test + public void test_k_config_listener() throws Exception { + String dataId = "test_h_config_listener"; + String group = "test_h_config_listener"; + String content = "test_h_config_listener"; + CountDownLatch[] latch = new CountDownLatch[] {new CountDownLatch(1), new CountDownLatch(0)}; + AtomicReference r = new AtomicReference<>(); + AtomicInteger i = new AtomicInteger(0); + iconfig7.addListener(dataId, group, new AbstractListener() { + @Override + public void receiveConfigInfo(String configInfo) { + System.out.println(configInfo); + r.set(configInfo); + latch[i.getAndIncrement()].countDown(); + } + }); + + iconfig7.publishConfig(dataId, group, content); + + ThreadUtils.sleep(10_000L); + latch[0].await(10_000L, TimeUnit.MILLISECONDS); + Assert.assertEquals(content, r.get()); + Assert.assertEquals(content, iconfig7.getConfig(dataId, group, 2_000L)); + + content = content + System.currentTimeMillis(); + iconfig7.publishConfig(dataId, group, content); + + ThreadUtils.sleep(10_000L); + latch[1].await(10_000L, TimeUnit.MILLISECONDS); + Assert.assertEquals(content, r.get()); + Assert.assertEquals(content, iconfig7.getConfig(dataId, group, 2_000L)); + } + + @Test + public void test_e_derby_ops() throws Exception { + String url = "http://127.0.0.1:8848/nacos/v1/cs/ops/derby"; + Query query = Query.newInstance().addParam("sql", "select * from users"); + RestResult>> result = httpClient + .get(url, Header.EMPTY, query, new GenericType>>>() { + }.getType()); + System.out.println(result.getData()); + Assert.assertTrue(result.ok()); + List> list = result.getData(); + Assert.assertEquals(1, list.size()); + Assert.assertEquals("nacos", list.get(0).get("USERNAME")); + } + + @Test + public void test_g_derby_ops_no_select() throws Exception { + String url = "http://127.0.0.1:8848/nacos/v1/cs/ops/derby"; + Query query = Query.newInstance().addParam("sql", "update users set username='nacos'"); + RestResult result = httpClient.get(url, Header.EMPTY, query, new GenericType>() { + }.getType()); + System.out.println(result); + Assert.assertFalse(result.ok()); + Assert.assertEquals("Only query statements are allowed to be executed", result.getMessage()); + } + + @Test + public void test_h_derby_has_error() throws Exception { + + ThreadUtils.sleep(5000); + + boolean result = iconfig7 + .publishConfig("raft_test_raft_error", "cluster_test_1", "this.is.raft_cluster=lessspring_7"); + Assert.assertTrue(result); + + NotifyCenter.registerToPublisher(RaftDbErrorRecoverEvent.class, 8); + + CountDownLatch latch1 = new CountDownLatch(1); + NotifyCenter.registerSubscriber(new Subscriber() { + @Override + public void onEvent(RaftDbErrorEvent event) { + latch1.countDown(); + } + + @Override + public Class subscribeType() { + return RaftDbErrorEvent.class; + } + }); + NotifyCenter.publishEvent(new RaftDbErrorEvent()); + latch1.await(10_000L, TimeUnit.MILLISECONDS); + + result = iconfig7.publishConfig("raft_test_raft_error", "cluster_test_1", "this.is.raft_cluster=lessspring_7"); + Assert.assertFalse(result); + + CountDownLatch latch2 = new CountDownLatch(1); + NotifyCenter.registerSubscriber(new Subscriber() { + + @Override + public void onEvent(RaftDbErrorRecoverEvent event) { + latch2.countDown(); + } + + @Override + public Class subscribeType() { + return RaftDbErrorRecoverEvent.class; + } + }); + NotifyCenter.publishEvent(new RaftDbErrorRecoverEvent()); + latch2.await(10_000L, TimeUnit.MILLISECONDS); + + result = iconfig7.publishConfig("raft_test_raft_error", "cluster_test_1", "this.is.raft_cluster=lessspring_7"); + Assert.assertTrue(result); + } + + @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 transfer = new HashMap<>(); + transfer.put(JRaftConstants.TRANSFER_LEADER, InetUtils.getSelfIp() + ":9847"); + RestResult 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); + + } + } diff --git a/test/src/test/java/com/alibaba/nacos/test/core/BaseClusterTest.java b/test/src/test/java/com/alibaba/nacos/test/core/BaseClusterTest.java index 0aaa2a29aff..c6d308462d3 100644 --- a/test/src/test/java/com/alibaba/nacos/test/core/BaseClusterTest.java +++ b/test/src/test/java/com/alibaba/nacos/test/core/BaseClusterTest.java @@ -22,17 +22,18 @@ import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.common.http.HttpClientManager; import com.alibaba.nacos.common.http.NSyncHttpClient; +import com.alibaba.nacos.common.notify.Event; +import com.alibaba.nacos.common.notify.NotifyCenter; +import com.alibaba.nacos.common.notify.listener.Subscriber; import com.alibaba.nacos.core.utils.DiskUtils; import com.alibaba.nacos.config.server.model.event.RaftDbErrorEvent; import com.alibaba.nacos.config.server.service.repository.embedded.DistributedDatabaseOperateImpl; import com.alibaba.nacos.consistency.cp.CPProtocol; import com.alibaba.nacos.consistency.cp.MetadataKey; -import com.alibaba.nacos.common.notify.Event; -import com.alibaba.nacos.common.notify.NotifyCenter; -import com.alibaba.nacos.common.notify.listener.Subscriber; import com.alibaba.nacos.core.utils.ApplicationUtils; import com.alibaba.nacos.core.utils.InetUtils; import com.alibaba.nacos.test.base.HttpClient4Test; + import org.junit.AfterClass; import org.junit.BeforeClass; import org.springframework.boot.SpringApplication; @@ -58,186 +59,183 @@ * @author liaochuntao */ public class BaseClusterTest extends HttpClient4Test { - - protected static final String CONFIG_INFO_ID = "config-info-id"; - - protected static ConfigService iconfig7; - protected static ConfigService iconfig8; - protected static ConfigService iconfig9; - - protected static NamingService inaming7; - protected static NamingService inaming8; - protected static NamingService inaming9; - - protected static final NSyncHttpClient httpClient = HttpClientManager.getSyncHttpClient(); - - protected static final AtomicBoolean[] finished = new AtomicBoolean[]{new AtomicBoolean(false), new AtomicBoolean(false), new AtomicBoolean(false)}; - - protected static Map applications = new HashMap<>(); - - protected static String clusterInfo; - - static { - System.getProperties().setProperty("nacos.core.auth.enabled", "false"); - System.getProperties().setProperty("embeddedStorage", "true"); - String ip = InetUtils.getSelfIp(); - clusterInfo = "nacos.member.list=" + ip + ":8847," + ip - + ":8848," + ip + ":8849"; - - NotifyCenter.registerSubscriber(new Subscriber() { - @Override - public void onEvent(RaftDbErrorEvent event) { - System.out.print(event.getEx()); - } - - @Override - public Class subscribeType() { - return RaftDbErrorEvent.class; - } - }); - } - - @BeforeClass - public static void before() throws Exception { - - CountDownLatch latch = new CountDownLatch(3); - - Runnable runnable = () -> { - for (int i = 0; i < 3; i++) { - try { - URL runnerUrl = new File("../console/target/classes").toURI().toURL(); - URL[] urls = new URL[] { runnerUrl }; - URLClassLoader cl = new URLClassLoader(urls); - Class runnerClass = cl.loadClass("com.alibaba.nacos.Nacos"); - run(i, latch, runnerClass); - } catch (Exception e) { - latch.countDown(); - } - } - }; - - new Thread(runnable).start(); - - latch.await(); - - System.out.println("The cluster node initialization is complete"); - - Properties setting7 = new Properties(); - String serverIp7 = "127.0.0.1:8847"; - setting7.put(PropertyKeyConst.SERVER_ADDR, serverIp7); - setting7.put(PropertyKeyConst.USERNAME, "nacos"); - setting7.put(PropertyKeyConst.PASSWORD, "nacos"); - iconfig7 = NacosFactory.createConfigService(setting7); - inaming7 = NacosFactory.createNamingService(setting7); - - Properties setting8 = new Properties(); - String serverIp8 = "127.0.0.1:8848"; - setting8.put(PropertyKeyConst.SERVER_ADDR, serverIp8); - setting8.put(PropertyKeyConst.USERNAME, "nacos"); - setting8.put(PropertyKeyConst.PASSWORD, "nacos"); - iconfig8 = NacosFactory.createConfigService(setting8); - inaming8 = NacosFactory.createNamingService(setting7); - - Properties setting9 = new Properties(); - String serverIp9 = "127.0.0.1:8849"; - setting9.put(PropertyKeyConst.SERVER_ADDR, serverIp9); - setting9.put(PropertyKeyConst.USERNAME, "nacos"); - setting9.put(PropertyKeyConst.PASSWORD, "nacos"); - iconfig9 = NacosFactory.createConfigService(setting9); - inaming9 = NacosFactory.createNamingService(setting7); - - TimeUnit.SECONDS.sleep(20L); - } - - @AfterClass - public static void after() throws Exception { - CountDownLatch latch = new CountDownLatch(applications.size()); - for (ConfigurableApplicationContext context : applications.values()) { - new Thread(() -> { - try { - System.out.println("start close : " + context); - context.close(); + + protected static final String CONFIG_INFO_ID = "config-info-id"; + + protected static ConfigService iconfig7; + + protected static ConfigService iconfig8; + + protected static ConfigService iconfig9; + + protected static NamingService inaming7; + + protected static NamingService inaming8; + + protected static NamingService inaming9; + + protected static final NSyncHttpClient httpClient = HttpClientManager.getSyncHttpClient(); + + protected static final AtomicBoolean[] finished = new AtomicBoolean[] {new AtomicBoolean(false), + new AtomicBoolean(false), new AtomicBoolean(false)}; + + protected static Map applications = new HashMap<>(); + + protected static String clusterInfo; + + static { + System.getProperties().setProperty("nacos.core.auth.enabled", "false"); + System.getProperties().setProperty("embeddedStorage", "true"); + String ip = InetUtils.getSelfIp(); + clusterInfo = "nacos.member.list=" + ip + ":8847," + ip + ":8848," + ip + ":8849"; + + NotifyCenter.registerSubscriber(new Subscriber() { + @Override + public void onEvent(RaftDbErrorEvent event) { + System.out.print(event.getEx()); + } + + @Override + public Class subscribeType() { + return RaftDbErrorEvent.class; + } + }); + } + + @BeforeClass + public static void before() throws Exception { + + CountDownLatch latch = new CountDownLatch(3); + + Runnable runnable = () -> { + for (int i = 0; i < 3; i++) { + try { + URL runnerUrl = new File("../console/target/classes").toURI().toURL(); + URL[] urls = new URL[] {runnerUrl}; + URLClassLoader cl = new URLClassLoader(urls); + Class runnerClass = cl.loadClass("com.alibaba.nacos.Nacos"); + run(i, latch, runnerClass); + } catch (Exception e) { + latch.countDown(); + } + } + }; + + new Thread(runnable).start(); + + latch.await(); + + System.out.println("The cluster node initialization is complete"); + + Properties setting7 = new Properties(); + String serverIp7 = "127.0.0.1:8847"; + setting7.put(PropertyKeyConst.SERVER_ADDR, serverIp7); + setting7.put(PropertyKeyConst.USERNAME, "nacos"); + setting7.put(PropertyKeyConst.PASSWORD, "nacos"); + iconfig7 = NacosFactory.createConfigService(setting7); + inaming7 = NacosFactory.createNamingService(setting7); + + Properties setting8 = new Properties(); + String serverIp8 = "127.0.0.1:8848"; + setting8.put(PropertyKeyConst.SERVER_ADDR, serverIp8); + setting8.put(PropertyKeyConst.USERNAME, "nacos"); + setting8.put(PropertyKeyConst.PASSWORD, "nacos"); + iconfig8 = NacosFactory.createConfigService(setting8); + inaming8 = NacosFactory.createNamingService(setting7); + + Properties setting9 = new Properties(); + String serverIp9 = "127.0.0.1:8849"; + setting9.put(PropertyKeyConst.SERVER_ADDR, serverIp9); + setting9.put(PropertyKeyConst.USERNAME, "nacos"); + setting9.put(PropertyKeyConst.PASSWORD, "nacos"); + iconfig9 = NacosFactory.createConfigService(setting9); + inaming9 = NacosFactory.createNamingService(setting7); + + TimeUnit.SECONDS.sleep(20L); + } + + @AfterClass + public static void after() throws Exception { + CountDownLatch latch = new CountDownLatch(applications.size()); + for (ConfigurableApplicationContext context : applications.values()) { + new Thread(() -> { + try { + System.out.println("start close : " + context); + context.close(); iconfig7.shutDown(); iconfig8.shutDown(); iconfig9.shutDown(); - + inaming7.shutDown(); inaming8.shutDown(); inaming9.shutDown(); } catch (Exception ignore) { - } finally { - System.out.println("finished close : " + context); - latch.countDown(); - } - }).start(); - } - latch.await(); - } - - private static void run(final int index, final CountDownLatch latch, final Class cls) { - Runnable runnable = () -> { - try { - ApplicationUtils.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 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, arg) -> { - System.out.println("node : 884" + (7 + index) + "-> select leader is : " + arg); - 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(); - } - + } finally { + System.out.println("finished close : " + context); + latch.countDown(); + } + }).start(); + } + latch.await(); + } + + private static void run(final int index, final CountDownLatch latch, final Class cls) { + Runnable runnable = () -> { + try { + ApplicationUtils.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 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, arg) -> { + System.out.println("node : 884" + (7 + index) + "-> select leader is : " + arg); + 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(); + } + }