权重轮询调度算法 java版本号
权重轮询调度算法(Weighted Round-Robin Scheduling)--java版本号
因为每台server的配置、安装的业务应用等不同。其处理能力会不一样。所以,我们依据server的不同处理能力,给每一个server分配不同的权值。使其可以接受对应权值数的服务请求。
2个java源文件,例如以下所看到的:
public interface IProduceStrategy { public int getPartitionIdForTopic(); }
public class WeightFactorProduceStrategy implements IProduceStrategy {
private int i = -1; //表示上一次选择的server
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
权重轮询调度算法 java版本号的更多相关文章
- 权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现2
权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现 ----参考Nginx中负载均衡算法实现 与上一遍博客 http://www.cnblogs.com/hu ...
- 权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现3
权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现3 之前两篇相关博文: 权重轮询调度算法(WeightedRound-RobinScheduling)-Ja ...
- 权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现
权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现 import java.math.BigInteger; import java.util.ArrayLi ...
- 权重轮询调度算法(WeightedRound-RobinScheduling)
权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现 ----参考Nginx中负载均衡算法实现 这里主要参考这篇文章的实现: Nginx 负载均衡-加权轮询策略 ...
- 权重轮询调度算法(Weighted Round-Robin Scheduling)-C#实现
在多台机器实现负载均衡的时候,存在调度分配的问题. 如果服务器的配置的处理能力都一致的话,平均轮询分配可以直接解决问题,然而有些时候机器的处理能力是不一致的. 假如有2台机器 A和B , A的处理能力 ...
- golang实现权重轮询调度算法
package main import ( "fmt" "time" ) var slaveDns = map[int]map[string]interface ...
- 通过 PowerShell 支持 Azure Traffic Manager 外部端点和权重轮询机制
Jonathan TulianiAzure网络 - DNS和 Traffic Manager高级项目经理 在北美 TechEd 大会上,我们宣布了 Azure Traffic Manager将支持 ...
- loadbalance轮询算法 java实现
/** * <html> * <body> * <P> Copyright JasonInternational</p> * <p> All ...
- php版权重轮询调度算法
2013-09-25 <?php class WeightedRoundRobin { private static $_weightArray = array(); private stati ...
随机推荐
- How Javascript works (Javascript工作原理) (二) 引擎,运行时,如何在 V8 引擎中书写最优代码的 5 条小技巧
个人总结: 一个Javascript引擎由一个标准解释程序,或者即时编译器来实现. 解释器(Interpreter): 解释一行,执行一行. 编译器(Compiler): 全部编译成机器码,统一执行. ...
- iOS开发者中心重置设备列表
苹果开发者账号允许的测试设备为100台,如果你注册了,这台机器就算是一个名额,禁用也算一个名额,仍被计入机器总数,每年可以重置一次,那我们怎么重置机器数量呢? 我们需要给苹果发送申请: https:/ ...
- [arc076e]connected?
题意: 给出一个$R\times C$的棋盘,其中$1$到$N$之间的每个正整数都会在棋盘上出现两次,第$i$个数出现的位置是$(X_{i,1},Y_{i,1})$和$(X_{i,2},Y_{i,2} ...
- POJ 2607 Fire Station
Fire Station Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on PKU. Original I ...
- 图像切割—基于图的图像切割(Graph-Based Image Segmentation)
图像切割-基于图的图像切割(Graph-Based Image Segmentation) Reference: Efficient Graph-Based Image Segmentation ...
- linux程序设计——个人总结
linux程序设计--个人总结 到今天为止,<linux程序设计>学习基本完毕了.从五月下旬開始接触linux,学习安装Ubuntu14.04,六月份開始学习<linux程序设计&g ...
- 支持并发的httpclient(基于tcp连接池以及netty)
闲来无事,将曾经自己写的一个库放出来吧. . 有的时候会有这样子的需求: (1)serverA通过HTTP协议来訪问serverB (2)serverA可能会并发的像B发送非常多HTTP请求 类似于上 ...
- JS学习笔记-数据类型
最初的JS学习已经过去大半年的时间了,至此感觉对JS的使用与理解并非非常深入,因此在近期的工作之余也開始了新一轮的JS学习. 几天时间过去了,对于一些基础内容的学习还是非常有必要的,就从今天的又一次整 ...
- Oracle 建表
-- Create table create table STUDENT ( sno ) not null, sname ) not null, ssex ) not null, sbirthday ...
- 最近Criteria
第一次用Criteria,于是查了一下http://langgufu.iteye.com/blog/2039554 新鲜事排序算法http://www.zhihu.com/question/20319 ...