您的位置:首页 > 编程语言 > Java开发

权重选择算法Java实现

2017-10-23 16:38 471 查看
 我们有时候会遇到这种需求,那就是根据权重,按照比例去获取相应的信息,比如配置信息获取,负载均衡RS获取等。

 在此就举一个例子,然后简单的实现。

需求:后端有三台机器,信息分别为,

S1<ip:"10.0.0.1",port:8081,weight:20>,

S2<ip:"10.0.0.2",port:8082,weight:40>, 

S3<ip:"10.0.0.3",port:8083,weight:60>,

根据weight按照比例返回响应的机器信息。

   算法思路:将三个权重映射到一个一维空间中,那么S1对应区间[0, 20), S2对应区间[20, 60), [60, 120],然后在[0,120]之间生成随机数,看此数落在哪个区间,那么就返回对应机器的信息。

talk is cheap, show me the code:

private int getServerByWeight(int[] weightArr) {
int[][] randArr = new int[weightArr.length][2];
int totalRank = 0;
int index = 0;
for(int i=0;i<weightArr.length;i++) {
if (weightArr[i] <= 0) {
continue;
}

totalRank += weightArr[i];
randArr[i][0] = i;
randArr[i][1] = totalRank;
}

int hitRank = new Random().nextInt(totalRank) + 1;//[1, totalRand]
for (int i = 0; i < randArr.length; i++) {
if (hitRank <= randArr[i][1]) {
return randArr[i][0];
}
}

return randArr[0][0];
}

public Server choose(List<Server> serverList) {
if (null == serverList) {
return null;
}

int[] weightArr = new int[serverList.size()];

for(int i = 0; i < serverList.size(); i++) {
if (serverList.get(i).getWeight() > 0) {
weightArr[i] = serverList.get(i).getWeight();
}
}

if (weightArr.length == 0) {
return null;
}

int chosenIndex = getServerByWeight(weightArr);
return serverList.get(chosenIndex);
}


主要有两个函数:choose和getServerByWeight。测试代码如下:

WeightAllocationAlg.java:

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Created by iqiyi on 2017/10/17.
*/

class Server {
private String ip;

private int port;

private int weight;

public Server(String ip, int port, int weight) {
this.ip = ip;
this.port = port;
this.weight = weight;
}

public String getIp() {
return ip;
}

public void setIp(String ip) {
this.ip = ip;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public int getWeight() {
return weight;
}

public void setWeight(int weight) {
this.weight = weight;
}

public String toString() {
return "ip : " + ip + ", port : " + port + ", weight : " + weight;
}
}

public class WeightAllocationAlg {

private AtomicInteger[] completedCount = new AtomicInteger[3];

public WeightAllocationAlg() {
completedCount[0] = new AtomicInteger(0);
completedCount[1] = new AtomicInteger(0);
completedCount[2] = new AtomicInteger(0);
}

private int getServerByWeight(int[] weightArr) { int[][] randArr = new int[weightArr.length][2]; int totalRank = 0; int index = 0; for(int i=0;i<weightArr.length;i++) { if (weightArr[i] <= 0) { continue; } totalRank += weightArr[i]; randArr[i][0] = i; randArr[i][1] = totalRank; } int hitRank = new Random().nextInt(totalRank) + 1;//[1, totalRand] for (int i = 0; i < randArr.length; i++) { if (hitRank <= randArr[i][1]) { return randArr[i][0]; } } return randArr[0][0]; } public Server choose(List<Server> serverList) { if (null == serverList) { return null; } int[] weightArr = new int[serverList.size()]; for(int i = 0; i < serverList.size(); i++) { if (serverList.get(i).getWeight() > 0) { weightArr[i] = serverList.get(i).getWeight(); } } if (weightArr.length == 0) { return null; } int chosenIndex = getServerByWeight(weightArr); return serverList.get(chosenIndex); }

public void doTestConcurrently(final List<Server> servers, int threadCount) {

class MyRunnable implements Runnable {

public void run() {
Server svr = choose(servers);
if (svr.getIp().equals("10.0.0.1")) {
completedCount[0].incrementAndGet();
} else if (svr.getIp().equals("10.0.0.2")) {
completedCount[1].incrementAndGet();
} else if(svr.getIp().equals("10.0.0.3")) {
completedCount[2].incrementAndGet();
}
}
}

try {
Thread [] ts = new Thread[threadCount];
for (int i=0; i<threadCount; i++) {
ts[i] = new Thread(new MyRunnable());
}

for (int i = 0; i < threadCount; i++) {
ts[i].start();
}

for (int i = 0; i < threadCount; i++) {
ts[i].join();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
int totalCompleted = completedCount[0].get() + completedCount[1].get() + completedCount[2].get();
if (totalCompleted == threadCount) {
System.out.println((double)completedCount[0].get()/totalCompleted);
System.out.println((double)completedCount[1].get()/totalCompleted);
System.out.println((double)completedCount[2].get()/totalCompleted);
}
}
}

public static void main(String[] args) {
Server[] servers = {
new Server("10.0.0.1", 8081, 20),
new Server("10.0.0.2", 8082, 40),
new Server("10.0.0.3", 8083, 60)
};

WeightAllocationAlg weightAllocationAlg = new WeightAllocationAlg();

Server server = weightAllocationAlg.choose(Arrays.asList(servers));

System.out.println(server.toString());

int threadCount = 100;

weightAllocationAlg.doTestConcurrently(Arrays.asList(servers), threadCount);

}

}
结果如下

ip : 10.0.0.2, port : 8082, weight : 40

0.18

0.34

0.48

可以看到基本上是按照1:2:3的比例返回的,跟预期的一致,当然略有偏差,如果在数据量大且随机函数分布较均匀的情况下结果应该就是按照1:2:3来的。

Author:忆之独秀

Email:leaguenew@qq.com

注明出处:http://blog.csdn.net/lavorange/article/details/78320349
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息