Search in sources :

Example 31 with RpcException

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

the class GrpcInvoker method doInvoke.

@Override
protected Result doInvoke(Invocation invocation) throws Throwable {
    try {
        Result result = target.invoke(invocation);
        // FIXME result is an AsyncRpcResult instance.
        Throwable e = result.getException();
        if (e != null) {
            throw getRpcException(getInterface(), getUrl(), invocation, e);
        }
        return result;
    } catch (RpcException e) {
        if (e.getCode() == RpcException.UNKNOWN_EXCEPTION) {
            e.setCode(getErrorCode(e.getCause()));
        }
        throw e;
    } catch (Throwable e) {
        throw getRpcException(getInterface(), getUrl(), invocation, e);
    }
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException) Result(org.apache.dubbo.rpc.Result)

Example 32 with RpcException

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

the class GrpcInvoker method getRpcException.

private RpcException getRpcException(Class<?> type, URL url, Invocation invocation, Throwable e) {
    RpcException re = new RpcException("Failed to invoke remote service: " + type + ", method: " + invocation.getMethodName() + ", cause: " + e.getMessage(), e);
    re.setCode(getErrorCode(e));
    return re;
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException)

Example 33 with RpcException

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

the class GrpcProtocol method doExport.

@Override
protected <T> Runnable doExport(T proxiedImpl, Class<T> type, URL url) throws RpcException {
    String key = url.getAddress();
    ProtocolServer protocolServer = serverMap.computeIfAbsent(key, k -> {
        DubboHandlerRegistry registry = new DubboHandlerRegistry();
        NettyServerBuilder builder = NettyServerBuilder.forPort(url.getPort()).fallbackHandlerRegistry(registry);
        Server originalServer = GrpcOptionsUtils.buildServerBuilder(url, builder).build();
        GrpcRemotingServer remotingServer = new GrpcRemotingServer(originalServer, registry);
        return new ProxyProtocolServer(remotingServer);
    });
    GrpcRemotingServer grpcServer = (GrpcRemotingServer) protocolServer.getRemotingServer();
    ServiceRepository serviceRepository = ApplicationModel.getServiceRepository();
    ProviderModel providerModel = serviceRepository.lookupExportedService(url.getServiceKey());
    if (providerModel == null) {
        throw new IllegalStateException("Service " + url.getServiceKey() + "should have already been stored in service repository, " + "but failed to find it.");
    }
    Object originalImpl = providerModel.getServiceInstance();
    Class<?> implClass = originalImpl.getClass();
    try {
        Method method = implClass.getMethod("setProxiedImpl", type);
        method.invoke(originalImpl, proxiedImpl);
    } catch (Exception e) {
        throw new IllegalStateException("Failed to set dubbo proxied service impl to stub, please make sure your stub " + "was generated by the dubbo-protoc-compiler.", e);
    }
    grpcServer.getRegistry().addService((BindableService) originalImpl, url.getServiceKey());
    if (!grpcServer.isStarted()) {
        grpcServer.start();
    }
    return () -> grpcServer.getRegistry().removeService(url.getServiceKey());
}
Also used : NettyServerBuilder(io.grpc.netty.NettyServerBuilder) ProtocolServer(org.apache.dubbo.rpc.ProtocolServer) Server(io.grpc.Server) Method(java.lang.reflect.Method) ServiceRepository(org.apache.dubbo.rpc.model.ServiceRepository) IOException(java.io.IOException) RpcException(org.apache.dubbo.rpc.RpcException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProtocolServer(org.apache.dubbo.rpc.ProtocolServer) ProviderModel(org.apache.dubbo.rpc.model.ProviderModel)

Example 34 with RpcException

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

the class MemcachedProtocol method protocolBindingRefer.

@Override
public <T> Invoker<T> protocolBindingRefer(final Class<T> type, final URL url) throws RpcException {
    try {
        String address = url.getAddress();
        String backup = url.getParameter(RemotingConstants.BACKUP_KEY);
        if (backup != null && backup.length() > 0) {
            address += "," + backup;
        }
        MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(address));
        final MemcachedClient memcachedClient = builder.build();
        final int expiry = url.getParameter("expiry", 0);
        final String get = url.getParameter("get", "get");
        final String set = url.getParameter("set", Map.class.equals(type) ? "put" : "set");
        final String delete = url.getParameter("delete", Map.class.equals(type) ? "remove" : "delete");
        return new AbstractInvoker<T>(type, url) {

            @Override
            protected Result doInvoke(Invocation invocation) throws Throwable {
                try {
                    Object value = null;
                    if (get.equals(invocation.getMethodName())) {
                        if (invocation.getArguments().length != 1) {
                            throw new IllegalArgumentException("The memcached get method arguments mismatch, must only one arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url);
                        }
                        value = memcachedClient.get(String.valueOf(invocation.getArguments()[0]));
                    } else if (set.equals(invocation.getMethodName())) {
                        if (invocation.getArguments().length != 2) {
                            throw new IllegalArgumentException("The memcached set method arguments mismatch, must be two arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url);
                        }
                        memcachedClient.set(String.valueOf(invocation.getArguments()[0]), expiry, invocation.getArguments()[1]);
                    } else if (delete.equals(invocation.getMethodName())) {
                        if (invocation.getArguments().length != 1) {
                            throw new IllegalArgumentException("The memcached delete method arguments mismatch, must only one arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url);
                        }
                        memcachedClient.delete(String.valueOf(invocation.getArguments()[0]));
                    } else {
                        throw new UnsupportedOperationException("Unsupported method " + invocation.getMethodName() + " in memcached service.");
                    }
                    return AsyncRpcResult.newDefaultAsyncResult(value, invocation);
                } catch (Throwable t) {
                    RpcException re = new RpcException("Failed to invoke memcached service method. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url + ", cause: " + t.getMessage(), t);
                    if (t instanceof TimeoutException || t instanceof SocketTimeoutException) {
                        re.setCode(RpcException.TIMEOUT_EXCEPTION);
                    } else if (t instanceof MemcachedException || t instanceof IOException) {
                        re.setCode(RpcException.NETWORK_EXCEPTION);
                    }
                    throw re;
                }
            }

            @Override
            public void destroy() {
                super.destroy();
                try {
                    memcachedClient.shutdown();
                } catch (Throwable e) {
                    logger.warn(e.getMessage(), e);
                }
            }
        };
    } catch (Throwable t) {
        throw new RpcException("Failed to refer memcached service. interface: " + type.getName() + ", url: " + url + ", cause: " + t.getMessage(), t);
    }
}
Also used : XMemcachedClientBuilder(net.rubyeye.xmemcached.XMemcachedClientBuilder) MemcachedClientBuilder(net.rubyeye.xmemcached.MemcachedClientBuilder) Invocation(org.apache.dubbo.rpc.Invocation) AbstractInvoker(org.apache.dubbo.rpc.protocol.AbstractInvoker) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) MemcachedClient(net.rubyeye.xmemcached.MemcachedClient) RpcException(org.apache.dubbo.rpc.RpcException) XMemcachedClientBuilder(net.rubyeye.xmemcached.XMemcachedClientBuilder) TimeoutException(java.util.concurrent.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) MemcachedException(net.rubyeye.xmemcached.exception.MemcachedException)

Example 35 with RpcException

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

the class ThriftProtocol method getTServer.

private <T> TServer getTServer(T impl, Class<T> type, URL url) {
    TThreadedSelectorServer.Args tArgs = null;
    String typeName = type.getName();
    TServer tserver;
    if (typeName.endsWith(THRIFT_IFACE)) {
        String processorClsName = typeName.substring(0, typeName.indexOf(THRIFT_IFACE)) + THRIFT_PROCESSOR;
        try {
            Class<?> clazz = Class.forName(processorClsName);
            Constructor constructor = clazz.getConstructor(type);
            TProcessor tprocessor = (TProcessor) constructor.newInstance(impl);
            processor.registerProcessor(typeName, tprocessor);
            tserver = SERVER_MAP.get(url.getAddress());
            if (tserver == null) {
                /**
                 *Solve the problem of only 50 of the default number of concurrent connections
                 */
                TNonblockingServerSocket.NonblockingAbstractServerSocketArgs args = new TNonblockingServerSocket.NonblockingAbstractServerSocketArgs();
                /**
                 *1000 connections
                 */
                args.backlog(1000);
                String bindIp = url.getParameter(Constants.BIND_IP_KEY, url.getHost());
                if (url.getParameter(ANYHOST_KEY, false)) {
                    bindIp = ANYHOST_VALUE;
                }
                int bindPort = url.getParameter(Constants.BIND_PORT_KEY, url.getPort());
                args.bindAddr(new InetSocketAddress(bindIp, bindPort));
                /**
                 *timeout: 10s
                 */
                args.clientTimeout(10000);
                TNonblockingServerSocket transport = new TNonblockingServerSocket(args);
                tArgs = new TThreadedSelectorServer.Args(transport);
                tArgs.workerThreads(200);
                tArgs.selectorThreads(4);
                tArgs.acceptQueueSizePerThread(256);
                tArgs.processor(processor);
                tArgs.transportFactory(new TFramedTransport.Factory());
                tArgs.protocolFactory(new TCompactProtocol.Factory());
            } else {
                // if server is starting, return and do nothing here
                return null;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new RpcException("Fail to create nativethrift server(" + url + ") : " + e.getMessage(), e);
        }
    }
    if (tArgs == null) {
        logger.error("Fail to create native thrift server(" + url + ") due to null args");
        throw new RpcException("Fail to create nativethrift server(" + url + ") due to null args");
    }
    tserver = new TThreadedSelectorServer(tArgs);
    return tserver;
}
Also used : TServer(org.apache.thrift.server.TServer) Constructor(java.lang.reflect.Constructor) InetSocketAddress(java.net.InetSocketAddress) TThreadedSelectorServer(org.apache.thrift.server.TThreadedSelectorServer) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) TException(org.apache.thrift.TException) RpcException(org.apache.dubbo.rpc.RpcException) TProcessor(org.apache.thrift.TProcessor) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) RpcException(org.apache.dubbo.rpc.RpcException)

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