Ribbon过滤器ZoneAvoidanceRule源码解读

一 ZoneAvoidanceRule在类图中的位置

Ribbon过滤器ZoneAvoidanceRule源码解读_第1张图片

二 ZoneAvoidanceRule源码解读

public class ZoneAvoidanceRule extends PredicateBasedRule {

    private static final Random random = new Random();
    //使用CompositePredicate来进行服务实例清单过滤。
    //组合过滤条件
    private CompositePredicate compositePredicate;
    
    public ZoneAvoidanceRule() {
        super();
        //主过滤条件
        ZoneAvoidancePredicate zonePredicate = new ZoneAvoidancePredicate(this);
        //次过滤条件
        AvailabilityPredicate availabilityPredicate = new AvailabilityPredicate(this);
        compositePredicate = createCompositePredicate(zonePredicate, availabilityPredicate);
    }
......
}

三 CompositePredicate源码解读

public class CompositePredicate extends AbstractServerPredicate {

    //主过滤条件
    private AbstractServerPredicate delegate;
    
    //次过滤条件列表
    private List fallbacks = Lists.newArrayList();
        
    private int minimalFilteredServers = 1;
    
    private float minimalFilteredPercentage = 0;    
    
    @Override
    public boolean apply(@Nullable PredicateKey input) {
        return delegate.apply(input);
    }

    
    ......


    @Override
    public List getEligibleServers(List servers, Object loadBalancerKey) {
        //使用主过滤条件对所有实例过滤并返回过滤后的清单
        List result = super.getEligibleServers(servers, loadBalancerKey);
        Iterator i = fallbacks.iterator();
        //依次使用次过滤条件对主过滤条件的结果进行过滤
        //不论是主过滤条件还是次过滤条件,都需要判断下面两个条件
        //只要有一个条件符合,就不再过滤,将当前结果返回供线性轮询
        //算法选择
        //第1个条件:过滤后的实例总数>=最小过滤实例数(默认为1)
        //第2个条件:过滤互的实例比例>最小过滤百分比(默认为0)
        while (!(result.size() >= minimalFilteredServers && result.size() > (int) (servers.size() * minimalFilteredPercentage))
                && i.hasNext()) {
            AbstractServerPredicate predicate = i.next();
            result = predicate.getEligibleServers(servers, loadBalancerKey);
        }
        return result;
    }
}

四 ZoneAvoidancePredicate源码解读

//服务实例所在的Zone必须可用
public class ZoneAvoidancePredicate extends  AbstractServerPredicate {

    ......

    @Override
    public boolean apply(@Nullable PredicateKey input) {
        if (!ENABLED.get()) {
            return true;
        }
        String serverZone = input.getServer().getZone();
        if (serverZone == null) {
            // there is no zone information from the server, we do not want to filter
            // out this server
            return true;
        }
        LoadBalancerStats lbStats = getLBStats();
        if (lbStats == null) {
            // no stats available, do not filter
            return true;
        }
        if (lbStats.getAvailableZones().size() <= 1) {
            // only one zone is available, do not filter
            return true;
        }
        Map zoneSnapshot = ZoneAvoidanceRule.createSnapshot(lbStats);
        if (!zoneSnapshot.keySet().contains(serverZone)) {
            // The server zone is unknown to the load balancer, do not filter it out
            return true;
        }
        logger.debug("Zone snapshots: {}", zoneSnapshot);
        Set availableZones = ZoneAvoidanceRule.getAvailableZones(zoneSnapshot, triggeringLoad.get(), triggeringBlackoutPercentage.get());
        logger.debug("Available zones: {}", availableZones);
        //服务实例所在的Zone必须可用
        if (availableZones != null) {
            return availableZones.contains(input.getServer().getZone());
        } else {
            return false;
        }
    }    
}

 

你可能感兴趣的:(Ribbon)