This is an automated email from the ASF dual-hosted git repository.
lujingshang 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 56db6aa Revise #15957 (#15987)
56db6aa is described below
commit 56db6aad7bb762ab5da3ffb6b47578f10a019735
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Mar 11 13:23:45 2022 +0800
Revise #15957 (#15987)
---
.../metadata/rule/ShardingSphereRuleMetaData.java | 6 ++---
.../config/TransactionRuleConfiguration.java | 14 ++---------
.../mode/manager/ContextManager.java | 27 ++++++++--------------
.../cluster/ClusterContextManagerBuilder.java | 26 ++++++++++-----------
4 files changed, 27 insertions(+), 46 deletions(-)
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/rule/ShardingSphereRuleMetaData.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/rule/ShardingSphereRuleMetaData.java
index e91cecf..73a3a0d 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/rule/ShardingSphereRuleMetaData.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/rule/ShardingSphereRuleMetaData.java
@@ -77,11 +77,11 @@ public final class ShardingSphereRuleMetaData {
*
* @param clazz target class
* @param <T> type of rule configuration
- * @return found rule configurations
+ * @return found rule configuration
*/
public <T extends RuleConfiguration> Optional<T>
findSingleRuleConfiguration(final Class<T> clazz) {
- Collection<T> foundRuleConfiguration = findRuleConfiguration(clazz);
- return foundRuleConfiguration.isEmpty() ? Optional.empty() :
Optional.of(foundRuleConfiguration.iterator().next());
+ Collection<T> foundRuleConfig = findRuleConfiguration(clazz);
+ return foundRuleConfig.isEmpty() ? Optional.empty() :
Optional.of(foundRuleConfig.iterator().next());
}
/**
diff --git
a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-api/src/main/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfiguration.java
b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-api/src/main/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfiguration.java
index 514b5a0..ebb4a8f 100644
---
a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-api/src/main/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfiguration.java
+++
b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-api/src/main/java/org/apache/shardingsphere/transaction/config/TransactionRuleConfiguration.java
@@ -17,11 +17,11 @@
package org.apache.shardingsphere.transaction.config;
+import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.scope.GlobalRuleConfiguration;
-import java.util.Objects;
import java.util.Properties;
/**
@@ -30,6 +30,7 @@ import java.util.Properties;
*/
@RequiredArgsConstructor
@Getter
+@EqualsAndHashCode
public final class TransactionRuleConfiguration implements
GlobalRuleConfiguration {
private final String defaultType;
@@ -37,15 +38,4 @@ public final class TransactionRuleConfiguration implements
GlobalRuleConfigurati
private final String providerType;
private final Properties props;
-
- /**
- * Compare to another transaction rule configuration.
- *
- * @param transactionRuleConfiguration transaction rule configuration
- * @return return true if the two transactionRuleConfiguration are the same
- */
- public boolean compare(final TransactionRuleConfiguration
transactionRuleConfiguration) {
- return Objects.equals(defaultType,
transactionRuleConfiguration.defaultType) && Objects.equals(providerType,
transactionRuleConfiguration.providerType)
- && Objects.equals(props, transactionRuleConfiguration.props);
- }
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
index 2a08298..1cad081 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
@@ -306,31 +306,22 @@ public final class ContextManager implements
AutoCloseable {
/**
* Alter global rule configuration.
*
- * @param ruleConfigurations global rule configuration
+ * @param ruleConfigs global rule configuration
*/
- public void alterGlobalRuleConfiguration(final
Collection<RuleConfiguration> ruleConfigurations) {
- if (!ruleConfigurations.isEmpty()) {
- boolean needRenewTransaction =
needRenewTransactionContext(ruleConfigurations);
- ShardingSphereRuleMetaData newGlobalRuleMetaData = new
ShardingSphereRuleMetaData(ruleConfigurations,
- GlobalRulesBuilder.buildRules(ruleConfigurations,
metaDataContexts.getMetaDataMap()));
+ public void alterGlobalRuleConfiguration(final
Collection<RuleConfiguration> ruleConfigs) {
+ if (!ruleConfigs.isEmpty()) {
+ ShardingSphereRuleMetaData newGlobalRuleMetaData = new
ShardingSphereRuleMetaData(ruleConfigs,
GlobalRulesBuilder.buildRules(ruleConfigs, metaDataContexts.getMetaDataMap()));
renewMetaDataContexts(rebuildMetaDataContexts(newGlobalRuleMetaData));
- if (needRenewTransaction) {
+ if (isNeedRenewTransactionContext(ruleConfigs)) {
renewAllTransactionContext();
}
}
}
- private boolean needRenewTransactionContext(final
Collection<RuleConfiguration> ruleConfigurations) {
- for (RuleConfiguration each : ruleConfigurations) {
- if (each instanceof TransactionRuleConfiguration) {
- Optional<TransactionRuleConfiguration> old =
metaDataContexts.getGlobalRuleMetaData().findSingleRuleConfiguration(TransactionRuleConfiguration.class);
- if (old.isPresent() && !((TransactionRuleConfiguration)
each).compare(old.get())) {
- return true;
- }
- break;
- }
- }
- return false;
+ private boolean isNeedRenewTransactionContext(final
Collection<RuleConfiguration> ruleConfigs) {
+ Optional<RuleConfiguration> newConfig =
ruleConfigs.stream().filter(each -> each instanceof
TransactionRuleConfiguration).findFirst();
+ Optional<TransactionRuleConfiguration> oldConfig =
metaDataContexts.getGlobalRuleMetaData().findSingleRuleConfiguration(TransactionRuleConfiguration.class);
+ return newConfig.isPresent() && oldConfig.isPresent() &&
!newConfig.get().equals(oldConfig.get());
}
/**
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
index 5894cc9..6253e39 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.mode.manager.cluster;
+import com.google.common.base.Preconditions;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.config.schema.SchemaConfiguration;
@@ -82,20 +83,19 @@ public final class ClusterContextManagerBuilder implements
ContextManagerBuilder
metaDataContexts.getGlobalRuleMetaData().getRules().stream().filter(each ->
each instanceof TransactionRule).map(each -> (TransactionRule)
each).findFirst();
Optional<TransactionConfigurationFileGenerator> fileGenerator =
transactionRule.isPresent()
?
TransactionConfigurationFileGeneratorFactory.newInstance(transactionRule.get().getProviderType())
: Optional.empty();
- if (schemaName.isPresent() && fileGenerator.isPresent()) {
- ShardingSphereMetaData metaData =
metaDataContexts.getMetaData(schemaName.get());
- Properties result =
fileGenerator.get().getTransactionProps(transactionRule.get().getProps(),
- new
DataSourceProvidedSchemaConfiguration(metaData.getResource().getDataSources(),
metaData.getRuleMetaData().getConfigurations()), getType());
- Optional<TransactionRuleConfiguration>
transactionRuleConfiguration =
metaDataContexts.getGlobalRuleMetaData().findSingleRuleConfiguration(TransactionRuleConfiguration.class);
- if (transactionRule.isPresent()) {
- transactionRuleConfiguration.get().getProps().clear();
- transactionRuleConfiguration.get().getProps().putAll(result);
- transactionRule.get().getProps().clear();
- transactionRule.get().getProps().putAll(result);
- }
- return result;
+ if (!schemaName.isPresent() || !fileGenerator.isPresent()) {
+ return new Properties();
}
- return new Properties();
+ ShardingSphereMetaData metaData =
metaDataContexts.getMetaData(schemaName.get());
+ Properties result =
fileGenerator.get().getTransactionProps(transactionRule.get().getProps(),
+ new
DataSourceProvidedSchemaConfiguration(metaData.getResource().getDataSources(),
metaData.getRuleMetaData().getConfigurations()), getType());
+ Optional<TransactionRuleConfiguration> transactionRuleConfig =
metaDataContexts.getGlobalRuleMetaData().findSingleRuleConfiguration(TransactionRuleConfiguration.class);
+ Preconditions.checkState(transactionRuleConfig.isPresent());
+ transactionRuleConfig.get().getProps().clear();
+ transactionRuleConfig.get().getProps().putAll(result);
+ transactionRule.get().getProps().clear();
+ transactionRule.get().getProps().putAll(result);
+ return result;
}
private void persistTransactionConfiguration(final
ContextManagerBuilderParameter parameter, final MetaDataPersistService
metaDataPersistService, final Properties transactionProps) {