org.apache.dubbo.rpc.support.RpcUtils Java Examples

The following examples show how to use org.apache.dubbo.rpc.support.RpcUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
@Override
public Result onResponse(Result result, Invoker<?> invoker, Invocation invocation) {
    String spanKey = getTracerSpanMapKey(invoker);
    try {
        // only the asynchronous callback to print
        boolean isAsync = RpcUtils.isAsync(invoker.getUrl(), invocation);
        if (!isAsync) {
            return result;
        }
        if (TracerSpanMap.containsKey(spanKey)) {
            SofaTracerSpan sofaTracerSpan = TracerSpanMap.get(spanKey);
            // to build tracer instance
            if (dubboConsumerSofaTracer == null) {
                this.dubboConsumerSofaTracer = DubboConsumerSofaTracer
                    .getDubboConsumerSofaTracerSingleton();
            }
            String resultCode = SofaTracerConstant.RESULT_CODE_SUCCESS;
            if (result.hasException()) {
                if (result.getException() instanceof RpcException) {
                    resultCode = Integer.toString(((RpcException) result.getException())
                        .getCode());
                    sofaTracerSpan.setTag(CommonSpanTags.RESULT_CODE, resultCode);
                } else {
                    resultCode = SofaTracerConstant.RESULT_CODE_ERROR;
                }
            }
            // add elapsed time
            appendElapsedTimeTags(invocation, sofaTracerSpan, result, true);
            dubboConsumerSofaTracer.clientReceiveTagFinish(sofaTracerSpan, resultCode);
        }
    } finally {
        if (TracerSpanMap.containsKey(spanKey)) {
            TracerSpanMap.remove(spanKey);
        }
    }
    return result;
}
 
Example #2
Source File: SentinelDubboConsumerFilter.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    InvokeMode invokeMode = RpcUtils.getInvokeMode(invoker.getUrl(), invocation);
    if (InvokeMode.SYNC == invokeMode) {
        return syncInvoke(invoker, invocation);
    } else {
        return asyncInvoke(invoker, invocation);
    }

}
 
Example #3
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void initFallback() {
    DubboFallbackRegistry.setConsumerFallback(new DubboFallback() {
        @Override
        public Result handle(Invoker<?> invoker, Invocation invocation, BlockException ex) {
            boolean async = RpcUtils.isAsync(invoker.getUrl(), invocation);
            Result fallbackResult = null;
            fallbackResult = AsyncRpcResult.newDefaultAsyncResult("fallback", invocation);
            return fallbackResult;
        }
    });
}
 
Example #4
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private Result invokeDubboRpc(boolean exception, Invoker invoker, Invocation invocation) {
    Result result = null;
    InvokeMode invokeMode = RpcUtils.getInvokeMode(invoker.getUrl(), invocation);
    if (InvokeMode.SYNC == invokeMode) {
        result = exception ? new AppResponse(new Exception("error")) : new AppResponse("normal");
    } else {
        result = exception ? AsyncRpcResult.newDefaultAsyncResult(new Exception("error"), invocation) : AsyncRpcResult.newDefaultAsyncResult("normal", invocation);
    }
    when(invoker.invoke(invocation)).thenReturn(result);
    return consumerFilter.invoke(invoker, invocation);
}
 
Example #5
Source File: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 4 votes vote down vote up
/**
 * rpc client handler
 * @param rpcContext
 * @param invoker
 * @param invocation
 * @return
 */
private Result doClientFilter(RpcContext rpcContext, Invoker<?> invoker, Invocation invocation) {
    // to build tracer instance
    if (dubboConsumerSofaTracer == null) {
        this.dubboConsumerSofaTracer = DubboConsumerSofaTracer
            .getDubboConsumerSofaTracerSingleton();
    }
    // get methodName
    String methodName = rpcContext.getMethodName();
    // get service interface
    String service = invoker.getInterface().getSimpleName();
    // build a dubbo rpc span
    SofaTracerSpan sofaTracerSpan = dubboConsumerSofaTracer.clientSend(service + "#"
                                                                       + methodName);
    // set tags to span
    appendRpcClientSpanTags(invoker, sofaTracerSpan);
    // do serialized and then transparent transmission to the rpc server
    String serializedSpanContext = sofaTracerSpan.getSofaTracerSpanContext()
        .serializeSpanContext();
    //put into attachments
    invocation.getAttachments().put(CommonSpanTags.RPC_TRACE_NAME, serializedSpanContext);
    // check invoke type
    boolean isAsync = RpcUtils.isAsync(invoker.getUrl(), invocation);
    boolean isOneWay = false;
    if (isAsync) {
        sofaTracerSpan.setTag(CommonSpanTags.INVOKE_TYPE, "future");
    } else {
        isOneWay = RpcUtils.isOneway(invoker.getUrl(), invocation);
        if (isOneWay) {
            sofaTracerSpan.setTag(CommonSpanTags.INVOKE_TYPE, "oneway");
        } else {
            sofaTracerSpan.setTag(CommonSpanTags.INVOKE_TYPE, "sync");
        }
    }
    Result result;
    Throwable exception = null;
    String resultCode = SofaTracerConstant.RESULT_CODE_SUCCESS;
    try {
        // do invoke
        result = invoker.invoke(invocation);
        // check result
        if (result == null) {
            // isOneWay, we think that the current request is successful
            if (isOneWay) {
                sofaTracerSpan.setTag(CommonSpanTags.RESP_SIZE, 0);
            }
        } else {
            // add elapsed time
            appendElapsedTimeTags(invocation, sofaTracerSpan, result,true);
        }
    } catch (RpcException e) {
        exception = e;
        throw e;
    } catch (Throwable t) {
        exception = t;
        throw new RpcException(t);
    } finally {
        if (exception != null) {
            if (exception instanceof RpcException) {
                sofaTracerSpan.setTag(Tags.ERROR.getKey(),exception.getMessage());
                RpcException rpcException = (RpcException) exception;
                resultCode = String.valueOf(rpcException.getCode());
            } else {
                resultCode = SofaTracerConstant.RESULT_CODE_ERROR;
            }
        }

        if (!isAsync) {
            dubboConsumerSofaTracer.clientReceive(resultCode);
        } else {
            SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
            SofaTracerSpan clientSpan = sofaTraceContext.pop();
            if (clientSpan != null) {
                // Record client send event
                sofaTracerSpan.log(LogData.CLIENT_SEND_EVENT_VALUE);
            }
            // cache the current span
            TracerSpanMap.put(getTracerSpanMapKey(invoker), sofaTracerSpan);
            if (clientSpan != null && clientSpan.getParentSofaTracerSpan() != null) {
                //restore parent
                sofaTraceContext.push(clientSpan.getParentSofaTracerSpan());
            }
            CompletableFuture<Object> future = (CompletableFuture<Object>) RpcContext.getContext().getFuture();
            future.whenComplete((object, throwable)-> {
                if (throwable instanceof TimeoutException) {
                    sofaTracerSpan.setTag(Tags.ERROR.getKey(),throwable.getMessage());
                    dubboConsumerSofaTracer.clientReceiveTagFinish(sofaTracerSpan, SofaTracerConstant.RESULT_CODE_TIME_OUT);
                }
            });
        }
    }
    return result;
}