Sentinel sentinel-apache-dubbo-adapter 在异步模式下永远取不到异常

ftf50wuq  于 2022-10-19  发布在  Apache
关注(0)|答案(2)|浏览(260)

代码:

Sentinel/sentinel-adapter/sentinel-dubbo-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboProviderFilter.java

Line 73 in 4498de4

| | if (result.hasException()) { |

在 dubbo 2.7.5 后,所有 Filter 均支持异步操作,返回的 Result 对象通常是 AsyncRpcResult ,对 AsyncRpcResult 调用 hasException() 时如果异步任务未完成,则使用默认的返回值,永远不包含异常。

https://github.com/apache/dubbo/blob/9c49efeacfd87d2d4409fb000cebd58e1114ec8a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/AsyncRpcResult.java#L143-L155

fdbelqdn

fdbelqdn2#

syncInvoke
....
           Result result = invoker.invoke(invocation);
            // 异步 result
            if (result instanceof AsyncRpcResult){
                Entry finalInterfaceEntry = interfaceEntry;
                Entry finalMethodEntry = methodEntry;
                result.whenCompleteWithContext((r,t)->{
                    Throwable error = t;
                    if (error == null) {
                        error = Optional.ofNullable(r).map(Result::getException).orElse(null);
                        Tracer.traceEntry(error, finalInterfaceEntry);
                        Tracer.traceEntry(error, finalMethodEntry);
                    }
                    if (finalMethodEntry != null) {
                        finalMethodEntry.exit(1, invocation.getArguments());
                    }
                    if (finalInterfaceEntry != null) {
                        finalInterfaceEntry.exit();
                    }
                });
                methodEntry = null;
                interfaceEntry = null;
            }else if (result instanceof AppResponse){
                // 同步result
                if (result.hasException()) {
                    Tracer.traceEntry(result.getException(), interfaceEntry);
                    Tracer.traceEntry(result.getException(), methodEntry);
                }
            }
            return result;

相关问题