You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@seata.apache.org by ji...@apache.org on 2024/11/04 01:34:03 UTC

(incubator-seata) branch 2.x updated: feature: add fastjson2 undolog parser (#6974)

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

jianbin pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git


The following commit(s) were added to refs/heads/2.x by this push:
     new 363e1792ba feature: add fastjson2 undolog parser (#6974)
363e1792ba is described below

commit 363e1792ba52e1dfeea049842ca5b71d373d49fe
Author: GoodBoyCoder <go...@outlook.com>
AuthorDate: Mon Nov 4 09:33:59 2024 +0800

    feature: add fastjson2 undolog parser (#6974)
---
 changes/en-us/2.x.md                               |  1 +
 changes/zh-cn/2.x.md                               |  1 +
 rm-datasource/pom.xml                              |  6 ++
 .../undo/parser/Fastjson2UndoLogParser.java        | 77 ++++++++++++++++++++++
 ...g.apache.seata.rm.datasource.undo.UndoLogParser |  3 +-
 .../undo/parser/Fastjson2UndoLogParserTest.java    | 36 ++++++++++
 6 files changed, 123 insertions(+), 1 deletion(-)

diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index 969b9578be..f0c1b9198a 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -7,6 +7,7 @@ Add changes here for all PR submitted to the 2.x branch.
 - [[#6876](https://github.com/apache/incubator-seata/pull/6876)] support kingbase
 - [[#6881](https://github.com/apache/incubator-seata/pull/6881)] support grpc
 - [[#6864](https://github.com/apache/incubator-seata/pull/6864)] support shentong database
+- [[#6974](https://github.com/apache/incubator-seata/pull/6974)] support fastjson2 undolog parser
 
 
 ### bugfix:
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index 873f126b64..ae9c1047dd 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -7,6 +7,7 @@
 - [[#6876](https://github.com/apache/incubator-seata/pull/6876)] 支持人大金仓数据库(kingbase)
 - [[#6881](https://github.com/apache/incubator-seata/pull/6881)] client和server支持grpc协议
 - [[#6864](https://github.com/apache/incubator-seata/pull/6864)] 支持神通数据库(oscar)
+- [[#6974](https://github.com/apache/incubator-seata/pull/6974)] 支持UndoLog的fastjson2序列化方式
 
 
 ### bugfix:
diff --git a/rm-datasource/pom.xml b/rm-datasource/pom.xml
index ed466e2cdf..af2c0d09f5 100644
--- a/rm-datasource/pom.xml
+++ b/rm-datasource/pom.xml
@@ -111,6 +111,12 @@
             <scope>provided</scope>
             <optional>true</optional>
         </dependency>
+        <dependency>
+            <groupId>com.alibaba.fastjson2</groupId>
+            <artifactId>fastjson2</artifactId>
+            <scope>provided</scope>
+            <optional>true</optional>
+        </dependency>
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>druid</artifactId>
diff --git a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/undo/parser/Fastjson2UndoLogParser.java b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/undo/parser/Fastjson2UndoLogParser.java
new file mode 100644
index 0000000000..d26a041f70
--- /dev/null
+++ b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/undo/parser/Fastjson2UndoLogParser.java
@@ -0,0 +1,77 @@
+/*
+ * 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.seata.rm.datasource.undo.parser;
+
+import com.alibaba.fastjson2.JSONB;
+import com.alibaba.fastjson2.JSONReader;
+import com.alibaba.fastjson2.JSONWriter;
+import org.apache.seata.common.executor.Initialize;
+import org.apache.seata.common.loader.LoadLevel;
+import org.apache.seata.rm.datasource.undo.BranchUndoLog;
+import org.apache.seata.rm.datasource.undo.UndoLogParser;
+
+@LoadLevel(name = Fastjson2UndoLogParser.NAME)
+public class Fastjson2UndoLogParser implements UndoLogParser, Initialize {
+    public static final String NAME = "fastjson2";
+
+    private JSONReader.Feature[] jsonReaderFeature;
+    private JSONWriter.Feature[] jsonWriterFeature;
+    @Override
+    public void init() {
+        jsonReaderFeature = new JSONReader.Feature[]{
+            JSONReader.Feature.UseDefaultConstructorAsPossible,
+            // If not configured, it will be serialized based on public field and getter methods by default.
+            // After configuration, it will be deserialized based on non-static fields (including private).
+            // It will be safer under FieldBased configuration
+            JSONReader.Feature.FieldBased,
+            JSONReader.Feature.IgnoreAutoTypeNotMatch,
+            JSONReader.Feature.UseNativeObject,
+            JSONReader.Feature.SupportAutoType
+        };
+
+        jsonWriterFeature = new JSONWriter.Feature[]{
+            JSONWriter.Feature.WriteClassName,
+            JSONWriter.Feature.FieldBased,
+            JSONWriter.Feature.ReferenceDetection,
+            JSONWriter.Feature.WriteNulls,
+            JSONWriter.Feature.NotWriteDefaultValue,
+            JSONWriter.Feature.NotWriteHashMapArrayListClassName,
+            JSONWriter.Feature.WriteNameAsSymbol
+        };
+    }
+
+    @Override
+    public String getName() {
+        return NAME;
+    }
+
+    @Override
+    public byte[] getDefaultContent() {
+        return encode(new BranchUndoLog());
+    }
+
+    @Override
+    public byte[] encode(BranchUndoLog branchUndoLog) {
+        return JSONB.toBytes(branchUndoLog, jsonWriterFeature);
+    }
+
+    @Override
+    public BranchUndoLog decode(byte[] bytes) {
+        return JSONB.parseObject(bytes, BranchUndoLog.class, jsonReaderFeature);
+    }
+
+}
diff --git a/rm-datasource/src/main/resources/META-INF/services/org.apache.seata.rm.datasource.undo.UndoLogParser b/rm-datasource/src/main/resources/META-INF/services/org.apache.seata.rm.datasource.undo.UndoLogParser
index 26abca209e..9a32cda00b 100644
--- a/rm-datasource/src/main/resources/META-INF/services/org.apache.seata.rm.datasource.undo.UndoLogParser
+++ b/rm-datasource/src/main/resources/META-INF/services/org.apache.seata.rm.datasource.undo.UndoLogParser
@@ -17,4 +17,5 @@
 org.apache.seata.rm.datasource.undo.parser.FastjsonUndoLogParser
 org.apache.seata.rm.datasource.undo.parser.JacksonUndoLogParser
 org.apache.seata.rm.datasource.undo.parser.ProtostuffUndoLogParser
-org.apache.seata.rm.datasource.undo.parser.KryoUndoLogParser
\ No newline at end of file
+org.apache.seata.rm.datasource.undo.parser.KryoUndoLogParser
+org.apache.seata.rm.datasource.undo.parser.Fastjson2UndoLogParser
\ No newline at end of file
diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/undo/parser/Fastjson2UndoLogParserTest.java b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/undo/parser/Fastjson2UndoLogParserTest.java
new file mode 100644
index 0000000000..21059c8a81
--- /dev/null
+++ b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/undo/parser/Fastjson2UndoLogParserTest.java
@@ -0,0 +1,36 @@
+/*
+ * 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.seata.rm.datasource.undo.parser;
+
+import org.apache.seata.common.loader.EnhancedServiceLoader;
+import org.apache.seata.rm.datasource.undo.BaseUndoLogParserTest;
+import org.apache.seata.rm.datasource.undo.UndoLogParser;
+
+
+public class Fastjson2UndoLogParserTest extends BaseUndoLogParserTest {
+
+    Fastjson2UndoLogParser parser = (Fastjson2UndoLogParser) EnhancedServiceLoader.load(UndoLogParser.class, Fastjson2UndoLogParser.NAME);
+
+    @Override
+    public UndoLogParser getParser() {
+        return parser;
+    }
+
+    @Override
+    public void testTimestampEncodeAndDecode() {
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org