You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2023/06/25 05:34:35 UTC

[shardingsphere] branch master updated: Initialize dataSource state only once (#26532)

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

duanzhengqiang 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 251c60f2bf7 Initialize dataSource state only once (#26532)
251c60f2bf7 is described below

commit 251c60f2bf706368a83713f4b77f1240b9d6d460
Author: ZhangCheng <ch...@apache.org>
AuthorDate: Sun Jun 25 13:34:28 2023 +0800

    Initialize dataSource state only once (#26532)
    
    * Initialize dataSource state only once
    
    * fix
---
 .../infra/datasource/state/DataSourceStateManager.java         | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceStateManager.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceStateManager.java
index b43531b5310..b2d27b38de9 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceStateManager.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/state/DataSourceStateManager.java
@@ -32,6 +32,7 @@ import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 /**
  * Data source state manager.
@@ -46,7 +47,7 @@ public final class DataSourceStateManager {
     
     private volatile boolean forceStart;
     
-    private volatile boolean initialized;
+    private final AtomicBoolean initialized = new AtomicBoolean(false);
     
     /**
      * Get data source state manager.
@@ -67,8 +68,9 @@ public final class DataSourceStateManager {
      */
     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;
+        if (initialized.compareAndSet(false, true)) {
+            dataSources.forEach((key, value) -> initState(databaseName, storageDataSourceStates, key, value));
+        }
     }
     
     private void initState(final String databaseName, final Map<String, DataSourceState> storageDataSourceStates, final String actualDataSourceName, final DataSource dataSource) {
@@ -108,7 +110,7 @@ public final class DataSourceStateManager {
      * @return enabled data source map
      */
     public Map<String, DataSource> getEnabledDataSourceMap(final String databaseName, final Map<String, DataSource> dataSources) {
-        if (dataSources.isEmpty() || !initialized) {
+        if (dataSources.isEmpty() || !initialized.get()) {
             return dataSources;
         }
         Map<String, DataSource> result = filterDisabledDataSources(databaseName, dataSources);