This is an automated email from the ASF dual-hosted git repository.
zhaojinchao 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 27459ffd667 Refactor DataSourcePoolCreator (#28061)
27459ffd667 is described below
commit 27459ffd667c63dec9927b63b78149dac4d798ee
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Aug 13 12:23:51 2023 +0800
Refactor DataSourcePoolCreator (#28061)
* Refactor DataSourcePoolCreator.createStorageResource()
* Refactor DataSourcePoolCreator
* Refactor DataSourcePoolCreator
* Refactor DataSourcePoolCreator
* Refactor DataSourcePoolCreator
* Refactor DataSourcePoolCreator
---
.../pool/creator/DataSourcePoolCreator.java | 65 ++++++----------------
.../pool/creator/DataSourcePoolCreatorTest.java | 2 +-
.../DriverDatabaseConnectionManager.java | 2 +-
.../DriverDatabaseConnectionManagerTest.java | 4 +-
.../swapper/YamlProxyConfigurationSwapper.java | 2 +-
5 files changed, 22 insertions(+), 53 deletions(-)
diff --git
a/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreator.java
b/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreator.java
index 56b7855da27..a1e2e11a4b0 100644
---
a/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreator.java
+++
b/infra/datasource/core/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreator.java
@@ -41,6 +41,7 @@ import
org.apache.shardingsphere.infra.datasource.storage.StorageUnitNodeMapper;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import javax.sql.DataSource;
+import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
@@ -60,38 +61,14 @@ public final class DataSourcePoolCreator {
* @return created storage resource
*/
public static StorageResource createStorageResource(final Map<String,
DataSourcePoolProperties> propsMap) {
- return createStorageResource(propsMap, true);
- }
-
- /**
- * Create storage resource.
- *
- * @param propsMap data source pool properties map
- * @param cacheEnabled cache enabled
- * @return created storage resource
- */
- public static StorageResource createStorageResource(final Map<String,
DataSourcePoolProperties> propsMap, final boolean cacheEnabled) {
Map<StorageNode, DataSource> storageNodes = new LinkedHashMap<>();
Map<String, StorageUnitNodeMapper> storageUnitNodeMappers = new
LinkedHashMap<>();
for (Entry<String, DataSourcePoolProperties> entry :
propsMap.entrySet()) {
StorageNodeProperties storageNodeProps =
getStorageNodeProperties(entry.getKey(), entry.getValue());
StorageNode storageNode = new
StorageNode(storageNodeProps.getName());
- if (storageNodes.containsKey(storageNode)) {
- appendStorageUnitNodeMapper(storageUnitNodeMappers,
storageNodeProps, entry.getKey(), entry.getValue());
- continue;
- }
- DataSource dataSource;
- try {
- dataSource = create(entry.getKey(), entry.getValue(),
cacheEnabled);
- // CHECKSTYLE:OFF
- } catch (final RuntimeException ex) {
- // CHECKSTYLE:ON
- if (!cacheEnabled) {
-
storageNodes.values().stream().map(DataSourcePoolDestroyer::new).forEach(DataSourcePoolDestroyer::asyncDestroy);
- }
- throw ex;
+ if (!storageNodes.containsKey(storageNode)) {
+ storageNodes.put(storageNode, createDataSource(entry.getKey(),
entry.getValue(), true, storageNodes.values()));
}
- storageNodes.put(storageNode, dataSource);
appendStorageUnitNodeMapper(storageUnitNodeMappers,
storageNodeProps, entry.getKey(), entry.getValue());
}
return new StorageResource(storageNodes, storageUnitNodeMappers);
@@ -157,16 +134,6 @@ public final class DataSourcePoolCreator {
return String.format("%s_%s_%s", hostname, port, username);
}
- /**
- * Create data sources.
- *
- * @param propsMap data source pool properties map
- * @return created data sources
- */
- public static Map<String, DataSource> create(final Map<String,
DataSourcePoolProperties> propsMap) {
- return create(propsMap, true);
- }
-
/**
* Create data sources.
*
@@ -177,18 +144,7 @@ public final class DataSourcePoolCreator {
public static Map<String, DataSource> create(final Map<String,
DataSourcePoolProperties> propsMap, final boolean cacheEnabled) {
Map<String, DataSource> result = new LinkedHashMap<>();
for (Entry<String, DataSourcePoolProperties> entry :
propsMap.entrySet()) {
- DataSource dataSource;
- try {
- dataSource = create(entry.getKey(), entry.getValue(),
cacheEnabled);
- // CHECKSTYLE:OFF
- } catch (final RuntimeException ex) {
- // CHECKSTYLE:ON
- if (!cacheEnabled) {
-
result.values().stream().map(DataSourcePoolDestroyer::new).forEach(DataSourcePoolDestroyer::asyncDestroy);
- }
- throw ex;
- }
- result.put(entry.getKey(), dataSource);
+ result.put(entry.getKey(), createDataSource(entry.getKey(),
entry.getValue(), cacheEnabled, result.values()));
}
return result;
}
@@ -230,6 +186,19 @@ public final class DataSourcePoolCreator {
return result;
}
+ private static DataSource createDataSource(final String dataSourceName,
final DataSourcePoolProperties props, final boolean cacheEnabled, final
Collection<DataSource> storageNodes) {
+ try {
+ return create(dataSourceName, props, cacheEnabled);
+ // CHECKSTYLE:OFF
+ } catch (final RuntimeException ex) {
+ // CHECKSTYLE:ON
+ if (!cacheEnabled) {
+
storageNodes.stream().map(DataSourcePoolDestroyer::new).forEach(DataSourcePoolDestroyer::asyncDestroy);
+ }
+ throw ex;
+ }
+ }
+
@SneakyThrows(ReflectiveOperationException.class)
private static DataSource createDataSource(final String
dataSourceClassName) {
return (DataSource)
Class.forName(dataSourceClassName).getConstructor().newInstance();
diff --git
a/infra/datasource/core/src/test/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreatorTest.java
b/infra/datasource/core/src/test/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreatorTest.java
index 3bf31ac407a..1b90e967736 100644
---
a/infra/datasource/core/src/test/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreatorTest.java
+++
b/infra/datasource/core/src/test/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreatorTest.java
@@ -34,7 +34,7 @@ class DataSourcePoolCreatorTest {
@Test
void assertCreateMap() {
- Map<String, DataSource> actual =
DataSourcePoolCreator.create(Collections.singletonMap("foo_ds", new
DataSourcePoolProperties(MockedDataSource.class.getName(),
createProperties())));
+ Map<String, DataSource> actual =
DataSourcePoolCreator.create(Collections.singletonMap("foo_ds", new
DataSourcePoolProperties(MockedDataSource.class.getName(),
createProperties())), true);
assertThat(actual.size(), is(1));
assertDataSource((MockedDataSource) actual.get("foo_ds"));
}
diff --git
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
index 9a8ebe6af6d..b8dba0cc885 100644
---
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
+++
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
@@ -100,7 +100,7 @@ public final class DriverDatabaseConnectionManager
implements DatabaseConnection
DataSourcePoolProperties propsSample =
propsMap.values().iterator().next();
Collection<ShardingSphereUser> users =
persistService.getGlobalRuleService().loadUsers();
Collection<InstanceMetaData> instances =
contextManager.getInstanceContext().getAllClusterInstances(InstanceType.PROXY,
rule.getLabels());
- return
DataSourcePoolCreator.create(createDataSourcePoolPropertiesMap(instances,
users, propsSample, actualDatabaseName));
+ return
DataSourcePoolCreator.create(createDataSourcePoolPropertiesMap(instances,
users, propsSample, actualDatabaseName), true);
}
private Map<String, DataSourcePoolProperties>
createDataSourcePoolPropertiesMap(final Collection<InstanceMetaData> instances,
final Collection<ShardingSphereUser> users,
diff --git
a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java
b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java
index bfe4be958da..ae72690f776 100644
---
a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java
+++
b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java
@@ -55,6 +55,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -71,7 +72,6 @@ class DriverDatabaseConnectionManagerTest {
databaseConnectionManager = new
DriverDatabaseConnectionManager(DefaultDatabase.LOGIC_NAME,
mockContextManager());
}
- @SuppressWarnings({"unchecked", "rawtypes"})
private ContextManager mockContextManager() throws SQLException {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
Map<String, DataSource> dataSourceMap = mockDataSourceMap();
@@ -83,7 +83,7 @@ class DriverDatabaseConnectionManagerTest {
when(result.getInstanceContext().getAllClusterInstances(InstanceType.PROXY,
Arrays.asList("OLTP", "OLAP"))).thenReturn(
Collections.singletonList(new ProxyInstanceMetaData("foo_id",
"127.0.0.1@3307", "foo_version")));
Map<String, DataSource> trafficDataSourceMap =
mockTrafficDataSourceMap();
- when(DataSourcePoolCreator.create((Map)
any())).thenReturn(trafficDataSourceMap);
+ when(DataSourcePoolCreator.create(any(),
eq(true))).thenReturn(trafficDataSourceMap);
return result;
}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapper.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapper.java
index 64e0dc93b07..6db4eab90dc 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapper.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/config/yaml/swapper/YamlProxyConfigurationSwapper.java
@@ -70,7 +70,7 @@ public final class YamlProxyConfigurationSwapper {
Map<String, DataSourceConfiguration> dataSourceConfigs =
swapDataSourceConfigurations(yamlDataSourceConfigs);
Map<String, DataSourcePoolProperties> propsMap =
dataSourceConfigs.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry ->
DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue,
currentValue) -> oldValue, LinkedHashMap::new));
- return DataSourcePoolCreator.create(propsMap);
+ return DataSourcePoolCreator.create(propsMap, true);
}
private Map<String, DatabaseConfiguration>
swapDatabaseConfigurations(final Map<String, YamlProxyDatabaseConfiguration>
databaseConfigurations) {