DUBBO RandomLoadBalance 随机算法:

 
 protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
       

      //invokers及weights的映射关系通过数组顺序

       // the size of machine 
        int length = invokers.size();
    
        int totalWeight = 0;

        // Every invoker has the same weigh
        boolean sameWeight = true; 

        int offset;
        int i;
        for(offset = 0; offset < length; ++offset) {
            //get the weight of every machine
            i = this.getWeight((Invoker)invokers.get(offset), invocation);
            // accumulate the totalWeight from every machine
            totalWeight += i;
            // if  current  sameWeight is true and the weight of current machine is not equal with the prev machine
            // then mark  the variable of sameWeight is false;
            if (sameWeight && offset > 0 && i != this.getWeight((Invoker)invokers.get(offset - 1), invocation)) {
                sameWeight = false;
            }
        }
        // if totalWeight is large than zero and sameWeight is false,
        if (totalWeight > 0 && !sameWeight) {
            //select a number from totalWeight;
            offset = this.random.nextInt(totalWeight);
            for(i = 0; i < length; ++i) {
                // mark  plus operation
                offset -= this.getWeight((Invoker)invokers.get(i), invocation);
                if (offset < 0) {
                    return (Invoker)invokers.get(i);
                }
            }
        }

        return (Invoker)invokers.get(this.random.nextInt(length));
    }

    protected int getWeight(Invoker<?> invoker, Invocation invocation) {
        int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), "weight", 100);
        if (weight > 0) {
            //开始运行参数
            long timestamp = invoker.getUrl().getParameter("timestamp", 0L);
            if (timestamp > 0L) {
                 // 程序启动到现在的历时计算。 一个服务运行的时间越长,也就越稳定,那对应的权重也就越高。
                int uptime = (int)(System.currentTimeMillis() - timestamp);
                // 预热时间 一个系统从启动到完美运行,是需要一段时间的,也就是这个预热时间。
                //所以在这里预热时间段内的请求,是不能完全根据设置的权重值来进行负载的,需要进行一下预热程度的计算。这样就能够保证系统在真正的完美运行时间,不会处理太多的请求
                int warmup = invoker.getUrl().getParameter("warmup", 600000); // 完美运行10分钟 
                //在运行时间大于0,也就是已经启动了,然后运行时间小于预热时间的,会进行权重的计算。 

                //这里是Dubbo针对系统契合环境做的一些优化,因为一个系统从启动到完美运行,是需要一段时间的,也就是这个预热时间。
                //所以在这里预热时间段内的请求,是不能完全根据设//置的权重值来进行负载的,需要进行一下预热程度的计算。
                //这样就能够保证系统在真正的完美运行时间,不会处理太多的请求 所以这里返回 低于默认100的权重 避免在完美运行时间前被调用

                if (uptime > 0 && uptime < warmup) {
                    weight = calculateWarmupWeight(uptime, warmup, weight);
                }
            }
        }

        return weight;
    }

    //根据预热时间与程序启动到现在的历时计算权重比例 warmup 预热时间。uptime 程序启动到现在的历时计算。5min ,10min,100
    static int calculateWarmupWeight(int uptime, int warmup, int weight) {
        int ww = (int)((float)uptime / ((float)warmup / (float)weight)); //    5.0/(10.0/100.0)=50
        return ww < 1 ? 1 : (ww > weight ? weight : ww); // 50
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

icool_ali

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值