Search in sources :

Example 6 with RpcContext

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

the class ConsumerContextClusterInterceptorTest method testAfter.

@Test
public void testAfter() {
    RpcContext contextBefore = RpcContext.getContext();
    interceptor.after(null, null);
    RpcContext contextAfter = RpcContext.getContext();
    Assertions.assertNotSame(contextBefore, contextAfter);
}
Also used : RpcContext(org.apache.dubbo.rpc.RpcContext) Test(org.junit.jupiter.api.Test)

Example 7 with RpcContext

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

the class ConsumerContextClusterInterceptorTest method testBefore.

@Test
public void testBefore() {
    AbstractClusterInvoker mockInvoker = Mockito.mock(AbstractClusterInvoker.class);
    RpcContext serverContextBefore = RpcContext.getServerContext();
    RpcInvocation rpcInvocation = new RpcInvocation();
    interceptor.before(mockInvoker, rpcInvocation);
    RpcContext serverContextAfter = RpcContext.getServerContext();
    Assertions.assertNotSame(serverContextBefore, serverContextAfter);
    Assertions.assertSame(mockInvoker, rpcInvocation.getInvoker());
}
Also used : RpcContext(org.apache.dubbo.rpc.RpcContext) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) AbstractClusterInvoker(org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker) Test(org.junit.jupiter.api.Test)

Example 8 with RpcContext

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

the class ZoneAwareClusterInterceptorTest method testBefore.

@Test
public void testBefore() {
    RpcInvocation invocation = new RpcInvocation();
    interceptor.before(null, invocation);
    Assertions.assertEquals("mock_zone", invocation.getAttachment(REGISTRY_ZONE));
    Assertions.assertEquals("mock_force", invocation.getAttachment(REGISTRY_ZONE_FORCE));
    RpcContext context = RpcContext.getContext();
    context.setAttachment(REGISTRY_ZONE, "context_zone");
    context.setAttachment(REGISTRY_ZONE_FORCE, "context_force");
    interceptor.before(null, invocation);
    Assertions.assertEquals("context_zone", invocation.getAttachment(REGISTRY_ZONE));
    Assertions.assertEquals("context_force", invocation.getAttachment(REGISTRY_ZONE_FORCE));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) RpcContext(org.apache.dubbo.rpc.RpcContext) Test(org.junit.jupiter.api.Test)

Example 9 with RpcContext

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

the class MetricsFilter method invoke.

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (exported.compareAndSet(false, true)) {
        this.protocolName = invoker.getUrl().getParameter(METRICS_PROTOCOL) == null ? DEFAULT_PROTOCOL : invoker.getUrl().getParameter(METRICS_PROTOCOL);
        Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(protocolName);
        this.port = invoker.getUrl().getParameter(METRICS_PORT) == null ? protocol.getDefaultPort() : Integer.parseInt(invoker.getUrl().getParameter(METRICS_PORT));
        Invoker<MetricsService> metricsInvoker = initMetricsInvoker();
        try {
            protocol.export(metricsInvoker);
        } catch (RuntimeException e) {
            logger.error("Metrics Service need to be configured" + " when multiple processes are running on a host" + e.getMessage());
        }
    }
    RpcContext context = RpcContext.getContext();
    boolean isProvider = context.isProviderSide();
    long start = System.currentTimeMillis();
    try {
        // proceed invocation chain
        Result result = invoker.invoke(invocation);
        long duration = System.currentTimeMillis() - start;
        reportMetrics(invoker, invocation, duration, "success", isProvider);
        return result;
    } catch (RpcException e) {
        long duration = System.currentTimeMillis() - start;
        String result = "error";
        if (e.isTimeout()) {
            result = "timeoutError";
        }
        if (e.isBiz()) {
            result = "bisError";
        }
        if (e.isNetwork()) {
            result = "networkError";
        }
        if (e.isSerialization()) {
            result = "serializationError";
        }
        reportMetrics(invoker, invocation, duration, result, isProvider);
        throw e;
    }
}
Also used : RpcContext(org.apache.dubbo.rpc.RpcContext) MetricsService(org.apache.dubbo.monitor.MetricsService) RpcException(org.apache.dubbo.rpc.RpcException) Protocol(org.apache.dubbo.rpc.Protocol) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result)

Example 10 with RpcContext

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

the class RmiRemoteInvocation method invoke.

/**
 * Need to restore context on provider side (Though context will be overridden by Invocation's attachment
 * when ContextFilter gets executed, we will restore the attachment when Invocation is constructed, check more
 * from {@link org.apache.dubbo.rpc.proxy.InvokerInvocationHandler}
 */
@SuppressWarnings("unchecked")
@Override
public Object invoke(Object targetObject) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    RpcContext context = RpcContext.getContext();
    context.setObjectAttachments((Map<String, Object>) getAttribute(DUBBO_ATTACHMENTS_ATTR_NAME));
    String generic = (String) getAttribute(GENERIC_KEY);
    if (StringUtils.isNotEmpty(generic)) {
        context.setAttachment(GENERIC_KEY, generic);
    }
    try {
        return super.invoke(targetObject);
    } finally {
        context.setObjectAttachments(null);
    }
}
Also used : RpcContext(org.apache.dubbo.rpc.RpcContext)

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