Search in sources :

Example 76 with Invocation

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

the class BroadcastCluster2Invoker method getCallables.

private List<Callable<BroadcastResult>> getCallables(List<Invoker<T>> invokers, Invocation invocation) {
    List<Callable<BroadcastResult>> tasks = invokers.stream().map(it -> (Callable<BroadcastResult>) () -> {
        BroadcastResult br = new BroadcastResult(it.getUrl().getIp(), it.getUrl().getPort());
        Result result = null;
        try {
            result = it.invoke(invocation);
            if (null != result && result.hasException()) {
                Throwable resultException = result.getException();
                if (null != resultException) {
                    RpcException exception = getRpcException(result.getException());
                    br.setExceptionMsg(exception.getMessage());
                    br.setException(exception);
                    logger.warn(exception.getMessage(), exception);
                }
            } else if (null != result) {
                br.setData(result.getValue());
                br.setResult(result);
            }
        } catch (Throwable ex) {
            RpcException exception = getRpcException(result.getException());
            br.setExceptionMsg(exception.getMessage());
            br.setException(exception);
            logger.warn(exception.getMessage(), exception);
        }
        return br;
    }).collect(Collectors.toList());
    return tasks;
}
Also used : LoggerFactory(org.apache.dubbo.common.logger.LoggerFactory) Logger(org.apache.dubbo.common.logger.Logger) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) RpcContext(org.apache.dubbo.rpc.RpcContext) Invocation(org.apache.dubbo.rpc.Invocation) Callable(java.util.concurrent.Callable) Invoker(org.apache.dubbo.rpc.Invoker) RpcException(org.apache.dubbo.rpc.RpcException) Collectors(java.util.stream.Collectors) Result(org.apache.dubbo.rpc.Result) Executors(java.util.concurrent.Executors) NamedInternalThreadFactory(org.apache.dubbo.common.threadlocal.NamedInternalThreadFactory) ArrayList(java.util.ArrayList) Directory(org.apache.dubbo.rpc.cluster.Directory) List(java.util.List) Future(java.util.concurrent.Future) AppResponse(org.apache.dubbo.rpc.AppResponse) Gson(com.google.gson.Gson) BiConsumer(java.util.function.BiConsumer) ExecutorService(java.util.concurrent.ExecutorService) RpcException(org.apache.dubbo.rpc.RpcException) Callable(java.util.concurrent.Callable) Result(org.apache.dubbo.rpc.Result)

Example 77 with Invocation

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

the class TraceFilterTest method testInvoke.

@Test
public void testInvoke() throws Exception {
    String method = "sayHello";
    Class<?> type = DemoService.class;
    String key = type.getName() + "." + method;
    // add tracer
    TraceFilter.addTracer(type, method, mockChannel, 2);
    Invoker<DemoService> mockInvoker = mock(Invoker.class);
    Invocation mockInvocation = mock(Invocation.class);
    Result mockResult = mock(Result.class);
    TraceFilter filter = new TraceFilter();
    given(mockInvoker.getInterface()).willReturn(DemoService.class);
    given(mockInvocation.getMethodName()).willReturn(method);
    given(mockInvocation.getArguments()).willReturn(new Object[0]);
    given(mockInvoker.invoke(mockInvocation)).willReturn(mockResult);
    given(mockResult.getValue()).willReturn("result");
    // test invoke
    filter.invoke(mockInvoker, mockInvocation);
    String message = listToString(mockChannel.getReceivedObjects());
    String expectMessage = "org.apache.dubbo.rpc.protocol.dubbo.support.DemoService.sayHello([]) -> \"result\"";
    System.out.println("actual message: " + message);
    Assertions.assertTrue(message.contains(expectMessage));
    Assertions.assertTrue(message.contains("elapsed:"));
    AtomicInteger traceCount = (AtomicInteger) mockChannel.getAttribute(TRACE_COUNT);
    Assertions.assertEquals(1, traceCount.get());
    // test remove channel when count >= max - 1
    filter.invoke(mockInvoker, mockInvocation);
    Field tracers = TraceFilter.class.getDeclaredField(TRACERS_FIELD_NAME);
    tracers.setAccessible(true);
    ConcurrentHashMap<String, Set<Channel>> o = (ConcurrentHashMap<String, Set<Channel>>) tracers.get(new ConcurrentHashMap<String, Set<Channel>>());
    Assertions.assertTrue(o.containsKey(key));
    Set<Channel> channels = o.get(key);
    Assertions.assertNotNull(channels);
    Assertions.assertFalse(channels.contains(mockChannel));
}
Also used : Set(java.util.Set) Invocation(org.apache.dubbo.rpc.Invocation) Channel(org.apache.dubbo.remoting.Channel) DemoService(org.apache.dubbo.rpc.protocol.dubbo.support.DemoService) Result(org.apache.dubbo.rpc.Result) Field(java.lang.reflect.Field) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.jupiter.api.Test)

Example 78 with Invocation

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

the class MonitorFilterTest method testFilter.

@Test
public void testFilter() throws Exception {
    MonitorFilter monitorFilter = new MonitorFilter();
    monitorFilter.setMonitorFactory(monitorFactory);
    Invocation invocation = new RpcInvocation("aaa", MonitorService.class.getName(), "", new Class<?>[0], new Object[0]);
    RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345);
    Result result = monitorFilter.invoke(serviceInvoker, invocation);
    result.whenCompleteWithContext((r, t) -> {
        if (t == null) {
            monitorFilter.onResponse(r, serviceInvoker, invocation);
        } else {
            monitorFilter.onError(t, serviceInvoker, invocation);
        }
    });
    while (lastStatistics == null) {
        Thread.sleep(10);
    }
    Assertions.assertEquals("abc", lastStatistics.getParameter(MonitorService.APPLICATION));
    Assertions.assertEquals(MonitorService.class.getName(), lastStatistics.getParameter(MonitorService.INTERFACE));
    Assertions.assertEquals("aaa", lastStatistics.getParameter(MonitorService.METHOD));
    Assertions.assertEquals(NetUtils.getLocalHost() + ":20880", lastStatistics.getParameter(MonitorService.PROVIDER));
    Assertions.assertEquals(NetUtils.getLocalHost(), lastStatistics.getAddress());
    Assertions.assertNull(lastStatistics.getParameter(MonitorService.CONSUMER));
    Assertions.assertEquals(1, lastStatistics.getParameter(MonitorService.SUCCESS, 0));
    Assertions.assertEquals(0, lastStatistics.getParameter(MonitorService.FAILURE, 0));
    Assertions.assertEquals(1, lastStatistics.getParameter(MonitorService.CONCURRENT, 0));
    Assertions.assertEquals(invocation, lastInvocation);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) MonitorService(org.apache.dubbo.monitor.MonitorService) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 79 with Invocation

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

the class RpcUtilsTest method testAttachInvocationIdIfAsync_nullAttachments.

/**
 * scenario: async invocation, add attachment by default
 * verify: no error report when the original attachment is null
 */
@Test
public void testAttachInvocationIdIfAsync_nullAttachments() {
    URL url = URL.valueOf("dubbo://localhost/?test.async=true");
    Invocation inv = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    RpcUtils.attachInvocationIdIfAsync(url, inv);
    assertTrue(RpcUtils.getInvocationId(inv) >= 0L);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 80 with Invocation

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

the class AccessLogFilterTest method testCustom.

@Test
public void testCustom() {
    URL url = URL.valueOf("test://test:11/test?accesslog=custom-access.log");
    Invoker<AccessLogFilterTest> invoker = new MyInvoker<AccessLogFilterTest>(url);
    Invocation invocation = new MockInvocation();
    accessLogFilter.invoke(invoker, invocation);
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) MyInvoker(org.apache.dubbo.rpc.support.MyInvoker) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Aggregations

Invocation (org.apache.dubbo.rpc.Invocation)98 Test (org.junit.jupiter.api.Test)78 URL (org.apache.dubbo.common.URL)77 Invoker (org.apache.dubbo.rpc.Invoker)44 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)44 Result (org.apache.dubbo.rpc.Result)33 AppResponse (org.apache.dubbo.rpc.AppResponse)29 RpcException (org.apache.dubbo.rpc.RpcException)18 MockInvocation (org.apache.dubbo.rpc.support.MockInvocation)17 HashMap (java.util.HashMap)16 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)13 BlockMyInvoker (org.apache.dubbo.rpc.support.BlockMyInvoker)11 MyInvoker (org.apache.dubbo.rpc.support.MyInvoker)10 ArrayList (java.util.ArrayList)8 Person (org.apache.dubbo.rpc.support.Person)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 IMetricManager (com.alibaba.metrics.IMetricManager)5 Method (java.lang.reflect.Method)5 DemoService (org.apache.dubbo.monitor.dubbo.service.DemoService)5 FastCompass (com.alibaba.metrics.FastCompass)4