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

panjuan 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 5ba23e6  Move RuleConfigurationsAlteredEvent to 
SchemaRuleRegistryService (#10432)
5ba23e6 is described below

commit 5ba23e6524d136d8bfcefb17bd825aeb6c6e2837
Author: Liang Zhang <[email protected]>
AuthorDate: Sat May 22 16:27:15 2021 +0800

    Move RuleConfigurationsAlteredEvent to SchemaRuleRegistryService (#10432)
    
    * For code format
    
    * Move RuleConfigurationsAlteredEvent to SchemaRuleRegistryService
---
 .../governance/core/registry/RegistryCenter.java   | 11 -----------
 .../config/impl/GlobalRuleRegistryService.java     |  9 ++++++---
 .../config/impl/SchemaRuleRegistryService.java     | 23 +++++++++++++++++++---
 .../core/registry/RegistryCenterTest.java          |  8 --------
 .../config/impl/SchemaRuleRegistryServiceTest.java | 10 ++++++++++
 .../state/DataSourceStatusRegistryServiceTest.java |  4 ++--
 6 files changed, 38 insertions(+), 27 deletions(-)

diff --git 
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenter.java
 
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenter.java
index ea7ff71..b570628 100644
--- 
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenter.java
+++ 
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenter.java
@@ -33,7 +33,6 @@ import 
org.apache.shardingsphere.governance.core.registry.listener.event.invocat
 import 
org.apache.shardingsphere.governance.core.registry.listener.event.metadata.MetaDataCreatedEvent;
 import 
org.apache.shardingsphere.governance.core.registry.listener.event.metadata.MetaDataDroppedEvent;
 import 
org.apache.shardingsphere.governance.core.registry.listener.event.rule.RuleConfigurationCachedEvent;
-import 
org.apache.shardingsphere.governance.core.registry.listener.event.rule.RuleConfigurationsAlteredEvent;
 import 
org.apache.shardingsphere.governance.core.registry.listener.event.rule.SwitchRuleConfigurationEvent;
 import 
org.apache.shardingsphere.governance.core.registry.listener.event.scaling.StartScalingEvent;
 import 
org.apache.shardingsphere.governance.core.registry.service.config.impl.DataSourceRegistryService;
@@ -165,16 +164,6 @@ public final class RegistryCenter {
     }
     
     /**
-     * Persist rule configurations.
-     *
-     * @param event rule configurations altered event
-     */
-    @Subscribe
-    public void renew(final RuleConfigurationsAlteredEvent event) {
-        schemaRuleService.persist(event.getSchemaName(), 
event.getRuleConfigurations());
-    }
-    
-    /**
      * Persist meta data.
      *
      * @param event meta data created event
diff --git 
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/service/config/impl/GlobalRuleRegistryService.java
 
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/service/config/impl/GlobalRuleRegistryService.java
index 09d7bdc..9930cbf 100644
--- 
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/service/config/impl/GlobalRuleRegistryService.java
+++ 
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/service/config/impl/GlobalRuleRegistryService.java
@@ -18,7 +18,6 @@
 package org.apache.shardingsphere.governance.core.registry.service.config.impl;
 
 import com.google.common.base.Strings;
-import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.governance.core.registry.RegistryCenterNode;
 import 
org.apache.shardingsphere.governance.core.registry.service.config.GlobalRegistryService;
 import 
org.apache.shardingsphere.governance.repository.spi.RegistryCenterRepository;
@@ -32,12 +31,16 @@ import java.util.Collections;
 /**
  * Global rule registry service.
  */
-@RequiredArgsConstructor
 public final class GlobalRuleRegistryService implements 
GlobalRegistryService<Collection<RuleConfiguration>> {
     
     private final RegistryCenterRepository repository;
     
-    private final RegistryCenterNode node = new RegistryCenterNode();
+    private final RegistryCenterNode node;
+    
+    public GlobalRuleRegistryService(final RegistryCenterRepository 
repository) {
+        this.repository = repository;
+        node = new RegistryCenterNode();
+    }
     
     @Override
     public void persist(final Collection<RuleConfiguration> globalRuleConfigs, 
final boolean isOverwrite) {
diff --git 
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/service/config/impl/SchemaRuleRegistryService.java
 
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/service/config/impl/SchemaRuleRegistryService.java
index 88ba489..9c8d4e1 100644
--- 
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/service/config/impl/SchemaRuleRegistryService.java
+++ 
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/service/config/impl/SchemaRuleRegistryService.java
@@ -18,13 +18,15 @@
 package org.apache.shardingsphere.governance.core.registry.service.config.impl;
 
 import com.google.common.base.Strings;
-import lombok.RequiredArgsConstructor;
+import com.google.common.eventbus.Subscribe;
 import org.apache.shardingsphere.governance.core.registry.RegistryCenterNode;
 import 
org.apache.shardingsphere.governance.core.registry.checker.RuleConfigurationChecker;
 import 
org.apache.shardingsphere.governance.core.registry.checker.RuleConfigurationCheckerFactory;
+import 
org.apache.shardingsphere.governance.core.registry.listener.event.rule.RuleConfigurationsAlteredEvent;
 import 
org.apache.shardingsphere.governance.core.registry.service.config.SchemaBasedRegistryService;
 import 
org.apache.shardingsphere.governance.repository.spi.RegistryCenterRepository;
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
 import org.apache.shardingsphere.infra.yaml.config.YamlRuleConfiguration;
 import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
 import 
org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
@@ -36,12 +38,17 @@ import java.util.Optional;
 /**
  * Schema rule registry service.
  */
-@RequiredArgsConstructor
 public final class SchemaRuleRegistryService implements 
SchemaBasedRegistryService<Collection<RuleConfiguration>> {
     
     private final RegistryCenterRepository repository;
     
-    private final RegistryCenterNode node = new RegistryCenterNode();
+    private final RegistryCenterNode node;
+    
+    public SchemaRuleRegistryService(final RegistryCenterRepository 
repository) {
+        this.repository = repository;
+        node = new RegistryCenterNode();
+        ShardingSphereEventBus.getInstance().register(this);
+    }
     
     @Override
     public void persist(final String schemaName, final 
Collection<RuleConfiguration> configs, final boolean isOverwrite) {
@@ -78,4 +85,14 @@ public final class SchemaRuleRegistryService implements 
SchemaBasedRegistryServi
     public boolean isExisted(final String schemaName) {
         return 
!Strings.isNullOrEmpty(repository.get(node.getRulePath(schemaName)));
     }
+    
+    /**
+     * Update rule configurations for alter.
+     *
+     * @param event rule configurations altered event
+     */
+    @Subscribe
+    public void update(final RuleConfigurationsAlteredEvent event) {
+        persist(event.getSchemaName(), event.getRuleConfigurations());
+    }
 }
diff --git 
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterTest.java
 
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterTest.java
index 878d618..c1df1a7 100644
--- 
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterTest.java
+++ 
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterTest.java
@@ -20,7 +20,6 @@ package org.apache.shardingsphere.governance.core.registry;
 import lombok.SneakyThrows;
 import 
org.apache.shardingsphere.governance.core.registry.listener.event.metadata.MetaDataCreatedEvent;
 import 
org.apache.shardingsphere.governance.core.registry.listener.event.metadata.MetaDataDroppedEvent;
-import 
org.apache.shardingsphere.governance.core.registry.listener.event.rule.RuleConfigurationsAlteredEvent;
 import 
org.apache.shardingsphere.governance.core.registry.listener.event.rule.SwitchRuleConfigurationEvent;
 import 
org.apache.shardingsphere.governance.core.registry.service.config.impl.DataSourceRegistryService;
 import 
org.apache.shardingsphere.governance.core.registry.service.config.impl.GlobalRuleRegistryService;
@@ -176,13 +175,6 @@ public final class RegistryCenterTest {
     }
     
     @Test
-    public void assertRenewRuleEvent() {
-        RuleConfigurationsAlteredEvent event = new 
RuleConfigurationsAlteredEvent("sharding_db", createRuleConfigurations());
-        registryCenter.renew(event);
-        verify(schemaRuleService).persist(event.getSchemaName(), 
event.getRuleConfigurations());
-    }
-    
-    @Test
     public void assertRenewSchemaNameEventWithDrop() {
         MetaDataDroppedEvent event = new MetaDataDroppedEvent("sharding_db");
         
when(registryCenterRepository.get("/metadata")).thenReturn("sharding_db,replica_query_db");
diff --git 
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/service/config/impl/SchemaRuleRegistryServiceTest.java
 
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/service/config/impl/SchemaRuleRegistryServiceTest.java
index 6600240..395c986 100644
--- 
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/service/config/impl/SchemaRuleRegistryServiceTest.java
+++ 
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/service/config/impl/SchemaRuleRegistryServiceTest.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.governance.core.registry.service.config.impl;
 
 import lombok.SneakyThrows;
+import 
org.apache.shardingsphere.governance.core.registry.listener.event.rule.RuleConfigurationsAlteredEvent;
 import 
org.apache.shardingsphere.governance.repository.spi.RegistryCenterRepository;
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
@@ -33,10 +34,12 @@ import java.net.URISyntaxException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.stream.Collectors;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
@@ -72,4 +75,11 @@ public final class SchemaRuleRegistryServiceTest {
         return 
Files.readAllLines(Paths.get(ClassLoader.getSystemResource(yamlFile).toURI()))
                 .stream().filter(each -> !each.startsWith("#")).map(each -> 
each + System.lineSeparator()).collect(Collectors.joining());
     }
+    
+    @Test
+    public void assertUpdate() {
+        RuleConfigurationsAlteredEvent event = new 
RuleConfigurationsAlteredEvent("sharding_db", Collections.emptyList());
+        schemaRuleRegistryService.update(event);
+        
verify(registryCenterRepository).persist("/metadata/sharding_db/rules", "!!map 
[]\n");
+    }
 }
diff --git 
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/service/state/DataSourceStatusRegistryServiceTest.java
 
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/service/state/DataSourceStatusRegistryServiceTest.java
index 2f32e47..3525429 100644
--- 
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/service/state/DataSourceStatusRegistryServiceTest.java
+++ 
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/service/state/DataSourceStatusRegistryServiceTest.java
@@ -70,7 +70,7 @@ public final class DataSourceStatusRegistryServiceTest {
     public void assertUpdateDataSourceEnabledState() {
         assertUpdateDataSourceState(false, "");
     }
-
+    
     private void assertUpdateDataSourceState(final boolean isDisabled, final 
String value) {
         String schemaName = "replica_query_db";
         String dataSourceName = "replica_ds_0";
@@ -78,7 +78,7 @@ public final class DataSourceStatusRegistryServiceTest {
         dataSourceStatusRegistryService.update(dataSourceDisabledEvent);
         verify(registryCenterRepository).persist(new 
RegistryCenterNode().getDataSourcePath(schemaName, dataSourceName), value);
     }
-
+    
     @Test
     public void assertUpdatePrimaryDataSourceState() {
         String schemaName = "replica_query_db";

Reply via email to