You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by qi...@apache.org on 2021/01/08 09:29:24 UTC

[shardingsphere] branch master updated: Add rule configuration cached event (#8955)

This is an automated email from the ASF dual-hosted git repository.

qiulu 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 d096385  Add rule configuration cached event (#8955)
d096385 is described below

commit d09638590503e21980aed0853961b9c686581a46
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Fri Jan 8 17:29:00 2021 +0800

    Add rule configuration cached event (#8955)
---
 .../governance/core/config/ConfigCacheManager.java | 30 ++++++++---------
 .../governance/core/config/ConfigCenter.java       | 17 +++++++++-
 .../governance/core/config/ConfigCenterNode.java   | 12 +++++++
 .../config/listener/SchemaChangedListener.java     | 26 ++++++++++++---
 .../model/rule/RuleConfigurationCachedEvent.java   | 39 ++++++++++++++++++++++
 5 files changed, 104 insertions(+), 20 deletions(-)

diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCacheManager.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCacheManager.java
index 217b599..19792ba 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCacheManager.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCacheManager.java
@@ -27,26 +27,27 @@ import java.util.UUID;
  */
 public final class ConfigCacheManager {
     
-    private static final String CACHE_KEY = "cache";
-    
     private static final String PATH_SEPARATOR = "/";
     
     private final ConfigurationRepository repository;
     
-    public ConfigCacheManager(final ConfigurationRepository repository) {
+    private final ConfigCenterNode node;
+    
+    public ConfigCacheManager(final ConfigurationRepository repository, final ConfigCenterNode node) {
         this.repository = repository;
+        this.node = node;
     }
     
     /**
      * Cache configuration.
      * 
-     * @param key key
+     * @param path path
      * @param configuration configuration
      * @return cache id
      */
-    public String cache(final String key, final String configuration) {
+    public String cache(final String path, final String configuration) {
         String cacheId = getCacheId();
-        repository.persist(getCacheKey(key, cacheId), configuration);
+        repository.persist(getCachePath(path, cacheId), configuration);
         return cacheId;
     }
     
@@ -57,26 +58,25 @@ public final class ConfigCacheManager {
     /**
      * Load cached configuration.
      * 
-     * @param key key 
+     * @param path path 
      * @param cacheId cache id
      * @return cached configuration
      */
-    public String loadCache(final String key, final String cacheId) {
-        return repository.get(getCacheKey(key, cacheId));
+    public String loadCache(final String path, final String cacheId) {
+        return repository.get(getCachePath(path, cacheId));
     }
     
     /**
      * Delete cached configuration.
      * 
-     * @param key key
+     * @param path path
      * @param cacheId cache id
      */
-    public void deleteCache(final String key, final String cacheId) {
-        repository.delete(getCacheKey(key, cacheId));
+    public void deleteCache(final String path, final String cacheId) {
+        repository.delete(getCachePath(path, cacheId));
     }
     
-    private String getCacheKey(final String key, final String cacheId) {
-        return Joiner.on(PATH_SEPARATOR).join(key, CACHE_KEY, cacheId);
+    private String getCachePath(final String path, final String cacheId) {
+        return Joiner.on(PATH_SEPARATOR).join(node.getCachePath(path), cacheId);
     }
-    
 }
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java
index 56b699f..5d4edf6 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.governance.core.config.checker.RuleConfiguratio
 import org.apache.shardingsphere.governance.core.config.checker.RuleConfigurationCheckerFactory;
 import org.apache.shardingsphere.governance.core.event.model.datasource.DataSourceChangedEvent;
 import org.apache.shardingsphere.governance.core.event.model.datasource.DataSourcePersistEvent;
+import org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationCachedEvent;
 import org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationsAlteredEvent;
 import org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationsPersistEvent;
 import org.apache.shardingsphere.governance.core.event.model.rule.SwitchRuleConfigurationEvent;
@@ -74,7 +75,7 @@ public final class ConfigCenter {
     public ConfigCenter(final ConfigurationRepository repository) {
         node = new ConfigCenterNode();
         this.repository = repository;
-        configCacheManager = new ConfigCacheManager(repository);
+        configCacheManager = new ConfigCacheManager(repository, node);
         ShardingSphereEventBus.getInstance().register(this);
     }
     
@@ -173,6 +174,20 @@ public final class ConfigCenter {
         configCacheManager.deleteCache(node.getRulePath(event.getSchemaName()), event.getRuleConfigurationCacheId());
     }
     
+    /**
+     * Rule configuration cached.
+     * 
+     * @param event rule configuration cached event
+     */
+    @Subscribe
+    public synchronized void renew(final RuleConfigurationCachedEvent event) {
+        RuleConfigurationsAlteredEvent ruleConfigurationsAlteredEvent = new RuleConfigurationsAlteredEvent(event.getSchemaName(),
+                repository.get(node.getDataSourcePath(event.getSchemaName())),
+                repository.get(node.getRulePath(event.getSchemaName())),
+                configCacheManager.loadCache(node.getRulePath(event.getSchemaName()), event.getCacheId()), event.getCacheId());
+        ShardingSphereEventBus.getInstance().post(ruleConfigurationsAlteredEvent);
+    }
+    
     private Collection<RuleConfiguration> loadCachedRuleConfigurations(final String schemaName, final String ruleConfigurationCacheId) {
         return new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(
                 YamlEngine.unmarshal(configCacheManager.loadCache(node.getRulePath(schemaName), ruleConfigurationCacheId), YamlRootRuleConfigurations.class).getRules());
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenterNode.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenterNode.java
index 91c853b..762021f 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenterNode.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenterNode.java
@@ -50,6 +50,8 @@ public final class ConfigCenterNode {
     
     private static final String PATH_SEPARATOR = "/";
     
+    private static final String CACHE_NODE = "cache";
+    
     /**
      * Get metadata node path.
      *
@@ -163,4 +165,14 @@ public final class ConfigCenterNode {
         }
         return result;
     }
+    
+    /**
+     * Get cache path.
+     * 
+     * @param path path
+     * @return cache path
+     */
+    public String getCachePath(final String path) {
+        return Joiner.on(PATH_SEPARATOR).join(path, CACHE_NODE);
+    }
 }
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/SchemaChangedListener.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/SchemaChangedListener.java
index d7eea23..52f82e7 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/SchemaChangedListener.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/SchemaChangedListener.java
@@ -26,6 +26,7 @@ import org.apache.shardingsphere.governance.core.event.model.GovernanceEvent;
 import org.apache.shardingsphere.governance.core.event.model.datasource.DataSourceChangedEvent;
 import org.apache.shardingsphere.governance.core.event.model.metadata.MetaDataAddedEvent;
 import org.apache.shardingsphere.governance.core.event.model.metadata.MetaDataDeletedEvent;
+import org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationCachedEvent;
 import org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationsChangedEvent;
 import org.apache.shardingsphere.governance.core.event.model.schema.SchemaChangedEvent;
 import org.apache.shardingsphere.governance.core.yaml.config.YamlDataSourceConfigurationWrap;
@@ -35,6 +36,7 @@ import org.apache.shardingsphere.governance.core.yaml.swapper.SchemaYamlSwapper;
 import org.apache.shardingsphere.governance.repository.api.ConfigurationRepository;
 import org.apache.shardingsphere.governance.repository.api.listener.DataChangedEvent;
 import org.apache.shardingsphere.governance.repository.api.listener.DataChangedEvent.Type;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.infra.yaml.config.YamlRootRuleConfigurations;
 import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
 import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
@@ -75,6 +77,9 @@ public final class SchemaChangedListener extends PostGovernanceRepositoryEventLi
             return Optional.empty();
         }
         if (Type.ADDED == event.getType()) {
+            if (event.getKey().startsWith(configurationNode.getCachePath(configurationNode.getRulePath(schemaName)))) {
+                return Optional.of(createRuleConfigurationCachedEvent(schemaName, event));
+            }
             return Optional.of(createAddedEvent(schemaName));
         }
         if (Type.UPDATED == event.getType()) {
@@ -105,7 +110,8 @@ public final class SchemaChangedListener extends PostGovernanceRepositoryEventLi
     private boolean isValidNodeChangedEvent(final String schemaName, final String nodeFullPath) {
         return !existedSchemaNames.contains(schemaName) || configurationNode.getDataSourcePath(schemaName).equals(nodeFullPath) 
                 || configurationNode.getRulePath(schemaName).equals(nodeFullPath)
-                || configurationNode.getSchemaPath(schemaName).equals(nodeFullPath);
+                || configurationNode.getSchemaPath(schemaName).equals(nodeFullPath)
+                || nodeFullPath.startsWith(configurationNode.getCachePath(configurationNode.getRulePath(schemaName)));
     }
     
     private GovernanceEvent createAddedEvent(final String schemaName) {
@@ -135,12 +141,24 @@ public final class SchemaChangedListener extends PostGovernanceRepositoryEventLi
     }
     
     private GovernanceEvent createRuleChangedEvent(final String schemaName, final DataChangedEvent event) {
-        YamlRootRuleConfigurations configurations = YamlEngine.unmarshal(event.getValue(), YamlRootRuleConfigurations.class);
-        Preconditions.checkState(null != configurations, "No available rule to load for governance.");
-        return new RuleConfigurationsChangedEvent(schemaName, new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(configurations.getRules()));
+        return new RuleConfigurationsChangedEvent(schemaName, getRuleConfigurations(event.getValue()));
     }
     
     private GovernanceEvent createSchemaChangedEvent(final String schemaName, final DataChangedEvent event) {
         return new SchemaChangedEvent(schemaName, new SchemaYamlSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), YamlSchema.class)));
     }
+    
+    private GovernanceEvent createRuleConfigurationCachedEvent(final String schemaName, final DataChangedEvent event) {
+        return new RuleConfigurationCachedEvent(getCacheId(event.getKey()), schemaName, getRuleConfigurations(event.getValue()));
+    }
+    
+    private String getCacheId(final String path) {
+        return path.substring(path.lastIndexOf("/") + 1);
+    }
+    
+    private Collection<RuleConfiguration> getRuleConfigurations(final String yamlContent) {
+        YamlRootRuleConfigurations configurations = YamlEngine.unmarshal(yamlContent, YamlRootRuleConfigurations.class);
+        Preconditions.checkState(null != configurations, "No available rule to load for governance.");
+        return new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(configurations.getRules());
+    }
 }
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/event/model/rule/RuleConfigurationCachedEvent.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/event/model/rule/RuleConfigurationCachedEvent.java
new file mode 100644
index 0000000..63448d2
--- /dev/null
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/event/model/rule/RuleConfigurationCachedEvent.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.governance.core.event.model.rule;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.governance.core.event.model.GovernanceEvent;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+
+import java.util.Collection;
+
+/**
+ * Rule configuration cached event.
+ */
+@RequiredArgsConstructor
+@Getter
+public final class RuleConfigurationCachedEvent implements GovernanceEvent {
+    
+    private final String cacheId;
+    
+    private final String schemaName;
+    
+    private final Collection<RuleConfiguration> ruleConfigurations;
+}