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 1466196d1f2 Refactor UsedDataSourceProvider (#25499)
1466196d1f2 is described below

commit 1466196d1f250cab9c03b6173cb5d0af1e5234c6
Author: Liang Zhang <[email protected]>
AuthorDate: Sat May 6 19:49:55 2023 +0800

    Refactor UsedDataSourceProvider (#25499)
    
    * Rename UsedDataSourceProvider
    
    * Rename UsedDataSourceProvider
---
 .../engine/type/unicast/ShardingUnicastRoutingEngine.java    |  4 ++--
 .../shardingsphere/infra/connection/ConnectionContext.java   | 12 ++++++------
 ...edDataSourceProvider.java => UsedDataSourceProvider.java} | 11 ++++++-----
 .../sql/prepare/driver/DatabaseConnectionManager.java        |  8 --------
 .../core/connection/DriverDatabaseConnectionManager.java     |  7 +------
 .../backend/connector/ProxyDatabaseConnectionManager.java    |  8 ++++++--
 .../proxy/backend/session/ConnectionSession.java             |  3 +--
 .../connector/ProxyDatabaseConnectionManagerTest.java        |  2 +-
 8 files changed, 23 insertions(+), 32 deletions(-)

diff --git 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngine.java
 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngine.java
index 3f7711e67d8..40ef5955949 100644
--- 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngine.java
+++ 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/unicast/ShardingUnicastRoutingEngine.java
@@ -116,8 +116,8 @@ public final class ShardingUnicastRoutingEngine implements 
ShardingRouteEngine {
     }
     
     private String getRandomDataSourceName(final Collection<String> 
dataSourceNames) {
-        Collection<String> preferredDataSourceNames = 
connectionContext.getPreferredDataSourceNames();
-        List<String> availableDataSourceNames = new 
ArrayList<>(preferredDataSourceNames.isEmpty() ? dataSourceNames : 
preferredDataSourceNames);
+        Collection<String> usedDataSourceNames = 
connectionContext.getUsedDataSourceNames();
+        List<String> availableDataSourceNames = new 
ArrayList<>(usedDataSourceNames.isEmpty() ? dataSourceNames : 
usedDataSourceNames);
         return 
availableDataSourceNames.get(ThreadLocalRandom.current().nextInt(availableDataSourceNames.size()));
     }
 }
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/connection/ConnectionContext.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/connection/ConnectionContext.java
index 1b29195ee39..3ab2b0399dc 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/connection/ConnectionContext.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/connection/ConnectionContext.java
@@ -22,7 +22,7 @@ import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 import lombok.Setter;
 import 
org.apache.shardingsphere.infra.connection.cursor.CursorConnectionContext;
-import 
org.apache.shardingsphere.infra.connection.datasource.PreferredDataSourceProvider;
+import 
org.apache.shardingsphere.infra.connection.datasource.UsedDataSourceProvider;
 import 
org.apache.shardingsphere.infra.connection.transaction.TransactionConnectionContext;
 
 import java.util.Collection;
@@ -41,7 +41,7 @@ public final class ConnectionContext implements AutoCloseable 
{
     private final TransactionConnectionContext transactionContext = new 
TransactionConnectionContext();
     
     @Getter(AccessLevel.NONE)
-    private final PreferredDataSourceProvider preferredDataSourceProvider;
+    private final UsedDataSourceProvider usedDataSourceProvider;
     
     @Setter
     private String trafficInstanceId;
@@ -51,12 +51,12 @@ public final class ConnectionContext implements 
AutoCloseable {
     }
     
     /**
-     * Get preferred data source names.
+     * Get used data source names.
      *
-     * @return preferred data source names
+     * @return used data source names
      */
-    public Collection<String> getPreferredDataSourceNames() {
-        return preferredDataSourceProvider.getPreferredDataSourceNames();
+    public Collection<String> getUsedDataSourceNames() {
+        return usedDataSourceProvider.getNames();
     }
     
     /**
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/connection/datasource/PreferredDataSourceProvider.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/connection/datasource/UsedDataSourceProvider.java
similarity index 81%
rename from 
infra/common/src/main/java/org/apache/shardingsphere/infra/connection/datasource/PreferredDataSourceProvider.java
rename to 
infra/common/src/main/java/org/apache/shardingsphere/infra/connection/datasource/UsedDataSourceProvider.java
index ed0df49c63d..204dba59d49 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/connection/datasource/PreferredDataSourceProvider.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/connection/datasource/UsedDataSourceProvider.java
@@ -20,14 +20,15 @@ package 
org.apache.shardingsphere.infra.connection.datasource;
 import java.util.Collection;
 
 /**
- * Preferred data source provider.
+ * Used data source provider.
  */
-public interface PreferredDataSourceProvider {
+@FunctionalInterface
+public interface UsedDataSourceProvider {
     
     /**
-     * Get preferred data source names.
+     * Get used data source names.
      *
-     * @return preferred data source names
+     * @return used data source names
      */
-    Collection<String> getPreferredDataSourceNames();
+    Collection<String> getNames();
 }
diff --git 
a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/driver/DatabaseConnectionManager.java
 
b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/driver/DatabaseConnectionManager.java
index 2bbcb171526..88883d1bb1a 100644
--- 
a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/driver/DatabaseConnectionManager.java
+++ 
b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/driver/DatabaseConnectionManager.java
@@ -20,7 +20,6 @@ package 
org.apache.shardingsphere.infra.executor.sql.prepare.driver;
 import 
org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode;
 
 import java.sql.SQLException;
-import java.util.Collection;
 import java.util.List;
 
 /**
@@ -40,11 +39,4 @@ public interface DatabaseConnectionManager<C> {
      * @throws SQLException SQL exception
      */
     List<C> getConnections(String dataSourceName, int connectionSize, 
ConnectionMode connectionMode) throws SQLException;
-    
-    /**
-     * Get data source names of cached connections.
-     *
-     * @return data source names of cached connections
-     */
-    Collection<String> getDataSourceNamesOfCachedConnections();
 }
diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
index c62e1906020..452df77d0b3 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
@@ -87,7 +87,7 @@ public final class DriverDatabaseConnectionManager implements 
DatabaseConnection
         dataSourceMap.putAll(getTrafficDataSourceMap(databaseName, 
contextManager));
         
physicalDataSourceMap.putAll(contextManager.getDataSourceMap(databaseName));
         connectionTransaction = createConnectionTransaction(databaseName, 
contextManager);
-        connectionContext = new 
ConnectionContext(this::getDataSourceNamesOfCachedConnections);
+        connectionContext = new ConnectionContext(cachedConnections::keySet);
     }
     
     private Map<String, DataSource> getTrafficDataSourceMap(final String 
databaseName, final ContextManager contextManager) {
@@ -370,11 +370,6 @@ public final class DriverDatabaseConnectionManager 
implements DatabaseConnection
         return physicalDataSourceMap.containsKey(dataSourceName);
     }
     
-    @Override
-    public Collection<String> getDataSourceNamesOfCachedConnections() {
-        return cachedConnections.keySet();
-    }
-    
     @Override
     public void close() throws SQLException {
         try {
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/ProxyDatabaseConnectionManager.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/ProxyDatabaseConnectionManager.java
index e15c612e415..00e8103a2e4 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/ProxyDatabaseConnectionManager.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/ProxyDatabaseConnectionManager.java
@@ -167,8 +167,12 @@ public final class ProxyDatabaseConnectionManager 
implements DatabaseConnectionM
         }
     }
     
-    @Override
-    public Collection<String> getDataSourceNamesOfCachedConnections() {
+    /**
+     * Get used data source names.
+     * 
+     * @return used data source names
+     */
+    public Collection<String> getUsedDataSourceNames() {
         Collection<String> result = new ArrayList<>(cachedConnections.size());
         String databaseName = 
connectionSession.getDatabaseName().toLowerCase();
         for (String each : cachedConnections.keySet()) {
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/session/ConnectionSession.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/session/ConnectionSession.java
index f2493de3ec5..a04d30ce1c5 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/session/ConnectionSession.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/session/ConnectionSession.java
@@ -24,7 +24,6 @@ import lombok.Setter;
 import org.apache.shardingsphere.infra.binder.QueryContext;
 import org.apache.shardingsphere.infra.connection.ConnectionContext;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import 
org.apache.shardingsphere.infra.executor.sql.prepare.driver.DatabaseConnectionManager;
 import 
org.apache.shardingsphere.infra.executor.sql.prepare.driver.ExecutorStatementManager;
 import org.apache.shardingsphere.infra.metadata.user.Grantee;
 import 
org.apache.shardingsphere.proxy.backend.connector.ProxyDatabaseConnectionManager;
@@ -81,7 +80,7 @@ public final class ConnectionSession {
         this.attributeMap = attributeMap;
         databaseConnectionManager = new ProxyDatabaseConnectionManager(this);
         statementManager = new JDBCBackendStatement();
-        connectionContext = new 
ConnectionContext(((DatabaseConnectionManager<?>) 
databaseConnectionManager)::getDataSourceNamesOfCachedConnections);
+        connectionContext = new 
ConnectionContext(databaseConnectionManager::getUsedDataSourceNames);
     }
     
     /**
diff --git 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxyDatabaseConnectionManagerTest.java
 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxyDatabaseConnectionManagerTest.java
index 1cb9463f68a..1386f575ec3 100644
--- 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxyDatabaseConnectionManagerTest.java
+++ 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxyDatabaseConnectionManagerTest.java
@@ -422,7 +422,7 @@ class ProxyDatabaseConnectionManagerTest {
         
databaseConnectionManager.getCachedConnections().put(connectionSession.getDatabaseName()
 + ".ds_0", null);
         
databaseConnectionManager.getCachedConnections().put(connectionSession.getDatabaseName()
 + ".ds_1", null);
         
databaseConnectionManager.getCachedConnections().put(connectionSession.getDatabaseName()
 + ".ds_2", null);
-        List<String> actual = new 
ArrayList<>(databaseConnectionManager.getDataSourceNamesOfCachedConnections());
+        List<String> actual = new 
ArrayList<>(databaseConnectionManager.getUsedDataSourceNames());
         Collections.sort(actual);
         assertThat(actual, is(Arrays.asList("ds_0", "ds_1", "ds_2")));
     }

Reply via email to