From 717e22f5e783d2f91d1896ace8aec34b0e4bd406 Mon Sep 17 00:00:00 2001 From: dongzl Date: Wed, 6 Nov 2019 14:19:26 +0800 Subject: [PATCH] fixes ConfigCenter api, reduce method. --- .../config/api/ConfigCenter.java | 35 +------------------ .../config/apollo/ApolloConfigCenter.java | 21 ----------- .../config/apollo/ApolloConfigCenterTest.java | 10 ------ .../curator/CuratorZookeeperConfigCenter.java | 32 +++++------------ .../CuratorZookeeperConfigCenterTest.java | 12 ------- .../fixture/FirstTestConfigCenter.java | 18 ---------- 6 files changed, 9 insertions(+), 119 deletions(-) diff --git a/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-api/src/main/java/org/apache/shardingsphere/orchestration/config/api/ConfigCenter.java b/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-api/src/main/java/org/apache/shardingsphere/orchestration/config/api/ConfigCenter.java index 1cb66d7371db6..98a3bc2922eec 100644 --- a/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-api/src/main/java/org/apache/shardingsphere/orchestration/config/api/ConfigCenter.java +++ b/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-api/src/main/java/org/apache/shardingsphere/orchestration/config/api/ConfigCenter.java @@ -26,6 +26,7 @@ * Config center. * * @author wangguangyuan + * @author dongzonglei */ public interface ConfigCenter extends TypeBasedSPI { @@ -46,24 +47,6 @@ public interface ConfigCenter extends TypeBasedSPI { */ String get(String key); - /** - * Get data from config center directly. - * - *

Cannot use cache.

- * - * @param key key of data - * @return value of data - */ - String getDirectly(String key); - - /** - * Judge data is existed or not. - * - * @param key key of data - * @return data is existed or not - */ - boolean isExisted(String key); - /** * Get node's sub-nodes list. * @@ -80,22 +63,6 @@ public interface ConfigCenter extends TypeBasedSPI { */ void persist(String key, String value); - /** - * Update data. - * - * @param key key of data - * @param value value of data - */ - void update(String key, String value); - - /** - * Persist ephemeral data. - * - * @param key key of data - * @param value value of data - */ - void persistEphemeral(String key, String value); - /** * Watch key or path of the config server. * diff --git a/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-apollo/src/main/java/org/apache/shardingsphere/orchestration/config/apollo/ApolloConfigCenter.java b/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-apollo/src/main/java/org/apache/shardingsphere/orchestration/config/apollo/ApolloConfigCenter.java index 142c1a59edfcc..a86e801615c9d 100644 --- a/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-apollo/src/main/java/org/apache/shardingsphere/orchestration/config/apollo/ApolloConfigCenter.java +++ b/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-apollo/src/main/java/org/apache/shardingsphere/orchestration/config/apollo/ApolloConfigCenter.java @@ -24,7 +24,6 @@ import com.ctrip.framework.apollo.enums.PropertyChangeType; import com.ctrip.framework.apollo.model.ConfigChange; import com.ctrip.framework.apollo.model.ConfigChangeEvent; -import com.google.common.base.Strings; import com.google.common.collect.Sets; import lombok.Getter; import lombok.Setter; @@ -65,16 +64,6 @@ public String get(final String key) { return apolloConfig.getProperty(key.replace("/", "."), ""); } - @Override - public String getDirectly(final String key) { - return get(key); - } - - @Override - public boolean isExisted(final String key) { - return !Strings.isNullOrEmpty(get(key)); - } - @Override public List getChildrenKeys(final String key) { throw new UnsupportedOperationException(); @@ -85,16 +74,6 @@ public void persist(final String key, final String value) { throw new UnsupportedOperationException(); } - @Override - public void update(final String key, final String value) { - throw new UnsupportedOperationException(); - } - - @Override - public void persistEphemeral(final String key, final String value) { - throw new UnsupportedOperationException(); - } - @Override public void watch(final String key, final DataChangedEventListener dataChangedEventListener) { apolloConfig.addChangeListener(new ConfigChangeListener() { diff --git a/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-apollo/src/test/java/org/apache/shardingsphere/orchestration/config/apollo/ApolloConfigCenterTest.java b/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-apollo/src/test/java/org/apache/shardingsphere/orchestration/config/apollo/ApolloConfigCenterTest.java index f407f61573912..588e70d23503a 100644 --- a/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-apollo/src/test/java/org/apache/shardingsphere/orchestration/config/apollo/ApolloConfigCenterTest.java +++ b/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-apollo/src/test/java/org/apache/shardingsphere/orchestration/config/apollo/ApolloConfigCenterTest.java @@ -53,14 +53,4 @@ public static void init() { public void assertGet() { assertThat(configCenter.get("key1"), is("value1")); } - - @Test - public void assertGetDirectly() { - assertThat(configCenter.getDirectly("key2"), is("value2")); - } - - @Test - public void assertIsExisted() { - assertThat(configCenter.isExisted("key1"), is(true)); - } } diff --git a/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-zookeeper-curator/src/main/java/org/apache/shardingsphere/orchestration/config/zookeeper/curator/CuratorZookeeperConfigCenter.java b/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-zookeeper-curator/src/main/java/org/apache/shardingsphere/orchestration/config/zookeeper/curator/CuratorZookeeperConfigCenter.java index adbf520b75b06..c8a925c0719a3 100644 --- a/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-zookeeper-curator/src/main/java/org/apache/shardingsphere/orchestration/config/zookeeper/curator/CuratorZookeeperConfigCenter.java +++ b/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-zookeeper-curator/src/main/java/org/apache/shardingsphere/orchestration/config/zookeeper/curator/CuratorZookeeperConfigCenter.java @@ -53,6 +53,7 @@ * Config center for zookeeper with curator. * * @author wangguangyuan + * @author dongzonglei */ public final class CuratorZookeeperConfigCenter implements ConfigCenter { @@ -133,8 +134,7 @@ private TreeCache findTreeCache(final String key) { return null; } - @Override - public String getDirectly(final String key) { + private String getDirectly(final String key) { try { return new String(client.getData().forPath(key), Charsets.UTF_8); // CHECKSTYLE:OFF @@ -145,18 +145,6 @@ public String getDirectly(final String key) { } } - @Override - public boolean isExisted(final String key) { - try { - return null != client.checkExists().forPath(key); - // CHECKSTYLE:OFF - } catch (final Exception ex) { - // CHECKSTYLE:ON - CuratorZookeeperExceptionHandler.handleException(ex); - return false; - } - } - @Override public List getChildrenKeys(final String key) { try { @@ -191,25 +179,21 @@ public void persist(final String key, final String value) { CuratorZookeeperExceptionHandler.handleException(ex); } } - - @Override - public void update(final String key, final String value) { + + private boolean isExisted(final String key) { try { - client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Charsets.UTF_8)).and().commit(); + return null != client.checkExists().forPath(key); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON CuratorZookeeperExceptionHandler.handleException(ex); + return false; } } - @Override - public void persistEphemeral(final String key, final String value) { + private void update(final String key, final String value) { try { - if (isExisted(key)) { - client.delete().deletingChildrenIfNeeded().forPath(key); - } - client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(key, value.getBytes(Charsets.UTF_8)); + client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Charsets.UTF_8)).and().commit(); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON diff --git a/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-zookeeper-curator/src/test/java/org/apache/shardingsphere/orchestration/config/zookeeper/curator/test/CuratorZookeeperConfigCenterTest.java b/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-zookeeper-curator/src/test/java/org/apache/shardingsphere/orchestration/config/zookeeper/curator/test/CuratorZookeeperConfigCenterTest.java index 7dbd4069117e5..2e0d2e270a4f3 100644 --- a/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-zookeeper-curator/src/test/java/org/apache/shardingsphere/orchestration/config/zookeeper/curator/test/CuratorZookeeperConfigCenterTest.java +++ b/sharding-orchestration/sharding-orchestration-config/sharding-orchestration-config-zookeeper-curator/src/test/java/org/apache/shardingsphere/orchestration/config/zookeeper/curator/test/CuratorZookeeperConfigCenterTest.java @@ -60,18 +60,6 @@ public void assertPersistEphemeral() { assertThat(curatorZookeeperConfigCenter.get("/test/ephemeral"), is("value3")); } - @Test - public void assertGetDirectly() { - curatorZookeeperConfigCenter.persist("/test", "value4"); - assertThat(curatorZookeeperConfigCenter.getDirectly("/test"), is("value4")); - } - - @Test - public void assertIsExisted() { - curatorZookeeperConfigCenter.persist("/test/existed", "value5"); - assertThat(curatorZookeeperConfigCenter.isExisted("/test/existed"), is(true)); - } - @Test public void assertGetChildrenKeys() { curatorZookeeperConfigCenter.persist("/test/children/1", "value11"); diff --git a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/configcenter/fixture/FirstTestConfigCenter.java b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/configcenter/fixture/FirstTestConfigCenter.java index a21bef3e7e521..30f238b3821bc 100644 --- a/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/configcenter/fixture/FirstTestConfigCenter.java +++ b/sharding-orchestration/sharding-orchestration-core/src/test/java/org/apache/shardingsphere/orchestration/internal/configcenter/fixture/FirstTestConfigCenter.java @@ -42,16 +42,6 @@ public String get(final String key) { return ""; } - @Override - public String getDirectly(final String key) { - return ""; - } - - @Override - public boolean isExisted(final String key) { - return true; - } - @Override public List getChildrenKeys(final String key) { return Collections.emptyList(); @@ -61,14 +51,6 @@ public List getChildrenKeys(final String key) { public void persist(final String key, final String value) { } - @Override - public void update(final String key, final String value) { - } - - @Override - public void persistEphemeral(final String key, final String value) { - } - @Override public void watch(final String key, final DataChangedEventListener dataChangedEventListener) { }