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

[shardingsphere] branch master updated: Switch rule configuration (#8879)

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

xiaoyu 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 7bcb0b1  Switch rule configuration (#8879)
7bcb0b1 is described below

commit 7bcb0b117e6fd4ff44da4d948563e878545e94ca
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Mon Jan 4 17:32:11 2021 +0800

    Switch rule configuration (#8879)
---
 .../governance/core/config/ConfigCacheManager.java | 28 +++++++++++++++++++++-
 .../governance/core/config/ConfigCenter.java       | 17 +++++++++++++
 .../governance/core/config/ConfigCenterTest.java   | 19 +++++++++++++++
 3 files changed, 63 insertions(+), 1 deletion(-)

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 3b334fc..217b599 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
@@ -46,11 +46,37 @@ public final class ConfigCacheManager {
      */
     public String cache(final String key, final String configuration) {
         String cacheId = getCacheId();
-        repository.persist(Joiner.on(PATH_SEPARATOR).join(key, CACHE_KEY, cacheId), configuration);
+        repository.persist(getCacheKey(key, cacheId), configuration);
         return cacheId;
     }
     
     private String getCacheId() {
         return UUID.randomUUID().toString().replace("-", "");
     }
+    
+    /**
+     * Load cached configuration.
+     * 
+     * @param key key 
+     * @param cacheId cache id
+     * @return cached configuration
+     */
+    public String loadCache(final String key, final String cacheId) {
+        return repository.get(getCacheKey(key, cacheId));
+    }
+    
+    /**
+     * Delete cached configuration.
+     * 
+     * @param key key
+     * @param cacheId cache id
+     */
+    public void deleteCache(final String key, final String cacheId) {
+        repository.delete(getCacheKey(key, cacheId));
+    }
+    
+    private String getCacheKey(final String key, final String cacheId) {
+        return Joiner.on(PATH_SEPARATOR).join(key, CACHE_KEY, 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 32c4bc5..b3b9d1e 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
@@ -26,6 +26,7 @@ import org.apache.shardingsphere.encrypt.algorithm.config.AlgorithmProvidedEncry
 import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
 import org.apache.shardingsphere.governance.core.event.model.datasource.DataSourcePersistEvent;
 import org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationsPersistEvent;
+import org.apache.shardingsphere.governance.core.event.model.rule.SwitchRuleConfigurationEvent;
 import org.apache.shardingsphere.governance.core.event.model.schema.SchemaNamePersistEvent;
 import org.apache.shardingsphere.governance.core.event.model.schema.SchemaPersistEvent;
 import org.apache.shardingsphere.governance.core.yaml.config.YamlDataSourceConfiguration;
@@ -175,6 +176,22 @@ public final class ConfigCenter {
         persistRuleConfigurations(event.getSchemaName(), ruleConfigurations);
     }
     
+    /**
+     * Switch rule configuration.
+     * 
+     * @param event switch rule configuration event
+     */
+    @Subscribe
+    public synchronized void renew(final SwitchRuleConfigurationEvent event) {
+        persistRuleConfigurations(event.getSchemaName(), loadCachedRuleConfigurations(event.getSchemaName(), event.getRuleConfigurationCacheId()));
+        configCacheManager.deleteCache(node.getRulePath(event.getSchemaName()), event.getRuleConfigurationCacheId());
+    }
+    
+    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());
+    }
+    
     private void updateHaDataSourceRuleConfigurations(final PrimaryDataSourceUpdateEvent event, final HARuleConfiguration haRuleConfiguration) {
         Collection<HADataSourceRuleConfiguration> haDataSourceRuleConfigurations = haRuleConfiguration.getDataSources();
         for (HADataSourceRuleConfiguration each : haDataSourceRuleConfigurations) {
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/config/ConfigCenterTest.java b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/config/ConfigCenterTest.java
index a8ae641..5bf5555 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/config/ConfigCenterTest.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/config/ConfigCenterTest.java
@@ -21,6 +21,7 @@ import lombok.SneakyThrows;
 import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
 import org.apache.shardingsphere.governance.core.event.model.datasource.DataSourcePersistEvent;
 import org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationsPersistEvent;
+import org.apache.shardingsphere.governance.core.event.model.rule.SwitchRuleConfigurationEvent;
 import org.apache.shardingsphere.governance.core.event.model.schema.SchemaNamePersistEvent;
 import org.apache.shardingsphere.governance.core.event.model.schema.SchemaPersistEvent;
 import org.apache.shardingsphere.governance.core.yaml.config.schema.YamlSchema;
@@ -50,6 +51,7 @@ import org.mockito.junit.MockitoJUnitRunner;
 
 import javax.sql.DataSource;
 import java.io.IOException;
+import java.lang.reflect.Field;
 import java.net.URISyntaxException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
@@ -102,6 +104,9 @@ public final class ConfigCenterTest {
     @Mock
     private ConfigurationRepository configurationRepository;
     
+    @Mock
+    private ConfigCacheManager configCacheManager;
+    
     @Test
     public void assertPersistConfigurationForShardingRuleWithoutAuthenticationAndIsNotOverwriteAndConfigurationIsExisted() {
         ConfigCenter configCenter = new ConfigCenter(configurationRepository);
@@ -575,4 +580,18 @@ public final class ConfigCenterTest {
         configCenter.deleteSchema("sharding_db");
         verify(configurationRepository).delete(eq("/metadata/sharding_db"));
     }
+    
+    @Test
+    @SneakyThrows
+    public void assertRenewSwitchRuleConfigurationEvent() {
+        ConfigCenter configCenter = new ConfigCenter(configurationRepository);
+        Field field = ConfigCenter.class.getDeclaredField("configCacheManager");
+        field.setAccessible(true);
+        field.set(configCenter, configCacheManager);
+        when(configCacheManager.loadCache(anyString(), eq("testCacheId"))).thenReturn(readYAML(SHARDING_RULE_YAML));
+        SwitchRuleConfigurationEvent event = new SwitchRuleConfigurationEvent("sharding_db", "testCacheId");
+        configCenter.renew(event);
+        verify(configurationRepository).persist(eq("/metadata/sharding_db/rule"), anyString());
+        verify(configCacheManager).deleteCache(eq("/metadata/sharding_db/rule"), eq("testCacheId"));
+    }
 }