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

jianglongtao pushed a commit to branch fix-33341
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git

commit 918036da2deacd8c690b89520298e248a32a774f
Author: Raigor <[email protected]>
AuthorDate: Tue Oct 15 21:26:32 2024 +0800

    Optimize force start of proxy (#20)
---
 .../state/datasource/DataSourceStateManager.java   | 22 ++++++++++++++--------
 .../exception/UnavailableDataSourceException.java  |  6 ++----
 .../handler/update/LoadSingleTableExecutor.java    |  2 +-
 3 files changed, 17 insertions(+), 13 deletions(-)

diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/state/datasource/DataSourceStateManager.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/state/datasource/DataSourceStateManager.java
index 30935c38d53..5587a10594f 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/state/datasource/DataSourceStateManager.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/state/datasource/DataSourceStateManager.java
@@ -48,7 +48,7 @@ public final class DataSourceStateManager {
     
     private volatile boolean forceStart;
     
-    private final AtomicBoolean initialized = new AtomicBoolean(false);
+    private final Map<String, AtomicBoolean> databaseInitializedFlags = new 
ConcurrentHashMap<>();
     
     /**
      * Get data source state manager.
@@ -69,8 +69,10 @@ public final class DataSourceStateManager {
      */
     public void initStates(final String databaseName, final Map<String, 
StorageUnit> storageUnits, final Map<String, DataSourceState> 
storageDataSourceStates, final boolean forceStart) {
         this.forceStart = forceStart;
+        AtomicBoolean initialized = 
databaseInitializedFlags.getOrDefault(databaseName, new AtomicBoolean(false));
         if (initialized.compareAndSet(false, true)) {
             storageUnits.forEach((key, value) -> initState(databaseName, 
storageDataSourceStates, key, value.getDataSource()));
+            databaseInitializedFlags.putIfAbsent(databaseName, initialized);
         }
     }
     
@@ -86,9 +88,12 @@ public final class DataSourceStateManager {
     private void checkState(final String databaseName, final String 
actualDataSourceName, final DataSource dataSource) {
         try (Connection ignored = dataSource.getConnection()) {
             dataSourceStates.put(getCacheKey(databaseName, 
actualDataSourceName), DataSourceState.ENABLED);
-        } catch (final SQLException ex) {
-            ShardingSpherePreconditions.checkState(forceStart, () -> new 
UnavailableDataSourceException(actualDataSourceName, ex));
-            log.error("Data source unavailable, ignored with the -f 
parameter.", ex);
+            // CHECKSTYLE:OFF
+        } catch (final Exception ex) {
+            // CHECKSTYLE:ON
+            ShardingSpherePreconditions.checkState(forceStart, () -> new 
UnavailableDataSourceException(databaseName, actualDataSourceName, ex));
+            log.error("Data source `{}` in `{}` is unavailable, ignored with 
the -f parameter.", actualDataSourceName, databaseName, ex);
+            dataSourceStates.put(getCacheKey(databaseName, 
actualDataSourceName), DataSourceState.DISABLED);
         }
     }
     
@@ -113,12 +118,13 @@ public final class DataSourceStateManager {
      * @return enabled data sources
      */
     public Map<String, DataSource> getEnabledDataSources(final String 
databaseName, final Map<String, DataSource> dataSources) {
-        if (dataSources.isEmpty() || !initialized.get()) {
+        if (dataSources.isEmpty() || 
!databaseInitializedFlags.getOrDefault(databaseName, new 
AtomicBoolean(false)).get()) {
             return dataSources;
         }
-        Map<String, DataSource> result = 
filterDisabledDataSources(databaseName, dataSources);
-        checkForceConnection(result);
-        return result;
+        //        Map<String, DataSource> result = 
filterDisabledDataSources(databaseName, dataSources);
+        //        checkForceConnection(result);
+        //        return result;
+        return filterDisabledDataSources(databaseName, dataSources);
     }
     
     private Map<String, DataSource> filterDisabledDataSources(final String 
databaseName, final Map<String, DataSource> dataSources) {
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/state/datasource/exception/UnavailableDataSourceException.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/state/datasource/exception/UnavailableDataSourceException.java
index 888db2dd95f..0272d327353 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/state/datasource/exception/UnavailableDataSourceException.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/state/datasource/exception/UnavailableDataSourceException.java
@@ -19,8 +19,6 @@ package 
org.apache.shardingsphere.infra.state.datasource.exception;
 
 import 
org.apache.shardingsphere.infra.exception.core.external.server.ShardingSphereServerException;
 
-import java.sql.SQLException;
-
 /**
  * Data source state exception.
  */
@@ -32,7 +30,7 @@ public final class UnavailableDataSourceException extends 
ShardingSphereServerEx
     
     private static final int ERROR_CODE = 1;
     
-    public UnavailableDataSourceException(final String dataSourceName, final 
SQLException cause) {
-        super(ERROR_CATEGORY, ERROR_CODE, String.format("Data source '%s' is 
unavailable.", dataSourceName), cause);
+    public UnavailableDataSourceException(final String databaseName, final 
String dataSourceName, final Exception cause) {
+        super(ERROR_CATEGORY, ERROR_CODE, String.format("Data source '%s' in 
database '%s' is unavailable.", dataSourceName, databaseName), cause);
     }
 }
diff --git 
a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutor.java
 
b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutor.java
index a10465d5e6e..feb18446f95 100644
--- 
a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutor.java
+++ 
b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutor.java
@@ -121,7 +121,7 @@ public final class LoadSingleTableExecutor implements 
DatabaseRuleCreateExecutor
             if (!SingleTableConstants.ASTERISK.equals(tableName)) {
                 String storageUnitName = each.getStorageUnitName();
                 
ShardingSpherePreconditions.checkState(actualTableNodes.containsKey(storageUnitName)
 && 
actualTableNodes.get(storageUnitName).get(defaultSchemaName).contains(tableName),
-                        () -> new TableNotFoundException(storageUnitName, 
tableName));
+                        () -> new TableNotFoundException(tableName, 
storageUnitName));
             }
         }
     }

Reply via email to