You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2020/07/22 10:34:14 UTC
[shardingsphere] branch master updated: Merge
OrchestrationSpringShardingSphereDataSource and
AbstractOrchestrationDataSource into OrchestrationShardingSphereDataSource
(#6398)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 2b4bbbb Merge OrchestrationSpringShardingSphereDataSource and AbstractOrchestrationDataSource into OrchestrationShardingSphereDataSource (#6398)
2b4bbbb is described below
commit 2b4bbbbec21e94446dbca0d27e859ddd100130c0
Author: Liang Zhang <te...@163.com>
AuthorDate: Wed Jul 22 18:34:03 2020 +0800
Merge OrchestrationSpringShardingSphereDataSource and AbstractOrchestrationDataSource into OrchestrationShardingSphereDataSource (#6398)
* Upgrade test coverage statistics tool
* Remove OrchestrationSpringShardingSphereDataSource
* Refactor FieldValueUtil
* Remove AbstractOrchestrationDataSource
* Refactor OrchestrationShardingSphereDataSource
---
.../core/facade/ShardingOrchestrationFacade.java | 12 +-
.../AbstractOrchestrationDataSource.java | 157 --------------------
.../OrchestrationShardingSphereDataSource.java | 160 +++++++++++++++++----
.../OrchestrationShardingSphereDataSourceTest.java | 16 ++-
...rchestrationSpringShardingSphereDataSource.java | 49 -------
.../parser/DataSourceBeanDefinitionParser.java | 6 +-
.../OrchestrationEncryptNamespaceTest.java | 11 +-
.../OrchestrationMasterSlaveNamespaceTest.java | 15 +-
...hestrationShardingMasterSlaveNamespaceTest.java | 11 +-
.../OrchestrationShardingNamespaceTest.java | 19 +--
.../orchestration/util/FieldValueUtil.java | 15 +-
11 files changed, 183 insertions(+), 288 deletions(-)
diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-facade/src/main/java/org/apache/shardingsphere/orchestration/core/facade/ShardingOrchestrationFacade.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-facade/src/main/java/org/apache/shardingsphere/orchestration/core/facade/ShardingOrchestrationFacade.java
index a857de3..989e9c7 100644
--- a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-facade/src/main/java/org/apache/shardingsphere/orchestration/core/facade/ShardingOrchestrationFacade.java
+++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-facade/src/main/java/org/apache/shardingsphere/orchestration/core/facade/ShardingOrchestrationFacade.java
@@ -140,15 +140,15 @@ public final class ShardingOrchestrationFacade implements AutoCloseable {
}
/**
- * Init configurations of orchestration.
+ * Initialize configurations of orchestration.
*
* @param dataSourceConfigurationMap schema data source configuration map
* @param schemaRuleMap schema rule map
* @param authentication authentication
* @param props properties
*/
- public void initConfigurations(final Map<String, Map<String, DataSourceConfiguration>> dataSourceConfigurationMap,
- final Map<String, Collection<RuleConfiguration>> schemaRuleMap, final Authentication authentication, final Properties props) {
+ public void initConfigurations(final Map<String, Map<String, DataSourceConfiguration>> dataSourceConfigurationMap,
+ final Map<String, Collection<RuleConfiguration>> schemaRuleMap, final Authentication authentication, final Properties props) {
configCenter.persistGlobalConfiguration(authentication, props, isOverwrite);
for (Entry<String, Map<String, DataSourceConfiguration>> entry : dataSourceConfigurationMap.entrySet()) {
configCenter.persistConfigurations(entry.getKey(), dataSourceConfigurationMap.get(entry.getKey()), schemaRuleMap.get(entry.getKey()), isOverwrite);
@@ -157,7 +157,7 @@ public final class ShardingOrchestrationFacade implements AutoCloseable {
}
/**
- * Init configurations of orchestration.
+ * Initialize configurations of orchestration.
*/
public void initConfigurations() {
registryCenter.persistInstanceOnline();
@@ -166,7 +166,7 @@ public final class ShardingOrchestrationFacade implements AutoCloseable {
}
/**
- * Init metrics configuration to config center.
+ * Initialize metrics configuration to config center.
*
* @param metricsConfiguration metrics configuration
*/
@@ -175,7 +175,7 @@ public final class ShardingOrchestrationFacade implements AutoCloseable {
}
/**
- * Init cluster configuration to config center.
+ * Initialize cluster configuration to config center.
*
* @param clusterConfiguration cluster configuration
*/
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/main/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/AbstractOrchestrationDataSource.java b/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/main/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/AbstractOrchestrationDataSource.java
deleted file mode 100644
index 095bc4d..0000000
--- a/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/main/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/AbstractOrchestrationDataSource.java
+++ /dev/null
@@ -1,157 +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.driver.orchestration.internal.datasource;
-
-import com.google.common.collect.Maps;
-import lombok.AccessLevel;
-import lombok.Getter;
-import lombok.Setter;
-import org.apache.shardingsphere.cluster.configuration.config.ClusterConfiguration;
-import org.apache.shardingsphere.control.panel.spi.FacadeConfiguration;
-import org.apache.shardingsphere.control.panel.spi.engine.ControlPanelFacadeEngine;
-import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
-import org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedOperationDataSource;
-import org.apache.shardingsphere.driver.orchestration.internal.util.DataSourceConverter;
-import org.apache.shardingsphere.infra.config.DataSourceConfiguration;
-import org.apache.shardingsphere.infra.config.RuleConfiguration;
-import org.apache.shardingsphere.infra.database.DefaultSchema;
-import org.apache.shardingsphere.infra.metadata.schema.RuleSchemaMetaData;
-import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
-import org.apache.shardingsphere.orchestration.core.common.eventbus.ShardingOrchestrationEventBus;
-import org.apache.shardingsphere.orchestration.core.facade.ShardingOrchestrationFacade;
-
-import javax.sql.DataSource;
-import java.io.PrintWriter;
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Properties;
-import java.util.logging.Logger;
-import java.util.stream.Collectors;
-
-/**
- * Abstract orchestration data source.
- */
-public abstract class AbstractOrchestrationDataSource extends AbstractUnsupportedOperationDataSource implements AutoCloseable {
-
- @Getter
- @Setter
- private PrintWriter logWriter = new PrintWriter(System.out);
-
- @Getter(AccessLevel.PROTECTED)
- private final ShardingOrchestrationFacade shardingOrchestrationFacade = ShardingOrchestrationFacade.getInstance();
-
- @Getter(AccessLevel.PROTECTED)
- private final Map<String, DataSourceConfiguration> dataSourceConfigurations = new LinkedHashMap<>();
-
- public AbstractOrchestrationDataSource(final OrchestrationConfiguration orchestrationConfig) {
- this.shardingOrchestrationFacade.init(orchestrationConfig, Collections.singletonList(DefaultSchema.LOGIC_NAME));
- ShardingOrchestrationEventBus.getInstance().register(this);
- }
-
- protected abstract DataSource getDataSource();
-
- @Override
- public final Connection getConnection(final String username, final String password) throws SQLException {
- return getConnection();
- }
-
- @Override
- public final Logger getParentLogger() {
- return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
- }
-
- @Override
- public final void close() throws Exception {
- ((ShardingSphereDataSource) getDataSource()).close();
- shardingOrchestrationFacade.close();
- }
-
- protected final void initShardingOrchestrationFacade() {
- shardingOrchestrationFacade.initConfigurations();
- dataSourceConfigurations.putAll(shardingOrchestrationFacade.getConfigCenter().loadDataSourceConfigurations(DefaultSchema.LOGIC_NAME));
- }
-
- protected final void initShardingOrchestrationFacade(final ClusterConfiguration clusterConfiguration) {
- shardingOrchestrationFacade.initConfigurations();
- shardingOrchestrationFacade.initClusterConfiguration(clusterConfiguration);
- dataSourceConfigurations.putAll(shardingOrchestrationFacade.getConfigCenter().loadDataSourceConfigurations(DefaultSchema.LOGIC_NAME));
- }
-
- protected final void initShardingOrchestrationFacade(
- final Map<String, Map<String, DataSourceConfiguration>> dataSourceConfigurations,
- final Map<String, Collection<RuleConfiguration>> schemaRules, final Properties props) {
- shardingOrchestrationFacade.initConfigurations(dataSourceConfigurations, schemaRules, null, props);
- this.dataSourceConfigurations.putAll(dataSourceConfigurations.get(DefaultSchema.LOGIC_NAME));
- }
-
- protected final void initShardingOrchestrationFacade(
- final Map<String, Map<String, DataSourceConfiguration>> dataSourceConfigurations,
- final Map<String, Collection<RuleConfiguration>> schemaRules, final Properties props, final ClusterConfiguration clusterConfiguration) {
- shardingOrchestrationFacade.initConfigurations(dataSourceConfigurations, schemaRules, null, props);
- shardingOrchestrationFacade.initClusterConfiguration(clusterConfiguration);
- this.dataSourceConfigurations.putAll(dataSourceConfigurations.get(DefaultSchema.LOGIC_NAME));
- }
-
- protected final synchronized Map<String, DataSource> getChangedDataSources(final Map<String, DataSource> oldDataSources, final Map<String, DataSourceConfiguration> newDataSources) {
- Map<String, DataSource> result = new LinkedHashMap<>(oldDataSources);
- Map<String, DataSourceConfiguration> modifiedDataSources = getModifiedDataSources(newDataSources);
- result.keySet().removeAll(getDeletedDataSources(newDataSources));
- result.keySet().removeAll(modifiedDataSources.keySet());
- result.putAll(DataSourceConverter.getDataSourceMap(modifiedDataSources));
- result.putAll(DataSourceConverter.getDataSourceMap(getAddedDataSources(newDataSources)));
- return result;
- }
-
- protected final synchronized Map<String, DataSourceConfiguration> getModifiedDataSources(final Map<String, DataSourceConfiguration> dataSourceConfigurations) {
- return dataSourceConfigurations.entrySet().stream().filter(this::isModifiedDataSource).collect(Collectors.toMap(Entry::getKey, Entry::getValue, (key, repeatKey) -> key, LinkedHashMap::new));
- }
-
- private synchronized boolean isModifiedDataSource(final Entry<String, DataSourceConfiguration> dataSourceNameAndConfig) {
- return dataSourceConfigurations.containsKey(dataSourceNameAndConfig.getKey()) && !dataSourceConfigurations.get(dataSourceNameAndConfig.getKey()).equals(dataSourceNameAndConfig.getValue());
- }
-
- protected final synchronized List<String> getDeletedDataSources(final Map<String, DataSourceConfiguration> dataSourceConfigurations) {
- List<String> result = new LinkedList<>(this.dataSourceConfigurations.keySet());
- result.removeAll(dataSourceConfigurations.keySet());
- return result;
- }
-
- private synchronized Map<String, DataSourceConfiguration> getAddedDataSources(final Map<String, DataSourceConfiguration> dataSourceConfigurations) {
- return Maps.filterEntries(dataSourceConfigurations, input -> !AbstractOrchestrationDataSource.this.dataSourceConfigurations.containsKey(input.getKey()));
- }
-
- protected final void persistMetaData(final RuleSchemaMetaData ruleSchemaMetaData) {
- ShardingOrchestrationFacade.getInstance().getMetaDataCenter().persistMetaDataCenterNode(DefaultSchema.LOGIC_NAME, ruleSchemaMetaData);
- }
-
- protected final void initCluster() {
- ClusterConfiguration clusterConfiguration = shardingOrchestrationFacade.getConfigCenter().loadClusterConfiguration();
- if (null != clusterConfiguration && null != clusterConfiguration.getHeartbeat()) {
- List<FacadeConfiguration> facadeConfigurations = new LinkedList<>();
- facadeConfigurations.add(clusterConfiguration);
- new ControlPanelFacadeEngine().init(facadeConfigurations);
- }
- }
-}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/main/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/OrchestrationShardingSphereDataSource.java b/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/main/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/OrchestrationShardingSphereDataSource.java
index eb2471b..f9bcd3e 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/main/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/OrchestrationShardingSphereDataSource.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/main/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/OrchestrationShardingSphereDataSource.java
@@ -19,15 +19,19 @@ package org.apache.shardingsphere.driver.orchestration.internal.datasource;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps;
import com.google.common.eventbus.Subscribe;
-import lombok.AccessLevel;
import lombok.Getter;
+import lombok.Setter;
import lombok.SneakyThrows;
import org.apache.shardingsphere.cluster.configuration.config.ClusterConfiguration;
import org.apache.shardingsphere.cluster.facade.ClusterFacade;
import org.apache.shardingsphere.cluster.facade.init.ClusterInitFacade;
import org.apache.shardingsphere.cluster.heartbeat.event.HeartbeatDetectNoticeEvent;
+import org.apache.shardingsphere.control.panel.spi.FacadeConfiguration;
+import org.apache.shardingsphere.control.panel.spi.engine.ControlPanelFacadeEngine;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
+import org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedOperationDataSource;
import org.apache.shardingsphere.driver.orchestration.internal.circuit.datasource.CircuitBreakerDataSource;
import org.apache.shardingsphere.driver.orchestration.internal.util.DataSourceConverter;
import org.apache.shardingsphere.infra.config.DataSourceConfiguration;
@@ -46,6 +50,7 @@ import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfig
import org.apache.shardingsphere.orchestration.core.common.event.DataSourceChangedEvent;
import org.apache.shardingsphere.orchestration.core.common.event.PropertiesChangedEvent;
import org.apache.shardingsphere.orchestration.core.common.event.RuleConfigurationsChangedEvent;
+import org.apache.shardingsphere.orchestration.core.common.eventbus.ShardingOrchestrationEventBus;
import org.apache.shardingsphere.orchestration.core.configcenter.ConfigCenter;
import org.apache.shardingsphere.orchestration.core.facade.ShardingOrchestrationFacade;
import org.apache.shardingsphere.orchestration.core.metadatacenter.event.MetaDataChangedEvent;
@@ -53,61 +58,68 @@ import org.apache.shardingsphere.orchestration.core.registrycenter.event.Circuit
import org.apache.shardingsphere.orchestration.core.registrycenter.event.DisabledStateChangedEvent;
import org.apache.shardingsphere.orchestration.core.registrycenter.schema.OrchestrationSchema;
+import javax.sql.DataSource;
+import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Properties;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
/**
* Orchestration ShardingSphere data source.
*/
-@Getter(AccessLevel.PROTECTED)
-public class OrchestrationShardingSphereDataSource extends AbstractOrchestrationDataSource {
+public final class OrchestrationShardingSphereDataSource extends AbstractUnsupportedOperationDataSource implements AutoCloseable {
+
+ @Getter
+ @Setter
+ private PrintWriter logWriter = new PrintWriter(System.out);
+
+ private final ShardingOrchestrationFacade shardingOrchestrationFacade = ShardingOrchestrationFacade.getInstance();
+
+ private final Map<String, DataSourceConfiguration> dataSourceConfigurations = new LinkedHashMap<>();
private ShardingSphereDataSource dataSource;
public OrchestrationShardingSphereDataSource(final OrchestrationConfiguration orchestrationConfig) throws SQLException {
- super(orchestrationConfig);
- ConfigCenter configService = getShardingOrchestrationFacade().getConfigCenter();
- Collection<RuleConfiguration> configurations = configService.loadRuleConfigurations(DefaultSchema.LOGIC_NAME);
- Preconditions.checkState(!configurations.isEmpty(), "Missing the sharding rule configuration on registry center");
- Map<String, DataSourceConfiguration> dataSourceConfigurations = configService.loadDataSourceConfigurations(DefaultSchema.LOGIC_NAME);
- dataSource = new ShardingSphereDataSource(DataSourceConverter.getDataSourceMap(dataSourceConfigurations), configurations, configService.loadProperties());
- initShardingOrchestrationFacade();
+ initOrchestration(orchestrationConfig);
+ dataSource = createDataSource();
+ initShardingOrchestrationFacade(null);
disableDataSources();
persistMetaData(dataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getMetaData().getSchema());
initCluster();
}
public OrchestrationShardingSphereDataSource(final ShardingSphereDataSource shardingSphereDataSource, final OrchestrationConfiguration orchestrationConfig) {
- super(orchestrationConfig);
+ initOrchestration(orchestrationConfig);
dataSource = shardingSphereDataSource;
initShardingOrchestrationFacade(Collections.singletonMap(DefaultSchema.LOGIC_NAME, DataSourceConverter.getDataSourceConfigurationMap(dataSource.getDataSourceMap())),
- getRuleConfigurationMap(), dataSource.getSchemaContexts().getProps().getProps());
+ getRuleConfigurationMap(), dataSource.getSchemaContexts().getProps().getProps(), null);
disableDataSources();
persistMetaData(dataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getMetaData().getSchema());
initCluster();
}
public OrchestrationShardingSphereDataSource(final OrchestrationConfiguration orchestrationConfig, final ClusterConfiguration clusterConfiguration) throws SQLException {
- super(orchestrationConfig);
- ConfigCenter configService = getShardingOrchestrationFacade().getConfigCenter();
- Collection<RuleConfiguration> configurations = configService.loadRuleConfigurations(DefaultSchema.LOGIC_NAME);
- Preconditions.checkState(!configurations.isEmpty(), "Missing the sharding rule configuration on registry center");
- Map<String, DataSourceConfiguration> dataSourceConfigurations = configService.loadDataSourceConfigurations(DefaultSchema.LOGIC_NAME);
- dataSource = new ShardingSphereDataSource(DataSourceConverter.getDataSourceMap(dataSourceConfigurations), configurations, configService.loadProperties());
+ initOrchestration(orchestrationConfig);
+ dataSource = createDataSource();
initShardingOrchestrationFacade(clusterConfiguration);
disableDataSources();
persistMetaData(dataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getMetaData().getSchema());
initCluster();
}
- public OrchestrationShardingSphereDataSource(final ShardingSphereDataSource shardingSphereDataSource,
+ public OrchestrationShardingSphereDataSource(final ShardingSphereDataSource shardingSphereDataSource,
final OrchestrationConfiguration orchestrationConfig, final ClusterConfiguration clusterConfiguration) {
- super(orchestrationConfig);
+ initOrchestration(orchestrationConfig);
dataSource = shardingSphereDataSource;
initShardingOrchestrationFacade(Collections.singletonMap(DefaultSchema.LOGIC_NAME, DataSourceConverter.getDataSourceConfigurationMap(dataSource.getDataSourceMap())),
getRuleConfigurationMap(), dataSource.getSchemaContexts().getProps().getProps(), clusterConfiguration);
@@ -116,15 +128,81 @@ public class OrchestrationShardingSphereDataSource extends AbstractOrchestration
initCluster();
}
+ private void initOrchestration(final OrchestrationConfiguration orchestrationConfig) {
+ shardingOrchestrationFacade.init(orchestrationConfig, Collections.singletonList(DefaultSchema.LOGIC_NAME));
+ ShardingOrchestrationEventBus.getInstance().register(this);
+ }
+
+ private ShardingSphereDataSource createDataSource() throws SQLException {
+ ConfigCenter configService = shardingOrchestrationFacade.getConfigCenter();
+ Collection<RuleConfiguration> configurations = configService.loadRuleConfigurations(DefaultSchema.LOGIC_NAME);
+ Preconditions.checkState(!configurations.isEmpty(), "Missing the sharding rule configuration on registry center");
+ Map<String, DataSourceConfiguration> dataSourceConfigurations = configService.loadDataSourceConfigurations(DefaultSchema.LOGIC_NAME);
+ return new ShardingSphereDataSource(DataSourceConverter.getDataSourceMap(dataSourceConfigurations), configurations, configService.loadProperties());
+ }
+
private Map<String, Collection<RuleConfiguration>> getRuleConfigurationMap() {
Map<String, Collection<RuleConfiguration>> result = new LinkedHashMap<>(1, 1);
result.put(DefaultSchema.LOGIC_NAME, dataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getConfigurations());
return result;
}
- @Override
- public final Connection getConnection() {
- return dataSource.getSchemaContexts().isCircuitBreak() ? new CircuitBreakerDataSource().getConnection() : getDataSource().getConnection();
+ private void initShardingOrchestrationFacade(final ClusterConfiguration clusterConfiguration) {
+ shardingOrchestrationFacade.initConfigurations();
+ if (null != clusterConfiguration) {
+ shardingOrchestrationFacade.initClusterConfiguration(clusterConfiguration);
+ }
+ dataSourceConfigurations.putAll(shardingOrchestrationFacade.getConfigCenter().loadDataSourceConfigurations(DefaultSchema.LOGIC_NAME));
+ }
+
+ private void initShardingOrchestrationFacade(final Map<String, Map<String, DataSourceConfiguration>> dataSourceConfigurations,
+ final Map<String, Collection<RuleConfiguration>> schemaRules, final Properties props, final ClusterConfiguration clusterConfiguration) {
+ shardingOrchestrationFacade.initConfigurations(dataSourceConfigurations, schemaRules, null, props);
+ if (null != clusterConfiguration) {
+ shardingOrchestrationFacade.initClusterConfiguration(clusterConfiguration);
+ }
+ this.dataSourceConfigurations.putAll(dataSourceConfigurations.get(DefaultSchema.LOGIC_NAME));
+ }
+
+ private synchronized Map<String, DataSource> getChangedDataSources(final Map<String, DataSource> oldDataSources, final Map<String, DataSourceConfiguration> newDataSources) {
+ Map<String, DataSource> result = new LinkedHashMap<>(oldDataSources);
+ Map<String, DataSourceConfiguration> modifiedDataSources = getModifiedDataSources(newDataSources);
+ result.keySet().removeAll(getDeletedDataSources(newDataSources));
+ result.keySet().removeAll(modifiedDataSources.keySet());
+ result.putAll(DataSourceConverter.getDataSourceMap(modifiedDataSources));
+ result.putAll(DataSourceConverter.getDataSourceMap(getAddedDataSources(newDataSources)));
+ return result;
+ }
+
+ private synchronized Map<String, DataSourceConfiguration> getModifiedDataSources(final Map<String, DataSourceConfiguration> dataSourceConfigurations) {
+ return dataSourceConfigurations.entrySet().stream().filter(this::isModifiedDataSource).collect(Collectors.toMap(Entry::getKey, Entry::getValue, (key, repeatKey) -> key, LinkedHashMap::new));
+ }
+
+ private synchronized boolean isModifiedDataSource(final Entry<String, DataSourceConfiguration> dataSourceNameAndConfig) {
+ return dataSourceConfigurations.containsKey(dataSourceNameAndConfig.getKey()) && !dataSourceConfigurations.get(dataSourceNameAndConfig.getKey()).equals(dataSourceNameAndConfig.getValue());
+ }
+
+ private synchronized List<String> getDeletedDataSources(final Map<String, DataSourceConfiguration> dataSourceConfigurations) {
+ List<String> result = new LinkedList<>(this.dataSourceConfigurations.keySet());
+ result.removeAll(dataSourceConfigurations.keySet());
+ return result;
+ }
+
+ private synchronized Map<String, DataSourceConfiguration> getAddedDataSources(final Map<String, DataSourceConfiguration> dataSourceConfigurations) {
+ return Maps.filterEntries(dataSourceConfigurations, input -> !this.dataSourceConfigurations.containsKey(input.getKey()));
+ }
+
+ private void persistMetaData(final RuleSchemaMetaData ruleSchemaMetaData) {
+ ShardingOrchestrationFacade.getInstance().getMetaDataCenter().persistMetaDataCenterNode(DefaultSchema.LOGIC_NAME, ruleSchemaMetaData);
+ }
+
+ private void initCluster() {
+ ClusterConfiguration clusterConfiguration = shardingOrchestrationFacade.getConfigCenter().loadClusterConfiguration();
+ if (null != clusterConfiguration && null != clusterConfiguration.getHeartbeat()) {
+ List<FacadeConfiguration> facadeConfigurations = new LinkedList<>();
+ facadeConfigurations.add(clusterConfiguration);
+ new ControlPanelFacadeEngine().init(facadeConfigurations);
+ }
}
/**
@@ -133,7 +211,7 @@ public class OrchestrationShardingSphereDataSource extends AbstractOrchestration
* @param event meta data changed event.
*/
@Subscribe
- public final synchronized void renew(final MetaDataChangedEvent event) {
+ public synchronized void renew(final MetaDataChangedEvent event) {
if (!event.getSchemaNames().contains(DefaultSchema.LOGIC_NAME)) {
return;
}
@@ -151,7 +229,7 @@ public class OrchestrationShardingSphereDataSource extends AbstractOrchestration
*/
@Subscribe
@SneakyThrows
- public final synchronized void renew(final RuleConfigurationsChangedEvent ruleConfigurationsChangedEvent) {
+ public synchronized void renew(final RuleConfigurationsChangedEvent ruleConfigurationsChangedEvent) {
if (!ruleConfigurationsChangedEvent.getShardingSchemaName().contains(DefaultSchema.LOGIC_NAME)) {
return;
}
@@ -166,7 +244,7 @@ public class OrchestrationShardingSphereDataSource extends AbstractOrchestration
*/
@Subscribe
@SneakyThrows
- public final synchronized void renew(final DataSourceChangedEvent dataSourceChangedEvent) {
+ public synchronized void renew(final DataSourceChangedEvent dataSourceChangedEvent) {
if (!dataSourceChangedEvent.getShardingSchemaName().contains(DefaultSchema.LOGIC_NAME)) {
return;
}
@@ -175,8 +253,8 @@ public class OrchestrationShardingSphereDataSource extends AbstractOrchestration
dataSource.close(getModifiedDataSources(dataSourceConfigurations).keySet());
dataSource = new ShardingSphereDataSource(getChangedDataSources(dataSource.getDataSourceMap(), dataSourceConfigurations),
dataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getConfigurations(), dataSource.getSchemaContexts().getProps().getProps());
- getDataSourceConfigurations().clear();
- getDataSourceConfigurations().putAll(dataSourceConfigurations);
+ this.dataSourceConfigurations.clear();
+ this.dataSourceConfigurations.putAll(dataSourceConfigurations);
}
/**
@@ -186,7 +264,7 @@ public class OrchestrationShardingSphereDataSource extends AbstractOrchestration
*/
@SneakyThrows
@Subscribe
- public final synchronized void renew(final PropertiesChangedEvent propertiesChangedEvent) {
+ public synchronized void renew(final PropertiesChangedEvent propertiesChangedEvent) {
dataSource = new ShardingSphereDataSource(dataSource.getDataSourceMap(),
dataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getConfigurations(), propertiesChangedEvent.getProps());
}
@@ -246,12 +324,32 @@ public class OrchestrationShardingSphereDataSource extends AbstractOrchestration
}
}
- private void disableDataSources(final MasterSlaveRule masterSlaveRule,
- final Collection<String> disabledDataSources, final String schemaName) {
+ private void disableDataSources(final MasterSlaveRule masterSlaveRule, final Collection<String> disabledDataSources, final String schemaName) {
masterSlaveRule.getSingleDataSourceRule().getSlaveDataSourceNames().forEach(each -> {
if (disabledDataSources.contains(Joiner.on(".").join(schemaName, each))) {
masterSlaveRule.updateRuleStatus(new DataSourceNameDisabledEvent(each, true));
}
});
}
+
+ @Override
+ public Connection getConnection() {
+ return dataSource.getSchemaContexts().isCircuitBreak() ? new CircuitBreakerDataSource().getConnection() : dataSource.getConnection();
+ }
+
+ @Override
+ public Connection getConnection(final String username, final String password) {
+ return getConnection();
+ }
+
+ @Override
+ public Logger getParentLogger() {
+ return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
+ }
+
+ @Override
+ public void close() {
+ dataSource.close();
+ shardingOrchestrationFacade.close();
+ }
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/test/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/OrchestrationShardingSphereDataSourceTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/test/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/OrchestrationShardingSphereDataSourceTest.java
index deab71a..1559073 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/test/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/OrchestrationShardingSphereDataSourceTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-orchestration/src/test/java/org/apache/shardingsphere/driver/orchestration/internal/datasource/OrchestrationShardingSphereDataSourceTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.driver.orchestration.internal.datasource;
import com.google.common.collect.ImmutableMap;
+import lombok.SneakyThrows;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
@@ -42,6 +43,7 @@ import org.junit.Test;
import java.io.File;
import java.io.IOException;
+import java.lang.reflect.Field;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.SQLException;
@@ -117,8 +119,7 @@ public final class OrchestrationShardingSphereDataSourceTest {
@Test
public void assertRenewRules() {
orchestrationDataSource.renew(new RuleConfigurationsChangedEvent(DefaultSchema.LOGIC_NAME, Arrays.asList(getShardingRuleConfiguration(), getMasterSlaveRuleConfiguration())));
- assertThat(((ShardingRule)
- orchestrationDataSource.getDataSource().getSchemaContexts().getDefaultSchemaContext().getSchema().getRules().iterator().next()).getTableRules().size(), is(1));
+ assertThat(((ShardingRule) getDataSource().getSchemaContexts().getDefaultSchemaContext().getSchema().getRules().iterator().next()).getTableRules().size(), is(1));
}
private ShardingRuleConfiguration getShardingRuleConfiguration() {
@@ -136,7 +137,7 @@ public final class OrchestrationShardingSphereDataSourceTest {
@Test
public void assertRenewDataSource() {
orchestrationDataSource.renew(new DataSourceChangedEvent(DefaultSchema.LOGIC_NAME, getDataSourceConfigurations()));
- assertThat(orchestrationDataSource.getDataSource().getDataSourceMap().size(), is(3));
+ assertThat(getDataSource().getDataSourceMap().size(), is(3));
}
@@ -156,7 +157,7 @@ public final class OrchestrationShardingSphereDataSourceTest {
@Test
public void assertRenewProperties() {
orchestrationDataSource.renew(getPropertiesChangedEvent());
- assertThat(orchestrationDataSource.getDataSource().getSchemaContexts().getProps().getProps().getProperty("sql.show"), is("true"));
+ assertThat(getDataSource().getSchemaContexts().getProps().getProps().getProperty("sql.show"), is("true"));
}
private PropertiesChangedEvent getPropertiesChangedEvent() {
@@ -169,4 +170,11 @@ public final class OrchestrationShardingSphereDataSourceTest {
public void assertRenewDisabledState() {
orchestrationDataSource.renew(new DisabledStateChangedEvent(new OrchestrationSchema("logic_db.ds_s"), true));
}
+
+ @SneakyThrows(ReflectiveOperationException.class)
+ private ShardingSphereDataSource getDataSource() {
+ Field field = OrchestrationShardingSphereDataSource.class.getDeclaredField("dataSource");
+ field.setAccessible(true);
+ return (ShardingSphereDataSource) field.get(orchestrationDataSource);
+ }
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/main/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationSpringShardingSphereDataSource.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/main/java/org/apache/shardingsphere/spring/namespace/orchestration/Orchestra [...]
deleted file mode 100644
index d6db3f6..0000000
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/main/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationSpringShardingSphereDataSource.java
+++ /dev/null
@@ -1,49 +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.spring.namespace.orchestration;
-
-import org.apache.shardingsphere.cluster.configuration.config.ClusterConfiguration;
-import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
-import org.apache.shardingsphere.driver.orchestration.internal.datasource.OrchestrationShardingSphereDataSource;
-import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
-
-import javax.sql.DataSource;
-import java.sql.SQLException;
-
-/**
- * Orchestration ShardingSphere datasource for spring namespace.
- */
-public final class OrchestrationSpringShardingSphereDataSource extends OrchestrationShardingSphereDataSource {
-
- public OrchestrationSpringShardingSphereDataSource(final DataSource dataSource, final OrchestrationConfiguration orchestrationConfig) {
- super((ShardingSphereDataSource) dataSource, orchestrationConfig);
- }
-
- public OrchestrationSpringShardingSphereDataSource(final OrchestrationConfiguration orchestrationConfig) throws SQLException {
- super(orchestrationConfig);
- }
-
- public OrchestrationSpringShardingSphereDataSource(final DataSource dataSource,
- final OrchestrationConfiguration orchestrationConfig, final ClusterConfiguration clusterConfiguration) {
- super((ShardingSphereDataSource) dataSource, orchestrationConfig, clusterConfiguration);
- }
-
- public OrchestrationSpringShardingSphereDataSource(final OrchestrationConfiguration orchestrationConfig, final ClusterConfiguration clusterConfiguration) throws SQLException {
- super(orchestrationConfig, clusterConfiguration);
- }
-}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/main/java/org/apache/shardingsphere/spring/namespace/orchestration/parser/DataSourceBeanDefinitionParser.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/main/java/org/apache/shardingsphere/spring/namespace/orchestration/parser/DataSour [...]
index ed83437..a85e3d1 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/main/java/org/apache/shardingsphere/spring/namespace/orchestration/parser/DataSourceBeanDefinitionParser.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/main/java/org/apache/shardingsphere/spring/namespace/orchestration/parser/DataSourceBeanDefinitionParser.java
@@ -19,9 +19,9 @@ package org.apache.shardingsphere.spring.namespace.orchestration.parser;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
-import org.apache.shardingsphere.spring.namespace.orchestration.OrchestrationSpringShardingSphereDataSource;
-import org.apache.shardingsphere.spring.namespace.orchestration.constants.DataSourceBeanDefinitionTag;
+import org.apache.shardingsphere.driver.orchestration.internal.datasource.OrchestrationShardingSphereDataSource;
import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
+import org.apache.shardingsphere.spring.namespace.orchestration.constants.DataSourceBeanDefinitionTag;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
@@ -41,7 +41,7 @@ public final class DataSourceBeanDefinitionParser extends AbstractBeanDefinition
@Override
protected AbstractBeanDefinition parseInternal(final Element element, final ParserContext parserContext) {
- BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(OrchestrationSpringShardingSphereDataSource.class);
+ BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(OrchestrationShardingSphereDataSource.class);
configureFactory(element, factory);
return factory.getBeanDefinition();
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationEncryptNamespaceTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationEncryp [...]
index 7dc0645..a81b715 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationEncryptNamespaceTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationEncryptNamespaceTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.spring.namespace.orchestration;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
+import org.apache.shardingsphere.driver.orchestration.internal.datasource.OrchestrationShardingSphereDataSource;
import org.apache.shardingsphere.encrypt.algorithm.config.AlgorithmProvidedEncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
@@ -50,13 +51,13 @@ public class OrchestrationEncryptNamespaceTest extends AbstractJUnit4SpringConte
@Test
public void assertEncryptDataSourceType() {
- assertNotNull(applicationContext.getBean("encryptDataSourceOrchestration", OrchestrationSpringShardingSphereDataSource.class));
+ assertNotNull(applicationContext.getBean("encryptDataSourceOrchestration", OrchestrationShardingSphereDataSource.class));
assertEncryptRule(getEncryptRuleConfiguration());
}
private AlgorithmProvidedEncryptRuleConfiguration getEncryptRuleConfiguration() {
- OrchestrationSpringShardingSphereDataSource orchestrationDataSource = (OrchestrationSpringShardingSphereDataSource) applicationContext.getBean("encryptDataSourceOrchestration");
- ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(orchestrationDataSource, "dataSource", true);
+ OrchestrationShardingSphereDataSource orchestrationDataSource = (OrchestrationShardingSphereDataSource) applicationContext.getBean("encryptDataSourceOrchestration");
+ ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(orchestrationDataSource, "dataSource");
return (AlgorithmProvidedEncryptRuleConfiguration) dataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getConfigurations().iterator().next();
}
@@ -85,8 +86,8 @@ public class OrchestrationEncryptNamespaceTest extends AbstractJUnit4SpringConte
}
private ConfigurationProperties getProperties(final String encryptDatasourceName) {
- OrchestrationSpringShardingSphereDataSource orchestrationDataSource = applicationContext.getBean(encryptDatasourceName, OrchestrationSpringShardingSphereDataSource.class);
- ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(orchestrationDataSource, "dataSource", true);
+ OrchestrationShardingSphereDataSource orchestrationDataSource = applicationContext.getBean(encryptDatasourceName, OrchestrationShardingSphereDataSource.class);
+ ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(orchestrationDataSource, "dataSource");
return dataSource.getSchemaContexts().getProps();
}
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationMasterSlaveNamespaceTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationMa [...]
index 94dd9d4..5382f00 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationMasterSlaveNamespaceTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationMasterSlaveNamespaceTest.java
@@ -18,13 +18,14 @@
package org.apache.shardingsphere.spring.namespace.orchestration;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
+import org.apache.shardingsphere.driver.orchestration.internal.datasource.OrchestrationShardingSphereDataSource;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
+import org.apache.shardingsphere.masterslave.algorithm.RandomMasterSlaveLoadBalanceAlgorithm;
+import org.apache.shardingsphere.masterslave.algorithm.RoundRobinMasterSlaveLoadBalanceAlgorithm;
import org.apache.shardingsphere.masterslave.rule.MasterSlaveDataSourceRule;
import org.apache.shardingsphere.masterslave.rule.MasterSlaveRule;
import org.apache.shardingsphere.masterslave.spi.MasterSlaveLoadBalanceAlgorithm;
-import org.apache.shardingsphere.masterslave.algorithm.RandomMasterSlaveLoadBalanceAlgorithm;
-import org.apache.shardingsphere.masterslave.algorithm.RoundRobinMasterSlaveLoadBalanceAlgorithm;
import org.apache.shardingsphere.spring.namespace.orchestration.util.EmbedTestingServer;
import org.apache.shardingsphere.spring.namespace.orchestration.util.FieldValueUtil;
import org.junit.BeforeClass;
@@ -50,7 +51,7 @@ public class OrchestrationMasterSlaveNamespaceTest extends AbstractJUnit4SpringC
@Test
public void assertMasterSlaveDataSourceType() {
- assertNotNull(applicationContext.getBean("defaultMasterSlaveDataSourceOrchestration", OrchestrationSpringShardingSphereDataSource.class));
+ assertNotNull(applicationContext.getBean("defaultMasterSlaveDataSourceOrchestration", OrchestrationShardingSphereDataSource.class));
}
@Test
@@ -87,8 +88,8 @@ public class OrchestrationMasterSlaveNamespaceTest extends AbstractJUnit4SpringC
}
private MasterSlaveRule getMasterSlaveRule(final String masterSlaveDataSourceName) {
- OrchestrationSpringShardingSphereDataSource masterSlaveDataSource = applicationContext.getBean(masterSlaveDataSourceName, OrchestrationSpringShardingSphereDataSource.class);
- ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(masterSlaveDataSource, "dataSource", true);
+ OrchestrationShardingSphereDataSource masterSlaveDataSource = applicationContext.getBean(masterSlaveDataSourceName, OrchestrationShardingSphereDataSource.class);
+ ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(masterSlaveDataSource, "dataSource");
return (MasterSlaveRule) dataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getRules().iterator().next();
}
@@ -99,8 +100,8 @@ public class OrchestrationMasterSlaveNamespaceTest extends AbstractJUnit4SpringC
}
private ConfigurationProperties getProperties(final String masterSlaveDataSourceName) {
- OrchestrationSpringShardingSphereDataSource masterSlaveDataSource = applicationContext.getBean(masterSlaveDataSourceName, OrchestrationSpringShardingSphereDataSource.class);
- ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(masterSlaveDataSource, "dataSource", true);
+ OrchestrationShardingSphereDataSource masterSlaveDataSource = applicationContext.getBean(masterSlaveDataSourceName, OrchestrationShardingSphereDataSource.class);
+ ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(masterSlaveDataSource, "dataSource");
return dataSource.getSchemaContexts().getProps();
}
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationShardingMasterSlaveNamespaceTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/Orchest [...]
index df0bc5b..f6d392b 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationShardingMasterSlaveNamespaceTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationShardingMasterSlaveNamespaceTest.java
@@ -18,9 +18,10 @@
package org.apache.shardingsphere.spring.namespace.orchestration;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
+import org.apache.shardingsphere.driver.orchestration.internal.datasource.OrchestrationShardingSphereDataSource;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.spring.namespace.orchestration.util.EmbedTestingServer;
import org.apache.shardingsphere.spring.namespace.orchestration.util.FieldValueUtil;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
@@ -57,14 +58,14 @@ public class OrchestrationShardingMasterSlaveNamespaceTest extends AbstractJUnit
@SuppressWarnings("unchecked")
private Map<String, DataSource> getDataSourceMap(final String shardingSphereDataSourceName) {
- OrchestrationSpringShardingSphereDataSource shardingSphereDataSource = applicationContext.getBean(shardingSphereDataSourceName, OrchestrationSpringShardingSphereDataSource.class);
- ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(shardingSphereDataSource, "dataSource", true);
+ OrchestrationShardingSphereDataSource shardingSphereDataSource = applicationContext.getBean(shardingSphereDataSourceName, OrchestrationShardingSphereDataSource.class);
+ ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(shardingSphereDataSource, "dataSource");
return dataSource.getDataSourceMap();
}
private ShardingRule getShardingRule(final String shardingSphereDataSourceName) {
- OrchestrationSpringShardingSphereDataSource shardingSphereDataSource = applicationContext.getBean(shardingSphereDataSourceName, OrchestrationSpringShardingSphereDataSource.class);
- ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(shardingSphereDataSource, "dataSource", true);
+ OrchestrationShardingSphereDataSource shardingSphereDataSource = applicationContext.getBean(shardingSphereDataSourceName, OrchestrationShardingSphereDataSource.class);
+ ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(shardingSphereDataSource, "dataSource");
return (ShardingRule) dataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getRules().iterator().next();
}
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationShardingNamespaceTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationShard [...]
index 6d245e3..f248f07 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationShardingNamespaceTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/OrchestrationShardingNamespaceTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.spring.namespace.orchestration;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
+import org.apache.shardingsphere.driver.orchestration.internal.datasource.OrchestrationShardingSphereDataSource;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
@@ -145,8 +146,8 @@ public class OrchestrationShardingNamespaceTest extends AbstractJUnit4SpringCont
@Test
public void assertPropsDataSource() {
- OrchestrationSpringShardingSphereDataSource shardingSphereDataSource = applicationContext.getBean("propsDataSourceOrchestration", OrchestrationSpringShardingSphereDataSource.class);
- ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(shardingSphereDataSource, "dataSource", true);
+ OrchestrationShardingSphereDataSource shardingSphereDataSource = applicationContext.getBean("propsDataSourceOrchestration", OrchestrationShardingSphereDataSource.class);
+ ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(shardingSphereDataSource, "dataSource");
assertTrue(dataSource.getSchemaContexts().getProps().<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW));
boolean showSql = dataSource.getSchemaContexts().getProps().getValue(ConfigurationPropertyKey.SQL_SHOW);
assertTrue(showSql);
@@ -156,13 +157,13 @@ public class OrchestrationShardingNamespaceTest extends AbstractJUnit4SpringCont
@Test
public void assertShardingSphereDataSourceType() {
- assertTrue(applicationContext.getBean("simpleShardingOrchestration") instanceof OrchestrationSpringShardingSphereDataSource);
+ assertTrue(applicationContext.getBean("simpleShardingOrchestration") instanceof OrchestrationShardingSphereDataSource);
}
@Test
public void assertDefaultActualDataNodes() {
- OrchestrationSpringShardingSphereDataSource multiTableRulesDataSource = applicationContext.getBean("multiTableRulesDataSourceOrchestration", OrchestrationSpringShardingSphereDataSource.class);
- ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(multiTableRulesDataSource, "dataSource", true);
+ OrchestrationShardingSphereDataSource multiTableRulesDataSource = applicationContext.getBean("multiTableRulesDataSourceOrchestration", OrchestrationShardingSphereDataSource.class);
+ ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(multiTableRulesDataSource, "dataSource");
ShardingRule shardingRule = (ShardingRule) dataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getRules().iterator().next();
assertThat(shardingRule.getTableRules().size(), is(2));
Iterator<TableRule> tableRules = shardingRule.getTableRules().iterator();
@@ -177,14 +178,14 @@ public class OrchestrationShardingNamespaceTest extends AbstractJUnit4SpringCont
}
private Map<String, DataSource> getDataSourceMap(final String dataSourceName) {
- OrchestrationSpringShardingSphereDataSource shardingSphereDataSource = applicationContext.getBean(dataSourceName, OrchestrationSpringShardingSphereDataSource.class);
- ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(shardingSphereDataSource, "dataSource", true);
+ OrchestrationShardingSphereDataSource shardingSphereDataSource = applicationContext.getBean(dataSourceName, OrchestrationShardingSphereDataSource.class);
+ ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(shardingSphereDataSource, "dataSource");
return dataSource.getDataSourceMap();
}
private ShardingRule getShardingRule(final String dataSourceName) {
- OrchestrationSpringShardingSphereDataSource shardingSphereDataSource = applicationContext.getBean(dataSourceName, OrchestrationSpringShardingSphereDataSource.class);
- ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(shardingSphereDataSource, "dataSource", true);
+ OrchestrationShardingSphereDataSource shardingSphereDataSource = applicationContext.getBean(dataSourceName, OrchestrationShardingSphereDataSource.class);
+ ShardingSphereDataSource dataSource = (ShardingSphereDataSource) FieldValueUtil.getFieldValue(shardingSphereDataSource, "dataSource");
return (ShardingRule) dataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getRules().iterator().next();
}
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/util/FieldValueUtil.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/util/FieldValueUtil.java
index db3f524..3dda37f 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/util/FieldValueUtil.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/orchestration/util/FieldValueUtil.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.spring.namespace.orchestration.util;
-import com.google.common.base.Strings;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
@@ -32,21 +31,13 @@ public final class FieldValueUtil {
*
* @param obj obj
* @param fieldName field name
- * @param isFromSuperClass is from super class
* @return field value
*/
- public static Object getFieldValue(final Object obj, final String fieldName, final boolean isFromSuperClass) {
- if (null == obj || Strings.isNullOrEmpty(fieldName)) {
- return null;
- }
- Class<?> clazz = isFromSuperClass ? obj.getClass().getSuperclass() : obj.getClass();
- return getFieldValue(clazz, obj, fieldName);
- }
-
@SneakyThrows(ReflectiveOperationException.class)
- private static Object getFieldValue(final Class<?> clazz, final Object obj, final String fieldName) {
- Field field = clazz.getDeclaredField(fieldName);
+ public static Object getFieldValue(final Object obj, final String fieldName) {
+ Field field = obj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(obj);
}
+
}