-
Notifications
You must be signed in to change notification settings - Fork 862
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
feat: support fury serializer #356
base: master
Are you sure you want to change the base?
Changes from 5 commits
12060fa
8157507
199b787
eed4b2b
7adb1f1
64e2d2c
940b0c6
ff900b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
package com.alipay.remoting.serialization; | ||
|
||
import java.util.concurrent.locks.ReentrantLock; | ||
import com.alipay.remoting.serialization.fury.FurySerializer; | ||
|
||
/** | ||
* Manage all serializers. | ||
|
@@ -32,19 +33,27 @@ public class SerializerManager { | |
|
||
private static Serializer[] serializers = new Serializer[5]; | ||
public static final byte Hessian2 = 1; | ||
|
||
//public static final byte Json = 2; | ||
|
||
public static final byte Fury = 3; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 另,bolt作为底层组件需要考虑引入依赖版本的兼容性,如现在rpc里使用fury版本是0.4,如果和当前0.6版本不兼容就会有问题 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
这个不太好改,因为fury捐给apache后,对老的包全部做了迁移,没有做向下兼容,导致0.6跟0.4不兼容,0.4跟0.6也不兼容。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 是否考虑rpc先把版本提升上来,统一维护一个版本就好了 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
好的,我去看看rpc那块的实现,考虑先提交到sofa-rpc侧. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
private static final ReentrantLock REENTRANT_LOCK = new ReentrantLock(); | ||
|
||
public static Serializer getSerializer(int idx) { | ||
Serializer currentSerializer = serializers[idx]; | ||
if (currentSerializer == null && idx == Hessian2) { | ||
if (currentSerializer == null) { | ||
REENTRANT_LOCK.lock(); | ||
try { | ||
currentSerializer = serializers[idx]; | ||
if (currentSerializer == null) { | ||
currentSerializer = new HessianSerializer(); | ||
addSerializer(Hessian2, currentSerializer); | ||
if (idx == Hessian2) { | ||
currentSerializer = new HessianSerializer(); | ||
addSerializer(Hessian2, currentSerializer); | ||
} else if (idx == Fury) { | ||
currentSerializer = new FurySerializer(); | ||
addSerializer(Fury, currentSerializer); | ||
} | ||
} | ||
} finally { | ||
REENTRANT_LOCK.unlock(); | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,59 @@ | ||||||||||||||||
/* | ||||||||||||||||
* 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 com.alipay.remoting.serialization.fury; | ||||||||||||||||
|
||||||||||||||||
import java.util.ArrayList; | ||||||||||||||||
import java.util.List; | ||||||||||||||||
import com.alipay.remoting.exception.CodecException; | ||||||||||||||||
import com.alipay.remoting.serialization.Serializer; | ||||||||||||||||
import org.apache.fury.Fury; | ||||||||||||||||
import org.apache.fury.ThreadLocalFury; | ||||||||||||||||
import org.apache.fury.ThreadSafeFury; | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* @author [email protected] | ||||||||||||||||
*/ | ||||||||||||||||
public class FurySerializer implements Serializer { | ||||||||||||||||
|
||||||||||||||||
private static final List<Class<?>> REGISTRY_LIST = new ArrayList<>(); | ||||||||||||||||
|
||||||||||||||||
private final ThreadSafeFury fury = new ThreadLocalFury(classLoader -> { | ||||||||||||||||
Fury fury = Fury.builder().withRefTracking(true) | ||||||||||||||||
.requireClassRegistration(true).withClassLoader(classLoader).build(); | ||||||||||||||||
REGISTRY_LIST.forEach(fury::register); | ||||||||||||||||
return fury; | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
@Override | ||||||||||||||||
public byte[] serialize(Object obj) throws CodecException { | ||||||||||||||||
try { | ||||||||||||||||
return fury.serialize(obj); | ||||||||||||||||
} catch (Exception e) { | ||||||||||||||||
throw new CodecException("Fury serialization failed", e); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
@Override | ||||||||||||||||
public <T> T deserialize(byte[] data, String classOfT) throws CodecException { | ||||||||||||||||
return (T)fury.deserialize(data); | ||||||||||||||||
} | ||||||||||||||||
Comment on lines
+50
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add type safety to The - public <T> T deserialize(byte[] data, String classOfT) throws CodecException {
- return (T)fury.deserialize(data);
+ public <T> T deserialize(byte[] data, Class<T> classOfT) throws CodecException {
+ return classOfT.cast(fury.deserialize(data)); Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个问题我无法决定是否如此修改,因为会涉及改动上层序列化接口 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @funky-eyes, I understand that changing the Given the widespread usage, this change would require careful consideration and testing. It might be best to discuss this further or address it in a separate PR to ensure all dependencies are handled properly. Let me know how you would like to proceed. Analysis chainScripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Search for all invocations of the `deserialize` method in the codebase to understand its usage and potential impact of changing its signature.
rg --context 5 'deserialize\('
Length of output: 18313 |
||||||||||||||||
|
||||||||||||||||
public static void registry(Class<?> clazz) { | ||||||||||||||||
REGISTRY_LIST.add(clazz); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* 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 com.alipay.remoting.serialization.fury; | ||
|
||
import java.util.UUID; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import com.alipay.remoting.serialization.HessianSerializerTest; | ||
|
||
import com.alipay.remoting.exception.CodecException; | ||
import org.apache.fury.exception.InsecureException; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
/** | ||
* @author [email protected] | ||
*/ | ||
public class FurySerializerTest { | ||
|
||
public static FurySerializer serializer; | ||
|
||
static { | ||
FurySerializer.registry(HessianSerializerTest.class); | ||
serializer = new FurySerializer(); | ||
} | ||
|
||
@Test | ||
public void concurrentSerializeTest() throws InterruptedException { | ||
int concurrentNum = 10; | ||
CountDownLatch countDownLatch = new CountDownLatch(concurrentNum); | ||
for (int i = 0; i < concurrentNum; ++i) { | ||
FurySerializerTest.MyThread thread = new FurySerializerTest.MyThread(countDownLatch); | ||
new Thread(thread).start(); | ||
} | ||
countDownLatch.await(2, TimeUnit.SECONDS); | ||
|
||
} | ||
|
||
@Test | ||
public void testSerializeError() { | ||
FurySerializerTest furySerializerTest = new FurySerializerTest(); | ||
try { | ||
serializer.serialize(furySerializerTest); | ||
} catch (CodecException e) { | ||
Assert.assertEquals(e.getCause().getClass(), InsecureException.class); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSerialize() { | ||
HessianSerializerTest furySerializerTest = new HessianSerializerTest(); | ||
try { | ||
Assert.assertNotNull(serializer.serialize(furySerializerTest)); | ||
} catch (CodecException e) { | ||
fail(); | ||
} | ||
} | ||
|
||
static class MyThread implements Runnable { | ||
CountDownLatch countDownLatch; | ||
|
||
public MyThread(CountDownLatch countDownLatch) { | ||
this.countDownLatch = countDownLatch; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
for (int i = 0; i < 100; i++) { | ||
String randomStr = UUID.randomUUID().toString(); | ||
byte[] bytes = serializer.serialize(randomStr); | ||
String o = serializer.deserialize(bytes, String.class.getName()); | ||
assertEquals(o, randomStr); | ||
} | ||
} catch (Exception e) { | ||
fail(); | ||
} finally { | ||
countDownLatch.countDown(); | ||
} | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bolt用于多个基础组件,最好不要用高版本的用法保持线下兼容