From 38e84cfb62a055f7310dbd3fa3d61ec2a2168f39 Mon Sep 17 00:00:00 2001 From: nkorange Date: Thu, 17 Jan 2019 19:50:57 +0800 Subject: [PATCH] #502 Fix PMD --- .../persistent/simpleraft/RaftCore.java | 2 +- .../persistent/simpleraft/RaftProxy.java | 2 +- .../alibaba/nacos/naming/core/ServiceManager.java | 11 ++++++++--- .../nacos/naming/healthcheck/HealthCheckTask.java | 15 --------------- .../test/naming/RegisterInstance_ITCase.java | 8 +++----- 5 files changed, 13 insertions(+), 25 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/simpleraft/RaftCore.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/simpleraft/RaftCore.java index 2fd21af4b..fd727e2ab 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/simpleraft/RaftCore.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/simpleraft/RaftCore.java @@ -173,7 +173,6 @@ public class RaftCore { parameters.put("key", key); raftProxy.proxyPostLarge(getLeader().ip, API_PUB, params.toJSONString(), parameters); - return; } @@ -818,6 +817,7 @@ public class RaftCore { if (listenerList == null) { listenerList = new CopyOnWriteArrayList<>(); + listeners.put(key, listenerList); } Loggers.RAFT.info("add listener: {}", key); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/simpleraft/RaftProxy.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/simpleraft/RaftProxy.java index 33d489696..9ba1d4290 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/simpleraft/RaftProxy.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/simpleraft/RaftProxy.java @@ -42,7 +42,7 @@ public class RaftProxy { } } - public static void proxyPostLarge(String server, String api, String content, Map headers) throws Exception { + public void proxyPostLarge(String server, String api, String content, Map headers) throws Exception { // do proxy if (!server.contains(UtilsAndCommons.CLUSTER_CONF_IP_SPLITER)) { server = server + UtilsAndCommons.CLUSTER_CONF_IP_SPLITER + RunningConfig.getServerPort(); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java index 9ff021031..1c206c93e 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java @@ -174,7 +174,6 @@ public class ServiceManager implements DataListener { consistencyService.listen(UtilsAndCommons.getDomStoreKey(dom), dom); Loggers.SRV_LOG.info("[NEW-DOM-RAFT] {}", dom.toJSON()); } - wakeUp(UtilsAndCommons.assembleFullServiceName(dom.getNamespaceId(), dom.getName())); } catch (Throwable e) { @@ -376,7 +375,6 @@ public class ServiceManager implements DataListener { public void registerInstance(String namespaceId, String serviceName, IpAddress instance) throws Exception { VirtualClusterDomain service = getService(namespaceId, serviceName); - boolean serviceUpdated = false; if (service == null) { service = new VirtualClusterDomain(); @@ -411,9 +409,16 @@ public class ServiceManager implements DataListener { } if (serviceUpdated) { + Lock lock = addLockIfAbsent(UtilsAndCommons.assembleFullServiceName(namespaceId, serviceName)); + Condition condition = addCondtion(UtilsAndCommons.assembleFullServiceName(namespaceId, serviceName)); addOrReplaceService(service); + try { + lock.lock(); + condition.await(5000, TimeUnit.MILLISECONDS); + } finally { + lock.unlock(); + } } - addInstance(namespaceId, serviceName, instance); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/HealthCheckTask.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/HealthCheckTask.java index e84f64242..faa5a8bd5 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/HealthCheckTask.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/HealthCheckTask.java @@ -105,21 +105,6 @@ public class HealthCheckTask implements Runnable { } } } -// -// @JSONField(serialize = false) -// public SwitchDomain getSwitchDomain() { -// return SpringContext.getAppContext().getBean(SwitchDomain.class); -// } -// -// @JSONField(serialize = false) -// public DistroMapper getDistroMapper() { -// return SpringContext.getAppContext().getBean(DistroMapper.class); -// } -// -// @JSONField(serialize = false) -// public HealthCheckProcessor getHealthCheckProcessor() { -// return SpringContext.getAppContext().getBean(HealthCheckProcessorDelegate.class); -// } public Cluster getCluster() { return cluster; diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java index 19fee66e4..a4ecb126d 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java @@ -55,7 +55,7 @@ public class RegisterInstance_ITCase { public void init() throws Exception { if (naming == null) { TimeUnit.SECONDS.sleep(10); - naming = NamingFactory.createNamingService("127.0.0.1" + ":" + 8848); + naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); } } @@ -92,16 +92,14 @@ public class RegisterInstance_ITCase { @Test public void regDomTest() throws Exception { String serviceName = randomDomainName(); - - naming.registerInstance(serviceName, TEST_IP_4_DOM_1, TEST_PORT); - System.out.println(serviceName); + naming.registerInstance(serviceName, TEST_IP_4_DOM_1, TEST_PORT); TimeUnit.SECONDS.sleep(3); List instances = naming.getAllInstances(serviceName); - Assert.assertEquals(instances.size(), 1); + Assert.assertEquals(1, instances.size()); Assert.assertTrue(instances.get(0).getInstanceId().contains(serviceName)); //Assert.assertEquals(instances.get(0).getService().getName(), serviceName); Assert.assertEquals(instances.get(0).getIp(), TEST_IP_4_DOM_1);