Search in sources :

Example 76 with Result

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

the class ProviderAuthFilterTest method testAuthFailed.

@Test
void testAuthFailed() {
    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(invocation.getAttachment(Constants.REQUEST_SIGNATURE_KEY)).thenReturn(null);
    when(invoker.getUrl()).thenReturn(url);
    ProviderAuthFilter providerAuthFilter = new ProviderAuthFilter();
    Result result = providerAuthFilter.invoke(invoker, invocation);
    assertTrue(result.hasException());
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 77 with Result

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

the class ProviderAuthFilterTest method testAuthFailedWhenNoSignature.

@Test
void testAuthFailedWhenNoSignature() {
    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(invocation.getAttachment(Constants.REQUEST_SIGNATURE_KEY)).thenReturn(null);
    when(invoker.getUrl()).thenReturn(url);
    ProviderAuthFilter providerAuthFilter = new ProviderAuthFilter();
    Result result = providerAuthFilter.invoke(invoker, invocation);
    assertTrue(result.hasException());
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 78 with Result

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

the class ProviderAuthFilterTest method testAuthFailedWhenNoAccessKeyPair.

@Test
void testAuthFailedWhenNoAccessKeyPair() {
    URL url = URL.valueOf("dubbo://10.10.10.10:2181").addParameter(CommonConstants.APPLICATION_KEY, "test-provider").addParameter(Constants.SERVICE_AUTH, true);
    Invoker invoker = mock(Invoker.class);
    Invocation invocation = mock(Invocation.class);
    when(invocation.getObjectAttachment(Constants.REQUEST_SIGNATURE_KEY)).thenReturn("dubbo");
    when(invocation.getObjectAttachment(Constants.AK_KEY)).thenReturn("ak");
    when(invocation.getObjectAttachment(CommonConstants.CONSUMER)).thenReturn("test-consumer");
    when(invocation.getObjectAttachment(Constants.REQUEST_TIMESTAMP_KEY)).thenReturn(System.currentTimeMillis());
    when(invoker.getUrl()).thenReturn(url);
    ProviderAuthFilter providerAuthFilter = new ProviderAuthFilter();
    Result result = providerAuthFilter.invoke(invoker, invocation);
    assertTrue(result.hasException());
    assertTrue(result.getException() instanceof RpcAuthenticationException);
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) RpcAuthenticationException(org.apache.dubbo.auth.exception.RpcAuthenticationException) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 79 with Result

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

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

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