You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2021/02/08 07:53:53 UTC

[shardingsphere] branch master updated: Optimize MetadataRefreshEngine (#9385)

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

menghaoran 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 6c03616  Optimize MetadataRefreshEngine (#9385)
6c03616 is described below

commit 6c03616baeb49ae9d039afefec59d2bd085b7ce5
Author: Juan Pan(Trista) <pa...@apache.org>
AuthorDate: Mon Feb 8 15:53:29 2021 +0800

    Optimize MetadataRefreshEngine (#9385)
    
    * Optimize MetadataRefreshEngine
    
    * add equal()
---
 .../infra/auth/builtin/DefaultAuthentication.java     | 10 ++++------
 .../builtin/yaml/swapper/UserRuleYamlSwapper.java     |  4 ++--
 .../shardingsphere/infra/auth/user/Grantee.java       | 19 +++++++++++++++++--
 .../infra/auth/user/ShardingSphereUser.java           | 18 ++++++------------
 .../infra/metadata/engine/MetadataRefreshEngine.java  |  3 ++-
 .../metadata/engine/MetadataRefresherFactory.java     |  2 +-
 .../privilege/refresher/AuthenticationRefresher.java  |  8 ++++----
 .../refresher/type/GrantStatementAuthRefresher.java   |  4 ++--
 8 files changed, 38 insertions(+), 30 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/DefaultAuthentication.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/DefaultAuthentication.java
index 3b75531..7904961 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/DefaultAuthentication.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/DefaultAuthentication.java
@@ -17,18 +17,17 @@
 
 package org.apache.shardingsphere.infra.auth.builtin;
 
-import com.google.common.base.Strings;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.infra.auth.Authentication;
+import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
 import org.apache.shardingsphere.infra.auth.user.Grantee;
 import org.apache.shardingsphere.infra.auth.user.ShardingSphereUser;
-import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
 
 import java.util.Collection;
-import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * Default authentication.
@@ -37,7 +36,7 @@ import java.util.Optional;
 @Getter
 public final class DefaultAuthentication implements Authentication {
     
-    private final Map<ShardingSphereUser, ShardingSpherePrivilege> authentication = new LinkedHashMap<>();
+    private final Map<ShardingSphereUser, ShardingSpherePrivilege> authentication = new ConcurrentHashMap<>();
     
     public DefaultAuthentication(final Collection<ShardingSphereUser> users) {
         for (ShardingSphereUser each : users) {
@@ -53,8 +52,7 @@ public final class DefaultAuthentication implements Authentication {
     
     @Override
     public Optional<ShardingSphereUser> findUser(final Grantee grantee) {
-        return authentication.keySet().stream().filter(each -> each.getUsername().equals(grantee.getUsername())
-                && (each.getHostname().equals(grantee.getHostname()) || Strings.isNullOrEmpty(each.getHostname()))).findFirst();
+        return authentication.keySet().stream().filter(each -> each.getGrantee().equals(grantee)).findFirst();
     }
     
     @Override
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserRuleYamlSwapper.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserRuleYamlSwapper.java
index bb9e64f..46ed788 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserRuleYamlSwapper.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserRuleYamlSwapper.java
@@ -38,7 +38,7 @@ public final class UserRuleYamlSwapper implements YamlSwapper<YamlUserRuleConfig
         YamlUserRuleConfiguration result = new YamlUserRuleConfiguration();
         Map<String, YamlUserConfiguration> users = new LinkedHashMap<>(data.size(), 1);
         for (ShardingSphereUser each : data) {
-            users.put(each.getUsername(), swapToYamlConfiguration(each));
+            users.put(each.getGrantee().getUsername(), swapToYamlConfiguration(each));
         }
         result.setUsers(users);
         return result;
@@ -46,7 +46,7 @@ public final class UserRuleYamlSwapper implements YamlSwapper<YamlUserRuleConfig
     
     private YamlUserConfiguration swapToYamlConfiguration(final ShardingSphereUser data) {
         YamlUserConfiguration result = new YamlUserConfiguration();
-        result.setHostname(data.getHostname());
+        result.setHostname(data.getGrantee().getHostname());
         result.setPassword(data.getPassword());
         return result;
     }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/user/Grantee.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/user/Grantee.java
index b5d545f..5aefa5d 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/user/Grantee.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/user/Grantee.java
@@ -17,7 +17,8 @@
 
 package org.apache.shardingsphere.infra.auth.user;
 
-import lombok.EqualsAndHashCode;
+import com.google.common.base.Objects;
+import com.google.common.base.Strings;
 import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 
@@ -26,10 +27,24 @@ import lombok.RequiredArgsConstructor;
  */
 @RequiredArgsConstructor
 @Getter
-@EqualsAndHashCode
 public final class Grantee {
     
     private final String username;
     
     private final String hostname;
+    
+    @Override
+    public boolean equals(final Object obj) {
+        if (obj instanceof Grantee) {
+            Grantee grantee = (Grantee) obj;
+            return grantee.getUsername().equalsIgnoreCase(username) && (grantee.getHostname().equalsIgnoreCase(hostname) || Strings.isNullOrEmpty(hostname));
+        }
+        return false;
+    }
+    
+    @Override
+    public int hashCode() {
+        return Strings.isNullOrEmpty(hostname)
+                ? Objects.hashCode(username.toUpperCase()) : Objects.hashCode(username.toUpperCase(), hostname.toUpperCase());
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/user/ShardingSphereUser.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/user/ShardingSphereUser.java
index 5dd8324..8f722c0 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/user/ShardingSphereUser.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/user/ShardingSphereUser.java
@@ -17,28 +17,22 @@
 
 package org.apache.shardingsphere.infra.auth.user;
 
+import lombok.EqualsAndHashCode;
 import lombok.Getter;
-import lombok.RequiredArgsConstructor;
 
 /**
  * ShardingSphere user.
  */
-@RequiredArgsConstructor
 @Getter
+@EqualsAndHashCode(of = "grantee")
 public final class ShardingSphereUser {
     
-    private final String username;
+    private final Grantee grantee;
     
     private final String password;
     
-    private final String hostname;
-    
-    /**
-     * Get grantee.
-     *
-     * @return grantee
-     */
-    public Grantee getGrantee() {
-        return new Grantee(username, hostname);
+    public ShardingSphereUser(final String username, final String password, final String hostname) {
+        this.grantee = new Grantee(username, hostname);
+        this.password = password;
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/engine/MetadataRefreshEngine.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/engine/MetadataRefreshEngine.java
index f740f5c..398f60e 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/engine/MetadataRefreshEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/engine/MetadataRefreshEngine.java
@@ -57,6 +57,7 @@ public final class MetadataRefreshEngine {
      * @param routeDataSourceNames route data source names
      * @throws SQLException SQL exception
      */
+    @SuppressWarnings("rawtypes")
     public void refresh(final SQLStatement sqlStatement, final Collection<String> routeDataSourceNames) throws SQLException {
         Optional<MetadataRefresher> metadataRefresher = MetadataRefresherFactory.newInstance(sqlStatement);
         if (metadataRefresher.isPresent()) {
@@ -77,7 +78,7 @@ public final class MetadataRefreshEngine {
     }
     
     private void refreshAuthentication(final SQLStatement sqlStatement, final AuthenticationRefresher refresher) {
-        refresher.refresh(authentication, sqlStatement, materials);
+        refresher.refresh(authentication, sqlStatement, metaData);
         // TODO :Subscribe and handle this event
         ShardingSphereEventBus.getInstance().post(new AuthenticationAlteredEvent(authentication));
     }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/engine/MetadataRefresherFactory.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/engine/MetadataRefresherFactory.java
index b5c8bfb..e20c780 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/engine/MetadataRefresherFactory.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/engine/MetadataRefresherFactory.java
@@ -63,7 +63,7 @@ public final class MetadataRefresherFactory {
     
     /**
      * Create new instance of schema refresher.
-     *
+     *†
      * @param sqlStatement SQL statement
      * @return instance of schema refresher
      */
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/privilege/refresher/AuthenticationRefresher.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/privilege/refresher/AuthenticationRefresher.java
index 4de1716..a7da77b 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/privilege/refresher/AuthenticationRefresher.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/privilege/refresher/AuthenticationRefresher.java
@@ -18,12 +18,12 @@
 package org.apache.shardingsphere.infra.metadata.privilege.refresher;
 
 import org.apache.shardingsphere.infra.auth.Authentication;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.infra.metadata.engine.MetadataRefresher;
-import org.apache.shardingsphere.infra.metadata.schema.builder.SchemaBuilderMaterials;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
 /**
- * Privilege refresher.
+ * Authentication refresher.
  */
 public interface AuthenticationRefresher extends MetadataRefresher {
     
@@ -32,7 +32,7 @@ public interface AuthenticationRefresher extends MetadataRefresher {
      *
      * @param authentication authentication
      * @param sqlStatement sql statement
-     * @param materials materials
+     * @param metaData metaData
      */
-    void refresh(Authentication authentication, SQLStatement sqlStatement, SchemaBuilderMaterials materials);
+    void refresh(Authentication authentication, SQLStatement sqlStatement, ShardingSphereMetaData metaData);
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/privilege/refresher/type/GrantStatementAuthRefresher.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/privilege/refresher/type/GrantStatementAuthRefresher.java
index 7ae578c..88a2344 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/privilege/refresher/type/GrantStatementAuthRefresher.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/privilege/refresher/type/GrantStatementAuthRefresher.java
@@ -18,8 +18,8 @@
 package org.apache.shardingsphere.infra.metadata.privilege.refresher.type;
 
 import org.apache.shardingsphere.infra.auth.Authentication;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.infra.metadata.privilege.refresher.AuthenticationRefresher;
-import org.apache.shardingsphere.infra.metadata.schema.builder.SchemaBuilderMaterials;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
 /**
@@ -28,6 +28,6 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 public final class GrantStatementAuthRefresher implements AuthenticationRefresher {
     
     @Override
-    public void refresh(final Authentication authentication, final SQLStatement sqlStatement, final SchemaBuilderMaterials materials) {
+    public void refresh(final Authentication authentication, final SQLStatement sqlStatement, final ShardingSphereMetaData metaData) {
     }
 }