You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2021/05/25 11:22:01 UTC

[shardingsphere] branch master updated: Move grant statement to GlobalRuleRegistryService (#10464)

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

duanzhengqiang 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 ce0a29c  Move grant statement to GlobalRuleRegistryService (#10464)
ce0a29c is described below

commit ce0a29c4af523ceda2717f1a38149c66753b5951
Author: Liang Zhang <te...@163.com>
AuthorDate: Tue May 25 19:21:34 2021 +0800

    Move grant statement to GlobalRuleRegistryService (#10464)
---
 .../governance/core/registry/RegistryCenter.java   | 55 ----------------------
 .../config/impl/GlobalRuleRegistryService.java     | 55 ++++++++++++++++++++++
 .../core/registry/RegistryCenterTest.java          | 14 +++---
 3 files changed, 62 insertions(+), 62 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 7522154..d682323 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
@@ -17,12 +17,9 @@
 
 package org.apache.shardingsphere.governance.core.registry;
 
-import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
 import com.google.common.base.Strings;
-import com.google.common.eventbus.Subscribe;
 import lombok.Getter;
-import org.apache.shardingsphere.authority.api.config.AuthorityRuleConfiguration;
 import org.apache.shardingsphere.governance.core.registry.instance.GovernanceInstance;
 import org.apache.shardingsphere.governance.core.registry.service.config.impl.DataSourceRegistryService;
 import org.apache.shardingsphere.governance.core.registry.service.config.impl.GlobalRuleRegistryService;
@@ -37,19 +34,11 @@ import org.apache.shardingsphere.governance.repository.spi.RegistryCenterReposit
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
 import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
-import org.apache.shardingsphere.infra.metadata.mapper.event.dcl.impl.CreateUserStatementEvent;
-import org.apache.shardingsphere.infra.metadata.mapper.event.dcl.impl.GrantStatementEvent;
-import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
-import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUsers;
-import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUsersConfigurationConverter;
-import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.LinkedList;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Optional;
 import java.util.Properties;
 
 /**
@@ -146,50 +135,6 @@ public final class RegistryCenter {
     }
     
     /**
-     * Renew create user statement.
-     *
-     * @param event create user statement event
-     */
-    @Subscribe
-    public void renew(final CreateUserStatementEvent event) {
-        Collection<RuleConfiguration> globalRuleConfigs = globalRuleService.load();
-        Optional<AuthorityRuleConfiguration> authorityRuleConfig = globalRuleConfigs.stream().filter(each -> each instanceof AuthorityRuleConfiguration)
-                .findAny().map(each -> (AuthorityRuleConfiguration) each);
-        Preconditions.checkState(authorityRuleConfig.isPresent());
-        refreshAuthorityRuleConfiguration(authorityRuleConfig.get(), event.getUsers());
-        globalRuleService.persist(globalRuleConfigs, true);
-    }
-    
-    /**
-     * User with changed privilege event.
-     *
-     * @param event grant event
-     */
-    @Subscribe
-    public void renew(final GrantStatementEvent event) {
-        if (!event.getUsers().isEmpty()) {
-            repository.persist(node.getPrivilegeNodePath(), YamlEngine.marshal(YamlUsersConfigurationConverter.convertYamlUserConfigurations(event.getUsers())));
-        }
-    }
-    
-    private void refreshAuthorityRuleConfiguration(final AuthorityRuleConfiguration authRuleConfig, final Collection<ShardingSphereUser> createUsers) {
-        Collection<ShardingSphereUser> oldUsers = authRuleConfig.getUsers();
-        Collection<ShardingSphereUser> newUsers = oldUsers.isEmpty() ? createUsers : getChangedShardingSphereUsers(oldUsers, createUsers);
-        authRuleConfig.getUsers().removeAll(oldUsers);
-        authRuleConfig.getUsers().addAll(newUsers);
-    }
-    
-    private Collection<ShardingSphereUser> getChangedShardingSphereUsers(final Collection<ShardingSphereUser> oldUsers, final Collection<ShardingSphereUser> newUsers) {
-        Collection<ShardingSphereUser> result = new LinkedList<>(oldUsers);
-        ShardingSphereUsers shardingSphereUsers = new ShardingSphereUsers(oldUsers);
-        for (ShardingSphereUser each : newUsers) {
-            shardingSphereUsers.findUser(each.getGrantee()).ifPresent(result::remove);
-            result.add(each);
-        }
-        return result;
-    }
-    
-    /**
      * Register instance online.
      */
     public void registerInstanceOnline() {
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 9930cbf..263c420 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
@@ -17,16 +17,26 @@
 
 package org.apache.shardingsphere.governance.core.registry.service.config.impl;
 
+import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
+import com.google.common.eventbus.Subscribe;
+import org.apache.shardingsphere.authority.api.config.AuthorityRuleConfiguration;
 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;
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.infra.metadata.mapper.event.dcl.impl.CreateUserStatementEvent;
+import org.apache.shardingsphere.infra.metadata.mapper.event.dcl.impl.GrantStatementEvent;
+import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
+import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUsers;
+import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUsersConfigurationConverter;
 import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
 import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
 
 import java.util.Collection;
 import java.util.Collections;
+import java.util.LinkedList;
+import java.util.Optional;
 
 /**
  * Global rule registry service.
@@ -59,4 +69,49 @@ public final class GlobalRuleRegistryService implements GlobalRegistryService<Co
     private boolean isExisted() {
         return !Strings.isNullOrEmpty(repository.get(node.getGlobalRuleNode()));
     }
+    
+    
+    /**
+     * Update when user created.
+     *
+     * @param event create user statement event
+     */
+    @Subscribe
+    public void update(final CreateUserStatementEvent event) {
+        Collection<RuleConfiguration> globalRuleConfigs = load();
+        Optional<AuthorityRuleConfiguration> authorityRuleConfig = globalRuleConfigs.stream().filter(each -> each instanceof AuthorityRuleConfiguration)
+                .findAny().map(each -> (AuthorityRuleConfiguration) each);
+        Preconditions.checkState(authorityRuleConfig.isPresent());
+        refreshAuthorityRuleConfiguration(authorityRuleConfig.get(), event.getUsers());
+        persist(globalRuleConfigs, true);
+    }
+    
+    /**
+     * Update when granted.
+     *
+     * @param event grant statement event
+     */
+    @Subscribe
+    public void update(final GrantStatementEvent event) {
+        if (!event.getUsers().isEmpty()) {
+            repository.persist(node.getPrivilegeNodePath(), YamlEngine.marshal(YamlUsersConfigurationConverter.convertYamlUserConfigurations(event.getUsers())));
+        }
+    }
+    
+    private void refreshAuthorityRuleConfiguration(final AuthorityRuleConfiguration authRuleConfig, final Collection<ShardingSphereUser> createUsers) {
+        Collection<ShardingSphereUser> oldUsers = authRuleConfig.getUsers();
+        Collection<ShardingSphereUser> newUsers = oldUsers.isEmpty() ? createUsers : getChangedShardingSphereUsers(oldUsers, createUsers);
+        authRuleConfig.getUsers().removeAll(oldUsers);
+        authRuleConfig.getUsers().addAll(newUsers);
+    }
+    
+    private Collection<ShardingSphereUser> getChangedShardingSphereUsers(final Collection<ShardingSphereUser> oldUsers, final Collection<ShardingSphereUser> newUsers) {
+        Collection<ShardingSphereUser> result = new LinkedList<>(oldUsers);
+        ShardingSphereUsers shardingSphereUsers = new ShardingSphereUsers(oldUsers);
+        for (ShardingSphereUser each : newUsers) {
+            shardingSphereUsers.findUser(each.getGrantee()).ifPresent(result::remove);
+            result.add(each);
+        }
+        return result;
+    }
 }
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 5cfc2d8..5233531 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
@@ -91,6 +91,13 @@ public final class RegistryCenterTest {
     }
     
     @Test
+    public void assertInitNodes() {
+        registryCenter.initNodes();
+        verify(registryCenterRepository).persist("/states/datanodes", "");
+        verify(registryCenterRepository).persist("/states/primarynodes", "");
+    }
+    
+    @Test
     public void assertPersistConfigurations() {
         Map<String, DataSourceConfiguration> dataSourceConfigs = createDataSourceConfigurations();
         Collection<RuleConfiguration> schemaRuleConfigs = createRuleConfigurations();
@@ -152,11 +159,4 @@ public final class RegistryCenterTest {
         registryCenter.registerInstanceOnline();
         verify(registryCenterRepository).persistEphemeral(anyString(), anyString());
     }
-    
-    @Test
-    public void assertInitNodes() {
-        registryCenter.initNodes();
-        verify(registryCenterRepository).persist("/states/datanodes", "");
-        verify(registryCenterRepository).persist("/states/primarynodes", "");
-    }
 }