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 2022/02/24 05:41:19 UTC

[shardingsphere] branch master updated: Refactor StorageContainer.getActualDataSourceMap and getVerificationDataSourceMap (#15605)

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 f92142b  Refactor StorageContainer.getActualDataSourceMap and getVerificationDataSourceMap (#15605)
f92142b is described below

commit f92142ba114615aab06333c4a2c6077d0e7de91e
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Thu Feb 24 13:40:19 2022 +0800

    Refactor StorageContainer.getActualDataSourceMap and getVerificationDataSourceMap (#15605)
    
    * Refactor StorageContainer.getActualDataSourceMap and getVerificationDataSourceMap
    
    * Refactor StorageContainer.getActualDataSourceMap and getVerificationDataSourceMap
    
    * For checkstyle
---
 .../container/atomic/DockerITContainer.java        |  4 +-
 .../atomic/storage/DockerStorageContainer.java     | 43 ++++-------------
 .../atomic/storage/EmbeddedStorageContainer.java   | 54 ++++++++--------------
 .../atomic/storage/impl/PostgreSQLContainer.java   |  5 +-
 .../src/test/resources/env/it-env.properties       |  2 +-
 5 files changed, 34 insertions(+), 74 deletions(-)

diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/DockerITContainer.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/DockerITContainer.java
index e4a98ff..b802968 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/DockerITContainer.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/DockerITContainer.java
@@ -47,7 +47,7 @@ public abstract class DockerITContainer extends GenericContainer<DockerITContain
     public void start() {
         startDependencies();
         super.start();
-        execute();
+        postStart();
     }
     
     private void startDependencies() {
@@ -73,6 +73,6 @@ public abstract class DockerITContainer extends GenericContainer<DockerITContain
                 });
     }
     
-    protected void execute() {
+    protected void postStart() {
     }
 }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/DockerStorageContainer.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/DockerStorageContainer.java
index 2ff6943..e321b1d 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/DockerStorageContainer.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/DockerStorageContainer.java
@@ -18,7 +18,6 @@
 package org.apache.shardingsphere.test.integration.framework.container.atomic.storage;
 
 import com.zaxxer.hikari.HikariDataSource;
-import lombok.AccessLevel;
 import lombok.Getter;
 import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
@@ -31,28 +30,29 @@ import org.testcontainers.utility.MountableFile;
 import javax.sql.DataSource;
 import javax.xml.bind.JAXBException;
 import java.io.IOException;
-import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
 /**
  * Docker storage container.
  */
+@Getter
 public abstract class DockerStorageContainer extends DockerITContainer implements StorageContainer {
     
-    @Getter(AccessLevel.PROTECTED)
     private final DatabaseType databaseType;
     
     private final String scenario;
     
-    private Map<String, DataSource> actualDataSourceMap;
+    private final Map<String, DataSource> actualDataSourceMap;
     
-    private Map<String, DataSource> verificationDataSourceMap;
+    private final Map<String, DataSource> verificationDataSourceMap;
     
     public DockerStorageContainer(final DatabaseType databaseType, final String dockerImageName, final String scenario) {
         super(databaseType.getName().toLowerCase(), dockerImageName);
         this.databaseType = databaseType;
         this.scenario = scenario;
+        actualDataSourceMap = new LinkedHashMap<>();
+        verificationDataSourceMap = new LinkedHashMap<>();
     }
     
     @Override
@@ -63,36 +63,9 @@ public abstract class DockerStorageContainer extends DockerITContainer implement
     
     @Override
     @SneakyThrows({IOException.class, JAXBException.class})
-    public final Map<String, DataSource> getActualDataSourceMap() {
-        if (null != actualDataSourceMap) {
-            return actualDataSourceMap;
-        }
-        synchronized (this) {
-            if (null != actualDataSourceMap) {
-                return actualDataSourceMap;
-            }
-            Collection<String> dataSourceNames = DatabaseEnvironmentManager.getDatabaseNames(scenario);
-            actualDataSourceMap = new LinkedHashMap<>(dataSourceNames.size(), 1);
-            dataSourceNames.forEach(each -> actualDataSourceMap.put(each, createDataSource(each)));
-            return actualDataSourceMap;
-        }
-    }
-    
-    @Override
-    @SneakyThrows({IOException.class, JAXBException.class})
-    public final Map<String, DataSource> getVerificationDataSourceMap() {
-        if (null != verificationDataSourceMap) {
-            return verificationDataSourceMap;
-        }
-        synchronized (this) {
-            if (null != verificationDataSourceMap) {
-                return verificationDataSourceMap;
-            }
-            Collection<String> dataSourceNames = DatabaseEnvironmentManager.getVerificationDatabaseNames(scenario);
-            verificationDataSourceMap = new LinkedHashMap<>(dataSourceNames.size(), 1);
-            dataSourceNames.forEach(each -> verificationDataSourceMap.put(each, createDataSource(each)));
-            return verificationDataSourceMap;
-        }
+    protected void postStart() {
+        DatabaseEnvironmentManager.getDatabaseNames(scenario).forEach(each -> actualDataSourceMap.put(each, createDataSource(each)));
+        DatabaseEnvironmentManager.getVerificationDatabaseNames(scenario).forEach(each -> verificationDataSourceMap.put(each, createDataSource(each)));
     }
     
     private DataSource createDataSource(final String dataSourceName) {
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/EmbeddedStorageContainer.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/EmbeddedStorageContainer.java
index 30e314d..7da4c3b 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/EmbeddedStorageContainer.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/EmbeddedStorageContainer.java
@@ -18,9 +18,7 @@
 package org.apache.shardingsphere.test.integration.framework.container.atomic.storage;
 
 import com.zaxxer.hikari.HikariDataSource;
-import lombok.AccessLevel;
 import lombok.Getter;
-import lombok.RequiredArgsConstructor;
 import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.test.integration.env.DataSourceEnvironment;
@@ -37,50 +35,38 @@ import java.util.Map;
 /**
  * Embedded storage container.
  */
-@RequiredArgsConstructor
-@Getter(AccessLevel.PROTECTED)
+@Getter
 public abstract class EmbeddedStorageContainer implements EmbeddedITContainer, StorageContainer {
     
     private final DatabaseType databaseType;
     
     private final String scenario;
     
-    private Map<String, DataSource> actualDataSourceMap;
+    private final Map<String, DataSource> actualDataSourceMap;
     
-    private Map<String, DataSource> verificationDataSourceMap;
+    private final Map<String, DataSource> verificationDataSourceMap;
+    
+    public EmbeddedStorageContainer(final DatabaseType databaseType, final String scenario) {
+        this.databaseType = databaseType;
+        this.scenario = scenario;
+        actualDataSourceMap = createActualDataSourceMap();
+        verificationDataSourceMap = createVerificationDataSourceMap();
+    }
     
-    @Override
     @SneakyThrows({IOException.class, JAXBException.class})
-    public Map<String, DataSource> getActualDataSourceMap() {
-        if (null != actualDataSourceMap) {
-            return actualDataSourceMap;
-        }
-        synchronized (this) {
-            if (null != actualDataSourceMap) {
-                return actualDataSourceMap;
-            } 
-            Collection<String> dataSourceNames = DatabaseEnvironmentManager.getDatabaseNames(scenario);
-            actualDataSourceMap = new LinkedHashMap<>(dataSourceNames.size(), 1);
-            dataSourceNames.forEach(each -> actualDataSourceMap.put(each, createDataSource(each)));
-            return actualDataSourceMap;
-        }
+    private Map<String, DataSource> createActualDataSourceMap() {
+        Collection<String> databaseNames = DatabaseEnvironmentManager.getDatabaseNames(scenario);
+        Map<String, DataSource> result = new LinkedHashMap<>(databaseNames.size(), 1);
+        databaseNames.forEach(each -> result.put(each, createDataSource(each)));
+        return result;
     }
     
-    @Override
     @SneakyThrows({IOException.class, JAXBException.class})
-    public final Map<String, DataSource> getVerificationDataSourceMap() {
-        if (null != verificationDataSourceMap) {
-            return verificationDataSourceMap;
-        }
-        synchronized (this) {
-            if (null != verificationDataSourceMap) {
-                return verificationDataSourceMap;
-            }
-            Collection<String> dataSourceNames = DatabaseEnvironmentManager.getVerificationDatabaseNames(scenario);
-            verificationDataSourceMap = new LinkedHashMap<>(dataSourceNames.size(), 1);
-            dataSourceNames.forEach(each -> verificationDataSourceMap.put(each, createDataSource(each)));
-            return verificationDataSourceMap;
-        }
+    private Map<String, DataSource> createVerificationDataSourceMap() {
+        Collection<String> databaseNames = DatabaseEnvironmentManager.getVerificationDatabaseNames(scenario);
+        Map<String, DataSource> result = new LinkedHashMap<>(databaseNames.size(), 1);
+        databaseNames.forEach(each -> result.put(each, createDataSource(each)));
+        return result;
     }
     
     private DataSource createDataSource(final String dataSourceName) {
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/impl/PostgreSQLContainer.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/impl/PostgreSQLContainer.java
index 5336f25..847eed5 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/impl/PostgreSQLContainer.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/impl/PostgreSQLContainer.java
@@ -48,8 +48,9 @@ public final class PostgreSQLContainer extends DockerStorageContainer {
     
     @Override
     @SneakyThrows({ClassNotFoundException.class, SQLException.class, InterruptedException.class})
-    // TODO if remove the method, DML and BatchDML run together may throw exception. Need to investigate the reason, it is better to use LogMessageWaitStrategy only
-    protected void execute() {
+    protected void postStart() {
+        super.postStart();
+        // TODO if remove the method, DML and BatchDML run together may throw exception. Need to investigate the reason, it is better to use LogMessageWaitStrategy only
         Class.forName(DataSourceEnvironment.getDriverClassName(getDatabaseType()));
         String url = DataSourceEnvironment.getURL(getDatabaseType(), getHost(), getMappedPort(getPort()));
         boolean connected = false;
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/env/it-env.properties b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/env/it-env.properties
index fa4be74..b3300a3 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/env/it-env.properties
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/env/it-env.properties
@@ -26,7 +26,7 @@ it.scenarios=db,tbl,readwrite_splitting,encrypt,shadow,dbtbl_with_readwrite_spli
 it.cluster.env.type=DOCKER
 
 # it.cluster.adapters=jdbc,proxy
-it.cluster.adapters=jdbc
+it.cluster.adapters=proxy
 
 # it.cluster.databases=MySQL,PostgreSQL
 it.cluster.databases=MySQL