基于java的百分比路由算法

1tuwyuhd  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(265)

在浏览基于百分比的路由时,偶然发现了这个线程。
根据以下建议的算法:
对于给定的模型,如下所示:

public class Host {
    private String name;
    private int percentageLoad;
    private int percentageAccum;
}

percentageaccum的初始值是percentageload的值。
收到请求时:
选择Accum百分比最大的主机
从所选主机的percentageaccum中减去100
将所有主机(包括所选主机)的percentageload添加到percentageaccum
下面是我的实现

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class HostWeightage{
    private String hostId;
    private int weightage;
    private int accumulatedWeightageSoFar;
}

java执行器示例

public String getRoutedHost(List<HostWeightage> hostWeightageList) {

    // assume 0th index as default 
    HostWeightage hostWithMaxAccWeight = hostWeightageList.get(0);

    // choose the host with the largest percentageAccum
    for (int i = 1; i < hostWeightageList.size(); i++) {
        if (hostWeightageList.get(i).getAccumulatedWeightageSoFar() >= hostWithMaxAccWeight.getAccumulatedWeightageSoFar()){
            hostWithMaxAccWeight = hostWeightageList.get(i);
        }
    }

    // subtract 100 from the percentageAccum for the chosen host
    int inverseAccWeight = hostWithMaxAccWeight.getAccumulatedWeightageSoFar() - 100;
    hostWithMaxAccWeight.setAccumulatedWeightageSoFar(inverseAccWeight);

    // add percentageLoad to percentageAccum for all hosts, including the chosen host
    int weight = hostWithMaxAccWeight.getWeightage();
    for (HostWeightage wightedHost : hostWeightageList) {
        int accWeight = wightedHost.getAccumulatedWeightageSoFar();
        wightedHost.setAccumulatedWeightageSoFar(weight + accWeight);
    }

    return hostWithMaxAccWeight.getHostId();
}

这是我的样本,每次10个电话

INFO: initial config
HostWeightage(hostId=redirect_host_1, weightage=10, accumulatedWeightageSoFar=10), 
HostWeightage(hostId=redirect_host_2, weightage=40, accumulatedWeightageSoFar=40), 
HostWeightage(hostId=redirect_host_3, weightage=50, accumulatedWeightageSoFar=50)
final distribution of 10 calls:
INFO: host1 3 ( should have been 1)
INFO: host2 3 ( should have been 4)
INFO: host3 4 ( should have been 5)
-------------------------
INFO: initial config 
HostWeightage(hostId=redirect_host_1, weightage=30, accumulatedWeightageSoFar=30), 
HostWeightage(hostId=redirect_host_2, weightage=30, accumulatedWeightageSoFar=30), 
HostWeightage(hostId=redirect_host_3, weightage=40, accumulatedWeightageSoFar=40)
final distribution of 10 calls:
INFO: host1 3 ( correct output )
INFO: host2 3 ( correct output )
INFO: host3 4 ( correct output )
-------------------------
INFO: initial config 
HostWeightage(hostId=redirect_host_1, weightage=10, accumulatedWeightageSoFar=10), 
HostWeightage(hostId=redirect_host_2, weightage=20, accumulatedWeightageSoFar=20), 
HostWeightage(hostId=redirect_host_3, weightage=70, accumulatedWeightageSoFar=70)
final distribution of 10 calls:
INFO: host1 3 ( should have been 1 )
INFO: host2 3 ( should have been 2 )
INFO: host3 4 ( should have been 7 )

任何关于algo实现中的错误的指针都是非常感谢的!!

iqih9akk

iqih9akk1#

问题在代码末尾的循环中。它对所有主机使用相同的重量,原因是:

int weight = hostWithMaxAccWeight.getWeightage();

添加到每个主机累加器的权重需要是该主机的权重,而不是所选主机的权重。所以循环应该是:

for (HostWeightage weightedHost : hostWeightageList) {
    int weight = weightedHost.getWeightage();
    int accWeight = weightedHost.getAccumulatedWeightageSoFar();
    weightedHost.setAccumulatedWeightageSoFar(weight + accWeight);
}

使用权重的算法的一个示例运行 A:10 B:80 C:10 看起来像这样:

accumulators
  A   B   C
 10  80  10  choose B  
 20  60  20  choose B  
 30  40  30  choose B 
 40  20  40  choose  A  
-50 100  50  choose B  
-40  80  60  choose B  
-30  60  70  choose  C  
-20 140 -20  choose B  
-10 120 -10  choose B  
  0 100   0  choose B  
 10  80  10  back to start

相关问题