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 cf1b364  Use SPI and rule to load authority (#9936)
cf1b364 is described below

commit cf1b364cb35f15e6034001703b8a9dedffeb963c
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Apr 5 15:06:48 2021 +0800

    Use SPI and rule to load authority (#9936)
    
    * Refactor AuthorityOrder
    
    * Refactor AuthorityOrder
    
    * Refactor PrivilegeBuilder
---
 .../authority/constant/AuthorityOrder.java         |  2 +-
 .../AuthorityRuleConfigurationYamlSwapper.java     |  2 +-
 ...dingsphere.authority.spi.PrivilegeLoadAlgorithm | 18 +++++
 .../metadata/auth/builder/PrivilegeBuilder.java    |  4 +-
 .../auth/builder/loader/PrivilegeLoaderEngine.java |  2 +-
 .../loader/dialect/MySQLPrivilegeLoader.java       |  7 +-
 .../loader/dialect/MySQLPrivilegeLoaderTest.java   | 86 +++++++++++-----------
 .../shardingsphere-proxy-backend/pom.xml           |  5 ++
 .../shardingsphere-proxy-common/pom.xml            |  5 ++
 9 files changed, 77 insertions(+), 54 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/constant/AuthorityOrder.java
 
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/constant/AuthorityOrder.java
index bf378cf..339d894 100644
--- 
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/constant/AuthorityOrder.java
+++ 
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/constant/AuthorityOrder.java
@@ -34,5 +34,5 @@ public final class AuthorityOrder {
     /**
      * Algorithm provider authority order.
      */
-    public static final int ALGORITHM_PROVIDER_AUTHORITY_ORDER = 501;
+    public static final int ALGORITHM_PROVIDER_AUTHORITY_ORDER = ORDER + 1;
 }
diff --git 
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/AuthorityRuleConfigurationYamlSwapper.java
 
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/AuthorityRuleConfigurationYamlSwapper.java
index 7c23f55..7e1ff37 100644
--- 
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/AuthorityRuleConfigurationYamlSwapper.java
+++ 
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/AuthorityRuleConfigurationYamlSwapper.java
@@ -63,7 +63,7 @@ public final class AuthorityRuleConfigurationYamlSwapper 
implements YamlRuleConf
     
     @Override
     public String getRuleTagName() {
-        return "Authority";
+        return "AUTHORITY";
     }
     
     @Override
diff --git 
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/resources/META-INF/services/org.apache.shardingsphere.authority.spi.PrivilegeLoadAlgorithm
 
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/resources/META-INF/services/org.apache.shardingsphere.authority.spi.PrivilegeLoadAlgorithm
new file mode 100644
index 0000000..9f45f85
--- /dev/null
+++ 
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/resources/META-INF/services/org.apache.shardingsphere.authority.spi.PrivilegeLoadAlgorithm
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.authority.loader.StoragePrivilegeLoadAlgorithm
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 add2cc4..a3a29f7 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
@@ -103,7 +103,7 @@ public final class PrivilegeBuilder {
     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()));
+        ExecutorService executorService = 
Executors.newFixedThreadPool(Math.min(CPU_CORES * 2, dataSources.isEmpty() ? 1 
: 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)));
@@ -147,7 +147,7 @@ public final class PrivilegeBuilder {
         for (Entry<ShardingSphereUser, Collection<ShardingSpherePrivilege>> 
entry : userPrivilegeMap.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()));
+                    throw new ShardingSphereException(String.format("There is 
no enough privileges for %s on all database instances.", 
entry.getKey().getGrantee().toString().replaceAll("%", "%%")));
                 }
             }
         }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builder/loader/PrivilegeLoaderEngine.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builder/loader/PrivilegeLoaderEngine.java
index d400af7..6793c95 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builder/loader/PrivilegeLoaderEngine.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builder/loader/PrivilegeLoaderEngine.java
@@ -31,7 +31,7 @@ import java.util.Optional;
 public final class PrivilegeLoaderEngine {
     
     static {
-        ShardingSphereServiceLoader.newServiceInstances(PrivilegeLoader.class);
+        ShardingSphereServiceLoader.register(PrivilegeLoader.class);
     }
     
     /**
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builder/loader/dialect/MySQLPrivilegeLoader.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builder/loader/dialect/MySQLPrivilegeLoader.java
index ecc608a..565c428 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builder/loader/dialect/MySQLPrivilegeLoader.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builder/loader/dialect/MySQLPrivilegeLoader.java
@@ -126,19 +126,18 @@ public final class MySQLPrivilegeLoader implements 
PrivilegeLoader {
     }
     
     private String getGlobalPrivilegeSQL(final Collection<ShardingSphereUser> 
users) {
-        String userHostTuples = users.stream().map(each -> String.format("(%s, 
%s)", each.getGrantee().getUsername(), each.getGrantee().getHostname()))
-                .collect(Collectors.joining(","));
+        String userHostTuples = users.stream().map(each -> 
String.format("('%s', '%s')", each.getGrantee().getUsername(), 
each.getGrantee().getHostname())).collect(Collectors.joining(","));
         return String.format(GLOBAL_PRIVILEGE_SQL, userHostTuples);
     }
     
     private String getSchemaPrivilegeSQL(final Collection<ShardingSphereUser> 
users) {
-        String userHostTuples = users.stream().map(each -> String.format("(%s, 
%s)", each.getGrantee().getUsername(), each.getGrantee().getHostname()))
+        String userHostTuples = users.stream().map(each -> 
String.format("('%s', '%s')", each.getGrantee().getUsername(), 
each.getGrantee().getHostname()))
                 .collect(Collectors.joining(","));
         return String.format(SCHEMA_PRIVILEGE_SQL, userHostTuples);
     }
     
     private String getTablePrivilegeSQL(final Collection<ShardingSphereUser> 
users) {
-        String userHostTuples = users.stream().map(each -> String.format("(%s, 
%s)", each.getGrantee().getUsername(), each.getGrantee().getHostname()))
+        String userHostTuples = users.stream().map(each -> 
String.format("('%s', '%s')", each.getGrantee().getUsername(), 
each.getGrantee().getHostname()))
                 .collect(Collectors.joining(","));
         return String.format(TABLE_PRIVILEGE_SQL, userHostTuples);
     }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/auth/builder/loader/dialect/MySQLPrivilegeLoaderTest.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/auth/builder/loader/dialect/MySQLPrivilegeLoaderTest.java
index 9284f60..d252d49 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/auth/builder/loader/dialect/MySQLPrivilegeLoaderTest.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/auth/builder/loader/dialect/MySQLPrivilegeLoaderTest.java
@@ -24,8 +24,6 @@ import 
org.apache.shardingsphere.infra.metadata.auth.model.user.ShardingSphereUs
 import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
 import org.junit.BeforeClass;
 import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
 
 import javax.sql.DataSource;
 import java.sql.ResultSet;
@@ -39,64 +37,46 @@ import java.util.stream.Collectors;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertEquals;
-import static org.mockito.Mockito.when;
-import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
-@RunWith(MockitoJUnitRunner.class)
 public final class MySQLPrivilegeLoaderTest {
-
+    
     @BeforeClass
     public static void setUp() {
         ShardingSphereServiceLoader.register(PrivilegeLoader.class);
     }
-
+    
     @Test
     public void assertLoad() throws SQLException {
-        Collection<ShardingSphereUser> users = buildUsers();
+        Collection<ShardingSphereUser> users = createUsers();
         DataSource dataSource = mockDataSource(users);
         assertPrivilege(getPrivilegeLoader().load(users, dataSource));
     }
-
-    private void assertPrivilege(final Map<ShardingSphereUser, 
ShardingSpherePrivilege> actual) {
-        assertThat(actual.size(), is(2));
-        ShardingSphereUser root = new ShardingSphereUser("root", "", 
"localhost");
-        
assertThat(actual.get(root).getAdministrativePrivilege().getPrivileges().size(),
 is(3));
-        Collection<PrivilegeType> expectedAdministrativePrivilege = new 
CopyOnWriteArraySet(Arrays.asList(PrivilegeType.SUPER, PrivilegeType.RELOAD, 
PrivilegeType.SHUTDOWN));
-        
assertEquals(actual.get(root).getAdministrativePrivilege().getPrivileges(), 
expectedAdministrativePrivilege);
-        Collection<PrivilegeType> expectedDatabasePrivilege = new 
CopyOnWriteArraySet(Arrays.asList(PrivilegeType.SELECT, PrivilegeType.INSERT, 
PrivilegeType.UPDATE,
-                PrivilegeType.DELETE, PrivilegeType.CREATE, 
PrivilegeType.ALTER));
-        
assertThat(actual.get(root).getDatabasePrivilege().getGlobalPrivileges().size(),
 is(6));
-        
assertEquals(actual.get(root).getDatabasePrivilege().getGlobalPrivileges(), 
expectedDatabasePrivilege);
-        ShardingSphereUser sys = new ShardingSphereUser("mysql.sys", "", 
"localhost");
-        
assertThat(actual.get(sys).getAdministrativePrivilege().getPrivileges().size(), 
is(0));
-        
assertThat(actual.get(sys).getDatabasePrivilege().getGlobalPrivileges().size(), 
is(0));
-        
assertThat(actual.get(sys).getDatabasePrivilege().getSpecificPrivileges().size(),
 is(1));
-    }
-
-    private Collection<ShardingSphereUser> buildUsers() {
-        LinkedList<ShardingSphereUser> users = new LinkedList<>();
-        users.add(new ShardingSphereUser("root", "", "localhost"));
-        users.add(new ShardingSphereUser("mysql.sys", "", "localhost"));
-        return users;
+    
+    private Collection<ShardingSphereUser> createUsers() {
+        LinkedList<ShardingSphereUser> result = new LinkedList<>();
+        result.add(new ShardingSphereUser("root", "", "localhost"));
+        result.add(new ShardingSphereUser("mysql.sys", "", "localhost"));
+        return result;
     }
-
+    
     private DataSource mockDataSource(final Collection<ShardingSphereUser> 
users) throws SQLException {
         ResultSet globalPrivilegeResultSet = mockGlobalPrivilegeResultSet();
         ResultSet schemaPrivilegeResultSet = mockSchemaPrivilegeResultSet();
         ResultSet tablePrivilegeResultSet = mockTablePrivilegeResultSet();
-        DataSource dataSource = mock(DataSource.class, RETURNS_DEEP_STUBS);
-        String globalPrivilegeSql = "SELECT * FROM mysql.user WHERE (user, 
host) in (%s)";
-        String schemaPrivilegeSql = "SELECT * FROM mysql.db WHERE (user, host) 
in (%s)";
-        String tablePrivilegeSql = "SELECT Db, Table_name, Table_priv FROM 
mysql.tables_priv WHERE (user, host) in (%s)";
-        String useHostTuples = users.stream().map(item -> String.format("(%s, 
%s)", item.getGrantee().getUsername(), 
item.getGrantee().getHostname())).collect(Collectors.joining(","));
-        
when(dataSource.getConnection().createStatement().executeQuery(String.format(globalPrivilegeSql,
 useHostTuples))).thenReturn(globalPrivilegeResultSet);
-        
when(dataSource.getConnection().createStatement().executeQuery(String.format(schemaPrivilegeSql,
 useHostTuples))).thenReturn(schemaPrivilegeResultSet);
-        
when(dataSource.getConnection().createStatement().executeQuery(String.format(tablePrivilegeSql,
 useHostTuples))).thenReturn(tablePrivilegeResultSet);
-        return dataSource;
+        DataSource result = mock(DataSource.class, RETURNS_DEEP_STUBS);
+        String globalPrivilegeSQL = "SELECT * FROM mysql.user WHERE (user, 
host) in (%s)";
+        String schemaPrivilegeSQL = "SELECT * FROM mysql.db WHERE (user, host) 
in (%s)";
+        String tablePrivilegeSQL = "SELECT Db, Table_name, Table_priv FROM 
mysql.tables_priv WHERE (user, host) in (%s)";
+        String useHostTuples = users.stream().map(item -> 
String.format("('%s', '%s')", item.getGrantee().getUsername(), 
item.getGrantee().getHostname())).collect(Collectors.joining(","));
+        
when(result.getConnection().createStatement().executeQuery(String.format(globalPrivilegeSQL,
 useHostTuples))).thenReturn(globalPrivilegeResultSet);
+        
when(result.getConnection().createStatement().executeQuery(String.format(schemaPrivilegeSQL,
 useHostTuples))).thenReturn(schemaPrivilegeResultSet);
+        
when(result.getConnection().createStatement().executeQuery(String.format(tablePrivilegeSQL,
 useHostTuples))).thenReturn(tablePrivilegeResultSet);
+        return result;
     }
-
+    
     private ResultSet mockGlobalPrivilegeResultSet() throws SQLException {
         ResultSet result = mock(ResultSet.class);
         when(result.next()).thenReturn(true, true, false);
@@ -135,7 +115,7 @@ public final class MySQLPrivilegeLoaderTest {
         when(result.getString("host")).thenReturn("localhost", "localhost");
         return result;
     }
-
+    
     private ResultSet mockSchemaPrivilegeResultSet() throws SQLException {
         ResultSet result = mock(ResultSet.class);
         when(result.next()).thenReturn(true, false);
@@ -163,7 +143,7 @@ public final class MySQLPrivilegeLoaderTest {
         when(result.getString("host")).thenReturn("localhost");
         return result;
     }
-
+    
     private ResultSet mockTablePrivilegeResultSet() throws SQLException {
         ResultSet result = mock(ResultSet.class, RETURNS_DEEP_STUBS);
         when(result.next()).thenReturn(true, false);
@@ -174,7 +154,23 @@ public final class MySQLPrivilegeLoaderTest {
         when(result.getString("host")).thenReturn("localhost");
         return result;
     }
-
+    
+    private void assertPrivilege(final Map<ShardingSphereUser, 
ShardingSpherePrivilege> actual) {
+        assertThat(actual.size(), is(2));
+        ShardingSphereUser root = new ShardingSphereUser("root", "", 
"localhost");
+        
assertThat(actual.get(root).getAdministrativePrivilege().getPrivileges().size(),
 is(3));
+        Collection<PrivilegeType> expectedAdministrativePrivileges = new 
CopyOnWriteArraySet<>(Arrays.asList(PrivilegeType.SUPER, PrivilegeType.RELOAD, 
PrivilegeType.SHUTDOWN));
+        
assertThat(actual.get(root).getAdministrativePrivilege().getPrivileges(), 
is(expectedAdministrativePrivileges));
+        Collection<PrivilegeType> expectedDatabasePrivileges = new 
CopyOnWriteArraySet<>(
+                Arrays.asList(PrivilegeType.SELECT, PrivilegeType.INSERT, 
PrivilegeType.UPDATE, PrivilegeType.DELETE, PrivilegeType.CREATE, 
PrivilegeType.ALTER));
+        
assertThat(actual.get(root).getDatabasePrivilege().getGlobalPrivileges().size(),
 is(6));
+        
assertThat(actual.get(root).getDatabasePrivilege().getGlobalPrivileges(), 
is(expectedDatabasePrivileges));
+        ShardingSphereUser sys = new ShardingSphereUser("mysql.sys", "", 
"localhost");
+        
assertThat(actual.get(sys).getAdministrativePrivilege().getPrivileges().size(), 
is(0));
+        
assertThat(actual.get(sys).getDatabasePrivilege().getGlobalPrivileges().size(), 
is(0));
+        
assertThat(actual.get(sys).getDatabasePrivilege().getSpecificPrivileges().size(),
 is(1));
+    }
+    
     private PrivilegeLoader getPrivilegeLoader() {
         for (PrivilegeLoader each : 
ShardingSphereServiceLoader.getSingletonServiceInstances(PrivilegeLoader.class))
 {
             if ("MySQL".equals(each.getDatabaseType())) {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml 
b/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml
index 9210129..cdcbc4d 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml
@@ -109,6 +109,11 @@
         </dependency>
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-authority-common</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
             <artifactId>shardingsphere-transaction-xa-core</artifactId>
             <version>${project.parent.version}</version>
         </dependency>
diff --git a/shardingsphere-proxy/shardingsphere-proxy-common/pom.xml 
b/shardingsphere-proxy/shardingsphere-proxy-common/pom.xml
index ce0c548..6c728a5 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-common/pom.xml
+++ b/shardingsphere-proxy/shardingsphere-proxy-common/pom.xml
@@ -57,6 +57,11 @@
             <artifactId>shardingsphere-db-discovery-common</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-authority-common</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         
         <dependency>
             <groupId>com.zaxxer</groupId>

Reply via email to