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 93d6bdaaa26 Refactor DataSourceStateException (#21282)
93d6bdaaa26 is described below

commit 93d6bdaaa2662f9efeb351eced9a4ac4c862598c
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Sep 30 15:32:20 2022 +0800

    Refactor DataSourceStateException (#21282)
---
 .../user-manual/error-code/server-error-code.cn.md |  1 +
 .../user-manual/error-code/server-error-code.en.md |  1 +
 .../datasource/state/DataSourceStateManager.java   | 22 ++++++++++------------
 ...on.java => UnavailableDataSourceException.java} | 10 +++++++---
 4 files changed, 19 insertions(+), 15 deletions(-)

diff --git 
a/docs/document/content/user-manual/error-code/server-error-code.cn.md 
b/docs/document/content/user-manual/error-code/server-error-code.cn.md
index 97b53732e36..6f3373b102f 100644
--- a/docs/document/content/user-manual/error-code/server-error-code.cn.md
+++ b/docs/document/content/user-manual/error-code/server-error-code.cn.md
@@ -9,6 +9,7 @@ chapter = true
 | 错误码                 | 错误信息 |
 | --------------------- | ------ |
 | SPI-00001             | No implementation class load from SPI \`%s\` with 
type \`%s\` |
+| DATA-SOURCE-00001     | Data source unavailable. |
 | PROPS-00001           | Value \`%s\` of \`%s\` cannot convert to type \`%s\` 
|
 | PROXY-00001           | Load database server info failed |
 | SPRING-00001          | Can not find JNDI data source |
diff --git 
a/docs/document/content/user-manual/error-code/server-error-code.en.md 
b/docs/document/content/user-manual/error-code/server-error-code.en.md
index cb2db60143d..c8023598eae 100644
--- a/docs/document/content/user-manual/error-code/server-error-code.en.md
+++ b/docs/document/content/user-manual/error-code/server-error-code.en.md
@@ -9,6 +9,7 @@ Unique codes provided when server exception occur, which 
printed by Proxy backen
 | Error Code            | Reason |
 | --------------------- | ------ |
 | SPI-00001             | No implementation class load from SPI \`%s\` with 
type \`%s\` |
+| DATA-SOURCE-00001     | Data source unavailable. |
 | PROPS-00001           | Value \`%s\` of \`%s\` cannot convert to type \`%s\` 
|
 | PROXY-00001           | Load database server info failed |
 | SPRING-00001          | Can not find JNDI data source |
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceStateManager.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceStateManager.java
index c59d8225676..223903358a1 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceStateManager.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceStateManager.java
@@ -21,7 +21,8 @@ import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
-import 
org.apache.shardingsphere.infra.datasource.state.exception.DataSourceStateException;
+import 
org.apache.shardingsphere.infra.datasource.state.exception.UnavailableDataSourceException;
+import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
@@ -35,15 +36,15 @@ import java.util.concurrent.ConcurrentHashMap;
 /**
  * Data source state manager.
  */
-@Slf4j
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
+@Slf4j
 public final class DataSourceStateManager {
     
     private static final DataSourceStateManager INSTANCE = new 
DataSourceStateManager();
     
     private final Map<String, DataSourceState> dataSourceStates = new 
ConcurrentHashMap<>();
     
-    private volatile boolean force;
+    private volatile boolean forceStart;
     
     private volatile boolean initialized;
     
@@ -62,10 +63,10 @@ public final class DataSourceStateManager {
      * @param databaseName database name
      * @param dataSources data sources
      * @param storageDataSourceStates storage node data source state
-     * @param force whether to force start
+     * @param forceStart whether to force start
      */
-    public void initStates(final String databaseName, final Map<String, 
DataSource> dataSources, final Map<String, DataSourceState> 
storageDataSourceStates, final boolean force) {
-        this.force = force;
+    public void initStates(final String databaseName, final Map<String, 
DataSource> dataSources, final Map<String, DataSourceState> 
storageDataSourceStates, final boolean forceStart) {
+        this.forceStart = forceStart;
         dataSources.forEach((key, value) -> initState(databaseName, 
storageDataSourceStates, key, value));
         initialized = true;
     }
@@ -83,11 +84,8 @@ public final class DataSourceStateManager {
         try (Connection ignored = dataSource.getConnection()) {
             dataSourceStates.put(getCacheKey(databaseName, 
actualDataSourceName), DataSourceState.ENABLED);
         } catch (final SQLException ex) {
-            if (this.force) {
-                log.error("Data source state unavailable, ignored with the -f 
parameter.", ex);
-            } else {
-                throw new DataSourceStateException("DataSourceState", 1, "Data 
source state unavailable.", ex);
-            }
+            ShardingSpherePreconditions.checkState(forceStart, 
UnavailableDataSourceException::new);
+            log.error("Data source unavailable, ignored with the -f 
parameter.", ex);
         }
     }
     
@@ -130,7 +128,7 @@ public final class DataSourceStateManager {
     }
     
     private void checkForceConnection(final Map<String, DataSource> 
dataSources) {
-        if (force) {
+        if (forceStart) {
             dataSources.entrySet().removeIf(entry -> {
                 try (Connection ignored = entry.getValue().getConnection()) {
                     return false;
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/exception/DataSourceStateException.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/exception/UnavailableDataSourceException.java
similarity index 77%
rename from 
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/exception/DataSourceStateException.java
rename to 
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/exception/UnavailableDataSourceException.java
index f9caffa25a0..c6dba88bb57 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/exception/DataSourceStateException.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/state/exception/UnavailableDataSourceException.java
@@ -22,11 +22,15 @@ import 
org.apache.shardingsphere.infra.util.exception.external.server.ShardingSp
 /**
  * Data source state exception.
  */
-public final class DataSourceStateException extends 
ShardingSphereServerException {
+public final class UnavailableDataSourceException extends 
ShardingSphereServerException {
     
     private static final long serialVersionUID = -8058761885303180333L;
     
-    public DataSourceStateException(final String errorCategory, final int 
errorCode, final String message, final Exception cause) {
-        super(errorCategory, errorCode, message, cause);
+    private static final String ERROR_CATEGORY = "DATA-SOURCE";
+    
+    private static final int ERROR_CODE = 1;
+    
+    public UnavailableDataSourceException() {
+        super(ERROR_CATEGORY, ERROR_CODE, "Data source unavailable.");
     }
 }

Reply via email to