Skip to content

Commit

Permalink
feature: add fury serializer support (#7038)
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodBoyCoder authored Jan 9, 2025
1 parent 5b31862 commit 2438d77
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 4 deletions.
5 changes: 5 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@
<artifactId>seata-hsf</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-serializer-fury</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-serializer-kryo</artifactId>
Expand Down
3 changes: 2 additions & 1 deletion 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.

- [[#7037](https://github.com/apache/incubator-seata/pull/7037)] support fury undolog parser
- [[#7069](https://github.com/apache/incubator-seata/pull/7069)] Raft cluster mode supports address translation
- [[#7038](https://github.com/apache/incubator-seata/pull/7038)] support fury serializer

### bugfix:

Expand Down Expand Up @@ -36,9 +37,9 @@ Thanks to these contributors for their code commits. Please report an unintended
<!-- Please make sure your Github ID is in the list below -->

- [slievrly](https://github.com/slievrly)
- [GoodBoyCoder](https://github.com/GoodBoyCoder)
- [lyl2008dsg](https://github.com/lyl2008dsg)
- [remind](https://github.com/remind)
- [GoodBoyCoder](https://github.com/GoodBoyCoder)
- [PeppaO](https://github.com/PeppaO)
- [funky-eyes](https://github.com/funky-eyes)

Expand Down
3 changes: 2 additions & 1 deletion changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- [[#7037](https://github.com/apache/incubator-seata/pull/7037)] 支持UndoLog的fury序列化方式
- [[#7069](https://github.com/apache/incubator-seata/pull/7069)] Raft集群模式支持地址转换
- [[#7038](https://github.com/apache/incubator-seata/pull/7038)] 支持Fury序列化器

### bugfix:

Expand Down Expand Up @@ -36,9 +37,9 @@
<!-- 请确保您的 GitHub ID 在以下列表中 -->

- [slievrly](https://github.com/slievrly)
- [GoodBoyCoder](https://github.com/GoodBoyCoder)
- [lyl2008dsg](https://github.com/lyl2008dsg)
- [remind](https://github.com/remind)
- [GoodBoyCoder](https://github.com/GoodBoyCoder)
- [PeppaO](https://github.com/PeppaO)
- [funky-eyes](https://github.com/funky-eyes)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.slf4j.LoggerFactory;

import static org.apache.seata.core.serializer.SerializerType.FASTJSON2;
import static org.apache.seata.core.serializer.SerializerType.FURY;
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 @@ -47,7 +48,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, FASTJSON2};
private static final SerializerType[] DEFAULT_SERIALIZER_TYPE = new SerializerType[]{SEATA, PROTOBUF, KRYO, HESSIAN, FASTJSON2, FURY};

private final static Map<String, Serializer> SERIALIZER_MAP = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ public enum SerializerType {
* <p>
* Math.pow(2, 7)
*/
GRPC((byte) 0x128);
GRPC((byte) 0x128),

/**
* The fury.
* <p>
* Math.pow(2, 8)
*/
FURY((byte) 0x256)
;

private final byte code;

Expand Down
1 change: 1 addition & 0 deletions serializer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<module>seata-serializer-kryo</module>
<module>seata-serializer-hessian</module>
<module>seata-serializer-fastjson2</module>
<module>seata-serializer-fury</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 @@ -55,5 +55,10 @@
<artifactId>seata-serializer-fastjson2</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>seata-serializer-fury</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
46 changes: 46 additions & 0 deletions serializer/seata-serializer-fury/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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-fury</artifactId>
<packaging>jar</packaging>
<name>seata-serializer-fury ${project.version}</name>
<description>serializer-fury for Seata built with Maven</description>

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

<dependency>
<groupId>org.apache.fury</groupId>
<artifactId>fury-core</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.fury;

import org.apache.fury.ThreadSafeFury;
import org.apache.seata.common.loader.LoadLevel;
import org.apache.seata.core.protocol.AbstractMessage;
import org.apache.seata.core.serializer.Serializer;

@LoadLevel(name = "FURY")
public class FurySerializer implements Serializer {
@Override
public <T> byte[] serialize(T t) {
if (!(t instanceof AbstractMessage)) {
throw new IllegalArgumentException("AbstractMessage isn't available.");
}
ThreadSafeFury threadSafeFury = FurySerializerFactory.getInstance().get();
return threadSafeFury.serialize(t);
}

@Override
public <T> T deserialize(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
throw new IllegalArgumentException("bytes is null");
}
ThreadSafeFury threadSafeFury = FurySerializerFactory.getInstance().get();
return (T) threadSafeFury.deserialize(bytes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.fury;

import org.apache.fury.Fury;
import org.apache.fury.ThreadLocalFury;
import org.apache.fury.ThreadSafeFury;
import org.apache.fury.config.CompatibleMode;
import org.apache.fury.config.Language;
import org.apache.seata.core.serializer.SerializerSecurityRegistry;

public class FurySerializerFactory {
private static final FurySerializerFactory FACTORY = new FurySerializerFactory();

private static final ThreadSafeFury FURY = new ThreadLocalFury(classLoader -> {
Fury f = Fury.builder()
.withLanguage(Language.JAVA)
// In JAVA mode, classes cannot be registered by tag, and the different registration order between the server and the client will cause deserialization failure
// In XLANG cross-language mode has problems with Java class serialization, such as enum classes [https://github.com/apache/fury/issues/1644].
.requireClassRegistration(false)
//enable reference tracking for shared/circular reference.
.withRefTracking(true)
.withClassLoader(classLoader)
.withCompatibleMode(CompatibleMode.COMPATIBLE)
.build();

// register allow class
f.getClassResolver().setClassChecker((classResolver,className) -> SerializerSecurityRegistry.getAllowClassPattern().contains(className));
return f;
});

public static FurySerializerFactory getInstance() {
return FACTORY;
}

public ThreadSafeFury get() {
return FURY;
}
}
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.fury.FurySerializer
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.fury;

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 FurySerializerTest {
private static FurySerializer furySerializer;

@BeforeAll
public static void before() {
furySerializer = new FurySerializer();
}

@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 = furySerializer.serialize(branchCommitRequest);
BranchCommitRequest t = furySerializer.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 = furySerializer.serialize(branchCommitResponse);
BranchCommitResponse t = furySerializer.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());

}
}

0 comments on commit 2438d77

Please sign in to comment.