From 0cede36fcb01108eaf5164bd44f835489c6c229c Mon Sep 17 00:00:00 2001 From: Zhang Yonglun Date: Thu, 31 Dec 2020 14:00:15 +0800 Subject: [PATCH] #7318, support disconnected data source (#8837) * #7318, support disconnected data source * #7318, support disconnected data source --- .../ha/rule/biulder/HARuleBuilder.java | 9 +++++++++ .../ha/rule/biulder/HARuleBuilderTest.java | 12 +++++++++--- .../governance/core/config/ConfigCenter.java | 3 --- .../context/metadata/MetaDataContextsBuilder.java | 2 ++ .../factory/JDBCRawBackendDataSourceFactory.java | 12 +++++++++++- .../impl/AbstractBootstrapInitializer.java | 5 ++++- 6 files changed, 35 insertions(+), 8 deletions(-) diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilder.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilder.java index b0a447da07195..25cabb0d7263e 100644 --- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilder.java +++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilder.java @@ -19,6 +19,7 @@ import lombok.Setter; import org.apache.shardingsphere.ha.api.config.HARuleConfiguration; +import org.apache.shardingsphere.ha.api.config.rule.HADataSourceRuleConfiguration; import org.apache.shardingsphere.ha.constant.HAOrder; import org.apache.shardingsphere.ha.rule.HARule; import org.apache.shardingsphere.infra.database.type.DatabaseType; @@ -26,7 +27,9 @@ import org.apache.shardingsphere.infra.rule.builder.aware.ResourceAware; import javax.sql.DataSource; +import java.util.HashSet; import java.util.Map; +import java.util.Set; /** * HA rule builder. @@ -42,6 +45,12 @@ public final class HARuleBuilder implements ShardingSphereRuleBuilder dataSourceSet = new HashSet<>(128, 1); + for (HADataSourceRuleConfiguration each : ruleConfig.getDataSources()) { + dataSourceSet.add(each.getPrimaryDataSourceName()); + dataSourceSet.addAll(each.getReplicaDataSourceNames()); + } + dataSourceMap.entrySet().removeIf(stringDataSourceEntry -> !dataSourceSet.contains(stringDataSourceEntry.getKey())); return new HARule(ruleConfig, databaseType, dataSourceMap, schemaName); } diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilderTest.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilderTest.java index b96c15bc47633..73e0654038d3c 100644 --- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilderTest.java +++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilderTest.java @@ -17,15 +17,18 @@ package org.apache.shardingsphere.ha.rule.biulder; -import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; -import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; -import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry; import org.apache.shardingsphere.ha.api.config.HARuleConfiguration; import org.apache.shardingsphere.ha.api.config.rule.HADataSourceRuleConfiguration; import org.apache.shardingsphere.ha.rule.HARule; +import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; +import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; +import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry; import org.junit.Test; +import javax.sql.DataSource; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.assertThat; @@ -46,6 +49,9 @@ public void assertBuild() { "name", "primaryDataSourceName", Collections.singletonList("name"), "loadBalancerName", true); when(ruleConfig.getDataSources()).thenReturn(Collections.singletonList(dataSourceRuleConfig)); ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig); + Map dataSourceMap = new HashMap<>(); + dataSourceMap.put("primaryDataSourceName", mock(DataSource.class)); + ((HARuleBuilder) builder).setDataSourceMap(dataSourceMap); assertThat(builder.build(ruleConfig), instanceOf(HARule.class)); } } diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java index f57a9301f0f5c..7ebe76d21997a 100644 --- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java +++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java @@ -163,15 +163,12 @@ public synchronized void renew(final SchemaPersistEvent event) { */ @Subscribe public synchronized void renew(final PrimaryDataSourceUpdateEvent event) { - Map dataSourceConfigurations = loadDataSourceConfigurations(event.getSchemaName()); - dataSourceConfigurations.remove(event.getOldPrimaryDataSource()); Collection ruleConfigurations = loadRuleConfigurations(event.getSchemaName()); for (RuleConfiguration each : ruleConfigurations) { if (each instanceof HARuleConfiguration) { updateHaDataSourceRuleConfigurations(event, (HARuleConfiguration) each); } } - persistDataSourceConfigurations(event.getSchemaName(), dataSourceConfigurations); persistRuleConfigurations(event.getSchemaName(), ruleConfigurations); } diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilder.java b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilder.java index 3a5e22a19d016..550c3fa2d9d6d 100644 --- a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilder.java +++ b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilder.java @@ -117,6 +117,8 @@ private DatabaseType getDatabaseType(final Map dataSourceMap private DatabaseType getDatabaseType(final DataSource dataSource) throws SQLException { try (Connection connection = dataSource.getConnection()) { return DatabaseTypeRegistry.getDatabaseTypeByURL(connection.getMetaData().getURL()); + } catch (final SQLException ex) { + return null; } } diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/factory/JDBCRawBackendDataSourceFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/factory/JDBCRawBackendDataSourceFactory.java index 3a16f444c0a7d..ff593371e4a35 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/factory/JDBCRawBackendDataSourceFactory.java +++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/datasource/factory/JDBCRawBackendDataSourceFactory.java @@ -21,6 +21,7 @@ import com.zaxxer.hikari.HikariDataSource; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.apache.shardingsphere.infra.config.datasource.JDBCParameterDecorator; import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter; import org.apache.shardingsphere.infra.exception.ShardingSphereException; @@ -34,6 +35,7 @@ * Backend data source factory using {@code HikariDataSource} for JDBC raw. */ @NoArgsConstructor(access = AccessLevel.PRIVATE) +@Slf4j public final class JDBCRawBackendDataSourceFactory implements JDBCBackendDataSourceFactory { private static final JDBCRawBackendDataSourceFactory INSTANCE = new JDBCRawBackendDataSourceFactory(); @@ -67,7 +69,15 @@ public DataSource build(final String dataSourceName, final DataSourceParameter d config.setMaximumPoolSize(dataSourceParameter.getMaxPoolSize()); config.setMinimumIdle(dataSourceParameter.getMinPoolSize()); config.setReadOnly(dataSourceParameter.isReadOnly()); - DataSource result = new HikariDataSource(config); + DataSource result; + try { + result = new HikariDataSource(config); + // CHECKSTYLE:OFF + } catch (final Exception ex) { + // CHECKSTYLE:ON + log.error("Exception occur: ", ex); + return null; + } Optional decorator = findJDBCParameterDecorator(result); return decorator.isPresent() ? decorator.get().decorate(result) : result; } diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java index 095333559cb60..76e6c30a0f527 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java +++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java @@ -80,7 +80,10 @@ private static Map> createDataSourcesMap(final M private static Map createDataSources(final Map dataSourceParameters) { Map result = new LinkedHashMap<>(dataSourceParameters.size(), 1); for (Entry entry : dataSourceParameters.entrySet()) { - result.put(entry.getKey(), JDBCRawBackendDataSourceFactory.getInstance().build(entry.getKey(), entry.getValue())); + DataSource dataSource = JDBCRawBackendDataSourceFactory.getInstance().build(entry.getKey(), entry.getValue()); + if (null != dataSource) { + result.put(entry.getKey(), dataSource); + } } return result; }