Search in sources :

Example 81 with RpcException

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

the class ThriftProtocol method doReferFrameAndCompact.

private <T> T doReferFrameAndCompact(Class<T> type, URL url) throws RpcException {
    try {
        T thriftClient = null;
        String typeName = type.getName();
        if (typeName.endsWith(THRIFT_IFACE)) {
            String clientClsName = typeName.substring(0, typeName.indexOf(THRIFT_IFACE)) + THRIFT_CLIENT;
            Class<?> clazz = Class.forName(clientClsName);
            Constructor constructor = clazz.getConstructor(TProtocol.class);
            try {
                TSocket tSocket = new TSocket(url.getHost(), url.getPort());
                TTransport transport = new TFramedTransport(tSocket);
                TProtocol tprotocol = new TCompactProtocol(transport);
                TMultiplexedProtocol protocol = new TMultiplexedProtocol(tprotocol, typeName);
                thriftClient = (T) constructor.newInstance(protocol);
                transport.open();
                logger.info("nativethrift client opened for service(" + url + ")");
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new RpcException("Fail to create remote client:" + e.getMessage(), e);
            }
        }
        return thriftClient;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RpcException("Fail to create remote client for service(" + url + "): " + e.getMessage(), e);
    }
}
Also used : TMultiplexedProtocol(org.apache.thrift.protocol.TMultiplexedProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) Constructor(java.lang.reflect.Constructor) TFramedTransport(org.apache.thrift.transport.TFramedTransport) RpcException(org.apache.dubbo.rpc.RpcException) TTransport(org.apache.thrift.transport.TTransport) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) TException(org.apache.thrift.TException) RpcException(org.apache.dubbo.rpc.RpcException) TSocket(org.apache.thrift.transport.TSocket)

Example 82 with RpcException

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

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

the class FailfastClusterInvoker method doInvoke.

@Override
public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    checkInvokers(invokers, invocation);
    Invoker<T> invoker = select(loadbalance, invocation, invokers, null);
    try {
        return invoker.invoke(invocation);
    } catch (Throwable e) {
        if (e instanceof RpcException && ((RpcException) e).isBiz()) {
            // biz exception.
            throw (RpcException) e;
        }
        throw new RpcException(e instanceof RpcException ? ((RpcException) e).getCode() : 0, "Failfast invoke providers " + invoker.getUrl() + " " + loadbalance.getClass().getSimpleName() + " select from all providers " + invokers + " for service " + getInterface().getName() + " method " + invocation.getMethodName() + " on consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", but no luck to perform the invocation. Last error is: " + e.getMessage(), e.getCause() != null ? e.getCause() : e);
    }
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException)

Example 84 with RpcException

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

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

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