Search in sources :

Example 1 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class ScriptRouter method route.

@SuppressWarnings("unchecked")
public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
    try {
        List<Invoker<T>> invokersCopy = new ArrayList<Invoker<T>>(invokers);
        Compilable compilable = (Compilable) engine;
        Bindings bindings = engine.createBindings();
        bindings.put("invokers", invokersCopy);
        bindings.put("invocation", invocation);
        bindings.put("context", RpcContext.getContext());
        CompiledScript function = compilable.compile(rule);
        Object obj = function.eval(bindings);
        if (obj instanceof Invoker[]) {
            invokersCopy = Arrays.asList((Invoker<T>[]) obj);
        } else if (obj instanceof Object[]) {
            invokersCopy = new ArrayList<Invoker<T>>();
            for (Object inv : (Object[]) obj) {
                invokersCopy.add((Invoker<T>) inv);
            }
        } else {
            invokersCopy = (List<Invoker<T>>) obj;
        }
        return invokersCopy;
    } catch (ScriptException e) {
        //fail then ignore rule .invokers.
        logger.error("route error , rule has been ignored. rule: " + rule + ", method:" + invocation.getMethodName() + ", url: " + RpcContext.getContext().getUrl(), e);
        return invokers;
    }
}
Also used : CompiledScript(javax.script.CompiledScript) ScriptException(javax.script.ScriptException) Invoker(com.alibaba.dubbo.rpc.Invoker) Compilable(javax.script.Compilable) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Bindings(javax.script.Bindings)

Example 2 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class AbstractClusterInvoker method invoke.

public Result invoke(final Invocation invocation) throws RpcException {
    checkWhetherDestroyed();
    LoadBalance loadbalance;
    List<Invoker<T>> invokers = list(invocation);
    if (invokers != null && invokers.size() > 0) {
        loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(invokers.get(0).getUrl().getMethodParameter(invocation.getMethodName(), Constants.LOADBALANCE_KEY, Constants.DEFAULT_LOADBALANCE));
    } else {
        loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(Constants.DEFAULT_LOADBALANCE);
    }
    RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
    return doInvoke(invocation, invokers, loadbalance);
}
Also used : Invoker(com.alibaba.dubbo.rpc.Invoker) LoadBalance(com.alibaba.dubbo.rpc.cluster.LoadBalance)

Example 3 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class FailoverClusterInvoker method doInvoke.

@SuppressWarnings({ "unchecked", "rawtypes" })
public Result doInvoke(Invocation invocation, final List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    List<Invoker<T>> copyinvokers = invokers;
    checkInvokers(copyinvokers, invocation);
    int len = getUrl().getMethodParameter(invocation.getMethodName(), Constants.RETRIES_KEY, Constants.DEFAULT_RETRIES) + 1;
    if (len <= 0) {
        len = 1;
    }
    // retry loop.
    // last exception.
    RpcException le = null;
    // invoked invokers.
    List<Invoker<T>> invoked = new ArrayList<Invoker<T>>(copyinvokers.size());
    Set<String> providers = new HashSet<String>(len);
    for (int i = 0; i < len; i++) {
        //注意:如果列表发生了变化,那么invoked判断会失效,因为invoker示例已经改变
        if (i > 0) {
            checkWhetherDestroyed();
            copyinvokers = list(invocation);
            //重新检查一下
            checkInvokers(copyinvokers, invocation);
        }
        Invoker<T> invoker = select(loadbalance, invocation, copyinvokers, invoked);
        invoked.add(invoker);
        RpcContext.getContext().setInvokers((List) invoked);
        try {
            Result result = invoker.invoke(invocation);
            if (le != null && logger.isWarnEnabled()) {
                logger.warn("Although retry the method " + invocation.getMethodName() + " in the service " + getInterface().getName() + " was successful by the provider " + invoker.getUrl().getAddress() + ", but there have been failed providers " + providers + " (" + providers.size() + "/" + copyinvokers.size() + ") from the registry " + directory.getUrl().getAddress() + " on the consumer " + NetUtils.getLocalHost() + " using the dubbo version " + Version.getVersion() + ". Last error is: " + le.getMessage(), le);
            }
            return result;
        } catch (RpcException e) {
            if (e.isBiz()) {
                // biz exception.
                throw e;
            }
            le = e;
        } catch (Throwable e) {
            le = new RpcException(e.getMessage(), e);
        } finally {
            providers.add(invoker.getUrl().getAddress());
        }
    }
    throw new RpcException(le != null ? le.getCode() : 0, "Failed to invoke the method " + invocation.getMethodName() + " in the service " + getInterface().getName() + ". Tried " + len + " times of the providers " + providers + " (" + providers.size() + "/" + copyinvokers.size() + ") from the registry " + directory.getUrl().getAddress() + " on the consumer " + NetUtils.getLocalHost() + " using the dubbo version " + Version.getVersion() + ". Last error is: " + (le != null ? le.getMessage() : ""), le != null && le.getCause() != null ? le.getCause() : le);
}
Also used : Invoker(com.alibaba.dubbo.rpc.Invoker) RpcException(com.alibaba.dubbo.rpc.RpcException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Result(com.alibaba.dubbo.rpc.Result)

Example 4 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class RoundRobinLoadBalance method doSelect.

protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
    String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
    // 总个数
    int length = invokers.size();
    // 最大权重
    int maxWeight = 0;
    // 最小权重
    int minWeight = Integer.MAX_VALUE;
    final LinkedHashMap<Invoker<T>, IntegerWrapper> invokerToWeightMap = new LinkedHashMap<Invoker<T>, IntegerWrapper>();
    int weightSum = 0;
    for (int i = 0; i < length; i++) {
        int weight = getWeight(invokers.get(i), invocation);
        // 累计最大权重
        maxWeight = Math.max(maxWeight, weight);
        // 累计最小权重
        minWeight = Math.min(minWeight, weight);
        if (weight > 0) {
            invokerToWeightMap.put(invokers.get(i), new IntegerWrapper(weight));
            weightSum += weight;
        }
    }
    AtomicPositiveInteger sequence = sequences.get(key);
    if (sequence == null) {
        sequences.putIfAbsent(key, new AtomicPositiveInteger());
        sequence = sequences.get(key);
    }
    int currentSequence = sequence.getAndIncrement();
    if (maxWeight > 0 && minWeight < maxWeight) {
        // 权重不一样
        int mod = currentSequence % weightSum;
        for (int i = 0; i < maxWeight; i++) {
            for (Map.Entry<Invoker<T>, IntegerWrapper> each : invokerToWeightMap.entrySet()) {
                final Invoker<T> k = each.getKey();
                final IntegerWrapper v = each.getValue();
                if (mod == 0 && v.getValue() > 0) {
                    return k;
                }
                if (v.getValue() > 0) {
                    v.decrement();
                    mod--;
                }
            }
        }
    }
    // 取模轮循
    return invokers.get(currentSequence % length);
}
Also used : Invoker(com.alibaba.dubbo.rpc.Invoker) AtomicPositiveInteger(com.alibaba.dubbo.common.utils.AtomicPositiveInteger) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class ForkingClusterInvoker method doInvoke.

@SuppressWarnings({ "unchecked", "rawtypes" })
public Result doInvoke(final Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    checkInvokers(invokers, invocation);
    final List<Invoker<T>> selected;
    final int forks = getUrl().getParameter(Constants.FORKS_KEY, Constants.DEFAULT_FORKS);
    final int timeout = getUrl().getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
    if (forks <= 0 || forks >= invokers.size()) {
        selected = invokers;
    } else {
        selected = new ArrayList<Invoker<T>>();
        for (int i = 0; i < forks; i++) {
            //在invoker列表(排除selected)后,如果没有选够,则存在重复循环问题.见select实现.
            Invoker<T> invoker = select(loadbalance, invocation, invokers, selected);
            if (!selected.contains(invoker)) {
                //防止重复添加invoker
                selected.add(invoker);
            }
        }
    }
    RpcContext.getContext().setInvokers((List) selected);
    final AtomicInteger count = new AtomicInteger();
    final BlockingQueue<Object> ref = new LinkedBlockingQueue<Object>();
    for (final Invoker<T> invoker : selected) {
        executor.execute(new Runnable() {

            public void run() {
                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);
    }
}
Also used : LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Result(com.alibaba.dubbo.rpc.Result) Invoker(com.alibaba.dubbo.rpc.Invoker) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RpcException(com.alibaba.dubbo.rpc.RpcException)

Aggregations

Invoker (com.alibaba.dubbo.rpc.Invoker)63 Test (org.junit.Test)48 ArrayList (java.util.ArrayList)37 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)36 URL (com.alibaba.dubbo.common.URL)32 RegistryDirectory (com.alibaba.dubbo.registry.integration.RegistryDirectory)19 Router (com.alibaba.dubbo.rpc.cluster.Router)12 Result (com.alibaba.dubbo.rpc.Result)11 MockInvoker (com.alibaba.dubbo.rpc.cluster.router.MockInvoker)11 Invocation (com.alibaba.dubbo.rpc.Invocation)9 RpcException (com.alibaba.dubbo.rpc.RpcException)9 RpcResult (com.alibaba.dubbo.rpc.RpcResult)9 LoadBalance (com.alibaba.dubbo.rpc.cluster.LoadBalance)8 DemoService (com.alibaba.dubbo.rpc.support.DemoService)7 LeastActiveLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance)5 RandomLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance)5 RoundRobinLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 List (java.util.List)4 StaticDirectory (com.alibaba.dubbo.rpc.cluster.directory.StaticDirectory)3