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 2022/02/09 11:00:12 UTC

[shardingsphere] branch master updated: Refactor NarayanaConfigurationFileGenerator (#15322)

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 e7fae1c  Refactor NarayanaConfigurationFileGenerator (#15322)
e7fae1c is described below

commit e7fae1c3f22fcd082f08f9ae67be62108d9b8535
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Feb 9 18:59:17 2022 +0800

    Refactor NarayanaConfigurationFileGenerator (#15322)
    
    * Refactor NarayanaConfigurationFileGenerator
    
    * Refactor NarayanaConfigurationFileGenerator
---
 .../NarayanaConfigurationFileGenerator.java        | 73 ++++++++++++----------
 ...ction.spi.TransactionConfigurationFileGenerator |  2 +-
 .../src/main/resources/conf/server.yaml            |  2 +-
 3 files changed, 42 insertions(+), 35 deletions(-)

diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-provider/shardingsphere-transaction-xa-narayana/src/main/java/org/apache/shardingsphere/transaction/xa/narayana/util/NarayanaConfigurationFileGenerator.java b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-provider/shardingsphere-transaction-xa- [...]
similarity index 69%
rename from shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-provider/shardingsphere-transaction-xa-narayana/src/main/java/org/apache/shardingsphere/transaction/xa/narayana/util/NarayanaConfigurationFileGenerator.java
rename to shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-provider/shardingsphere-transaction-xa-narayana/src/main/java/org/apache/shardingsphere/transaction/xa/narayana/config/NarayanaConfigurationFileGenerator.java
index 1dfb712..89b7a8e 100644
--- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-provider/shardingsphere-transaction-xa-narayana/src/main/java/org/apache/shardingsphere/transaction/xa/narayana/util/NarayanaConfigurationFileGenerator.java
+++ b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-provider/shardingsphere-transaction-xa-narayana/src/main/java/org/apache/shardingsphere/transaction/xa/narayana/config/NarayanaConfigurationFileGenerator.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.transaction.xa.narayana.util;
+package org.apache.shardingsphere.transaction.xa.narayana.config;
 
 import com.arjuna.ats.internal.arjuna.objectstore.jdbc.JDBCStore;
 import com.arjuna.ats.internal.arjuna.objectstore.jdbc.accessors.DynamicDataSourceJDBCAccess;
@@ -38,6 +38,7 @@ import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 
 /**
  * Narayana transaction configuration file generator.
@@ -47,36 +48,27 @@ public final class NarayanaConfigurationFileGenerator implements TransactionConf
     
     @Override
     public void generateFile(final TransactionRule transactionRule, final String instanceId) {
-        Map<Object, Object> content = generateDefaultNarayanaConfig(instanceId);
+        Map<String, Object> config = createDefaultConfiguration(instanceId);
         if (null != transactionRule.getProps()) {
-            swapJdbcStore(transactionRule, content);
+            appendJdbcStoreConfiguration(transactionRule, config);
         }
-        String value = narayanaConfigMapToXml(content);
-        String path = ClassLoader.getSystemResource("").getPath();
-        try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(path, "jbossts-properties.xml"))) {
-            bufferedWriter.write(value);
-            bufferedWriter.flush();
+        try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(ClassLoader.getSystemResource("").getPath(), "jbossts-properties.xml"))) {
+            writer.write(convertToXMLFormat(config));
+            writer.flush();
         } catch (final IOException ex) {
-            log.error("generate narayana config file failed.");
+            log.error("Generate narayana configuration file failed.");
         }
     }
     
-    private static Map<Object, Object> generateDefaultNarayanaConfig(final String instanceId) {
-        Map<Object, Object> result = new LinkedHashMap<>(32, 1);
+    private static Map<String, Object> createDefaultConfiguration(final String instanceId) {
+        Map<String, Object> result = new LinkedHashMap<>(32, 1);
         result.put("CoordinatorEnvironmentBean.commitOnePhase", "YES");
         result.put("ObjectStoreEnvironmentBean.transactionSync", "ON");
         result.put("CoreEnvironmentBean.nodeIdentifier", null == instanceId ? 1 : instanceId);
         result.put("JTAEnvironmentBean.xaRecoveryNodes", null == instanceId ? 1 : instanceId);
-        Collection<String> xaResourceOrphanFilterClassNames = new LinkedList<>();
-        xaResourceOrphanFilterClassNames.add(JTATransactionLogXAResourceOrphanFilter.class.getName());
-        xaResourceOrphanFilterClassNames.add(JTANodeNameXAResourceOrphanFilter.class.getName());
-        xaResourceOrphanFilterClassNames.add(JTAActionStatusServiceXAResourceOrphanFilter.class.getName());
-        result.put("JTAEnvironmentBean.xaResourceOrphanFilterClassNames", xaResourceOrphanFilterClassNames);
+        result.put("JTAEnvironmentBean.xaResourceOrphanFilterClassNames", createXAResourceOrphanFilterClassNames());
         result.put("CoreEnvironmentBean.socketProcessIdPort", 0);
-        Collection<String> recoveryModuleClassNames = new LinkedList<>();
-        recoveryModuleClassNames.add(AtomicActionRecoveryModule.class.getName());
-        recoveryModuleClassNames.add(XARecoveryModule.class.getName());
-        result.put("RecoveryEnvironmentBean.recoveryModuleClassNames", recoveryModuleClassNames);
+        result.put("RecoveryEnvironmentBean.recoveryModuleClassNames", getRecoveryModuleClassNames());
         result.put("RecoveryEnvironmentBean.expiryScannerClassNames", ExpiredTransactionStatusManagerScanner.class.getName());
         result.put("RecoveryEnvironmentBean.recoveryPort", 4712);
         result.put("RecoveryEnvironmentBean.recoveryAddress", null);
@@ -87,15 +79,29 @@ public final class NarayanaConfigurationFileGenerator implements TransactionConf
         return result;
     }
     
-    private static void swapJdbcStore(final TransactionRule transactionRule, final Map<Object, Object> config) {
-        Object host = transactionRule.getProps().get("host");
-        Object port = transactionRule.getProps().get("port");
-        Object user = transactionRule.getProps().getProperty("user");
-        Object password = transactionRule.getProps().getProperty("password");
-        Object databaseName = transactionRule.getProps().getProperty("databaseName");
+    private static Collection<String> createXAResourceOrphanFilterClassNames() {
+        Collection<String> result = new LinkedList<>();
+        result.add(JTATransactionLogXAResourceOrphanFilter.class.getName());
+        result.add(JTANodeNameXAResourceOrphanFilter.class.getName());
+        result.add(JTAActionStatusServiceXAResourceOrphanFilter.class.getName());
+        return result;
+    }
+    
+    private static Collection<String> getRecoveryModuleClassNames() {
+        Collection<String> result = new LinkedList<>();
+        result.add(AtomicActionRecoveryModule.class.getName());
+        result.add(XARecoveryModule.class.getName());
+        return result;
+    }
+    
+    private static void appendJdbcStoreConfiguration(final TransactionRule transactionRule, final Map<String, Object> config) {
+        String host = transactionRule.getProps().getProperty("host");
+        String port = transactionRule.getProps().getProperty("port");
+        String user = transactionRule.getProps().getProperty("user");
+        String password = transactionRule.getProps().getProperty("password");
+        String databaseName = transactionRule.getProps().getProperty("databaseName");
         if (null != host && null != port && null != user && null != password && null != databaseName) {
-            String jdbcAccessPatten = DynamicDataSourceJDBCAccess.class.getName()
-                    + ";ClassName=com.mysql.cj.jdbc.MysqlDataSource;URL=jdbc:mysql://%s:%d/%s;User=%s;Password=%s";
+            String jdbcAccessPatten =  DynamicDataSourceJDBCAccess.class.getName() + "ClassName=com.mysql.cj.jdbc.MysqlDataSource;URL=jdbc:mysql://%s:%d/%s;User=%s;Password=%s";
             String jdbcAccess = String.format(jdbcAccessPatten, host, port, databaseName, user, password);
             config.put("ObjectStoreEnvironmentBean.objectStoreType", JDBCStore.class.getName());
             config.put("ObjectStoreEnvironmentBean.jdbcAccess", jdbcAccess);
@@ -112,14 +118,15 @@ public final class NarayanaConfigurationFileGenerator implements TransactionConf
         }
     }
     
-    private static String narayanaConfigMapToXml(final Map<Object, Object> config) {
+    // TODO use JAXB to process XML gracefully
+    private static String convertToXMLFormat(final Map<String, Object> config) {
         StringBuilder result = new StringBuilder("<properties>");
-        for (Object each : config.keySet()) {
+        for (Entry<String, Object> entry : config.entrySet()) {
             result.append("\n\t");
-            Object value = config.get(each);
-            result.append(String.format("<entry key=\"%s\">", each));
+            Object value = entry.getValue();
+            result.append(String.format("<entry key=\"%s\">", entry.getKey()));
             if (value instanceof List) {
-                for (Object i : (List) value) {
+                for (Object i : (List<?>) value) {
                     result.append("\n\t\t");
                     result.append(i);
                 }
diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-provider/shardingsphere-transaction-xa-narayana/src/main/resources/META-INF/services/org.apache.shardingsphere.transaction.spi.TransactionConfigurationFileGenerator b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-provider/shardingsphere-transac [...]
index fef27c8..9b4e5e6 100644
--- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-provider/shardingsphere-transaction-xa-narayana/src/main/resources/META-INF/services/org.apache.shardingsphere.transaction.spi.TransactionConfigurationFileGenerator
+++ b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-provider/shardingsphere-transaction-xa-narayana/src/main/resources/META-INF/services/org.apache.shardingsphere.transaction.spi.TransactionConfigurationFileGenerator
@@ -15,4 +15,4 @@
 # limitations under the License.
 #
 
-org.apache.shardingsphere.transaction.xa.narayana.util.NarayanaConfigurationFileGenerator
+org.apache.shardingsphere.transaction.xa.narayana.config.NarayanaConfigurationFileGenerator
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/server.yaml b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/server.yaml
index e4f34a0..b8e2e8a 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/server.yaml
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/server.yaml
@@ -44,7 +44,7 @@
 #  - !TRANSACTION
 #    defaultType: XA
 #    providerType: Atomikos
-#    # When the providertype is Narayana, the following properties can be configured or not
+#    # When the provider type is Narayana, the following properties can be configured or not
 #    props:
 #      databaseName: jbossts
 #      host: 127.0.0.1