基于百分比的路由算法

2024-06-28

四处浏览基于百分比的路由, 偶然发现这个线程 https://stackoverflow.com/a/52044571/3154233.

根据建议的算法如下:

对于给定模型如下:

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 )

任何指出算法实现中的错误的指示都将受到赞赏!


问题出在代码末尾的循环中。由于以下行,它对所有主机使用相同的权重:

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

基于百分比的路由算法 的相关文章

随机推荐

  • 无法发布 API 项目的 Google Apps Marketplace 列表审核请求

    我的要求是启用OAuth 2 0对于现有的OAuth 1 0应用在Google Apps Marketplace 我在以下位置创建了新的 Google 项目console https cloud google com console 并为其
  • 如果 Count(*) 为零,则返回 NULL

    我有以下 mysql 查询 SELECT count student name AS total student school name FROM student LEFT JOIN school info ON school info s
  • Nginx 返回文件路径

    我希望 Nginx 返回路径 example com r 的 r json 文件 我尝试过的 location r alias home user media json r json 但这一切都不起作用 我收到了 500 条消息 home
  • GWT - Intellij IDEA 中 GWT 的优秀 GUI 编辑器是什么?

    IDEA 附带的工具只不过是一个 GWT 项目创建工具 有更好的插件吗 GWT 有独立的 GUI 编辑器吗 直接回答你的问题 目前还没有适用于 GWT 的 Intellij IDEA GUI WYSIWYG 编辑器 GWT 最受欢迎 功能完
  • Django:在视图中访问时,settings.py 中的重音字符被破坏

    我在 settings py 中有重音字符 我使用 getattr settings MY CONSTANT NAME 在视图中访问这些字符 但 getattr 调用返回损坏的字符 例如 变为 xc3 xb4 这是 view py 中的代码
  • 多步算法的设计模式

    我正在编写一个控制台应用程序 该应用程序执行包含 N 个步骤的算法 这一步很重要N在步骤之前正确完成N 1被执行 否则程序应该停止工作并显示错误消息 我可以用嵌套来做到这一点if当然和用途的陈述try catch finally 在fina
  • Jquery:将rel属性添加到某个类的所有
  • 标签内的标签
  • 我正在尝试添加一个rel lightframe 归因于我的所有 编辑 链接admin links node edit class li class admin links node edit a href link title Edit E
  • 刷新电量查询VBA

    因此 我使用下面的代码来刷新我的查询连接 但是 如果由于 wtv 原因刷新失败 如何显示消息 因为此 VBA 显示刷新已完成 即使我的查询存在多个错误 Worksheets Details Unprotect Dim Connection
  • 编译器优化 - 函数没有地址

    我没有使用太多指向成员函数的指针 但我认为在使用此类指针时发现了一些危险的情况 当编译器由于某些优化而决定不为函数分配地址时 就会出现问题 VS 2015 即使在调试 x86 中也会发生这种情况 禁用优化 Od 我正在重构一个旧系统 将一些
  • 如何在线性布局周围添加边框(底部除外)?

    如何在线性布局周围添加边框 底部除外 LinearLayout 需要在左侧 顶部和右侧有边框 但底部不需要 在drawable文件夹中创建一个名为border xml的XML文件 并将以下代码放入其中
  • 如何删除警告:link.res包含输出部分;你忘了-T吗?

    我正在使用 fpc 编译器 我想删除此警告 我已经阅读了 fpc 的选项 但我找不到如何做到这一点 这可能吗 当我运行命令时出现 fpc foo pas out 目标操作系统 Linux for i386 编译 foo pas 链接 p2
  • zurb 基金会是否可以拥有完整的行宽度

    我正在使用 Foundation 3 构建响应式网站 但我想让页脚和导航背景宽度占据整个宽度 我将我的行命名为 class row navigation class row footer 我尝试寻找如何解决这个问题 但我没有选择 我假设这是
  • 如何使用 pip 安装 Openpyxl

    我有 Windows 10 64 位 我想利用Openpyxl包开始学习如何与 Excel 和其他电子表格交互 我安装了Python windowsx86 64web basedinstaller 我有 64 位操作系统 尝试安装此版本时我
  • Helm 安装未知标志 --name

    当我尝试使用 helm 安装图表时 helm install stable nginx ingress name my nginx 我收到错误 错误 未知标志 name 但我在很多文档中都看到了上面的命令格式 版本 version Buil
  • IntelliJ 的 javafx 集成场景生成器在 Oracle JDK 12 中无法工作

    我正在运行 Arch Linux 安装了最新的 IntelliJ 包以及 Oracle 的 JDK12 项目使用的 和 Gluon 的场景生成器 场景生成器的路径已正确设置 场景生成器独立工作 也是从 IntelliJ 启动时 我右键单击我
  • 房间数据库:插入的ID始终为0

    我正在尝试创建列表项的房间数据库 我在这里遵循这个例子 https medium com mindorks room kotlin android architecture components 71cad5a1bb35 https med
  • Spring 排序的 beans 列表

    我有几个实现相同接口的 bean 每个 bean 都注释有 Component Order SORT ORDER public class MyClass implements BeanInterface 有一次 我自动装配了一个组件列表
  • EventMachine 的优势是什么

    这是我的测试用例 我发现EM并不比一般的TCP服务器快 EM 服务器 require rubygems require benchmark require eventmachine class Handler lt EventMachine
  • 断点在 xcode pod 文件中不起作用

    我有一个 xcode 项目 其中包含一些可可豆荚文件 当我在 cocoa pod 文件中放置断点时 调试器不会在这些断点处停止 为什么 有人对此有什么想法吗 我关注了 UdaySingh 的评论 它起作用了 我不确定他为什么没有发布答案 但
  • 基于百分比的路由算法

    四处浏览基于百分比的路由 偶然发现这个线程 https stackoverflow com a 52044571 3154233 根据建议的算法如下 对于给定模型如下 public class Host private String nam