You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2021/02/05 06:06:42 UTC
[shardingsphere] branch master updated: Add EmbeddedDatabaseManager
(#9340)
This is an automated email from the ASF dual-hosted git repository.
zhangyonglun 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 7380197 Add EmbeddedDatabaseManager (#9340)
7380197 is described below
commit 7380197886a7c84809b47d781f919215abd2adf2
Author: Liang Zhang <te...@163.com>
AuthorDate: Fri Feb 5 14:06:11 2021 +0800
Add EmbeddedDatabaseManager (#9340)
* Add EmbeddedDatabaseManager
* Remove EmbeddedDatabaseFactory
* Update javadoc
---
.../env/IntegrationTestEnvironment.java | 4 +-
.../env/database/DatabaseEnvironmentManager.java | 39 ------------
.../database/embedded/EmbeddedDatabaseFactory.java | 46 --------------
.../database/embedded/EmbeddedDatabaseManager.java | 73 ++++++++++++++++++++++
4 files changed, 75 insertions(+), 87 deletions(-)
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/IntegrationTestEnvironment.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/IntegrationTestEnvironment.java
index 430c50b..efd4690 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/IntegrationTestEnvironment.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/IntegrationTestEnvironment.java
@@ -23,8 +23,8 @@ import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.database.type.dialect.H2DatabaseType;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
-import org.apache.shardingsphere.test.integration.env.database.DatabaseEnvironmentManager;
import org.apache.shardingsphere.test.integration.env.database.embedded.EmbeddedDatabaseDistributionProperties;
+import org.apache.shardingsphere.test.integration.env.database.embedded.EmbeddedDatabaseManager;
import org.apache.shardingsphere.test.integration.env.datasource.DataSourceEnvironment;
import org.apache.shardingsphere.test.integration.env.props.DatabaseScenarioProperties;
import org.apache.shardingsphere.test.integration.env.props.EnvironmentProperties;
@@ -139,7 +139,7 @@ public final class IntegrationTestEnvironment {
private void createEmbeddedDatabases(final DatabaseType databaseType,
final Map<String, DataSourceEnvironment> dataSourceEnvs, final EmbeddedDatabaseDistributionProperties embeddedDatabaseProps) {
for (Entry<String, DataSourceEnvironment> entry : dataSourceEnvs.entrySet()) {
- DatabaseEnvironmentManager.createEmbeddedDatabase(databaseType, entry.getKey(), embeddedDatabaseProps, entry.getValue().getPort());
+ EmbeddedDatabaseManager.startUp(databaseType, entry.getKey(), embeddedDatabaseProps, entry.getValue().getPort());
}
}
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/database/DatabaseEnvironmentManager.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/database/DatabaseEnvironmentManager.java
index bcccc91..d816e75 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/database/DatabaseEnvironmentManager.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/database/DatabaseEnvironmentManager.java
@@ -23,9 +23,6 @@ import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.database.type.dialect.H2DatabaseType;
import org.apache.shardingsphere.test.integration.env.EnvironmentPath;
import org.apache.shardingsphere.test.integration.env.IntegrationTestEnvironment;
-import org.apache.shardingsphere.test.integration.env.database.embedded.EmbeddedDatabaseDistributionProperties;
-import org.apache.shardingsphere.test.integration.env.database.embedded.EmbeddedDatabase;
-import org.apache.shardingsphere.test.integration.env.database.embedded.EmbeddedDatabaseFactory;
import org.apache.shardingsphere.test.integration.env.datasource.builder.ActualDataSourceBuilder;
import org.h2.tools.RunScript;
@@ -38,10 +35,6 @@ import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
/**
* Database environment manager.
@@ -49,10 +42,6 @@ import java.util.concurrent.locks.ReentrantLock;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DatabaseEnvironmentManager {
- private static final Map<String, EmbeddedDatabase> EMBEDDED_DATABASES_CACHE = new ConcurrentHashMap<>();
-
- private static final Lock DATABASE_RESOURCE_LOCK = new ReentrantLock();
-
/**
* Get database names.
*
@@ -113,32 +102,4 @@ public final class DatabaseEnvironmentManager {
RunScript.execute(connection, reader);
}
}
-
- /**
- * Create embedded database.
- *
- * @param databaseType database type
- * @param scenario scenario
- * @param embeddedDatabaseProps embedded database distribution properties
- * @param port port
- */
- public static void createEmbeddedDatabase(final DatabaseType databaseType, final String scenario, final EmbeddedDatabaseDistributionProperties embeddedDatabaseProps, final int port) {
- String embeddedDatabaseKey = String.join("_", databaseType.getName(), scenario);
- EmbeddedDatabase embeddedDatabase = EMBEDDED_DATABASES_CACHE.get(embeddedDatabaseKey);
- if (null != embeddedDatabase) {
- return;
- }
- DATABASE_RESOURCE_LOCK.lock();
- try {
- embeddedDatabase = EMBEDDED_DATABASES_CACHE.get(embeddedDatabaseKey);
- if (null != embeddedDatabase) {
- return;
- }
- embeddedDatabase = EmbeddedDatabaseFactory.newInstance(databaseType, embeddedDatabaseProps, port);
- embeddedDatabase.start();
- EMBEDDED_DATABASES_CACHE.put(embeddedDatabaseKey, embeddedDatabase);
- } finally {
- DATABASE_RESOURCE_LOCK.unlock();
- }
- }
}
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/database/embedded/EmbeddedDatabaseFactory.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/database/embedded/EmbeddedDatabaseFactory.java
deleted file mode 100644
index a9face2..0000000
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/database/embedded/EmbeddedDatabaseFactory.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.test.integration.env.database.embedded;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
-import org.apache.shardingsphere.test.integration.env.database.embedded.type.MySQLEmbeddedDatabase;
-
-/**
- * Embedded database factory.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class EmbeddedDatabaseFactory {
-
- /**
- * Create new instance of embedded database.
- *
- * @param databaseType database type
- * @param embeddedDatabaseProps embedded database properties
- * @param port database access port
- * @return instance of embedded database
- */
- public static EmbeddedDatabase newInstance(final DatabaseType databaseType, final EmbeddedDatabaseDistributionProperties embeddedDatabaseProps, final int port) {
- if (databaseType instanceof MySQLDatabaseType) {
- return new MySQLEmbeddedDatabase(embeddedDatabaseProps, port);
- }
- throw new UnsupportedOperationException(String.format("Unsupported embedded database type: `%s`", databaseType.getName()));
- }
-}
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/database/embedded/EmbeddedDatabaseManager.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/database/embedded/EmbeddedDatabaseManager.java
new file mode 100644
index 0000000..9b74aff
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/database/embedded/EmbeddedDatabaseManager.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.test.integration.env.database.embedded;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.apache.shardingsphere.test.integration.env.database.embedded.type.MySQLEmbeddedDatabase;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * Embedded database manager.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class EmbeddedDatabaseManager {
+
+ private static final Map<String, EmbeddedDatabase> EMBEDDED_DATABASES_CACHE = new ConcurrentHashMap<>();
+
+ private static final Lock DATABASE_RESOURCE_LOCK = new ReentrantLock();
+
+ /**
+ * Start up embedded database.
+ *
+ * @param databaseType database type
+ * @param scenario scenario
+ * @param embeddedDatabaseProps embedded database distribution properties
+ * @param port port
+ */
+ public static void startUp(final DatabaseType databaseType, final String scenario, final EmbeddedDatabaseDistributionProperties embeddedDatabaseProps, final int port) {
+ String embeddedDatabaseKey = String.join("_", databaseType.getName(), scenario);
+ if (EMBEDDED_DATABASES_CACHE.containsKey(embeddedDatabaseKey)) {
+ return;
+ }
+ DATABASE_RESOURCE_LOCK.lock();
+ try {
+ if (EMBEDDED_DATABASES_CACHE.containsKey(embeddedDatabaseKey)) {
+ return;
+ }
+ EmbeddedDatabase embeddedDatabase = newInstance(databaseType, embeddedDatabaseProps, port);
+ embeddedDatabase.start();
+ EMBEDDED_DATABASES_CACHE.put(embeddedDatabaseKey, embeddedDatabase);
+ } finally {
+ DATABASE_RESOURCE_LOCK.unlock();
+ }
+ }
+
+ private static EmbeddedDatabase newInstance(final DatabaseType databaseType, final EmbeddedDatabaseDistributionProperties embeddedDatabaseProps, final int port) {
+ if (databaseType instanceof MySQLDatabaseType) {
+ return new MySQLEmbeddedDatabase(embeddedDatabaseProps, port);
+ }
+ throw new UnsupportedOperationException(String.format("Unsupported embedded database type: `%s`", databaseType.getName()));
+ }
+}