Search in sources :

Example 81 with Result

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

the class BroadcastClusterInvoker method doInvoke.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Result doInvoke(final Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    checkInvokers(invokers, invocation);
    RpcContext.getContext().setInvokers((List) invokers);
    RpcException exception = null;
    Result result = null;
    URL url = getUrl();
    // The value range of broadcast.fail.threshold must be 0~100.
    // 100 means that an exception will be thrown last, and 0 means that as long as an exception occurs, it will be thrown.
    // see https://github.com/apache/dubbo/pull/7174
    int broadcastFailPercent = url.getParameter(BROADCAST_FAIL_PERCENT_KEY, MAX_BROADCAST_FAIL_PERCENT);
    if (broadcastFailPercent < MIN_BROADCAST_FAIL_PERCENT || broadcastFailPercent > MAX_BROADCAST_FAIL_PERCENT) {
        logger.info(String.format("The value corresponding to the broadcast.fail.percent parameter must be between 0 and 100. " + "The current setting is %s, which is reset to 100.", broadcastFailPercent));
        broadcastFailPercent = MAX_BROADCAST_FAIL_PERCENT;
    }
    int failThresholdIndex = invokers.size() * broadcastFailPercent / MAX_BROADCAST_FAIL_PERCENT;
    int failIndex = 0;
    for (Invoker<T> invoker : invokers) {
        try {
            result = invoker.invoke(invocation);
            if (null != result && result.hasException()) {
                Throwable resultException = result.getException();
                if (null != resultException) {
                    exception = getRpcException(result.getException());
                    logger.warn(exception.getMessage(), exception);
                    if (failIndex == failThresholdIndex) {
                        break;
                    } else {
                        failIndex++;
                    }
                }
            }
        } catch (Throwable e) {
            exception = getRpcException(e);
            logger.warn(exception.getMessage(), exception);
            if (failIndex == failThresholdIndex) {
                break;
            } else {
                failIndex++;
            }
        }
    }
    if (exception != null) {
        if (failIndex == failThresholdIndex) {
            logger.debug(String.format("The number of BroadcastCluster call failures has reached the threshold %s", failThresholdIndex));
        } else {
            logger.debug(String.format("The number of BroadcastCluster call failures has not reached the threshold %s, fail size is %s", failThresholdIndex, failIndex));
        }
        throw exception;
    }
    return result;
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result)

Example 82 with Result

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

the class FutureFilterTest method testSyncCallback.

@Test
public void testSyncCallback() {
    @SuppressWarnings("unchecked") Invoker<DemoService> invoker = mock(Invoker.class);
    given(invoker.isAvailable()).willReturn(true);
    given(invoker.getInterface()).willReturn(DemoService.class);
    AppResponse result = new AppResponse();
    result.setValue("High");
    given(invoker.invoke(invocation)).willReturn(result);
    URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1");
    given(invoker.getUrl()).willReturn(url);
    Result filterResult = eventFilter.invoke(invoker, invocation);
    assertEquals("High", filterResult.getValue());
}
Also used : AppResponse(org.apache.dubbo.rpc.AppResponse) DemoService(org.apache.dubbo.rpc.protocol.dubbo.support.DemoService) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 83 with Result

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

the class InjvmInvoker method doInvoke.

@Override
public Result doInvoke(Invocation invocation) throws Throwable {
    Exporter<?> exporter = InjvmProtocol.getExporter(delegateExporterMap, getUrl());
    if (exporter == null) {
        throw new RpcException("Service [" + key + "] not found.");
    }
    RpcContext.getContext().setRemoteAddress(LOCALHOST_VALUE, 0);
    // Solve local exposure, the server opens the token, and the client call fails.
    URL serverURL = exporter.getInvoker().getUrl();
    boolean serverHasToken = serverURL.hasParameter(Constants.TOKEN_KEY);
    if (serverHasToken) {
        invocation.setAttachment(Constants.TOKEN_KEY, serverURL.getParameter(Constants.TOKEN_KEY));
    }
    if (isAsync(exporter.getInvoker().getUrl(), getUrl())) {
        ((RpcInvocation) invocation).setInvokeMode(InvokeMode.ASYNC);
        // use consumer executor
        ExecutorService executor = executorRepository.createExecutorIfAbsent(getUrl());
        CompletableFuture<AppResponse> appResponseFuture = CompletableFuture.supplyAsync(() -> {
            Result result = exporter.getInvoker().invoke(invocation);
            if (result.hasException()) {
                return new AppResponse(result.getException());
            } else {
                return new AppResponse(result.getValue());
            }
        }, executor);
        // save for 2.6.x compatibility, for example, TraceFilter in Zipkin uses com.alibaba.xxx.FutureAdapter
        FutureContext.getContext().setCompatibleFuture(appResponseFuture);
        AsyncRpcResult result = new AsyncRpcResult(appResponseFuture, invocation);
        result.setExecutor(executor);
        return result;
    } else {
        return exporter.getInvoker().invoke(invocation);
    }
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) AppResponse(org.apache.dubbo.rpc.AppResponse) RpcException(org.apache.dubbo.rpc.RpcException) ExecutorService(java.util.concurrent.ExecutorService) URL(org.apache.dubbo.common.URL) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult)

Example 84 with Result

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

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

Aggregations

Result (org.apache.dubbo.rpc.Result)97 Test (org.junit.jupiter.api.Test)74 URL (org.apache.dubbo.common.URL)55 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)39 Invocation (org.apache.dubbo.rpc.Invocation)33 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)31 AppResponse (org.apache.dubbo.rpc.AppResponse)28 Invoker (org.apache.dubbo.rpc.Invoker)27 RpcException (org.apache.dubbo.rpc.RpcException)22 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)7 RpcContext (org.apache.dubbo.rpc.RpcContext)7 BlockMyInvoker (org.apache.dubbo.rpc.support.BlockMyInvoker)6 DemoService (org.apache.dubbo.rpc.support.DemoService)6 List (java.util.List)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Protocol (org.apache.dubbo.rpc.Protocol)5 Person (org.apache.dubbo.rpc.support.Person)5 Method (java.lang.reflect.Method)4 MockProtocol (org.apache.dubbo.rpc.support.MockProtocol)4