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

bugfix: Compatible with Protobuf empty object. #1376

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,12 @@
<version>4.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.3.0</version>
<scope>test</scope>
</dependency>
<!-- h2 SSL libs -->
<dependency>
<groupId>io.netty</groupId>
Expand Down
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
Expand Down
11 changes: 11 additions & 0 deletions remoting/remoting-bolt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
<artifactId>netty-all</artifactId>
</dependency>

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-test-common</artifactId>
Expand All @@ -52,6 +58,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,9 @@ public <Request extends RequestCommand> boolean deserializeContent(Request reque
long deserializeStartTime = System.nanoTime();
try {
byte[] content = requestCommand.getContent();
if (content == null || content.length == 0) {
EvenLjj marked this conversation as resolved.
Show resolved Hide resolved
throw new DeserializationException("Content of request is null");
// The content may be empty in protobuf protocol scenario.
if (content == null) {
content = new byte[0];
}
String service = headerMap.get(RemotingConstants.HEAD_SERVICE);
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
Expand All @@ -282,8 +283,7 @@ public <Request extends RequestCommand> boolean deserializeContent(Request reque
Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory
.getSerializer(requestCommand.getSerializer());
Object sofaRequest = ClassUtils.forName(requestCommand.getRequestClass()).newInstance();
rpcSerializer.decode(new ByteArrayWrapperByteBuf(requestCommand.getContent()),
sofaRequest, headerMap);
rpcSerializer.decode(new ByteArrayWrapperByteBuf(content), sofaRequest, headerMap);

//for service mesh or other scene, we need to add more info from header
setRequestPropertiesWithHeaderInfo(headerMap, sofaRequest);
Expand Down Expand Up @@ -431,8 +431,8 @@ public <Response extends ResponseCommand> boolean deserializeContent(Response re
RpcResponseCommand responseCommand = (RpcResponseCommand) response;
byte serializer = response.getSerializer();
byte[] content = responseCommand.getContent();
if (content == null || content.length == 0) {
return false;
if (content == null) {
content = new byte[0];
}
long deserializeStartTime = System.nanoTime();

Expand All @@ -451,7 +451,7 @@ public <Response extends ResponseCommand> boolean deserializeContent(Response re
(String) invokeContext.get(RemotingConstants.HEAD_GENERIC_TYPE));

Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(serializer);
rpcSerializer.decode(new ByteArrayWrapperByteBuf(responseCommand.getContent()), sofaResponse, header);
rpcSerializer.decode(new ByteArrayWrapperByteBuf(content), sofaResponse, header);
if (sofaResponse instanceof SofaResponse) {
parseResponseHeader(header, (SofaResponse) sofaResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,93 @@
*/
package com.alipay.sofa.rpc.codec.bolt;

import static org.mockito.ArgumentMatchers.anyByte;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import com.alipay.remoting.InvokeContext;
import com.alipay.remoting.exception.DeserializationException;
import com.alipay.remoting.exception.SerializationException;
import com.alipay.remoting.rpc.protocol.RpcRequestCommand;
import com.alipay.remoting.rpc.protocol.RpcResponseCommand;
import com.alipay.sofa.rpc.codec.Serializer;
import com.alipay.sofa.rpc.codec.SerializerFactory;
import com.alipay.sofa.rpc.common.RemotingConstants;
import com.alipay.sofa.rpc.context.RpcInternalContext;
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import org.junit.Assert;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import com.alipay.sofa.rpc.transport.AbstractByteBuf;
import com.google.protobuf.Empty;
import com.google.protobuf.Internal;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.MessageLite;

public class SofaRpcSerializationTest {
static class SimpleResponse {
public SimpleResponse() {
}

private Object realResponse;

public Object getRealResponse() {
return realResponse;
}

public void setRealResponse(Object realResponse) {
this.realResponse = realResponse;
}
}

static class SimpleSerializer implements Serializer {
@Override
public AbstractByteBuf encode(Object object, Map<String, String> context) throws SofaRpcException {
throw new UnsupportedOperationException(object.getClass().getName());
}

@Override
public Object decode(AbstractByteBuf data, Class clazz, Map<String, String> context) throws SofaRpcException {
try {
if (MessageLite.class.isAssignableFrom(clazz)) {
MessageLite defaultInstance = Internal.getDefaultInstance(clazz);
return defaultInstance.getParserForType().parseFrom(data.array());
}
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e);
}
throw new UnsupportedOperationException();
}

@Override
public void decode(AbstractByteBuf data, Object template, Map<String, String> context) throws SofaRpcException {
if (template instanceof SimpleResponse) {
Object realResponse = decode(data, Empty.class, context);
((SimpleResponse) template).setRealResponse(realResponse);
return;
}
throw new UnsupportedOperationException();
}
}

@Test
public void deserializeRequestContent() {
String traceId = "traceId";
String rpcId = "rpcId";
Map<String, String> headerMap = new HashMap<>();
headerMap.put("rpc_trace_context.sofaTraceId", traceId);
headerMap.put("rpc_trace_context.sofaRpcId", rpcId);
public void testSerializeEmptyRequest() throws DeserializationException {
SofaRpcSerialization serialization = new SofaRpcSerialization();

RpcRequestCommand command = new RpcRequestCommand();
command.setRequestHeader(headerMap);
SofaRpcSerialization sofaRpcSerialization = new SofaRpcSerialization();
boolean exp = false;
try {
sofaRpcSerialization.deserializeContent(command);
} catch (DeserializationException e) {
exp = true;
Assert.assertEquals("Content of request is null, traceId=" + traceId + ", rpcId=" + rpcId, e.getMessage());
try (MockedStatic<SerializerFactory> mocked = Mockito.mockStatic(SerializerFactory.class)) {
mocked.when(() -> SerializerFactory.getSerializer(anyByte())).thenReturn(new SimpleSerializer());

RpcResponseCommand response = new RpcResponseCommand();
response.setResponseClass(SimpleResponse.class.getName());
response.setContent(null);

Assert.assertTrue(serialization.deserializeContent(response, new InvokeContext()));
SimpleResponse simpleResponse = (SimpleResponse) response.getResponseObject();
Assert.assertEquals(Empty.getDefaultInstance(), simpleResponse.getRealResponse());
}
Assert.assertTrue(exp);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,12 @@ public SofaResponse invoke(SofaRequest sofaRequest, int timeout)
buildCustomCallOptions(sofaRequest, timeout), request);

SofaResponse sofaResponse = new SofaResponse();
byte[] responseDate = response.getData().toByteArray();
byte[] responseData = response.getData().toByteArray();
Class returnType = sofaRequest.getMethod().getReturnType();
if (returnType != void.class) {
if (responseDate != null && responseDate.length > 0) {
Serializer responseSerializer = SerializerFactory.getSerializer(response.getSerializeType());
Object appResponse = responseSerializer.decode(new ByteArrayWrapperByteBuf(responseDate), returnType, null);
sofaResponse.setAppResponse(appResponse);
}
Serializer responseSerializer = SerializerFactory.getSerializer(response.getSerializeType());
Object appResponse = responseSerializer.decode(new ByteArrayWrapperByteBuf(responseData), returnType, null);
sofaResponse.setAppResponse(appResponse);
}

return sofaResponse;
Expand Down Expand Up @@ -250,13 +248,11 @@ private void processSuccess(boolean needDecode, RpcInternalContext context, Sofa
Object appResponse = o;
if (needDecode) {
Response response = (Response) o;
byte[] responseDate = response.getData().toByteArray();
byte[] responseData = response.getData().toByteArray();
Class returnType = sofaRequest.getMethod().getReturnType();
if (returnType != void.class) {
if (responseDate != null && responseDate.length > 0) {
Serializer responseSerializer = SerializerFactory.getSerializer(response.getSerializeType());
appResponse = responseSerializer.decode(new ByteArrayWrapperByteBuf(responseDate), returnType, null);
}
Serializer responseSerializer = SerializerFactory.getSerializer(response.getSerializeType());
appResponse = responseSerializer.decode(new ByteArrayWrapperByteBuf(responseData), returnType, null);
}
}

Expand Down
Loading