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

wuweijie 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 be06d73  Remove ConfigurationProperties dependency for 
PrivilegeBuilder (#9931)
be06d73 is described below

commit be06d73aa9a75c6db4121ff6fa33ebf9b992960c
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Apr 4 19:33:44 2021 +0800

    Remove ConfigurationProperties dependency for PrivilegeBuilder (#9931)
    
    * Remove ConfigurationProperties dependency for PrivilegeBuilder
    
    * Refactor PrivilegeBuilder
    
    * Remove useless codes
---
 .../authority/spi/PrivilegeLoadAlgorithm.java      |  4 +--
 .../authority/rule/AuthorityRule.java              |  2 +-
 .../auth/GovernanceAuthenticationContext.java      |  2 +-
 .../metadata/auth/builder/PrivilegeBuilder.java    | 42 ++++++++++------------
 .../context/metadata/MetaDataContextsBuilder.java  | 11 ++----
 5 files changed, 23 insertions(+), 38 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/PrivilegeLoadAlgorithm.java
 
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/PrivilegeLoadAlgorithm.java
index b0f2349..b3784ea 100644
--- 
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/PrivilegeLoadAlgorithm.java
+++ 
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/PrivilegeLoadAlgorithm.java
@@ -18,7 +18,6 @@
 package org.apache.shardingsphere.authority.spi;
 
 import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithm;
-import 
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import 
org.apache.shardingsphere.infra.metadata.auth.model.privilege.ShardingSpherePrivilege;
 import 
org.apache.shardingsphere.infra.metadata.auth.model.user.ShardingSphereUser;
@@ -36,8 +35,7 @@ public interface PrivilegeLoadAlgorithm extends 
ShardingSphereAlgorithm {
      * 
      * @param metaDataList meta data list
      * @param users users
-     * @param props configuration properties
      * @return map of users and privileges
      */
-    Map<ShardingSphereUser, ShardingSpherePrivilege> 
load(Collection<ShardingSphereMetaData> metaDataList, 
Collection<ShardingSphereUser> users, ConfigurationProperties props);
+    Map<ShardingSphereUser, ShardingSpherePrivilege> 
load(Collection<ShardingSphereMetaData> metaDataList, 
Collection<ShardingSphereUser> users);
 }
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 8c68c51..1343353 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
@@ -43,7 +43,7 @@ public final class AuthorityRule implements 
ShardingSphereRule {
         privilegeLoader = 
ShardingSphereAlgorithmFactory.createAlgorithm(config.getPrivilegeLoaders().values().iterator().next(),
 PrivilegeLoadAlgorithm.class);
         Authentication authentication = new DefaultAuthentication();
         // TODO pass correct parameters
-        authentication.init(privilegeLoader.load(null, null, null));
+        authentication.init(privilegeLoader.load(null, null));
         AuthenticationContext.getInstance().init(authentication);
     }
 }
diff --git 
a/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/auth/GovernanceAuthenticationContext.java
 
b/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/auth/GovernanceAuthenticationContext.java
index 546ac0f..67e77be 100644
--- 
a/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/auth/GovernanceAuthenticationContext.java
+++ 
b/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/auth/GovernanceAuthenticationContext.java
@@ -100,7 +100,7 @@ public final class GovernanceAuthenticationContext 
implements MetaDataAwareEvent
         Authentication authentication = 
AuthenticationContext.getInstance().getAuthentication();
         Optional<PrivilegeLoader> loader = 
PrivilegeLoaderEngine.findPrivilegeLoader(metaDataContexts.getMetaDataMap().values().iterator().next().getResource().getDatabaseType());
         if (loader.isPresent()) {
-            Map<ShardingSphereUser, ShardingSpherePrivilege> privileges = 
PrivilegeBuilder.build(metaDataContexts.getMetaDataMap().values(), users, 
metaDataContexts.getProps());
+            Map<ShardingSphereUser, ShardingSpherePrivilege> privileges = 
PrivilegeBuilder.build(metaDataContexts.getMetaDataMap().values(), users);
             
authentication.getAuthentication().putAll(getPrivilegesWithPassword(authentication,
 privileges));
         }
         AuthenticationContext.getInstance().init(authentication);
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builder/PrivilegeBuilder.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builder/PrivilegeBuilder.java
index a931a3f..fd663e9 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builder/PrivilegeBuilder.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builder/PrivilegeBuilder.java
@@ -19,8 +19,6 @@ package org.apache.shardingsphere.infra.metadata.auth.builder;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
-import 
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import 
org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
 import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import 
org.apache.shardingsphere.infra.metadata.auth.builder.loader.PrivilegeLoader;
@@ -30,6 +28,7 @@ import 
org.apache.shardingsphere.infra.metadata.auth.model.user.ShardingSphereUs
 
 import javax.sql.DataSource;
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.Map;
@@ -57,40 +56,36 @@ public final class PrivilegeBuilder {
      *
      * @param metaDataList meta data list
      * @param users users
-     * @param props configuration properties
      * @return privileges
      */
-    public static Map<ShardingSphereUser, ShardingSpherePrivilege> build(final 
Collection<ShardingSphereMetaData> metaDataList,
-                                                                         final 
Collection<ShardingSphereUser> users, final ConfigurationProperties props) {
+    public static Map<ShardingSphereUser, ShardingSpherePrivilege> build(final 
Collection<ShardingSphereMetaData> metaDataList, final 
Collection<ShardingSphereUser> users) {
         if (metaDataList.isEmpty()) {
             return createDefaultPrivileges(users);
         }
         Optional<PrivilegeLoader> loader = 
PrivilegeLoaderEngine.findPrivilegeLoader(metaDataList.iterator().next().getResource().getDatabaseType());
-        return loader.map(optional -> build(metaDataList, users, props, 
optional)).orElseGet(() -> createDefaultPrivileges(users));
+        return loader.map(optional -> build(metaDataList, users, 
optional)).orElseGet(() -> createDefaultPrivileges(users));
     }
     
-    private static Map<ShardingSphereUser, ShardingSpherePrivilege> 
build(final Collection<ShardingSphereMetaData> metaDataList,
-                                                                          
final Collection<ShardingSphereUser> users, final ConfigurationProperties 
props, final PrivilegeLoader loader) {
+    private static Map<ShardingSphereUser, ShardingSpherePrivilege> 
build(final Collection<ShardingSphereMetaData> metaDataList, 
+                                                                          
final Collection<ShardingSphereUser> users, final PrivilegeLoader loader) {
         Map<ShardingSphereUser, ShardingSpherePrivilege> result = new 
LinkedHashMap<>();
         for (ShardingSphereMetaData each : metaDataList) {
-            result.putAll(build(each, users, props, loader));
+            result.putAll(build(each, users, loader));
         }
         return result;
     }
     
-    private static Map<ShardingSphereUser, ShardingSpherePrivilege> 
build(final ShardingSphereMetaData metaData, final 
Collection<ShardingSphereUser> users,
-                                                                          
final ConfigurationProperties props, final PrivilegeLoader loader) {
-        int maxConnectionsSizePerQuery = 
props.getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY);
-        Map<ShardingSphereUser, Collection<ShardingSpherePrivilege>> result = 
build(metaData.getResource().getAllInstanceDataSources(), users, loader, 
maxConnectionsSizePerQuery);
+    private static Map<ShardingSphereUser, ShardingSpherePrivilege> 
build(final ShardingSphereMetaData metaData, final 
Collection<ShardingSphereUser> users, final PrivilegeLoader loader) {
+        Map<ShardingSphereUser, Collection<ShardingSpherePrivilege>> result = 
load(metaData.getResource().getAllInstanceDataSources(), users, loader);
         checkPrivileges(result);
         return PrivilegeMerger.merge(result, metaData.getName(), 
metaData.getRuleMetaData().getRules());
     }
     
-    private static Map<ShardingSphereUser, 
Collection<ShardingSpherePrivilege>> build(final Collection<DataSource> 
dataSources,
-                                                                               
       final Collection<ShardingSphereUser> users, final PrivilegeLoader 
loader, final int maxConnectionsSizePerQuery) {
+    private static Map<ShardingSphereUser, 
Collection<ShardingSpherePrivilege>> load(final Collection<DataSource> 
dataSources, 
+                                                                               
      final Collection<ShardingSphereUser> users, final PrivilegeLoader loader) 
{
         Map<ShardingSphereUser, Collection<ShardingSpherePrivilege>> result = 
new LinkedHashMap<>(users.size(), 1);
-        ExecutorService executorService = 
Executors.newFixedThreadPool(Math.min(CPU_CORES * 2, dataSources.size() * 
maxConnectionsSizePerQuery));
-        Collection<Future<Map<ShardingSphereUser, ShardingSpherePrivilege>>> 
futures = new LinkedHashSet<>(dataSources.size(), 1);
+        ExecutorService executorService = 
Executors.newFixedThreadPool(Math.min(CPU_CORES * 2, dataSources.size()));
+        Collection<Future<Map<ShardingSphereUser, ShardingSpherePrivilege>>> 
futures = new HashSet<>(dataSources.size(), 1);
         for (DataSource each : dataSources) {
             futures.add(executorService.submit(() -> loader.load(users, 
each)));
         }
@@ -105,13 +100,13 @@ public final class PrivilegeBuilder {
         return result;
     }
     
-    private static void fillShardingSpherePrivileges(final 
Map<ShardingSphereUser, Collection<ShardingSpherePrivilege>> result, final 
Future<Map<ShardingSphereUser, ShardingSpherePrivilege>> future)
-            throws InterruptedException, ExecutionException, TimeoutException {
+    private static void fillShardingSpherePrivileges(final 
Map<ShardingSphereUser, Collection<ShardingSpherePrivilege>> userPrivilegeMap, 
+                                                     final 
Future<Map<ShardingSphereUser, ShardingSpherePrivilege>> future) throws 
InterruptedException, ExecutionException, TimeoutException {
         for (Entry<ShardingSphereUser, ShardingSpherePrivilege> entry : 
future.get(FUTURE_GET_TIME_OUT_MILLISECONDS, TimeUnit.MILLISECONDS).entrySet()) 
{
-            if (!result.containsKey(entry.getKey())) {
-                result.put(entry.getKey(), new LinkedHashSet<>());
+            if (!userPrivilegeMap.containsKey(entry.getKey())) {
+                userPrivilegeMap.put(entry.getKey(), new LinkedHashSet<>());
             }
-            result.get(entry.getKey()).add(entry.getValue());
+            userPrivilegeMap.get(entry.getKey()).add(entry.getValue());
         }
     }
     
@@ -127,8 +122,7 @@ public final class PrivilegeBuilder {
         for (Entry<ShardingSphereUser, Collection<ShardingSpherePrivilege>> 
entry : result.entrySet()) {
             for (ShardingSpherePrivilege each : entry.getValue()) {
                 if (each.isEmpty()) {
-                    throw new ShardingSphereException(
-                            String.format("There is no enough privileges for 
%s on all database instances.", entry.getKey().getGrantee().toString()));
+                    throw new ShardingSphereException(String.format("There is 
no enough privileges for %s on all database instances.", 
entry.getKey().getGrantee()));
                 }
             }
         }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilder.java
 
b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilder.java
index caa470b..24f9db2 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilder.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilder.java
@@ -18,7 +18,6 @@
 package org.apache.shardingsphere.infra.context.metadata;
 
 import com.google.common.base.Preconditions;
-import org.apache.shardingsphere.infra.check.SQLChecker;
 import org.apache.shardingsphere.infra.config.DatabaseAccessConfiguration;
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import 
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
@@ -43,7 +42,6 @@ import 
org.apache.shardingsphere.infra.metadata.schema.builder.SchemaBuilder;
 import 
org.apache.shardingsphere.infra.metadata.schema.builder.SchemaBuilderMaterials;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRulesBuilder;
-import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
@@ -73,10 +71,6 @@ public final class MetaDataContextsBuilder {
     
     private final ExecutorEngine executorEngine;
     
-    static {
-        ShardingSphereServiceLoader.newServiceInstances(SQLChecker .class);
-    }
-    
     public MetaDataContextsBuilder(final Map<String, Map<String, DataSource>> 
dataSources, final Map<String, Collection<RuleConfiguration>> ruleConfigs, 
final Properties props) {
         this(dataSources, ruleConfigs, Collections.emptyList(), props);
     }
@@ -159,14 +153,13 @@ public final class MetaDataContextsBuilder {
         }
     }
     
-    private ShardingSphereSchema buildSchema(final DatabaseType databaseType,
-                                             final Map<String, DataSource> 
dataSourceMap, final Collection<ShardingSphereRule> rules) throws SQLException {
+    private ShardingSphereSchema buildSchema(final DatabaseType databaseType, 
final Map<String, DataSource> dataSourceMap, final 
Collection<ShardingSphereRule> rules) throws SQLException {
         return SchemaBuilder.build(new SchemaBuilderMaterials(databaseType, 
dataSourceMap, rules, props));
     }
     
     private Authentication buildAuthentication(final 
Collection<ShardingSphereUser> users, final Map<String, ShardingSphereMetaData> 
metaDataMap) {
         DefaultAuthentication result = new DefaultAuthentication();
-        result.init(PrivilegeBuilder.build(metaDataMap.values(), users, 
props));
+        result.init(PrivilegeBuilder.build(metaDataMap.values(), users));
         return result;
     }
 }

Reply via email to