This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 be554f5  Restart TransactionContext when alter transaction rule in 
narayana xa mode (#15957)
be554f5 is described below

commit be554f50e4ec3d2791c017cd4174c8093f1dc0cb
Author: JingShang Lu <[email protected]>
AuthorDate: Fri Mar 11 12:43:31 2022 +0800

    Restart TransactionContext when alter transaction rule in narayana xa mode 
(#15957)
    
    * renew transaction manager engine after alter transaction rule
---
 .../type/dialect/OpenGaussDatabaseType.java        |  2 +-
 .../metadata/rule/ShardingSphereRuleMetaData.java  | 21 ++++++++++++++--
 .../config/TransactionRuleConfiguration.java       | 12 ++++++++++
 .../mode/manager/ContextManager.java               | 28 ++++++++++++++++++++++
 .../cluster/ClusterContextManagerBuilder.java      | 11 ++++++++-
 .../ClusterContextManagerCoordinator.java          |  1 +
 .../updatable/AlterTransactionRuleHandler.java     |  9 ++++++-
 7 files changed, 79 insertions(+), 5 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/OpenGaussDatabaseType.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/OpenGaussDatabaseType.java
index b41293b..79934ec 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/OpenGaussDatabaseType.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/OpenGaussDatabaseType.java
@@ -52,6 +52,6 @@ public final class OpenGaussDatabaseType implements 
DatabaseType {
     
     @Override
     public Optional<String> getDataSourceClassName() {
-        return Optional.of("org.postgresql.ds.PGSimpleDataSource");
+        return Optional.of("org.opengauss.ds.PGSimpleDataSource");
     }
 }
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 26723cf..e91cecf 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
@@ -26,7 +26,6 @@ import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Optional;
-import java.util.stream.Collectors;
 
 /**
  * ShardingSphere rule meta data.
@@ -64,7 +63,25 @@ public final class ShardingSphereRuleMetaData {
      * @return found rule configurations
      */
     public <T extends RuleConfiguration> Collection<T> 
findRuleConfiguration(final Class<T> clazz) {
-        return configurations.stream().filter(each -> 
clazz.isAssignableFrom(each.getClass())).map(clazz::cast).collect(Collectors.toList());
+        Collection<T> result = new LinkedList<>();
+        for (RuleConfiguration each : configurations) {
+            if (clazz.isAssignableFrom(each.getClass())) {
+                result.add(clazz.cast(each));
+            }
+        }
+        return result;
+    }
+    
+    /**
+     * Find single rule configuration by class.
+     *
+     * @param clazz target class
+     * @param <T> type of rule configuration
+     * @return found rule configurations
+     */
+    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());
     }
     
     /**
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 bce2fa6..514b5a0 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
@@ -21,6 +21,7 @@ import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.infra.config.scope.GlobalRuleConfiguration;
 
+import java.util.Objects;
 import java.util.Properties;
 
 /**
@@ -36,4 +37,15 @@ 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 2eb79a7..2a08298 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
@@ -47,6 +47,7 @@ import 
org.apache.shardingsphere.mode.metadata.MetaDataContexts;
 import org.apache.shardingsphere.mode.metadata.MetaDataContextsBuilder;
 import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
 import 
org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine;
+import 
org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
 import org.apache.shardingsphere.transaction.context.TransactionContexts;
 import org.apache.shardingsphere.transaction.rule.TransactionRule;
 import 
org.apache.shardingsphere.transaction.rule.builder.DefaultTransactionRuleConfigurationBuilder;
@@ -61,6 +62,7 @@ import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.Properties;
 import java.util.stream.Collectors;
@@ -308,10 +310,27 @@ public final class ContextManager implements 
AutoCloseable {
      */
     public void alterGlobalRuleConfiguration(final 
Collection<RuleConfiguration> ruleConfigurations) {
         if (!ruleConfigurations.isEmpty()) {
+            boolean needRenewTransaction = 
needRenewTransactionContext(ruleConfigurations);
             ShardingSphereRuleMetaData newGlobalRuleMetaData = new 
ShardingSphereRuleMetaData(ruleConfigurations,
                     GlobalRulesBuilder.buildRules(ruleConfigurations, 
metaDataContexts.getMetaDataMap()));
             
renewMetaDataContexts(rebuildMetaDataContexts(newGlobalRuleMetaData));
+            if (needRenewTransaction) {
+                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;
     }
     
     /**
@@ -528,6 +547,15 @@ public final class ContextManager implements AutoCloseable 
{
         return 
DataSourcePoolCreator.create(getChangedDataSourceConfiguration(originalMetaData,
 newDataSourcePropsMap));
     }
     
+    /**
+     * Reload all transaction context.
+     */
+    public void renewAllTransactionContext() {
+        for (Entry<String, ShardingSphereMetaData> entry : 
metaDataContexts.getMetaDataMap().entrySet()) {
+            renewTransactionContext(entry.getKey(), 
entry.getValue().getResource());
+        }
+    }
+    
     private void renewTransactionContext(final String schemaName, final 
ShardingSphereResource resource) {
         ShardingSphereTransactionManagerEngine changedStaleEngine = 
transactionContexts.getEngines().get(schemaName);
         if (null != changedStaleEngine) {
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 271f15a..5894cc9 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
@@ -39,6 +39,7 @@ import 
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositor
 import 
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration;
 import 
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryFactory;
 import org.apache.shardingsphere.schedule.core.api.ModeScheduleContextFactory;
+import 
org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
 import org.apache.shardingsphere.transaction.context.TransactionContexts;
 import 
org.apache.shardingsphere.transaction.context.TransactionContextsBuilder;
 import org.apache.shardingsphere.transaction.rule.TransactionRule;
@@ -83,8 +84,16 @@ public final class ClusterContextManagerBuilder implements 
ContextManagerBuilder
                 ? 
TransactionConfigurationFileGeneratorFactory.newInstance(transactionRule.get().getProviderType())
 : Optional.empty();
         if (schemaName.isPresent() && fileGenerator.isPresent()) {
             ShardingSphereMetaData metaData = 
metaDataContexts.getMetaData(schemaName.get());
-            return 
fileGenerator.get().getTransactionProps(transactionRule.get().getProps(),
+            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;
         }
         return new Properties();
     }
diff --git 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
index 7103dad..c65be2f 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
@@ -223,6 +223,7 @@ public final class ClusterContextManagerCoordinator {
     public synchronized void renew(final XaRecoveryIdEvent event) {
         if 
(contextManager.getInstanceContext().getInstance().getInstanceDefinition().getInstanceId().getId().equals(event.getInstanceId()))
 {
             
contextManager.getInstanceContext().updateXaRecoveryId(event.getXaRecoveryId());
+            contextManager.renewAllTransactionContext();
         }
     }
     
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTransactionRuleHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTransactionRuleHandler.java
index b2135fb..aa4aa0c 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTransactionRuleHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTransactionRuleHandler.java
@@ -20,14 +20,17 @@ package 
org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.updatabl
 import 
org.apache.shardingsphere.distsql.parser.statement.ral.common.updatable.AlterTransactionRuleStatement;
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import 
org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
 import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import 
org.apache.shardingsphere.proxy.backend.text.distsql.ral.UpdatableRALBackendHandler;
 import 
org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
+import org.apache.shardingsphere.transaction.rule.TransactionRule;
 
 import java.util.Collection;
+import java.util.LinkedList;
 import java.util.Optional;
 
 /**
@@ -43,10 +46,14 @@ public final class AlterTransactionRuleHandler extends 
UpdatableRALBackendHandle
     private void updateTransactionRule() {
         MetaDataContexts metaDataContexts = 
ProxyContext.getInstance().getContextManager().getMetaDataContexts();
         ShardingSphereRuleMetaData globalRuleMetaData = 
metaDataContexts.getGlobalRuleMetaData();
-        Collection<RuleConfiguration> globalRuleConfigurations = 
globalRuleMetaData.getConfigurations();
+        Collection<ShardingSphereRule> globalRules = 
globalRuleMetaData.getRules();
+        globalRules.removeIf(each -> each instanceof TransactionRule);
+        Collection<RuleConfiguration> globalRuleConfigurations = new 
LinkedList<>(globalRuleMetaData.getConfigurations());
         globalRuleConfigurations.removeIf(each -> each instanceof 
TransactionRuleConfiguration);
         TransactionRuleConfiguration toBeAlteredRuleConfig = 
buildTransactionRuleConfiguration();
+        globalRules.add(new TransactionRule(toBeAlteredRuleConfig));
         globalRuleConfigurations.add(toBeAlteredRuleConfig);
+        
ProxyContext.getInstance().getContextManager().renewAllTransactionContext();
         Optional<MetaDataPersistService> metaDataPersistService = 
metaDataContexts.getMetaDataPersistService();
         if (metaDataPersistService.isPresent() && null != 
metaDataPersistService.get().getGlobalRuleService()) {
             
metaDataPersistService.get().getGlobalRuleService().persist(globalRuleConfigurations,
 true);

Reply via email to