From fe0bb81d92331b503d3b6a813018a38c56c1127e Mon Sep 17 00:00:00 2001 From: zhaowang Date: Thu, 20 Oct 2022 14:15:26 +0800 Subject: [PATCH 1/2] fix DoaminRegistry multi inited --- .../rpc/registry/local/DomainRegistry.java | 31 ++++++++++--------- .../registry/local/DomainRegistryTest.java | 12 ++++++- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/registry/registry-local/src/main/java/com/alipay/sofa/rpc/registry/local/DomainRegistry.java b/registry/registry-local/src/main/java/com/alipay/sofa/rpc/registry/local/DomainRegistry.java index fcb4cd387..d6cc90ae6 100644 --- a/registry/registry-local/src/main/java/com/alipay/sofa/rpc/registry/local/DomainRegistry.java +++ b/registry/registry-local/src/main/java/com/alipay/sofa/rpc/registry/local/DomainRegistry.java @@ -43,6 +43,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Function; import java.util.stream.Collectors; @@ -72,6 +73,8 @@ public class DomainRegistry extends Registry { */ protected ScheduledService scheduledExecutorService; + protected AtomicBoolean inited = new AtomicBoolean(); + /** * 注册中心配置 * @@ -83,29 +86,27 @@ protected DomainRegistry(RegistryConfig registryConfig) { @Override public void init() { - this.scanPeriod = CommonUtils.parseInt(registryConfig.getParameter("registry.domain.scan.period"), - scanPeriod); + if (inited.compareAndSet(false, true)) { + this.scanPeriod = CommonUtils.parseInt(registryConfig.getParameter("registry.domain.scan.period"), + scanPeriod); - Runnable task = new Runnable() { - @Override - public void run() { + Runnable task = () -> { try { refreshDomain(); notifyListener(); } catch (Throwable e) { LOGGER.error(e.getMessage(), e); } - } - }; - - scheduledExecutorService = new ScheduledService("DomainRegistry-Back-Load", - ScheduledService.MODE_FIXEDDELAY, - task, //定时load任务 - scanPeriod, // 延迟一个周期 - scanPeriod, // 一个周期循环 - TimeUnit.MILLISECONDS - ).start(); + }; + scheduledExecutorService = new ScheduledService("DomainRegistry-Back-Load", + ScheduledService.MODE_FIXEDDELAY, + task, //定时load任务 + scanPeriod, // 延迟一个周期 + scanPeriod, // 一个周期循环 + TimeUnit.MILLISECONDS + ).start(); + } } protected void refreshDomain() { diff --git a/registry/registry-local/src/test/java/com/alipay/sofa/rpc/registry/local/DomainRegistryTest.java b/registry/registry-local/src/test/java/com/alipay/sofa/rpc/registry/local/DomainRegistryTest.java index 7c49e915b..a041d449a 100644 --- a/registry/registry-local/src/test/java/com/alipay/sofa/rpc/registry/local/DomainRegistryTest.java +++ b/registry/registry-local/src/test/java/com/alipay/sofa/rpc/registry/local/DomainRegistryTest.java @@ -20,6 +20,7 @@ import com.alipay.sofa.rpc.client.ProviderHelper; import com.alipay.sofa.rpc.client.ProviderInfo; import com.alipay.sofa.rpc.common.RpcConstants; +import com.alipay.sofa.rpc.common.struct.ScheduledService; import com.alipay.sofa.rpc.config.ConsumerConfig; import com.alipay.sofa.rpc.config.ProviderConfig; import com.alipay.sofa.rpc.config.RegistryConfig; @@ -67,6 +68,15 @@ public void testInit() { assertTrue(domainRegistry.scheduledExecutorService.isStarted()); } + @Test + public void testInitOnce() { + DomainRegistry domainRegistry = new DomainRegistry(new RegistryConfig()); + domainRegistry.init(); + ScheduledService origin = domainRegistry.scheduledExecutorService; + domainRegistry.init(); + assertSame(origin, domainRegistry.scheduledExecutorService); + } + @Test public void testDirectUrl2IpUrl() { ProviderInfo providerInfo = ProviderHelper.toProviderInfo("bolt://alipay.com:12200"); @@ -276,7 +286,7 @@ private static class MockProviderInfoListener implements ProviderInfoListener { ConcurrentHashMap> ps = new ConcurrentHashMap(); - private CountDownLatch countDownLatch; + private CountDownLatch countDownLatch; public void setCountDownLatch(CountDownLatch countDownLatch) { this.countDownLatch = countDownLatch; From 93cb08480cfdee01e0beaa7f7744120458dd8e70 Mon Sep 17 00:00:00 2001 From: zhaowang Date: Thu, 20 Oct 2022 14:59:24 +0800 Subject: [PATCH 2/2] fmt code --- .../com/alipay/sofa/rpc/registry/local/DomainRegistryTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/registry/registry-local/src/test/java/com/alipay/sofa/rpc/registry/local/DomainRegistryTest.java b/registry/registry-local/src/test/java/com/alipay/sofa/rpc/registry/local/DomainRegistryTest.java index a041d449a..be064d43a 100644 --- a/registry/registry-local/src/test/java/com/alipay/sofa/rpc/registry/local/DomainRegistryTest.java +++ b/registry/registry-local/src/test/java/com/alipay/sofa/rpc/registry/local/DomainRegistryTest.java @@ -286,7 +286,7 @@ private static class MockProviderInfoListener implements ProviderInfoListener { ConcurrentHashMap> ps = new ConcurrentHashMap(); - private CountDownLatch countDownLatch; + private CountDownLatch countDownLatch; public void setCountDownLatch(CountDownLatch countDownLatch) { this.countDownLatch = countDownLatch;