Search in sources :

Example 96 with Invoker

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

the class ConsumerSignFilterTest method testAuthEnabled.

@Test
void testAuthEnabled() {
    URL url = URL.valueOf("dubbo://10.10.10.10:2181").addParameter(Constants.ACCESS_KEY_ID_KEY, "ak").addParameter(Constants.SECRET_ACCESS_KEY_KEY, "sk").addParameter(CommonConstants.APPLICATION_KEY, "test").addParameter(Constants.SERVICE_AUTH, true);
    Invoker invoker = mock(Invoker.class);
    Invocation invocation = mock(Invocation.class);
    when(invoker.getUrl()).thenReturn(url);
    ConsumerSignFilter consumerSignFilter = new ConsumerSignFilter();
    consumerSignFilter.invoke(invoker, invocation);
    verify(invocation, times(1)).setAttachment(eq(Constants.REQUEST_SIGNATURE_KEY), anyString());
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 97 with Invoker

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

the class ForkingClusterInvoker method doInvoke.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Result doInvoke(final Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    try {
        checkInvokers(invokers, invocation);
        final List<Invoker<T>> selected;
        final int forks = getUrl().getParameter(FORKS_KEY, DEFAULT_FORKS);
        final int timeout = getUrl().getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT);
        if (forks <= 0 || forks >= invokers.size()) {
            selected = invokers;
        } else {
            selected = new ArrayList<>(forks);
            while (selected.size() < forks) {
                Invoker<T> invoker = select(loadbalance, invocation, invokers, selected);
                if (!selected.contains(invoker)) {
                    // Avoid add the same invoker several times.
                    selected.add(invoker);
                }
            }
        }
        RpcContext.getContext().setInvokers((List) selected);
        final AtomicInteger count = new AtomicInteger();
        final BlockingQueue<Object> ref = new LinkedBlockingQueue<>();
        for (final Invoker<T> invoker : selected) {
            executor.execute(() -> {
                try {
                    Result result = invoker.invoke(invocation);
                    ref.offer(result);
                } catch (Throwable e) {
                    int value = count.incrementAndGet();
                    if (value >= selected.size()) {
                        ref.offer(e);
                    }
                }
            });
        }
        try {
            Object ret = ref.poll(timeout, TimeUnit.MILLISECONDS);
            if (ret instanceof Throwable) {
                Throwable e = (Throwable) ret;
                throw new RpcException(e instanceof RpcException ? ((RpcException) e).getCode() : 0, "Failed to forking invoke provider " + selected + ", but no luck to perform the invocation. Last error is: " + e.getMessage(), e.getCause() != null ? e.getCause() : e);
            }
            return (Result) ret;
        } catch (InterruptedException e) {
            throw new RpcException("Failed to forking invoke provider " + selected + ", but no luck to perform the invocation. Last error is: " + e.getMessage(), e);
        }
    } finally {
        // clear attachments which is binding to current thread.
        RpcContext.getContext().clearAttachments();
    }
}
Also used : LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Result(org.apache.dubbo.rpc.Result) Invoker(org.apache.dubbo.rpc.Invoker) DEFAULT_TIMEOUT(org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RpcException(org.apache.dubbo.rpc.RpcException)

Example 98 with Invoker

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

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

the class AbstractClusterInvoker method invoke.

@Override
public Result invoke(final Invocation invocation) throws RpcException {
    checkWhetherDestroyed();
    // binding attachments into invocation.
    Map<String, Object> contextAttachments = RpcContext.getContext().getObjectAttachments();
    if (contextAttachments != null && contextAttachments.size() != 0) {
        ((RpcInvocation) invocation).addObjectAttachments(contextAttachments);
    }
    List<Invoker<T>> invokers = list(invocation);
    LoadBalance loadbalance = initLoadBalance(invokers, invocation);
    RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
    return doInvoke(invocation, invokers, loadbalance);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) ClusterInvoker(org.apache.dubbo.rpc.cluster.ClusterInvoker) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance)

Example 100 with Invoker

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

the class RegistryDirectoryTest method testNofityOverrideUrls_CleanOnly.

/**
 * The test clears the override rule and only sends the override cleanup rules
 * Whether the test can be restored to the providerUrl when it is pushed
 */
@Test
public void testNofityOverrideUrls_CleanOnly() {
    RegistryDirectory registryDirectory = getRegistryDirectory();
    invocation = new RpcInvocation();
    List<URL> durls = new ArrayList<URL>();
    durls.add(SERVICEURL.setHost("10.20.30.140").addParameter("timeout", "1"));
    registryDirectory.notify(durls);
    Assertions.assertNull(((InvokerWrapper<?>) registryDirectory.getInvokers().get(0)).getUrl().getParameter("mock"));
    // override
    durls = new ArrayList<URL>();
    durls.add(URL.valueOf("override://0.0.0.0?timeout=1000&mock=fail"));
    registryDirectory.notify(durls);
    List<Invoker<?>> invokers = registryDirectory.list(invocation);
    Invoker<?> aInvoker = invokers.get(0);
    Assertions.assertEquals("1000", aInvoker.getUrl().getParameter("timeout"));
    Assertions.assertEquals("fail", ((InvokerWrapper<?>) registryDirectory.getInvokers().get(0)).getUrl().getParameter("mock"));
    // override clean
    durls = new ArrayList<URL>();
    durls.add(URL.valueOf("override://0.0.0.0/dubbo.test.api.HelloService"));
    registryDirectory.notify(durls);
    invokers = registryDirectory.list(invocation);
    aInvoker = invokers.get(0);
    // Need to be restored to the original providerUrl
    Assertions.assertEquals("1", aInvoker.getUrl().getParameter("timeout"));
    Assertions.assertNull(((InvokerWrapper<?>) registryDirectory.getInvokers().get(0)).getUrl().getParameter("mock"));
}
Also used : RegistryDirectory(org.apache.dubbo.registry.integration.RegistryDirectory) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) MockClusterInvoker(org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker) Invoker(org.apache.dubbo.rpc.Invoker) ArrayList(java.util.ArrayList) InvokerWrapper(org.apache.dubbo.rpc.protocol.InvokerWrapper) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Aggregations

Invoker (org.apache.dubbo.rpc.Invoker)128 Test (org.junit.jupiter.api.Test)104 URL (org.apache.dubbo.common.URL)74 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)66 ArrayList (java.util.ArrayList)48 Invocation (org.apache.dubbo.rpc.Invocation)42 Result (org.apache.dubbo.rpc.Result)26 AppResponse (org.apache.dubbo.rpc.AppResponse)21 MockClusterInvoker (org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker)21 RegistryDirectory (org.apache.dubbo.registry.integration.RegistryDirectory)20 Router (org.apache.dubbo.rpc.cluster.Router)18 MockInvoker (org.apache.dubbo.rpc.cluster.router.MockInvoker)18 HashMap (java.util.HashMap)13 LoadBalance (org.apache.dubbo.rpc.cluster.LoadBalance)13 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)12 RpcException (org.apache.dubbo.rpc.RpcException)12 LeastActiveLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance)8 RandomLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance)8 RoundRobinLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8