Search in sources :

Example 1 with InvokerStrategy

use of com.huawei.gray.dubbo.strategy.InvokerStrategy in project Sermant by huaweicloud.

the class WeightRuleStrategy method getTargetInvoker.

@Override
public List<Invoker<?>> getTargetInvoker(List<Route> routes, Invocation invocation, List<Invoker<?>> invokers, VersionFrom versionFrom) {
    if (routes.get(0).getWeight() == null) {
        // 规定第一个规则的流量为空,则设置为100
        routes.get(0).setWeight(ONO_HUNDRED);
    }
    String targetVersion = null;
    Set<String> notMatchVersions = new HashSet<String>();
    int begin = 1;
    int num = new Random().nextInt(ONO_HUNDRED) + 1;
    for (Route route : routes) {
        @SuppressWarnings("checkstyle:RegexpSingleline") Integer weight = route.getWeight();
        if (weight == null) {
            continue;
        }
        String currentVersion = route.getTags().getVersion();
        if (num >= begin && num <= begin + weight - 1) {
            targetVersion = currentVersion;
            break;
        }
        begin += weight;
        notMatchVersions.add(currentVersion);
    }
    VersionStrategy versionStrategy = VersionChooser.INSTANCE.choose(versionFrom);
    InvokerStrategy invokerStrategy = InvokerChooser.INSTANCE.choose(targetVersion);
    List<Invoker<?>> resultList = new ArrayList<Invoker<?>>();
    for (Invoker<?> invoker : invokers) {
        if (invokerStrategy.isMatch(invoker, targetVersion, notMatchVersions, versionStrategy)) {
            resultList.add(invoker);
        }
    }
    return CollectionUtils.isEmpty(resultList) ? invokers : resultList;
}
Also used : ArrayList(java.util.ArrayList) InvokerStrategy(com.huawei.gray.dubbo.strategy.InvokerStrategy) Random(java.util.Random) Invoker(org.apache.dubbo.rpc.Invoker) VersionStrategy(com.huawei.gray.dubbo.strategy.VersionStrategy) Route(com.huawei.route.common.gray.label.entity.Route) HashSet(java.util.HashSet)

Example 2 with InvokerStrategy

use of com.huawei.gray.dubbo.strategy.InvokerStrategy in project Sermant by huaweicloud.

the class WeightRuleStrategy method getTargetInvoker.

@Override
public List<Object> getTargetInvoker(List<Route> routes, List<Object> invokers, VersionFrom versionFrom) {
    if (routes.get(0).getWeight() == null) {
        // 规定第一个规则的流量为空,则设置为100
        routes.get(0).setWeight(ONO_HUNDRED);
    }
    String targetVersion = null;
    Set<String> notMatchVersions = new HashSet<>();
    int begin = 1;
    int num = new Random().nextInt(ONO_HUNDRED) + 1;
    for (Route route : routes) {
        Integer weight = route.getWeight();
        if (weight == null) {
            continue;
        }
        String currentVersion = route.getTags().getVersion();
        if (num >= begin && num <= begin + weight - 1) {
            targetVersion = currentVersion;
            break;
        }
        begin += weight;
        notMatchVersions.add(currentVersion);
    }
    VersionStrategy versionStrategy = VersionChooser.INSTANCE.choose(versionFrom);
    InvokerStrategy invokerStrategy = InvokerChooser.INSTANCE.choose(targetVersion);
    List<Object> resultList = new ArrayList<>();
    for (Object invoker : invokers) {
        if (invokerStrategy.isMatch(invoker, targetVersion, notMatchVersions, versionStrategy)) {
            resultList.add(invoker);
        }
    }
    return CollectionUtils.isEmpty(resultList) ? invokers : resultList;
}
Also used : InvokerStrategy(com.huawei.gray.dubbo.strategy.InvokerStrategy) Random(java.util.Random) VersionStrategy(com.huawei.gray.dubbo.strategy.VersionStrategy) ArrayList(java.util.ArrayList) Route(com.huawei.route.common.gray.label.entity.Route) HashSet(java.util.HashSet)

Aggregations

InvokerStrategy (com.huawei.gray.dubbo.strategy.InvokerStrategy)2 VersionStrategy (com.huawei.gray.dubbo.strategy.VersionStrategy)2 Route (com.huawei.route.common.gray.label.entity.Route)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Random (java.util.Random)2 Invoker (org.apache.dubbo.rpc.Invoker)1