This is an automated email from the ASF dual-hosted git repository.
zhonghongsheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 0dcde88 Add cache configuration for SchemaRulePersistService (#13319)
0dcde88 is described below
commit 0dcde889e4bc33513e1c1f3cf1b0dcfc724e63aa
Author: Haoran Meng <[email protected]>
AuthorDate: Wed Oct 27 20:51:57 2021 +0800
Add cache configuration for SchemaRulePersistService (#13319)
---
.../mode/metadata/persist}/node/CacheNode.java | 35 +++++++++++-----------
.../persist/service/SchemaBasedPersistService.java | 8 +++++
.../service/impl/SchemaRulePersistService.java | 6 ++++
.../mode/metadata/persist}/node/CacheNodeTest.java | 18 ++++++-----
.../registry/cache/RegistryCacheManager.java | 2 +-
.../metadata/watcher/MetaDataChangedWatcher.java | 2 +-
6 files changed, 43 insertions(+), 28 deletions(-)
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/cache/node/CacheNode.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/node/CacheNode.java
similarity index 81%
rename from
shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/cache/node/CacheNode.java
rename to
shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/node/CacheNode.java
index f6b0e7c..f428577 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/cache/node/CacheNode.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/node/CacheNode.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.cache.node;
+package org.apache.shardingsphere.mode.metadata.persist.node;
import com.google.common.base.Joiner;
import lombok.AccessLevel;
@@ -37,16 +37,6 @@ public final class CacheNode {
private static final String PATH_SEPARATOR = "/";
/**
- * Get cache node path.
- *
- * @param path path
- * @return cache node path
- */
- public static String getCacheNodePath(final String path) {
- return Joiner.on("/").join(path, CACHE_NODE);
- }
-
- /**
* Get cache id by cache path.
*
* @param path patch
@@ -59,12 +49,7 @@ public final class CacheNode {
return matcher.find() ? Optional.of(matcher.group(1)) :
Optional.empty();
}
- /**
- * Get cache id.
- *
- * @return cache id
- */
- public static String getCacheId() {
+ private static String getCacheId() {
return UUID.randomUUID().toString().replace("-", "");
}
@@ -76,6 +61,20 @@ public final class CacheNode {
* @return cache path
*/
public static String getCachePath(final String path, final String cacheId)
{
- return
Joiner.on(PATH_SEPARATOR).join(CacheNode.getCacheNodePath(path), cacheId);
+ return Joiner.on(PATH_SEPARATOR).join(getCacheNodePath(path), cacheId);
+ }
+
+ /**
+ * Get cache path.
+ *
+ * @param path path
+ * @return cache path
+ */
+ public static String getCachePath(final String path) {
+ return Joiner.on(PATH_SEPARATOR).join(getCacheNodePath(path),
getCacheId());
+ }
+
+ private static String getCacheNodePath(final String path) {
+ return Joiner.on("/").join(path, CACHE_NODE);
}
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/service/SchemaBasedPersistService.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/service/SchemaBasedPersistService.java
index b7bc018..d484cb6 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/service/SchemaBasedPersistService.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/service/SchemaBasedPersistService.java
@@ -56,4 +56,12 @@ public interface SchemaBasedPersistService<T> {
* @return configuration existed or not
*/
boolean isExisted(String schemaName);
+
+ /**
+ * Cache configurations.
+ *
+ * @param schemaName schema name
+ * @param configs configurations
+ */
+ default void cache(String schemaName, T configs) { }
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/service/impl/SchemaRulePersistService.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/service/impl/SchemaRulePersistService.java
index 21a9df3..bcc22e7 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/service/impl/SchemaRulePersistService.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/service/impl/SchemaRulePersistService.java
@@ -23,6 +23,7 @@ import
org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRuleConfiguration;
import
org.apache.shardingsphere.infra.yaml.config.swapper.YamlRuleConfigurationSwapperEngine;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+import org.apache.shardingsphere.mode.metadata.persist.node.CacheNode;
import org.apache.shardingsphere.mode.persist.PersistRepository;
import org.apache.shardingsphere.mode.metadata.persist.node.SchemaMetaDataNode;
import
org.apache.shardingsphere.mode.metadata.persist.service.SchemaBasedPersistService;
@@ -67,4 +68,9 @@ public final class SchemaRulePersistService implements
SchemaBasedPersistService
public boolean isExisted(final String schemaName) {
return
!Strings.isNullOrEmpty(repository.get(SchemaMetaDataNode.getRulePath(schemaName)));
}
+
+ @Override
+ public void cache(final String schemaName, final
Collection<RuleConfiguration> configs) {
+
repository.persist(CacheNode.getCachePath(SchemaMetaDataNode.getRulePath(schemaName)),
YamlEngine.marshal(createYamlRuleConfigurations(configs)));
+ }
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/cache/node/CacheNodeTest.java
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/node/CacheNodeTest.java
similarity index 81%
rename from
shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/cache/node/CacheNodeTest.java
rename to
shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/node/CacheNodeTest.java
index e82d0a6..b9e7d6f 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/cache/node/CacheNodeTest.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/node/CacheNodeTest.java
@@ -15,24 +15,20 @@
* limitations under the License.
*/
-package
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.cache.node;
+package org.apache.shardingsphere.mode.metadata.persist.node;
import org.junit.Test;
+
import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public final class CacheNodeTest {
@Test
- public void assertGetCacheNodePath() {
- assertThat(CacheNode.getCacheNodePath("/metadata/sharding_db/rules"),
- is("/metadata/sharding_db/rules/cache"));
- }
-
- @Test
public void assertGetCacheId() {
Optional<String> cacheId =
CacheNode.getCacheId("/metadata/sharding_db/rules",
"/metadata/sharding_db/rules/cache/testCacheId");
@@ -41,8 +37,14 @@ public final class CacheNodeTest {
}
@Test
- public void assertGetCachePath() {
+ public void assertGetCachePathWithPathAndCacheId() {
assertThat(CacheNode.getCachePath("/metadata/sharding_db/rules", "1"),
is("/metadata/sharding_db/rules/cache/1"));
}
+
+ @Test
+ public void assertGetCachePathWithPath() {
+ assertThat(CacheNode.getCachePath("/metadata/sharding_db/rules"),
+ startsWith("/metadata/sharding_db/rules/"));
+ }
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/cache/RegistryCacheManager.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/cache/RegistryCacheManager.java
index c9642ae..c0f3d13 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/cache/RegistryCacheManager.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/cache/RegistryCacheManager.java
@@ -18,7 +18,7 @@
package
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.cache;
import lombok.RequiredArgsConstructor;
-import
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.cache.node.CacheNode;
+import org.apache.shardingsphere.mode.metadata.persist.node.CacheNode;
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
/**
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcher.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcher.java
index 39c5a61..e03af82 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcher.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/watcher/MetaDataChangedWatcher.java
@@ -20,7 +20,7 @@ package
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.meta
import com.google.common.base.Strings;
import
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceEvent;
import
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher;
-import
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.cache.node.CacheNode;
+import org.apache.shardingsphere.mode.metadata.persist.node.CacheNode;
import
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.datasource.DataSourceChangedEvent;
import
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.rule.RuleConfigurationCachedEvent;
import
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.rule.RuleConfigurationsChangedEvent;