-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add fury serializer support (#7038)
- Loading branch information
1 parent
5b31862
commit 2438d77
Showing
12 changed files
with
266 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
43 changes: 43 additions & 0 deletions
43
.../seata-serializer-fury/src/main/java/org/apache/seata/serializer/fury/FurySerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...serializer-fury/src/main/java/org/apache/seata/serializer/fury/FurySerializerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...zer-fury/src/main/resources/META-INF/services/org.apache.seata.core.serializer.Serializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
81 changes: 81 additions & 0 deletions
81
...ta-serializer-fury/src/test/java/org/apache/seata/serializer/fury/FurySerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
||
} | ||
} |