From 504edb141b3f0ced57ecdd248d152e1c6aa82263 Mon Sep 17 00:00:00 2001 From: nkorange Date: Wed, 17 Oct 2018 16:21:33 +0800 Subject: [PATCH 1/2] Add metadata test case --- .../test/naming/RegisterInstance_ITCase.java | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) 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 7c61ef910..f1c462c7d 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 @@ -29,7 +29,9 @@ import org.springframework.boot.web.server.LocalServerPort; import org.springframework.test.context.junit4.SpringRunner; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.concurrent.TimeUnit; import static com.alibaba.nacos.test.naming.NamingBase.*; @@ -42,31 +44,35 @@ import static com.alibaba.nacos.test.naming.NamingBase.*; */ @RunWith(SpringRunner.class) @SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"}, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class RegisterInstance_ITCase { private NamingService naming; private NamingService naming2; @LocalServerPort private int port; + @Before - public void init() throws Exception{ + public void init() throws Exception { if (naming == null) { TimeUnit.SECONDS.sleep(10); - naming = NamingFactory.createNamingService("127.0.0.1"+":"+port); + naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); } } /** * 注册一个默认cluster的Instance,并验证 + * * @throws Exception */ @Test - public void regDomTest() throws Exception{ + public void regDomTest() throws Exception { String serviceName = randomDomainName(); naming.registerInstance(serviceName, TEST_IP_4_DOM_1, TEST_PORT); + System.out.println(serviceName); + TimeUnit.SECONDS.sleep(3); List instances = naming.getAllInstances(serviceName); @@ -80,10 +86,11 @@ public class RegisterInstance_ITCase { /** * 注册一个自定义cluster的Instance,并验证 + * * @throws Exception */ @Test - public void regDomClusterTest() throws Exception{ + public void regDomClusterTest() throws Exception { String serviceName = randomDomainName(); naming.registerInstance(serviceName, TEST_IP_4_DOM_1, TEST_PORT, TEST_NEW_CLUSTER_4_DOM_1); @@ -111,6 +118,7 @@ public class RegisterInstance_ITCase { /** * 注册一个自定义的Instance,并验证 + * * @throws Exception */ @Test @@ -132,6 +140,7 @@ public class RegisterInstance_ITCase { /** * 注册一个不健康的Instance,并验证 + * * @throws Exception */ @Test @@ -145,9 +154,34 @@ public class RegisterInstance_ITCase { TimeUnit.SECONDS.sleep(3); - List instances = naming.selectInstances(serviceName,false); + List instances = naming.selectInstances(serviceName, false); Assert.assertEquals(instances.size(), 1); Assert.assertEquals(instances.get(0).isHealthy(), false); } + + @Test + public void regServiceWithMetadata() throws Exception { + + String serviceName = randomDomainName(); + System.out.println(serviceName); + + Instance instance = new Instance(); + instance.setIp("1.1.1.2"); + instance.setPort(9999); + Map metadata = new HashMap(); + metadata.put("version", "1.0"); + metadata.put("env", "prod"); + instance.setMetadata(metadata); + + naming.registerInstance(serviceName, instance); + + TimeUnit.SECONDS.sleep(3); + + List instances = naming.getAllInstances(serviceName); + + Assert.assertEquals(1, instances.size()); + Assert.assertEquals("1.0", instances.get(0).getMetadata().get("version")); + Assert.assertEquals("prod", instances.get(0).getMetadata().get("env")); + } } From 698cf480e009ad66cd3fee735ec2b6ff66735242 Mon Sep 17 00:00:00 2001 From: nkorange Date: Wed, 17 Oct 2018 18:12:39 +0800 Subject: [PATCH 2/2] Fix #130 --- .../healthcheck/HealthCheckReactor.java | 28 ++++++++++------ .../MysqlHealthCheckProcessor.java | 33 +++++++++++-------- .../healthcheck/TcpSuperSenseProcessor.java | 3 +- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/HealthCheckReactor.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/HealthCheckReactor.java index dd19664c9..2e363bbdc 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/HealthCheckReactor.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/HealthCheckReactor.java @@ -21,16 +21,24 @@ import java.util.concurrent.*; * @author nacos */ public class HealthCheckReactor { - private static final ScheduledExecutorService EXECUTOR = Executors - .newScheduledThreadPool(Runtime.getRuntime().availableProcessors() / 2, new ThreadFactory() { - @Override - public Thread newThread(Runnable r) { - Thread thread = new Thread(r); - thread.setDaemon(true); - thread.setName("com.alibaba.nacos.naming.health"); - return thread; - } - }); + + private static final ScheduledExecutorService EXECUTOR; + + static { + + int processorCount = Runtime.getRuntime().availableProcessors(); + EXECUTOR + = Executors + .newScheduledThreadPool(processorCount <= 1 ? 1 : processorCount / 2, new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(r); + thread.setDaemon(true); + thread.setName("com.alibaba.nacos.naming.health"); + return thread; + } + }); + } public static ScheduledFuture scheduleCheck(HealthCheckTask task) { task.setStartTime(System.currentTimeMillis()); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/MysqlHealthCheckProcessor.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/MysqlHealthCheckProcessor.java index 8e90de7b4..36691af01 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/MysqlHealthCheckProcessor.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/MysqlHealthCheckProcessor.java @@ -48,18 +48,24 @@ public class MysqlHealthCheckProcessor extends AbstractHealthCheckProcessor { private static ConcurrentMap CONNECTION_POOL = new ConcurrentHashMap(); - private static ExecutorService EXECUTOR - = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() / 2, - new ThreadFactory() { - @Override - public Thread newThread(Runnable r) { - Thread thread = new Thread(r); - thread.setDaemon(true); - thread.setName("com.nacos.mysql.checker"); - return thread; + private static ExecutorService EXECUTOR; + + static { + + int processorCount = Runtime.getRuntime().availableProcessors(); + EXECUTOR + = Executors.newFixedThreadPool(processorCount <= 1 ? 1 : processorCount / 2, + new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(r); + thread.setDaemon(true); + thread.setName("com.nacos.mysql.checker"); + return thread; + } } - } - ); + ); + } public MysqlHealthCheckProcessor() { } @@ -129,7 +135,8 @@ public class MysqlHealthCheckProcessor extends AbstractHealthCheckProcessor { Statement statement = null; ResultSet resultSet = null; - try {; + try { + ; Cluster cluster = task.getCluster(); String key = cluster.getDom().getName() + ":" + cluster.getName() + ":" + ip.getIp() + ":" + ip.getPort(); Connection connection = CONNECTION_POOL.get(key); @@ -191,7 +198,7 @@ public class MysqlHealthCheckProcessor extends AbstractHealthCheckProcessor { reEvaluateCheckRT(Switch.getMysqlHealthParams().getMax(), task, Switch.getMysqlHealthParams()); } finally { ip.setCheckRT(System.currentTimeMillis() - startTime); - if (statement!=null) { + if (statement != null) { try { statement.close(); } catch (SQLException e) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/TcpSuperSenseProcessor.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/TcpSuperSenseProcessor.java index c5b6c6d26..059b2ced0 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/TcpSuperSenseProcessor.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/TcpSuperSenseProcessor.java @@ -47,7 +47,8 @@ public class TcpSuperSenseProcessor extends AbstractHealthCheckProcessor impleme /** * this value has been carefully tuned, do not modify unless you're confident */ - public static final int NIO_THREAD_COUNT = Runtime.getRuntime().availableProcessors() / 2; + public static final int NIO_THREAD_COUNT = Runtime.getRuntime().availableProcessors() <= 1 ? + 1 : Runtime.getRuntime().availableProcessors() / 2; /** * because some hosts doesn't support keep-alive connections, disabled temporarily