Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add fastjson2 serializer support #6904

Open
wants to merge 9 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
<artifactId>seata-serializer-hessian</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-serializer-fastjson2</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-compressor-gzip</artifactId>
Expand Down
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
<!-- 请根据PR的类型添加 `变更记录` 到以下对应位置(feature/bugfix/optimize/test) 下 -->

### 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打包后的读取
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, Serializer> SERIALIZER_MAP = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ public enum SerializerType {
* Math.pow(2, 5)
*/
JACKSON((byte)0x32),

/**
* The fastjson2.
* <p>
* Math.pow(2, 5)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Math.pow(2, 5)
* Math.pow(2, 6)

*/
FASTJSON2((byte)0x64),
;

private final byte code;
Expand Down
6 changes: 6 additions & 0 deletions dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<kryo.version>5.4.0</kryo.version>
<kryo-serializers.version>0.45</kryo-serializers.version>
<hessian.version>4.0.63</hessian.version>
<fastjson2.version>2.0.52</fastjson2.version>
<groovy.version>2.4.4</groovy.version>
<zstd.version>1.5.0-4</zstd.version>
<xstream.version>1.4.20</xstream.version>
Expand Down Expand Up @@ -662,6 +663,11 @@
<artifactId>hessian</artifactId>
<version>${hessian.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>${fastjson2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
Expand Down
1 change: 1 addition & 0 deletions serializer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<module>seata-serializer-seata</module>
<module>seata-serializer-kryo</module>
<module>seata-serializer-hessian</module>
<module>seata-serializer-fastjson2</module>
</modules>

</project>
5 changes: 5 additions & 0 deletions serializer/seata-serializer-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,10 @@
<artifactId>seata-serializer-hessian</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>seata-serializer-fastjson2</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
51 changes: 51 additions & 0 deletions serializer/seata-serializer-fastjson2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

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.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.seata</groupId>
<artifactId>seata-serializer</artifactId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>seata-serializer-fastjson2</artifactId>
<packaging>jar</packaging>
<name>seata-serializer-fastjson2 ${project.version}</name>
<description>serializer-fastjson2 for Seata built with Maven</description>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>seata-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>seata-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove author

*/
@LoadLevel(name = "FASTJSON2")
public class Fastjson2Serializer implements Serializer {

@Override
public <T> byte[] serialize(T t) {
return JSONB.toBytes(t, Fastjson2SerializerFactory.getInstance().getJsonWriterFeatureList());
}

@Override
public <T> T deserialize(byte[] bytes) {
return (T) JSONB.parseObject(bytes, Object.class, Fastjson2SerializerFactory.getInstance().getFilter(), Fastjson2SerializerFactory.getInstance().getJsonReaderFeatureList());
}
}
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove author

*/
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;
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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());

}

}
Loading