You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/04/10 14:07:54 UTC

[shardingsphere] branch master updated: Revise GovernanceAuthorityContext to reuse AuthorityProvideAlgorithm (#10030)

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 ee0fe82  Revise GovernanceAuthorityContext to reuse AuthorityProvideAlgorithm (#10030)
ee0fe82 is described below

commit ee0fe821b4a05b574eaeb08e0e295e74c3857549
Author: Liang Zhang <te...@163.com>
AuthorDate: Sat Apr 10 22:07:12 2021 +0800

    Revise GovernanceAuthorityContext to reuse AuthorityProvideAlgorithm (#10030)
---
 .../apache/shardingsphere/authority/rule/AuthorityRule.java    | 10 +++++++---
 .../context/authority/GovernanceAuthorityContext.java          | 10 ++++------
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
index 5d9af8f..49ff750 100644
--- a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
+++ b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
@@ -17,8 +17,9 @@
 
 package org.apache.shardingsphere.authority.rule;
 
-import org.apache.shardingsphere.authority.api.config.AuthorityRuleConfiguration;
+import lombok.Getter;
 import org.apache.shardingsphere.authority.AuthorityContext;
+import org.apache.shardingsphere.authority.api.config.AuthorityRuleConfiguration;
 import org.apache.shardingsphere.authority.spi.AuthorityProvideAlgorithm;
 import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
@@ -32,14 +33,17 @@ import java.util.Map;
 /**
  * Authority rule.
  */
+@Getter
 public final class AuthorityRule implements GlobalRule {
-
+    
     static {
         ShardingSphereServiceLoader.register(AuthorityProvideAlgorithm.class);
     }
     
+    private final AuthorityProvideAlgorithm provider;
+    
     public AuthorityRule(final AuthorityRuleConfiguration config, final Map<String, ShardingSphereMetaData> mataDataMap, final Collection<ShardingSphereUser> users) {
-        AuthorityProvideAlgorithm provider = ShardingSphereAlgorithmFactory.createAlgorithm(config.getProvider(), AuthorityProvideAlgorithm.class);
+        provider = ShardingSphereAlgorithmFactory.createAlgorithm(config.getProvider(), AuthorityProvideAlgorithm.class);
         provider.init(mataDataMap, users);
         AuthorityContext.getInstance().init(provider);
     }
diff --git a/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/authority/GovernanceAuthorityContext.java b/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/authority/GovernanceAuthorityContext.java
index ec888ad..031fea4 100644
--- a/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/authority/GovernanceAuthorityContext.java
+++ b/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/authority/GovernanceAuthorityContext.java
@@ -21,10 +21,9 @@ import com.google.common.base.Preconditions;
 import com.google.common.eventbus.Subscribe;
 import lombok.Setter;
 import org.apache.shardingsphere.authority.AuthorityContext;
-import org.apache.shardingsphere.authority.api.config.AuthorityRuleConfiguration;
+import org.apache.shardingsphere.authority.rule.AuthorityRule;
 import org.apache.shardingsphere.authority.spi.AuthorityProvideAlgorithm;
 import org.apache.shardingsphere.governance.core.event.model.authority.AuthorityChangedEvent;
-import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory;
 import org.apache.shardingsphere.infra.context.metadata.MetaDataAwareEventSubscriber;
 import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
@@ -51,10 +50,9 @@ public final class GovernanceAuthorityContext implements MetaDataAwareEventSubsc
     }
     
     private void reloadAuthority(final Collection<ShardingSphereUser> users) {
-        Optional<AuthorityRuleConfiguration> authorityRuleConfig = metaDataContexts.getGlobalRuleMetaData().getConfigurations().stream().filter(
-            each -> each instanceof AuthorityRuleConfiguration).findAny().map(each -> (AuthorityRuleConfiguration) each);
-        Preconditions.checkState(authorityRuleConfig.isPresent());
-        AuthorityProvideAlgorithm provider = ShardingSphereAlgorithmFactory.createAlgorithm(authorityRuleConfig.get().getProvider(), AuthorityProvideAlgorithm.class);
+        Optional<AuthorityRule> authorityRule = metaDataContexts.getGlobalRuleMetaData().getRules().stream().filter(each -> each instanceof AuthorityRule).findAny().map(each -> (AuthorityRule) each);
+        Preconditions.checkState(authorityRule.isPresent());
+        AuthorityProvideAlgorithm provider = authorityRule.get().getProvider();
         provider.init(metaDataContexts.getMetaDataMap(), users);
         AuthorityContext.getInstance().init(provider);
     }