权重轮询调度算法 java版本

权重轮询调度算法(Weighted Round-Robin Scheduling)--java版本

由于每台服务器的配置、安装的业务应用等不同,其处理能力会不一样。所以,我们根据服务器的不同处理能力,给每个服务器分配不同的权值,使其能够接受相应权值数的服务请求。

2个java源文件,如下所示:

public interface IProduceStrategy {

    public int getPartitionIdForTopic();

}

public class WeightFactorProduceStrategy implements IProduceStrategy {
    private int i = -1; //表示上一次选择的服务器
    private int cw = 0; //表示当前调度的权值
    private int gcd = 0; //当前所有权重的最大公约数 比如 2,4,8 的最大公约数为:2
    private int maxWeight;

    private List<Integer> weights = null;  //作用计算最大公约数
    private PartitionWeightRRParameter weightRRParametersDns[] = null;

    /**
     *  按照轮询调研权重配置,格式如下:partition1:weight,partition2:weight
     * @param partConfig
     */
    public WeightFactorProduceStrategy(String partConfig) {
        validate(partConfig);
        this.initWeigthParam(Tools.parseCsvMap(partConfig));
    }

    private Pattern pattern = Pattern.compile("([\\d+\\:\\d+],?){1,}");

    private void validate(String partConfig) {
        if (partConfig.length() <= 0)
            throw new InvalidPartitonConfigException("partition config is incorrect :" + partConfig);
        else if (partConfig.equals(".") || partConfig.equals(".."))
            throw new InvalidPartitonConfigException("partition config is incorrect :" + partConfig);

        Matcher matcher = pattern.matcher(partConfig);
        if(matcher.find()) {
            String rexStr = matcher.group();
            if (!rexStr.equals(partConfig))
                throw new InvalidPartitonConfigException("partition config is incorrect :" + partConfig);
        } else {
            throw new InvalidPartitonConfigException("partition config is incorrect :" + partConfig);
        }
    }

    /**
     * 格式如下:partition1:weight,partition2:weight
     * @param csvMap
     */
    private void initWeigthParam(Map<String, String> csvMap) {
        weightRRParametersDns = new PartitionWeightRRParameter[csvMap.size()];
        int numPart = 0;
        weights = new ArrayList<Integer>(csvMap.size());
        Set<Map.Entry<String, String>> entrySet = csvMap.entrySet();
        for(Iterator<Map.Entry<String, String>> its = entrySet.iterator(); its.hasNext(); ) {
            Map.Entry<String, String> entry = its.next();
            weights.add(Integer.valueOf(entry.getValue()));
            weightRRParametersDns[numPart++] = new PartitionWeightRRParameter(Integer.valueOf(entry.getKey()), Integer.valueOf(entry.getValue()));
        }

        gcd = getGcdByList(weights);
        maxWeight = getMaxWeight();
    }

    /**
     * 计算最大公约数
     * @param weight_m 权重数
     * @param weight_n 权重数
     * @return
     */
    private int GCD(int weight_m,int weight_n)
    {
        int temp;
        while(weight_n != 0){
            if(weight_m < weight_n){
                temp = weight_m;
                weight_m = weight_n;
                weight_n = temp;
            }
            temp = weight_m - weight_n;
            weight_m = weight_n;
            weight_n = temp;
        }
        return weight_m;
    }

    /**
     *
     * @param weights      权重列表
     * @param startIndex   list索引值,起始位置。
     * @param nextGcd      传入最大公约数
     * @return
     */
    private int getGcdByList(List<Integer> weights, int startIndex, int nextGcd) {
        if ( weights.size() < 2) {
            throw new IllegalArgumentException("At least a number of parameters for 2");
        }
        if (weights.size() == 2 && startIndex == 0) {
            return this.GCD(weights.get(startIndex), weights.get(startIndex + 1));
        }

        if (startIndex + 1 > weights.size() -1 )
            return nextGcd;
        int curGcd = nextGcd > 0 ? nextGcd : weights.get(startIndex);
        int nextIndex = startIndex + 1;
        nextGcd = GCD(curGcd, weights.get(startIndex + 1));              //0,1

        return getGcdByList(weights, nextIndex, nextGcd);
    }

    private int getGcdByList(List<Integer> weights) {
        return this.getGcdByList(weights, 0, 0);
    }

    private int getWeightDns() {
        for ( ; ; ) {
            i = (i + 1) % weightRRParametersDns.length;
            if (i == 0) {
                cw = cw - gcd;  //表示当前调度的权值
                if (cw <= 0) {
                    cw = maxWeight;
                    if (cw == 0) {
                        return 0;
                    }
                }
            }

            if (weightRRParametersDns[i].getWeight() >= cw ) {
                return weightRRParametersDns[i].getPartition();
            }
        }
    }

    private int getMaxWeight() {
        int max = 0;
        for (int i = 0; i< weightRRParametersDns.length;i++) {
            if (weightRRParametersDns[i].getWeight() >= max) {
                max = weightRRParametersDns[i].getWeight();
            }
        }

        return max;
    }

    public int getPartitionIdForTopic() {
        return this.getWeightDns();
    }

    /**
     * 分区权重参数类
     */
    static class PartitionWeightRRParameter {
        private int partition;
        private int weight;

        public PartitionWeightRRParameter(int partition, int weight) {
            this.partition = partition;
            this.weight = weight;
        }

        public int getPartition() {
            return partition;
        }

        public int getWeight() {
            return weight;
        }
    }

}


单元测试类:
public class WeightFactorProduceStrategyTest {

    @Test
    public void testGetPartitionIdForTopic() throws Exception {
        IProduceStrategy weightFcProStrategy = new WeightFactorProduceStrategy("0:5,1:15,2:20");

        for (int i = 0; i < 40; i++) {
//            weightFcProStrategy.getPartitionIdForTopic();
            System.out.println(weightFcProStrategy.getPartitionIdForTopic());
        }
    }
}

测试结果如下:

2
2
1
2
1
2
1
0
2
2
1
2
1
2
1
0
2
2
1
2
1
2
1
0
2
2
1
2
1
2
1
0
2
2
1
2
1
2
1
0


确定权重算法是一种用于计算权重或权值的数学算法。在计算机科学领域,确定权重算法常常应用于信息检索、机器学习、图论等各种领域。 在CSND(中国最大的IT技术社区)上,确定文章的权重算法是一个非常关键的问题。文章的权重决定了文章在网站上的显示顺序和推荐指数。一般来说,权重高的文章会被更多的读者看到,获得更多的关注和回复。 CSND的权重算法可能包括以下一些因素: 1. 点击量:文章被点击的次数越多,可能被认为是受读者欢迎的,从而具有较高的权重。 2. 点赞和分享数:如果文章得到了很多人的点赞和分享,说明它能够引起读者的共鸣,也将有较高的权重。 3. 评论数量和质量:如果文章能够引发大量热烈的讨论和高质量的评论,表明它对读者具有较高的吸引力和价值,因此可能具有较高的权重。 4. 用户等级:在CSND上,用户有不同的等级,根据其等级的高低,可能会对发布的文章的权重产生影响。例如,高等级的用户可能拥有更大的影响力,他们发布的文章可能会有更高的权重。 5. 发布时间:某些权重算法可能考虑文章的发布时间。比如,在特定时间段发布的文章可能会获得更高的权重,以保证信息的及时性。 上述仅是一些可能被考虑的因素,实际的权重算法可能更加复杂和多样化。CSND可能会根据用户反馈和不断改进算法,以提供更好的推荐内容和用户体验。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值