Skip to content

Commit e6cd7d7

Browse files
author
liujianjun.ljj
committed
fix triple stream deserialize issue when in serverless scene
1 parent db4f464 commit e6cd7d7

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

remoting/remoting-triple/src/main/java/com/alipay/sofa/rpc/message/triple/stream/ClientStreamObserverAdapter.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ public class ClientStreamObserverAdapter implements StreamObserver<triple.Respon
3535

3636
private volatile Class<?> returnType;
3737

38-
public ClientStreamObserverAdapter(SofaStreamObserver<Object> sofaStreamObserver, byte serializeType) {
38+
private final ClassLoader classLoader;
39+
40+
public ClientStreamObserverAdapter(SofaStreamObserver<Object> sofaStreamObserver, byte serializeType,
41+
ClassLoader classLoader) {
3942
this.sofaStreamObserver = sofaStreamObserver;
4043
this.serializer = SerializerFactory.getSerializer(serializeType);
44+
this.classLoader = classLoader;
4145
}
4246

4347
@Override
@@ -46,14 +50,19 @@ public void onNext(triple.Response response) {
4650
Object appResponse = null;
4751
String returnTypeName = response.getType();
4852
if (responseData != null && responseData.length > 0) {
49-
if (returnType == null && !returnTypeName.isEmpty()) {
50-
try {
51-
returnType = Class.forName(returnTypeName);
52-
} catch (ClassNotFoundException e) {
53-
throw new SofaRpcException(RpcErrorType.CLIENT_SERIALIZE, "Can not find return type :" + returnType);
53+
ClassLoader oldClassloader = Thread.currentThread().getContextClassLoader();
54+
try {
55+
Thread.currentThread().setContextClassLoader(classLoader);
56+
if (returnType == null && !returnTypeName.isEmpty()) {
57+
returnType = Class.forName(returnTypeName, true, classLoader);
5458
}
59+
appResponse = serializer.decode(new ByteArrayWrapperByteBuf(responseData), returnType, null);
60+
} catch (ClassNotFoundException e) {
61+
throw new SofaRpcException(RpcErrorType.CLIENT_DESERIALIZE, "Can not find return type :" + returnType,
62+
e);
63+
} finally {
64+
Thread.currentThread().setContextClassLoader(oldClassloader);
5565
}
56-
appResponse = serializer.decode(new ByteArrayWrapperByteBuf(responseData), returnType, null);
5766
}
5867

5968
sofaStreamObserver.onNext(appResponse);

remoting/remoting-triple/src/main/java/com/alipay/sofa/rpc/transport/triple/TripleClientInvoker.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ private SofaResponse genericBinaryStreamCall(SofaRequest sofaRequest, int timeou
193193
call,
194194
new ClientStreamObserverAdapter(
195195
sofaStreamObserver,
196-
sofaRequest.getSerializeType()
196+
sofaRequest.getSerializeType(),
197+
ClassLoaderUtils.getCurrentClassLoader()
197198
)
198199
);
199200
SofaStreamObserver<Request> handler = new SofaStreamObserver() {
@@ -231,7 +232,7 @@ private SofaResponse genericServerStreamCall(SofaRequest sofaRequest, int timeou
231232

232233
Request req = SofaProtoUtils.buildRequest(sofaRequest.getMethodArgSigs(), sofaRequest.getMethodArgs(), serialization, serializer, 1);
233234

234-
ClientStreamObserverAdapter responseObserver = new ClientStreamObserverAdapter(sofaStreamObserver, sofaRequest.getSerializeType());
235+
ClientStreamObserverAdapter responseObserver = new ClientStreamObserverAdapter(sofaStreamObserver, sofaRequest.getSerializeType(), ClassLoaderUtils.getCurrentClassLoader());
235236

236237
ClientCalls.asyncServerStreamingCall(call, req, responseObserver);
237238

0 commit comments

Comments
 (0)