Search in sources :

Example 96 with RpcInvocation

use of org.apache.dubbo.rpc.RpcInvocation 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)

Example 97 with RpcInvocation

use of org.apache.dubbo.rpc.RpcInvocation 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 98 with RpcInvocation

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

the class AbstractLoadBalanceTest method testGetWeight.

@Test
public void testGetWeight() {
    RpcInvocation invocation = new RpcInvocation();
    invocation.setMethodName("say");
    Invoker invoker1 = mock(Invoker.class, Mockito.withSettings().stubOnly());
    URL url1 = new URL("", "", 0, "DemoService", new HashMap<>());
    url1 = url1.addParameter(TIMESTAMP_KEY, System.currentTimeMillis() - Integer.MAX_VALUE - 1);
    given(invoker1.getUrl()).willReturn(url1);
    Invoker invoker2 = mock(Invoker.class, Mockito.withSettings().stubOnly());
    URL url2 = new URL("", "", 0, "DemoService", new HashMap<>());
    url2 = url2.addParameter(TIMESTAMP_KEY, System.currentTimeMillis() - 10 * 60 * 1000L - 1);
    given(invoker2.getUrl()).willReturn(url2);
    Assertions.assertEquals(balance.getWeight(invoker1, invocation), balance.getWeight(invoker2, invocation));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 99 with RpcInvocation

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

the class FileRouterEngineTest method initInvocation.

private void initInvocation(String methodName) {
    invocation = new RpcInvocation();
    ((RpcInvocation) invocation).setMethodName(methodName);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation)

Example 100 with RpcInvocation

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

the class AbstractClusterInvokerTest method testMockedInvokerSelect.

/**
 * Test mock invoker selector works as expected
 */
@Test
public void testMockedInvokerSelect() {
    initlistsize5();
    invokers.add(mockedInvoker1);
    initDic();
    RpcInvocation mockedInvocation = new RpcInvocation();
    mockedInvocation.setMethodName("sayHello");
    mockedInvocation.setAttachment(INVOCATION_NEED_MOCK, "true");
    List<Invoker<IHelloService>> mockedInvokers = dic.list(mockedInvocation);
    Assertions.assertEquals(1, mockedInvokers.size());
    List<Invoker<IHelloService>> invokers = dic.list(invocation);
    Assertions.assertEquals(5, invokers.size());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) Test(org.junit.jupiter.api.Test)

Aggregations

RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)172 Test (org.junit.jupiter.api.Test)122 URL (org.apache.dubbo.common.URL)101 Invoker (org.apache.dubbo.rpc.Invoker)65 ArrayList (java.util.ArrayList)51 Result (org.apache.dubbo.rpc.Result)38 Invocation (org.apache.dubbo.rpc.Invocation)36 RpcException (org.apache.dubbo.rpc.RpcException)26 RegistryDirectory (org.apache.dubbo.registry.integration.RegistryDirectory)23 AppResponse (org.apache.dubbo.rpc.AppResponse)20 MockClusterInvoker (org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker)20 Router (org.apache.dubbo.rpc.cluster.Router)19 MockInvoker (org.apache.dubbo.rpc.cluster.router.MockInvoker)18 HashMap (java.util.HashMap)16 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)16 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 Method (java.lang.reflect.Method)9 List (java.util.List)8 Person (org.apache.dubbo.rpc.support.Person)7 Protocol (org.apache.dubbo.rpc.Protocol)6