From 28353aff9e3b6ab57de784ecc6974cc612b9b241 Mon Sep 17 00:00:00 2001 From: haocao Date: Tue, 9 May 2017 11:58:43 +0800 Subject: [PATCH] Fixed #310. --- RELEASE-NOTES.md | 1 + .../reg/zookeeper/ZookeeperRegistryCenter.java | 16 +++++----------- ...eeperRegistryCenterQueryWithoutCacheTest.java | 9 ++++++--- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 3e72ebb825..c0c897978e 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -7,6 +7,7 @@ ### 缺陷修正 1. [ISSUE #306](https://github.com/dangdangdotcom/elastic-job/issues/306) 切换是否监控作业执行状态且作业间隔时间短时可能发生作业无法继续运行 +1. [ISSUE #306](https://github.com/dangdangdotcom/elastic-job/issues/310) 配置检查本机与注册中心的时间误差秒后,创建过多sequence节点 ## 2.1.1 diff --git a/elastic-job-common/elastic-job-common-core/src/main/java/com/dangdang/ddframe/job/reg/zookeeper/ZookeeperRegistryCenter.java b/elastic-job-common/elastic-job-common-core/src/main/java/com/dangdang/ddframe/job/reg/zookeeper/ZookeeperRegistryCenter.java index 0db97211a8..36d12b512b 100644 --- a/elastic-job-common/elastic-job-common-core/src/main/java/com/dangdang/ddframe/job/reg/zookeeper/ZookeeperRegistryCenter.java +++ b/elastic-job-common/elastic-job-common-core/src/main/java/com/dangdang/ddframe/job/reg/zookeeper/ZookeeperRegistryCenter.java @@ -187,9 +187,9 @@ public int compare(final String o1, final String o2) { @Override public int getNumChildren(final String key) { try { - Stat stat = client.getZookeeperClient().getZooKeeper().exists(getNameSpace() + key, false); + Stat stat = client.checkExists().forPath(key); if (null != stat) { - return stat.getNumChildren(); + return stat.getNumChildren(); } //CHECKSTYLE:OFF } catch (final Exception ex) { @@ -198,12 +198,7 @@ public int getNumChildren(final String key) { } return 0; } - - private String getNameSpace() { - String result = this.getZkConfig().getNamespace(); - return Strings.isNullOrEmpty(result) ? "" : "/" + result; - } - + @Override public boolean isExisted(final String key) { try { @@ -294,9 +289,8 @@ public void remove(final String key) { public long getRegistryCenterTime(final String key) { long result = 0L; try { - String path = client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key); - result = client.checkExists().forPath(path).getCtime(); - client.delete().forPath(path); + persist(key, ""); + result = client.checkExists().forPath(key).getMtime(); //CHECKSTYLE:OFF } catch (final Exception ex) { //CHECKSTYLE:ON diff --git a/elastic-job-common/elastic-job-common-core/src/test/java/com/dangdang/ddframe/job/reg/zookeeper/ZookeeperRegistryCenterQueryWithoutCacheTest.java b/elastic-job-common/elastic-job-common-core/src/test/java/com/dangdang/ddframe/job/reg/zookeeper/ZookeeperRegistryCenterQueryWithoutCacheTest.java index b6ead64c1d..9fe32b099a 100644 --- a/elastic-job-common/elastic-job-common-core/src/test/java/com/dangdang/ddframe/job/reg/zookeeper/ZookeeperRegistryCenterQueryWithoutCacheTest.java +++ b/elastic-job-common/elastic-job-common-core/src/test/java/com/dangdang/ddframe/job/reg/zookeeper/ZookeeperRegistryCenterQueryWithoutCacheTest.java @@ -85,10 +85,13 @@ public void assertIsExisted() { @Test public void assertGetRegistryCenterTime() { - assertTrue(zkRegCenter.getRegistryCenterTime("/_systemTime/current") <= System.currentTimeMillis()); + long regCenterTime = zkRegCenter.getRegistryCenterTime("/_systemTime/current"); + assertTrue(regCenterTime <= System.currentTimeMillis()); + long updatedRegCenterTime = zkRegCenter.getRegistryCenterTime("/_systemTime/current"); + System.out.println(regCenterTime + "," + updatedRegCenterTime); + assertTrue(regCenterTime < updatedRegCenterTime); } - - + @Test public void assertGetWithoutNode() { assertNull(zkRegCenter.get("/notExisted"));