From 422b58de27cb0189058a72d229a4c161c218b0af Mon Sep 17 00:00:00 2001 From: GoodBoyCoder Date: Tue, 8 Oct 2024 21:47:16 +0800 Subject: [PATCH 01/10] feature: add fastjson2 serializer support --- all/pom.xml | 5 ++ .../serializer/SerializerServiceLoader.java | 3 +- .../seata/core/serializer/SerializerType.java | 7 ++ dependencies/pom.xml | 6 ++ serializer/pom.xml | 1 + serializer/seata-serializer-all/pom.xml | 5 ++ serializer/seata-serializer-fastjson2/pom.xml | 57 +++++++++++++ .../Fastjson2Serializer.java | 38 +++++++++ .../Fastjson2SerializerFactory.java | 76 +++++++++++++++++ ...rg.apache.seata.core.serializer.Serializer | 17 ++++ .../fastjson2/Fastjson2SerializerTest.java | 85 +++++++++++++++++++ 11 files changed, 299 insertions(+), 1 deletion(-) create mode 100644 serializer/seata-serializer-fastjson2/pom.xml create mode 100644 serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2Serializer.java create mode 100644 serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java create mode 100644 serializer/seata-serializer-fastjson2/src/main/resources/META-INF/services/org.apache.seata.core.serializer.Serializer create mode 100644 serializer/seata-serializer-fastjson2/src/test/java/org/apache/seata/serializer/fastjson2/Fastjson2SerializerTest.java diff --git a/all/pom.xml b/all/pom.xml index 7e4eb9ab293..dfffb07feea 100644 --- a/all/pom.xml +++ b/all/pom.xml @@ -272,6 +272,11 @@ seata-serializer-hessian ${project.version} + + org.apache.seata + seata-serializer-fastjson2 + ${project.version} + org.apache.seata seata-compressor-gzip diff --git a/core/src/main/java/org/apache/seata/core/serializer/SerializerServiceLoader.java b/core/src/main/java/org/apache/seata/core/serializer/SerializerServiceLoader.java index 0aa9bd340e8..359867b5104 100644 --- a/core/src/main/java/org/apache/seata/core/serializer/SerializerServiceLoader.java +++ b/core/src/main/java/org/apache/seata/core/serializer/SerializerServiceLoader.java @@ -30,6 +30,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.apache.seata.core.serializer.SerializerType.FASTJSON2; import static org.apache.seata.core.serializer.SerializerType.HESSIAN; import static org.apache.seata.core.serializer.SerializerType.KRYO; import static org.apache.seata.core.serializer.SerializerType.PROTOBUF; @@ -46,7 +47,7 @@ public final class SerializerServiceLoader { private static final Logger LOGGER = LoggerFactory.getLogger(SerializerServiceLoader.class); private static final Configuration CONFIG = ConfigurationFactory.getInstance(); - private static final SerializerType[] DEFAULT_SERIALIZER_TYPE = new SerializerType[]{SEATA, PROTOBUF, KRYO, HESSIAN}; + private static final SerializerType[] DEFAULT_SERIALIZER_TYPE = new SerializerType[]{SEATA, PROTOBUF, KRYO, HESSIAN, FASTJSON2}; private final static Map SERIALIZER_MAP = new HashMap<>(); diff --git a/core/src/main/java/org/apache/seata/core/serializer/SerializerType.java b/core/src/main/java/org/apache/seata/core/serializer/SerializerType.java index c60067e72bc..197340305e8 100644 --- a/core/src/main/java/org/apache/seata/core/serializer/SerializerType.java +++ b/core/src/main/java/org/apache/seata/core/serializer/SerializerType.java @@ -63,6 +63,13 @@ public enum SerializerType { * Math.pow(2, 5) */ JACKSON((byte)0x32), + + /** + * The fastjson2. + *

+ * Math.pow(2, 5) + */ + FASTJSON2((byte)0x64), ; private final byte code; diff --git a/dependencies/pom.xml b/dependencies/pom.xml index 15178f14166..6b93148e613 100644 --- a/dependencies/pom.xml +++ b/dependencies/pom.xml @@ -81,6 +81,7 @@ 5.4.0 0.45 4.0.63 + 2.0.52 2.4.4 1.5.0-4 1.4.20 @@ -662,6 +663,11 @@ hessian ${hessian.version} + + com.alibaba.fastjson2 + fastjson2 + ${fastjson2.version} + org.apache.commons commons-compress diff --git a/serializer/pom.xml b/serializer/pom.xml index 6b06a80f4e9..246ba6c2757 100644 --- a/serializer/pom.xml +++ b/serializer/pom.xml @@ -37,6 +37,7 @@ seata-serializer-seata seata-serializer-kryo seata-serializer-hessian + seata-serializer-fastjson2 diff --git a/serializer/seata-serializer-all/pom.xml b/serializer/seata-serializer-all/pom.xml index a772be672aa..a0d816153bf 100644 --- a/serializer/seata-serializer-all/pom.xml +++ b/serializer/seata-serializer-all/pom.xml @@ -50,5 +50,10 @@ seata-serializer-hessian ${project.version} + + ${project.groupId} + seata-serializer-fastjson2 + ${project.version} + diff --git a/serializer/seata-serializer-fastjson2/pom.xml b/serializer/seata-serializer-fastjson2/pom.xml new file mode 100644 index 00000000000..106de87cccd --- /dev/null +++ b/serializer/seata-serializer-fastjson2/pom.xml @@ -0,0 +1,57 @@ + + + + + org.apache.seata + seata-serializer + ${revision} + + 4.0.0 + seata-serializer-fastjson2 + jar + seata-serializer-fastjson2 ${project.version} + serializer-fastjson2 for Seata built with Maven + + + 8 + 8 + UTF-8 + + + + + ${project.groupId} + seata-common + ${project.version} + + + ${project.groupId} + seata-core + ${project.version} + + + com.alibaba.fastjson2 + fastjson2 + + + + \ No newline at end of file diff --git a/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2Serializer.java b/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2Serializer.java new file mode 100644 index 00000000000..4949928f756 --- /dev/null +++ b/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2Serializer.java @@ -0,0 +1,38 @@ +/* + * 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.serializer.fastjson2; + +import com.alibaba.fastjson2.JSONB; +import org.apache.seata.common.loader.LoadLevel; +import org.apache.seata.core.serializer.Serializer; + +/** + * @Author GoodBoyCoder + */ +@LoadLevel(name = "FASTJSON2") +public class Fastjson2Serializer implements Serializer { + + @Override + public byte[] serialize(T t) { + return JSONB.toBytes(t, Fastjson2SerializerFactory.getInstance().getJsonWriterFeatureList()); + } + + @Override + public T deserialize(byte[] bytes) { + return (T) JSONB.parseObject(bytes, Object.class, Fastjson2SerializerFactory.getInstance().getFilter(), Fastjson2SerializerFactory.getInstance().getJsonReaderFeatureList()); + } +} diff --git a/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java b/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java new file mode 100644 index 00000000000..1a299f99c78 --- /dev/null +++ b/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java @@ -0,0 +1,76 @@ +/* + * 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.serializer.fastjson2; + +import com.alibaba.fastjson2.JSONReader; +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.filter.Filter; +import org.apache.seata.core.serializer.SerializerSecurityRegistry; + +/** + * @Author GoodBoyCoder + */ +public class Fastjson2SerializerFactory { + private Filter autoTypeFilter; + + private JSONReader.Feature[] jsonReaderFeature; + + private JSONWriter.Feature[] jsonWriterFeature; + private static final class InstanceHolder { + public static final Fastjson2SerializerFactory instance = new Fastjson2SerializerFactory(); + } + + public Fastjson2SerializerFactory() { + autoTypeFilter = JSONReader.autoTypeFilter(true, SerializerSecurityRegistry.getAllowClassType().toArray(new Class[]{})); + + 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 + }; + + 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 + }; + } + + public static Fastjson2SerializerFactory getInstance() { + return Fastjson2SerializerFactory.InstanceHolder.instance; + } + + public Filter getFilter() { + return autoTypeFilter; + } + + public JSONReader.Feature[] getJsonReaderFeatureList() { + return jsonReaderFeature; + } + + public JSONWriter.Feature[] getJsonWriterFeatureList() { + return jsonWriterFeature; + } +} diff --git a/serializer/seata-serializer-fastjson2/src/main/resources/META-INF/services/org.apache.seata.core.serializer.Serializer b/serializer/seata-serializer-fastjson2/src/main/resources/META-INF/services/org.apache.seata.core.serializer.Serializer new file mode 100644 index 00000000000..1dab3f811c0 --- /dev/null +++ b/serializer/seata-serializer-fastjson2/src/main/resources/META-INF/services/org.apache.seata.core.serializer.Serializer @@ -0,0 +1,17 @@ +# +# 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. +# +org.apache.seata.serializer.fastjson2.Fastjson2Serializer \ No newline at end of file diff --git a/serializer/seata-serializer-fastjson2/src/test/java/org/apache/seata/serializer/fastjson2/Fastjson2SerializerTest.java b/serializer/seata-serializer-fastjson2/src/test/java/org/apache/seata/serializer/fastjson2/Fastjson2SerializerTest.java new file mode 100644 index 00000000000..d0dc1314670 --- /dev/null +++ b/serializer/seata-serializer-fastjson2/src/test/java/org/apache/seata/serializer/fastjson2/Fastjson2SerializerTest.java @@ -0,0 +1,85 @@ +/* + * 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.serializer.fastjson2; + +import org.apache.seata.core.exception.TransactionExceptionCode; +import org.apache.seata.core.model.BranchStatus; +import org.apache.seata.core.model.BranchType; +import org.apache.seata.core.protocol.ResultCode; +import org.apache.seata.core.protocol.transaction.BranchCommitRequest; +import org.apache.seata.core.protocol.transaction.BranchCommitResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + + +public class Fastjson2SerializerTest { + + private static Fastjson2Serializer fastjson2Serializer; + + @BeforeAll + public static void before() { + fastjson2Serializer = new Fastjson2Serializer(); + } + + + @Test + public void testBranchCommitRequest() { + + BranchCommitRequest branchCommitRequest = new BranchCommitRequest(); + branchCommitRequest.setBranchType(BranchType.AT); + branchCommitRequest.setXid("xid"); + branchCommitRequest.setResourceId("resourceId"); + branchCommitRequest.setBranchId(20190809); + branchCommitRequest.setApplicationData("app"); + + byte[] bytes = fastjson2Serializer.serialize(branchCommitRequest); + BranchCommitRequest t = fastjson2Serializer.deserialize(bytes); + + assertThat(t.getTypeCode()).isEqualTo(branchCommitRequest.getTypeCode()); + assertThat(t.getBranchType()).isEqualTo(branchCommitRequest.getBranchType()); + assertThat(t.getXid()).isEqualTo(branchCommitRequest.getXid()); + assertThat(t.getResourceId()).isEqualTo(branchCommitRequest.getResourceId()); + assertThat(t.getBranchId()).isEqualTo(branchCommitRequest.getBranchId()); + assertThat(t.getApplicationData()).isEqualTo(branchCommitRequest.getApplicationData()); + + } + + @Test + public void testBranchCommitResponse() { + + BranchCommitResponse branchCommitResponse = new BranchCommitResponse(); + branchCommitResponse.setTransactionExceptionCode(TransactionExceptionCode.BranchTransactionNotExist); + branchCommitResponse.setBranchId(20190809); + branchCommitResponse.setBranchStatus(BranchStatus.PhaseOne_Done); + branchCommitResponse.setMsg("20190809"); + branchCommitResponse.setXid("20190809"); + branchCommitResponse.setResultCode(ResultCode.Failed); + + byte[] bytes = fastjson2Serializer.serialize(branchCommitResponse); + BranchCommitResponse t = fastjson2Serializer.deserialize(bytes); + + assertThat(t.getTransactionExceptionCode()).isEqualTo(branchCommitResponse.getTransactionExceptionCode()); + assertThat(t.getBranchId()).isEqualTo(branchCommitResponse.getBranchId()); + assertThat(t.getBranchStatus()).isEqualTo(branchCommitResponse.getBranchStatus()); + assertThat(t.getMsg()).isEqualTo(branchCommitResponse.getMsg()); + assertThat(t.getResultCode()).isEqualTo(branchCommitResponse.getResultCode()); + + } + +} From 3c30bc4d20831fcf8dd322fa74d54531a3d08539 Mon Sep 17 00:00:00 2001 From: GoodBoyCoder Date: Tue, 8 Oct 2024 22:04:06 +0800 Subject: [PATCH 02/10] =?UTF-8?q?fix:=20pom=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- serializer/seata-serializer-fastjson2/pom.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/serializer/seata-serializer-fastjson2/pom.xml b/serializer/seata-serializer-fastjson2/pom.xml index 106de87cccd..b586ecac999 100644 --- a/serializer/seata-serializer-fastjson2/pom.xml +++ b/serializer/seata-serializer-fastjson2/pom.xml @@ -31,12 +31,6 @@ seata-serializer-fastjson2 ${project.version} serializer-fastjson2 for Seata built with Maven - - 8 - 8 - UTF-8 - - ${project.groupId} From d09023d53a2742dd6f3eaeecf931022ee04cd49a Mon Sep 17 00:00:00 2001 From: GoodBoyCoder Date: Tue, 8 Oct 2024 22:15:44 +0800 Subject: [PATCH 03/10] fix: code style --- .../Fastjson2SerializerFactory.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java b/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java index 1a299f99c78..8200f73f260 100644 --- a/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java +++ b/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java @@ -31,7 +31,7 @@ public class Fastjson2SerializerFactory { private JSONWriter.Feature[] jsonWriterFeature; private static final class InstanceHolder { - public static final Fastjson2SerializerFactory instance = new Fastjson2SerializerFactory(); + public static final Fastjson2SerializerFactory INSTANCE = new Fastjson2SerializerFactory(); } public Fastjson2SerializerFactory() { @@ -59,7 +59,7 @@ public Fastjson2SerializerFactory() { } public static Fastjson2SerializerFactory getInstance() { - return Fastjson2SerializerFactory.InstanceHolder.instance; + return Fastjson2SerializerFactory.InstanceHolder.INSTANCE; } public Filter getFilter() { From 1d5f81f6806ff9bdfb4908fd83d322bb93be168c Mon Sep 17 00:00:00 2001 From: GoodBoyCoder Date: Wed, 9 Oct 2024 00:22:39 +0800 Subject: [PATCH 04/10] fix: code style --- .../Fastjson2SerializerFactory.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java b/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java index 8200f73f260..81b254e15e7 100644 --- a/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java +++ b/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java @@ -38,23 +38,23 @@ public Fastjson2SerializerFactory() { autoTypeFilter = JSONReader.autoTypeFilter(true, SerializerSecurityRegistry.getAllowClassType().toArray(new Class[]{})); 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.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 }; 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 + JSONWriter.Feature.WriteClassName, + JSONWriter.Feature.FieldBased, + JSONWriter.Feature.ReferenceDetection, + JSONWriter.Feature.WriteNulls, + JSONWriter.Feature.NotWriteDefaultValue, + JSONWriter.Feature.NotWriteHashMapArrayListClassName, + JSONWriter.Feature.WriteNameAsSymbol }; } From ccdde1aa71abae1911ec30e97b72be717eab075b Mon Sep 17 00:00:00 2001 From: GoodBoyCoder Date: Wed, 9 Oct 2024 00:24:40 +0800 Subject: [PATCH 05/10] add md --- changes/en-us/2.x.md | 1 + changes/zh-cn/2.x.md | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index e252b79b246..0d331032740 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -6,6 +6,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 +- [[#6904](https://github.com/apache/incubator-seata/pull/6904)] add fastjson2 serializer support ### bugfix: - [[#6899](https://github.com/apache/incubator-seata/pull/6899)] fix file.conf read failed after package diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index 2c69ea823b7..32315c396f2 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -3,8 +3,9 @@ ### feature: -[[#6876](https://github.com/apache/incubator-seata/pull/6876)]支持人大金仓数据库(kingbase) -[[#6881](https://github.com/apache/incubator-seata/pull/6881)]全链路支持grpc +- [[#6876](https://github.com/apache/incubator-seata/pull/6876)]支持人大金仓数据库(kingbase) +- [[#6881](https://github.com/apache/incubator-seata/pull/6881)]全链路支持grpc +- [[#6904](https://github.com/apache/incubator-seata/pull/6904)] 增加Fastjson2序列化Rpc消息支持 ### bugfix: - [[#6899](https://github.com/apache/incubator-seata/pull/6899)] 修复file.conf打包后的读取 From 17cac762a6258890d9fcdd56929cbb39767a9db8 Mon Sep 17 00:00:00 2001 From: GoodBoyCoder Date: Wed, 9 Oct 2024 21:55:28 +0800 Subject: [PATCH 06/10] test: add mock server test case --- .../seata/core/serializer/SerializerType.java | 2 +- .../netty/mockserver/MockFastJson2Test.java | 130 ++++++++++++++++++ 2 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java diff --git a/core/src/main/java/org/apache/seata/core/serializer/SerializerType.java b/core/src/main/java/org/apache/seata/core/serializer/SerializerType.java index 197340305e8..56fd8136d17 100644 --- a/core/src/main/java/org/apache/seata/core/serializer/SerializerType.java +++ b/core/src/main/java/org/apache/seata/core/serializer/SerializerType.java @@ -67,7 +67,7 @@ public enum SerializerType { /** * The fastjson2. *

- * Math.pow(2, 5) + * Math.pow(2, 6) */ FASTJSON2((byte)0x64), ; diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java b/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java new file mode 100644 index 00000000000..71355f16648 --- /dev/null +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java @@ -0,0 +1,130 @@ +/* + * 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.core.rpc.netty.mockserver; + +import org.apache.seata.common.ConfigurationKeys; +import org.apache.seata.common.ConfigurationTestHelper; +import org.apache.seata.config.ConfigurationFactory; +import org.apache.seata.core.exception.TransactionException; +import org.apache.seata.core.model.BranchType; +import org.apache.seata.core.model.GlobalStatus; +import org.apache.seata.core.model.TransactionManager; +import org.apache.seata.core.rpc.netty.RmNettyRemotingClient; +import org.apache.seata.core.rpc.netty.TmNettyRemotingClient; +import org.apache.seata.core.serializer.SerializerType; +import org.apache.seata.mockserver.MockCoordinator; +import org.apache.seata.mockserver.MockServer; +import org.apache.seata.rm.DefaultResourceManager; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * the type MockServerTest + */ +public class MockFastJson2Test { + + static String RESOURCE_ID = "mock-action"; + + Logger logger = LoggerFactory.getLogger(MockFastJson2Test.class); + + @BeforeAll + public static void before() { + ConfigurationFactory.reload(); + ConfigurationTestHelper.putConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL, String.valueOf(ProtocolTestConstants.MOCK_SERVER_PORT)); + ConfigurationTestHelper.putConfig(ConfigurationKeys.SERIALIZE_FOR_RPC, String.valueOf(SerializerType.FASTJSON2)); + MockServer.start(ProtocolTestConstants.MOCK_SERVER_PORT); + TmNettyRemotingClient.getInstance().destroy(); + RmNettyRemotingClient.getInstance().destroy(); + } + + @AfterAll + public static void after() { + //MockServer.close(); + ConfigurationTestHelper.removeConfig(ConfigurationKeys.SERIALIZE_FOR_RPC); + TmNettyRemotingClient.getInstance().destroy(); + RmNettyRemotingClient.getInstance().destroy(); + } + + @Test + public void testCommit() throws TransactionException { + String xid = doTestCommit(0); + Assertions.assertEquals(1, Action1Impl.getCommitTimes(xid)); + Assertions.assertEquals(0, Action1Impl.getRollbackTimes(xid)); + } + + @Test + public void testCommitRetry() throws TransactionException { + String xid = doTestCommit(2); + Assertions.assertEquals(3, Action1Impl.getCommitTimes(xid)); + Assertions.assertEquals(0, Action1Impl.getRollbackTimes(xid)); + } + + @Test + public void testRollback() throws TransactionException { + String xid = doTestRollback(0); + Assertions.assertEquals(0, Action1Impl.getCommitTimes(xid)); + Assertions.assertEquals(1, Action1Impl.getRollbackTimes(xid)); + } + + @Test + public void testRollbackRetry() throws TransactionException { + String xid = doTestRollback(2); + Assertions.assertEquals(0, Action1Impl.getCommitTimes(xid)); + Assertions.assertEquals(3, Action1Impl.getRollbackTimes(xid)); + } + + @Test + public void testTm() throws Exception { + TmClientTest.testTm(); + } + + @Test + public void testRm() throws Exception { + RmClientTest.testRm(); + } + + private String doTestCommit(int times) throws TransactionException { + TransactionManager tm = TmClientTest.getTm(); + DefaultResourceManager rm = RmClientTest.getRm(RESOURCE_ID); + + String xid = tm.begin(ProtocolTestConstants.APPLICATION_ID, ProtocolTestConstants.SERVICE_GROUP, "test-commit", 60000); + MockCoordinator.getInstance().setExpectedRetry(xid, times); + Long branchId = rm.branchRegister(BranchType.TCC, RESOURCE_ID, "1", xid, "{\"mock\":\"mock\"}", "1"); + GlobalStatus commit = tm.commit(xid); + Assertions.assertEquals(GlobalStatus.Committed, commit); + return xid; + + } + + private String doTestRollback(int times) throws TransactionException { + TransactionManager tm = TmClientTest.getTm(); + DefaultResourceManager rm = RmClientTest.getRm(RESOURCE_ID); + + String xid = tm.begin(ProtocolTestConstants.APPLICATION_ID, ProtocolTestConstants.SERVICE_GROUP, "test-rollback", 60000); + logger.info("doTestRollback xid:{}", xid); + MockCoordinator.getInstance().setExpectedRetry(xid, times); + Long branchId = rm.branchRegister(BranchType.TCC, RESOURCE_ID, "1", xid, "{\"mock\":\"mock\"}", "1"); + GlobalStatus rollback = tm.rollback(xid); + Assertions.assertEquals(GlobalStatus.Rollbacked, rollback); + return xid; + + } +} From cab182c756e254688e538a1ce0a4bf681e0aa4bf Mon Sep 17 00:00:00 2001 From: GoodBoyCoder Date: Wed, 9 Oct 2024 21:56:07 +0800 Subject: [PATCH 07/10] optimize: code style --- .../Fastjson2Serializer.java | 3 --- .../Fastjson2SerializerFactory.java | 3 --- 2 files changed, 6 deletions(-) diff --git a/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2Serializer.java b/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2Serializer.java index 4949928f756..9c9e081db0e 100644 --- a/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2Serializer.java +++ b/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2Serializer.java @@ -20,9 +20,6 @@ import org.apache.seata.common.loader.LoadLevel; import org.apache.seata.core.serializer.Serializer; -/** - * @Author GoodBoyCoder - */ @LoadLevel(name = "FASTJSON2") public class Fastjson2Serializer implements Serializer { diff --git a/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java b/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java index 81b254e15e7..5e2ef29de58 100644 --- a/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java +++ b/serializer/seata-serializer-fastjson2/src/main/java/org.apache.seata.serializer.fastjson2/Fastjson2SerializerFactory.java @@ -21,9 +21,6 @@ import com.alibaba.fastjson2.filter.Filter; import org.apache.seata.core.serializer.SerializerSecurityRegistry; -/** - * @Author GoodBoyCoder - */ public class Fastjson2SerializerFactory { private Filter autoTypeFilter; From 5a73d7d1689491dd9ee0df9f9754e7f0af41eee6 Mon Sep 17 00:00:00 2001 From: GoodBoyCoder Date: Wed, 9 Oct 2024 23:04:34 +0800 Subject: [PATCH 08/10] test: fix test fail --- .../seata/core/rpc/netty/mockserver/MockFastJson2Test.java | 1 + 1 file changed, 1 insertion(+) diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java b/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java index 71355f16648..7f93b7f5419 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java @@ -59,6 +59,7 @@ public static void before() { public static void after() { //MockServer.close(); ConfigurationTestHelper.removeConfig(ConfigurationKeys.SERIALIZE_FOR_RPC); + ConfigurationTestHelper.putConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL, String.valueOf(ProtocolTestConstants.MOCK_SERVER_PORT)); TmNettyRemotingClient.getInstance().destroy(); RmNettyRemotingClient.getInstance().destroy(); } From 3e25a5effc439d2921bdc46236c49b044c01ff2d Mon Sep 17 00:00:00 2001 From: GoodBoyCoder Date: Wed, 9 Oct 2024 23:50:14 +0800 Subject: [PATCH 09/10] test: fix test fail --- .../seata/core/rpc/netty/mockserver/MockFastJson2Test.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java b/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java index 7f93b7f5419..e7a08624fcd 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java @@ -58,8 +58,8 @@ public static void before() { @AfterAll public static void after() { //MockServer.close(); + ConfigurationTestHelper.removeConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL); ConfigurationTestHelper.removeConfig(ConfigurationKeys.SERIALIZE_FOR_RPC); - ConfigurationTestHelper.putConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL, String.valueOf(ProtocolTestConstants.MOCK_SERVER_PORT)); TmNettyRemotingClient.getInstance().destroy(); RmNettyRemotingClient.getInstance().destroy(); } From 6ec209b666324f19db71a82481b91bf46f952aa7 Mon Sep 17 00:00:00 2001 From: GoodBoyCoder Date: Mon, 14 Oct 2024 22:22:52 +0800 Subject: [PATCH 10/10] test: temporarily disable fastjson2's mockserver test --- .../seata/core/rpc/netty/mockserver/MockFastJson2Test.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java b/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java index e7a08624fcd..4ad06ebe857 100644 --- a/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java +++ b/test/src/test/java/org/apache/seata/core/rpc/netty/mockserver/MockFastJson2Test.java @@ -49,7 +49,8 @@ public class MockFastJson2Test { public static void before() { ConfigurationFactory.reload(); ConfigurationTestHelper.putConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL, String.valueOf(ProtocolTestConstants.MOCK_SERVER_PORT)); - ConfigurationTestHelper.putConfig(ConfigurationKeys.SERIALIZE_FOR_RPC, String.valueOf(SerializerType.FASTJSON2)); + //Enable it when testing is needed. The settings here will affect the global configuration. + //ConfigurationTestHelper.putConfig(ConfigurationKeys.SERIALIZE_FOR_RPC, String.valueOf(SerializerType.FASTJSON2)); MockServer.start(ProtocolTestConstants.MOCK_SERVER_PORT); TmNettyRemotingClient.getInstance().destroy(); RmNettyRemotingClient.getInstance().destroy();