Search in sources :

Example 1 with RpcContext

use of org.apache.dubbo.rpc.RpcContext in project dubbo by alibaba.

the class RpcContextInterceptor method interceptCall.

@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
    RpcContext rpcContext = RpcContext.getContext();
    Map<String, Object> attachments = new HashMap<>(rpcContext.getObjectAttachments());
    return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {

        @Override
        public void start(Listener<RespT> responseListener, Metadata headers) {
            if (!attachments.isEmpty()) {
                for (Map.Entry<String, Object> entry : attachments.entrySet()) {
                    // only used for string
                    if (entry.getValue() instanceof String) {
                        headers.put(Metadata.Key.of(DUBBO + entry.getKey(), ASCII_STRING_MARSHALLER), ((String) entry.getValue()));
                    }
                }
            }
            super.start(responseListener, headers);
        }
    };
}
Also used : RpcContext(org.apache.dubbo.rpc.RpcContext) ForwardingServerCallListener(io.grpc.ForwardingServerCallListener) HashMap(java.util.HashMap) Metadata(io.grpc.Metadata) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with RpcContext

use of org.apache.dubbo.rpc.RpcContext in project dubbo by alibaba.

the class FailoverClusterInvokerTest method testInvoke_retryTimes2.

@Test()
public void testInvoke_retryTimes2() {
    int finalRetries = 1;
    given(invoker1.invoke(invocation)).willThrow(new RpcException(RpcException.TIMEOUT_EXCEPTION));
    given(invoker1.isAvailable()).willReturn(false);
    given(invoker1.getUrl()).willReturn(url);
    given(invoker1.getInterface()).willReturn(FailoverClusterInvokerTest.class);
    given(invoker2.invoke(invocation)).willThrow(new RpcException());
    given(invoker2.isAvailable()).willReturn(false);
    given(invoker2.getUrl()).willReturn(url);
    given(invoker2.getInterface()).willReturn(FailoverClusterInvokerTest.class);
    RpcContext rpcContext = RpcContext.getContext();
    rpcContext.setAttachment("retries", finalRetries);
    FailoverClusterInvoker<FailoverClusterInvokerTest> invoker = new FailoverClusterInvoker<FailoverClusterInvokerTest>(dic);
    try {
        Result ret = invoker.invoke(invocation);
        assertSame(result, ret);
        fail();
    } catch (RpcException expected) {
        assertTrue((expected.isTimeout() || expected.getCode() == 0));
        assertTrue(expected.getMessage().indexOf((finalRetries + 1) + " times") > 0);
    }
}
Also used : RpcContext(org.apache.dubbo.rpc.RpcContext) RpcException(org.apache.dubbo.rpc.RpcException) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 3 with RpcContext

use of org.apache.dubbo.rpc.RpcContext in project dubbo by alibaba.

the class ConsumerContextFilter method invoke.

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    RpcContext context = RpcContext.getContext();
    context.setInvoker(invoker).setInvocation(invocation).setLocalAddress(NetUtils.getLocalHost(), 0).setRemoteAddress(invoker.getUrl().getHost(), invoker.getUrl().getPort()).setRemoteApplicationName(invoker.getUrl().getParameter(REMOTE_APPLICATION_KEY)).setAttachment(REMOTE_APPLICATION_KEY, invoker.getUrl().getParameter(APPLICATION_KEY));
    if (invocation instanceof RpcInvocation) {
        ((RpcInvocation) invocation).setInvoker(invoker);
    }
    // pass default timeout set by end user (ReferenceConfig)
    Object countDown = context.get(TIME_COUNTDOWN_KEY);
    if (countDown != null) {
        TimeoutCountDown timeoutCountDown = (TimeoutCountDown) countDown;
        if (timeoutCountDown.isExpired()) {
            return AsyncRpcResult.newDefaultAsyncResult(new RpcException(RpcException.TIMEOUT_TERMINATE, "No time left for making the following call: " + invocation.getServiceName() + "." + invocation.getMethodName() + ", terminate directly."), invocation);
        }
    }
    return invoker.invoke(invocation);
}
Also used : RpcContext(org.apache.dubbo.rpc.RpcContext) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) RpcException(org.apache.dubbo.rpc.RpcException) TimeoutCountDown(org.apache.dubbo.rpc.TimeoutCountDown)

Example 4 with RpcContext

use of org.apache.dubbo.rpc.RpcContext in project pinpoint by naver.

the class ApacheDubboProviderInterceptor method recordRequest.

private void recordRequest(SpanRecorder recorder, Object target, Object[] args) {
    final RpcInvocation invocation = (RpcInvocation) args[0];
    final RpcContext rpcContext = RpcContext.getContext();
    // Record rpc name, client address, server address.
    recorder.recordRpcName(invocation.getInvoker().getInterface().getSimpleName() + ":" + invocation.getMethodName());
    recorder.recordEndPoint(rpcContext.getLocalAddressString());
    if (rpcContext.getRemoteHost() != null) {
        recorder.recordRemoteAddress(rpcContext.getRemoteAddressString());
    } else {
        recorder.recordRemoteAddress("Unknown");
    }
    // If this transaction did not begin here, record parent(client who sent this request) information
    if (!recorder.isRoot()) {
        final String parentApplicationName = invocation.getAttachment(ApacheDubboConstants.META_PARENT_APPLICATION_NAME);
        if (parentApplicationName != null) {
            final short parentApplicationType = NumberUtils.parseShort(invocation.getAttachment(ApacheDubboConstants.META_PARENT_APPLICATION_TYPE), ServiceType.UNDEFINED.getCode());
            recorder.recordParentApplication(parentApplicationName, parentApplicationType);
            final String host = invocation.getAttachment(ApacheDubboConstants.META_HOST);
            if (host != null) {
                recorder.recordAcceptorHost(host);
            } else {
                // old version fallback
                final String estimatedLocalHost = getLocalHost(rpcContext);
                if (estimatedLocalHost != null) {
                    recorder.recordAcceptorHost(estimatedLocalHost);
                }
            }
        }
    }
    // clear attachments
    this.clearAttachments(rpcContext);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) RpcContext(org.apache.dubbo.rpc.RpcContext)

Example 5 with RpcContext

use of org.apache.dubbo.rpc.RpcContext in project dubbo by alibaba.

the class ContextFilter method invoke.

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    Map<String, Object> attachments = invocation.getObjectAttachments();
    if (attachments != null) {
        Map<String, Object> newAttach = new HashMap<>(attachments.size());
        for (Map.Entry<String, Object> entry : attachments.entrySet()) {
            String key = entry.getKey();
            if (!UNLOADING_KEYS.contains(key)) {
                newAttach.put(key, entry.getValue());
            }
        }
        attachments = newAttach;
    }
    RpcContext context = RpcContext.getContext();
    context.setInvoker(invoker).setInvocation(invocation).setLocalAddress(invoker.getUrl().getHost(), invoker.getUrl().getPort());
    String remoteApplication = (String) invocation.getAttachment(REMOTE_APPLICATION_KEY);
    if (StringUtils.isNotEmpty(remoteApplication)) {
        context.setRemoteApplicationName(remoteApplication);
    } else {
        context.setRemoteApplicationName((String) context.getAttachment(REMOTE_APPLICATION_KEY));
    }
    long timeout = RpcUtils.getTimeout(invocation, -1);
    if (timeout != -1) {
        context.set(TIME_COUNTDOWN_KEY, TimeoutCountDown.newCountDown(timeout, TimeUnit.MILLISECONDS));
    }
    // we may already added some attachments into RpcContext before this filter (e.g. in rest protocol)
    if (attachments != null) {
        if (context.getObjectAttachments() != null) {
            context.getObjectAttachments().putAll(attachments);
        } else {
            context.setObjectAttachments(attachments);
        }
    }
    if (invocation instanceof RpcInvocation) {
        ((RpcInvocation) invocation).setInvoker(invoker);
    }
    try {
        context.clearAfterEachInvoke(false);
        return invoker.invoke(invocation);
    } finally {
        context.clearAfterEachInvoke(true);
        // IMPORTANT! For async scenario, we must remove context from current thread, so we always create a new RpcContext for the next invoke for the same thread.
        RpcContext.removeContext(true);
        RpcContext.removeServerContext();
    }
}
Also used : RpcContext(org.apache.dubbo.rpc.RpcContext) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

RpcContext (org.apache.dubbo.rpc.RpcContext)18 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)5 Test (org.junit.jupiter.api.Test)5 Result (org.apache.dubbo.rpc.Result)4 RpcException (org.apache.dubbo.rpc.RpcException)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)2 Span (brave.Span)1 Kind (brave.Span.Kind)1 CurrentTraceContext (brave.propagation.CurrentTraceContext)1 Scope (brave.propagation.CurrentTraceContext.Scope)1 TraceContext (brave.propagation.TraceContext)1 HessianConnection (com.caucho.hessian.client.HessianConnection)1 ForwardingServerCallListener (io.grpc.ForwardingServerCallListener)1 Metadata (io.grpc.Metadata)1 InetSocketAddress (java.net.InetSocketAddress)1 MetricsService (org.apache.dubbo.monitor.MetricsService)1 Protocol (org.apache.dubbo.rpc.Protocol)1 TimeoutCountDown (org.apache.dubbo.rpc.TimeoutCountDown)1