Search in sources :

Example 96 with RpcException

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

the class ExecuteLimitFilterTest method testMoreThanExecuteLimitInvoke.

@Test
public void testMoreThanExecuteLimitInvoke() throws Exception {
    int maxExecute = 10;
    int totalExecute = 20;
    final AtomicInteger failed = new AtomicInteger(0);
    final Invocation invocation = Mockito.mock(Invocation.class);
    when(invocation.getMethodName()).thenReturn("testMoreThanExecuteLimitInvoke");
    URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&executes=" + maxExecute);
    final Invoker<ExecuteLimitFilter> invoker = new BlockMyInvoker<ExecuteLimitFilter>(url, 1000);
    final CountDownLatch latch = new CountDownLatch(1);
    for (int i = 0; i < totalExecute; i++) {
        Thread thread = new Thread(new Runnable() {

            public void run() {
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    executeLimitFilter.invoke(invoker, invocation);
                } catch (RpcException expected) {
                    failed.incrementAndGet();
                }
            }
        });
        thread.start();
    }
    latch.countDown();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Assertions.assertEquals(totalExecute - maxExecute, failed.get());
}
Also used : BlockMyInvoker(org.apache.dubbo.rpc.support.BlockMyInvoker) Invocation(org.apache.dubbo.rpc.Invocation) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RpcException(org.apache.dubbo.rpc.RpcException) CountDownLatch(java.util.concurrent.CountDownLatch) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 97 with RpcException

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

the class ExecuteLimitFilterTest method testExecuteLimitInvokeWitException.

@Test
public void testExecuteLimitInvokeWitException() throws Exception {
    Invoker invoker = Mockito.mock(Invoker.class);
    doThrow(new RpcException()).when(invoker).invoke(any(Invocation.class));
    URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&executes=10");
    when(invoker.getUrl()).thenReturn(url);
    Invocation invocation = Mockito.mock(Invocation.class);
    when(invocation.getMethodName()).thenReturn("testExecuteLimitInvokeWitException");
    try {
        executeLimitFilter.invoke(invoker, invocation);
    } catch (Exception e) {
        Assertions.assertTrue(e instanceof RpcException);
        executeLimitFilter.onError(e, invoker, invocation);
    }
    Assertions.assertEquals(1, RpcStatus.getStatus(url, invocation.getMethodName()).getFailed());
}
Also used : BlockMyInvoker(org.apache.dubbo.rpc.support.BlockMyInvoker) Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) RpcException(org.apache.dubbo.rpc.RpcException) URL(org.apache.dubbo.common.URL) RpcException(org.apache.dubbo.rpc.RpcException) Test(org.junit.jupiter.api.Test)

Example 98 with RpcException

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

the class DubboProtocol method optimizeSerialization.

private void optimizeSerialization(URL url) throws RpcException {
    String className = url.getParameter(OPTIMIZER_KEY, "");
    if (StringUtils.isEmpty(className) || optimizers.contains(className)) {
        return;
    }
    logger.info("Optimizing the serialization process for Kryo, FST, etc...");
    try {
        Class clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
        if (!SerializationOptimizer.class.isAssignableFrom(clazz)) {
            throw new RpcException("The serialization optimizer " + className + " isn't an instance of " + SerializationOptimizer.class.getName());
        }
        SerializationOptimizer optimizer = (SerializationOptimizer) clazz.newInstance();
        if (optimizer.getSerializableClasses() == null) {
            return;
        }
        for (Class c : optimizer.getSerializableClasses()) {
            SerializableClassRegistry.registerClass(c);
        }
        optimizers.add(className);
    } catch (ClassNotFoundException e) {
        throw new RpcException("Cannot find the serialization optimizer class: " + className, e);
    } catch (InstantiationException | IllegalAccessException e) {
        throw new RpcException("Cannot instantiate the serialization optimizer class: " + className, e);
    }
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException) SerializationOptimizer(org.apache.dubbo.common.serialize.support.SerializationOptimizer)

Example 99 with RpcException

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

the class AbstractProxyInvoker method invoke.

@Override
public Result invoke(Invocation invocation) throws RpcException {
    try {
        Object value = doInvoke(proxy, invocation.getMethodName(), invocation.getParameterTypes(), invocation.getArguments());
        CompletableFuture<Object> future = wrapWithFuture(value);
        CompletableFuture<AppResponse> appResponseFuture = future.handle((obj, t) -> {
            AppResponse result = new AppResponse(invocation);
            if (t != null) {
                if (t instanceof CompletionException) {
                    result.setException(t.getCause());
                } else {
                    result.setException(t);
                }
            } else {
                result.setValue(obj);
            }
            return result;
        });
        return new AsyncRpcResult(appResponseFuture, invocation);
    } catch (InvocationTargetException e) {
        if (RpcContext.getContext().isAsyncStarted() && !RpcContext.getContext().stopAsync()) {
            logger.error("Provider async started, but got an exception from the original method, cannot write the exception back to consumer because an async result may have returned the new thread.", e);
        }
        return AsyncRpcResult.newDefaultAsyncResult(null, e.getTargetException(), invocation);
    } catch (Throwable e) {
        throw new RpcException("Failed to invoke remote proxy method " + invocation.getMethodName() + " to " + getUrl() + ", cause: " + e.getMessage(), e);
    }
}
Also used : AppResponse(org.apache.dubbo.rpc.AppResponse) CompletionException(java.util.concurrent.CompletionException) RpcException(org.apache.dubbo.rpc.RpcException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult)

Example 100 with RpcException

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

the class StubProxyFactoryWrapper method getProxy.

@Override
public <T> T getProxy(Invoker<T> invoker, boolean generic) throws RpcException {
    T proxy = proxyFactory.getProxy(invoker, generic);
    if (GenericService.class != invoker.getInterface()) {
        URL url = invoker.getUrl();
        String stub = url.getParameter(STUB_KEY, url.getParameter(LOCAL_KEY));
        if (ConfigUtils.isNotEmpty(stub)) {
            Class<?> serviceType = invoker.getInterface();
            if (ConfigUtils.isDefault(stub)) {
                if (url.hasParameter(STUB_KEY)) {
                    stub = serviceType.getName() + "Stub";
                } else {
                    stub = serviceType.getName() + "Local";
                }
            }
            try {
                Class<?> stubClass = ReflectUtils.forName(stub);
                if (!serviceType.isAssignableFrom(stubClass)) {
                    throw new IllegalStateException("The stub implementation class " + stubClass.getName() + " not implement interface " + serviceType.getName());
                }
                try {
                    Constructor<?> constructor = ReflectUtils.findConstructor(stubClass, serviceType);
                    proxy = (T) constructor.newInstance(new Object[] { proxy });
                    // export stub service
                    URLBuilder urlBuilder = URLBuilder.from(url);
                    if (url.getParameter(STUB_EVENT_KEY, DEFAULT_STUB_EVENT)) {
                        urlBuilder.addParameter(STUB_EVENT_METHODS_KEY, StringUtils.join(Wrapper.getWrapper(proxy.getClass()).getDeclaredMethodNames(), ","));
                        urlBuilder.addParameter(IS_SERVER_KEY, Boolean.FALSE.toString());
                        try {
                            export(proxy, (Class) invoker.getInterface(), urlBuilder.build());
                        } catch (Exception e) {
                            LOGGER.error("export a stub service error.", e);
                        }
                    }
                } catch (NoSuchMethodException e) {
                    throw new IllegalStateException("No such constructor \"public " + stubClass.getSimpleName() + "(" + serviceType.getName() + ")\" in stub implementation class " + stubClass.getName(), e);
                }
            } catch (Throwable t) {
                LOGGER.error("Failed to create stub implementation class " + stub + " in consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", cause: " + t.getMessage(), t);
            // ignore
            }
        }
    }
    return proxy;
}
Also used : DEFAULT_STUB_EVENT(org.apache.dubbo.rpc.Constants.DEFAULT_STUB_EVENT) URL(org.apache.dubbo.common.URL) RpcException(org.apache.dubbo.rpc.RpcException) URLBuilder(org.apache.dubbo.common.URLBuilder)

Aggregations

RpcException (org.apache.dubbo.rpc.RpcException)102 URL (org.apache.dubbo.common.URL)37 Test (org.junit.jupiter.api.Test)29 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)28 Result (org.apache.dubbo.rpc.Result)21 Invocation (org.apache.dubbo.rpc.Invocation)17 ArrayList (java.util.ArrayList)15 AppResponse (org.apache.dubbo.rpc.AppResponse)13 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)13 Invoker (org.apache.dubbo.rpc.Invoker)13 IOException (java.io.IOException)9 List (java.util.List)9 Method (java.lang.reflect.Method)8 Gson (com.google.gson.Gson)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 HashMap (java.util.HashMap)5 RemotingException (org.apache.dubbo.remoting.RemotingException)5 TException (org.apache.thrift.TException)5 SocketTimeoutException (java.net.SocketTimeoutException)4 CountDownLatch (java.util.concurrent.CountDownLatch)4