Skip to content

Commit 9220c18

Browse files
authored
fix: dubbo replay NPE if null (#451)
1 parent aed406d commit 9220c18

File tree

4 files changed

+39
-45
lines changed

4 files changed

+39
-45
lines changed

arex-instrumentation/dubbo/arex-dubbo-alibaba/src/main/java/io/arex/inst/dubbo/alibaba/DubboAdapter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ public Map<String, String> getServerAttachment() {
193193
* < 2.6.3 not support serverContext
194194
*/
195195
public void setServerAttachment(String key, String val) {
196+
RpcContext.getContext().setAttachment(key, val);
196197
if (SERVER_CONTEXT_METHOD != null) {
197198
RpcContext.getServerContext().setAttachment(key, val);
198199
}

arex-instrumentation/dubbo/arex-dubbo-alibaba/src/main/java/io/arex/inst/dubbo/alibaba/DubboConsumerExtractor.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,16 @@ private Mocker makeMocker() {
3030
public MockResult replay() {
3131
Object result = MockUtils.replayBody(makeMocker());
3232
boolean ignoreMockResult = IgnoreUtils.ignoreMockResult(adapter.getPath(), adapter.getOperationName());
33-
RpcResult rpcResult = null;
34-
if (result != null && !ignoreMockResult) {
35-
rpcResult = new RpcResult();
36-
boolean isAsync = RpcUtils.isAsync(adapter.getUrl(), adapter.getInvocation());
37-
if (isAsync) {
38-
ResponseFuture future = new SimpleFuture(result);
39-
RpcContext.getContext().setFuture(new FutureAdapter<>(future));
33+
RpcResult rpcResult = new RpcResult();
34+
boolean isAsync = RpcUtils.isAsync(adapter.getUrl(), adapter.getInvocation());
35+
if (isAsync) {
36+
ResponseFuture future = new SimpleFuture(result);
37+
RpcContext.getContext().setFuture(new FutureAdapter<>(future));
38+
} else {
39+
if (result instanceof Throwable) {
40+
rpcResult.setException((Throwable) result);
4041
} else {
41-
if (result instanceof Throwable) {
42-
rpcResult.setException((Throwable) result);
43-
} else {
44-
rpcResult.setValue(result);
45-
}
42+
rpcResult.setValue(result);
4643
}
4744
}
4845
return MockResult.success(ignoreMockResult, rpcResult);

arex-instrumentation/dubbo/arex-dubbo-apache-v2/src/main/java/io/arex/inst/dubbo/apache/v2/DubboConsumerExtractor.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,24 @@ private Mocker makeMocker() {
2828
public MockResult replay() {
2929
Object result = MockUtils.replayBody(makeMocker());
3030
boolean ignoreMockResult = IgnoreUtils.ignoreMockResult(adapter.getPath(), adapter.getOperationName());
31-
AsyncRpcResult asyncRpcResult = null;
32-
if (result != null && !ignoreMockResult) {
33-
Invocation invocation = adapter.getInvocation();
34-
if (result instanceof Throwable) {
35-
asyncRpcResult = AsyncRpcResult.newDefaultAsyncResult((Throwable) result, invocation);
36-
} else {
37-
asyncRpcResult = AsyncRpcResult.newDefaultAsyncResult(result, invocation);
38-
}
39-
// need to set invoke mode to FUTURE if return type is CompletableFuture
40-
if (invocation instanceof RpcInvocation) {
41-
RpcInvocation rpcInv = (RpcInvocation) invocation;
42-
rpcInv.setInvokeMode(RpcUtils.getInvokeMode(adapter.getUrl(), invocation));
43-
}
44-
// compatible with dubbo 2.7.8, refer to org.apache.dubbo.rpc.protocol.AbstractInvoker.invoke
45-
CompletableFuture<AppResponse> future = new CompletableFuture<>();
46-
future.complete((AppResponse)asyncRpcResult.getAppResponse());
47-
RpcContext.getContext().setFuture(new FutureAdapter<AppResponse>(future));
48-
// save for 2.6.x compatibility, for example, TraceFilter in Zipkin uses com.alibaba.xxx.FutureAdapter
49-
FutureContext.getContext().setCompatibleFuture(future);
31+
AsyncRpcResult asyncRpcResult;
32+
Invocation invocation = adapter.getInvocation();
33+
if (result instanceof Throwable) {
34+
asyncRpcResult = AsyncRpcResult.newDefaultAsyncResult((Throwable) result, invocation);
35+
} else {
36+
asyncRpcResult = AsyncRpcResult.newDefaultAsyncResult(result, invocation);
5037
}
38+
// need to set invoke mode to FUTURE if return type is CompletableFuture
39+
if (invocation instanceof RpcInvocation) {
40+
RpcInvocation rpcInv = (RpcInvocation) invocation;
41+
rpcInv.setInvokeMode(RpcUtils.getInvokeMode(adapter.getUrl(), invocation));
42+
}
43+
// compatible with dubbo 2.7.8, refer to org.apache.dubbo.rpc.protocol.AbstractInvoker.invoke
44+
CompletableFuture<AppResponse> future = new CompletableFuture<>();
45+
future.complete((AppResponse)asyncRpcResult.getAppResponse());
46+
RpcContext.getContext().setFuture(new FutureAdapter<AppResponse>(future));
47+
// save for 2.6.x compatibility, for example, TraceFilter in Zipkin uses com.alibaba.xxx.FutureAdapter
48+
FutureContext.getContext().setCompatibleFuture(future);
5149
return MockResult.success(ignoreMockResult, asyncRpcResult);
5250
}
5351
}

arex-instrumentation/dubbo/arex-dubbo-apache-v3/src/main/java/io/arex/inst/dubbo/apache/v3/DubboConsumerExtractor.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,19 @@ private Mocker makeMocker() {
2424
public MockResult replay() {
2525
Object result = MockUtils.replayBody(makeMocker());
2626
boolean ignoreMockResult = IgnoreUtils.ignoreMockResult(adapter.getPath(), adapter.getOperationName());
27-
AsyncRpcResult asyncRpcResult = null;
28-
if (result != null && !ignoreMockResult) {
29-
Invocation invocation = adapter.getInvocation();
30-
if (result instanceof Throwable) {
31-
asyncRpcResult = AsyncRpcResult.newDefaultAsyncResult((Throwable) result, invocation);
32-
} else {
33-
asyncRpcResult = AsyncRpcResult.newDefaultAsyncResult(result, invocation);
34-
}
35-
// need to set invoke mode to FUTURE if return type is CompletableFuture
36-
if (invocation instanceof RpcInvocation) {
37-
RpcInvocation rpcInv = (RpcInvocation) invocation;
38-
rpcInv.setInvokeMode(RpcUtils.getInvokeMode(adapter.getUrl(), invocation));
39-
}
40-
RpcContext.getContext().setFuture(new FutureAdapter<>(asyncRpcResult.getResponseFuture()));
27+
AsyncRpcResult asyncRpcResult;
28+
Invocation invocation = adapter.getInvocation();
29+
if (result instanceof Throwable) {
30+
asyncRpcResult = AsyncRpcResult.newDefaultAsyncResult((Throwable) result, invocation);
31+
} else {
32+
asyncRpcResult = AsyncRpcResult.newDefaultAsyncResult(result, invocation);
4133
}
34+
// need to set invoke mode to FUTURE if return type is CompletableFuture
35+
if (invocation instanceof RpcInvocation) {
36+
RpcInvocation rpcInv = (RpcInvocation) invocation;
37+
rpcInv.setInvokeMode(RpcUtils.getInvokeMode(adapter.getUrl(), invocation));
38+
}
39+
RpcContext.getContext().setFuture(new FutureAdapter<>(asyncRpcResult.getResponseFuture()));
4240
return MockResult.success(ignoreMockResult, asyncRpcResult);
4341
}
4442
}

0 commit comments

Comments
 (0)