首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP应用中的加权负载均衡算法

PHP应用中的加权负载均衡算法
EN

Stack Overflow用户
提问于 2015-11-18 17:18:07
回答 3查看 491关注 0票数 1

我希望解析来自工厂的加权适配器,该适配器可以由用户配置(启用/禁用和权重%)。

示例:

  • AdapterW≃占交易的20%
  • AdapterX≃占交易的30%
  • AdapterY≃40%的交易
  • AdapterZ≃占交易的10%

我可以承认,所有项目的总和永远不会超过100% (100%),但有时任何适配器都可能被停用。

我有以下参数:

代码语言:javascript
复制
public function handleAdapter()
{
    $isWActive = (boolean)$this->_config[self::W];
    $isXActive = (boolean)$this->_config[self::X];
    $isYActive = (boolean)$this->_config[self::Y];
    $isZActive = (boolean)$this->_config[self::Z];
    $WPercentage = (int)$this->_config[self::LOAD_BALANCE_W];
    $XPercentage = (int)$this->_config[self::LOAD_BALANCE_X];
    $YPercentage = (int)$this->_config[self::LOAD_BALANCE_Y];
    $ZPercentage = (int)$this->_config[self::LOAD_BALANCE_Z];
    .
    .
    .
    return (self::W | self::X | self::Y | self::Z); 
}

如何动态地平衡这些适配器之间的加权?

编辑

为可执行代码创建一个要点:https://gist.github.com/markomafs/5d892d06d6670909f9b4

EN

回答 3

Stack Overflow用户

发布于 2015-11-19 14:04:22

这可能不是最好的方法,但您可以尝试这样的方法:

代码语言:javascript
复制
public function handleAdapter()
{
    //an array to return the balanced entries
    $balancedEntries[] = false;

    //verifies which of the options are active
    $isWActive = (boolean)$this->_config[self::W];
    $isXActive = (boolean)$this->_config[self::X];
    $isYActive = (boolean)$this->_config[self::Y];
    $isZActive = (boolean)$this->_config[self::Z];

    //get configured percentage of each
    $WPercentage = (int)$this->_config[self::LOAD_BALANCE_W];
    $XPercentage = (int)$this->_config[self::LOAD_BALANCE_X];
    $YPercentage = (int)$this->_config[self::LOAD_BALANCE_Y];
    $ZPercentage = (int)$this->_config[self::LOAD_BALANCE_Z];

    //here you fill the array according to the proportion defined by the percentages
    if ($isWActive) {
            for ($i = 0; $i < $WPercentage; $i++) {
                $balancedEntries[] = self::W;
            }
        }

        if ($isXActive) {
            for ($i = 0; $i < $XPercentage; $i++) {
                $balancedEntries[] = self::X;
            }
        }

        if ($isYActive) {
            for ($i = 0; $i < $YPercentage; $i++) {
                $balancedEntries[] = self::Y;
            }
        }

        if ($isZActive) {
            for ($i = 0; $i < $ZPercentage; $i++) {
                $balancedEntries[] = self::Z;
            }
        }

        return $balancedEntries;
}

然后,如果您想要1比100的比例(按百分比计算):

代码语言:javascript
复制
$balancedResult = $balancedEntries[array_rand($balancedEntries, 1)];

因为array_rand将从原始数组返回1键,所以您可以使用它来获取它的值。

票数 2
EN

Stack Overflow用户

发布于 2015-11-20 06:25:08

另一种尝试,这应该适用于您的情况--但只有当您有一个适配器作为单个字符字符串时,这是不可见的问题。

代码语言:javascript
复制
public function handleAdapter()
{
    # a map with all adapters
    $map = array(
        self::W => self::LOAD_BALANCE_W,
        self::X => self::LOAD_BALANCE_X,
        self::Y => self::LOAD_BALANCE_Y,
        self::Z => self::LOAD_BALANCE_Z
    );
    # generate a string map with one char per percentage point
    $stringMap = "";
    foreach($map as $key => $value){
        # skip if disabled
        if(!$this->_config[$key]) continue;
        # repeat the key for each percentage point
        $stringMap .= str_repeat($key, (int)$this->_config[$value]);
    }
    # return a random string char from the map
    return $stringMap[rand(0, strlen($stringMap) - 1)];
}
票数 1
EN

Stack Overflow用户

发布于 2015-11-19 14:19:09

编辑:我误解了这个问题,答案是错误的。

我理解您的问题,所以您总是希望返回负载最低的适配器,以强制此适配器的流量。

代码语言:javascript
复制
public function handleAdapter()
{
    $isWActive = (boolean)$this->_config[self::W];
    $isXActive = (boolean)$this->_config[self::X];
    $isYActive = (boolean)$this->_config[self::Y];
    $isZActive = (boolean)$this->_config[self::Z];
    $WPercentage = (int)$this->_config[self::LOAD_BALANCE_W];
    $XPercentage = (int)$this->_config[self::LOAD_BALANCE_X];
    $YPercentage = (int)$this->_config[self::LOAD_BALANCE_Y];
    $ZPercentage = (int)$this->_config[self::LOAD_BALANCE_Z];

    $map = array();
    if($isWActive) $map[self::W] = $WPercentage;
    if($isXActive) $map[self::X] = $XPercentage;
    if($isYActive) $map[self::Y] = $YPercentage;
    if($isZActive) $map[self::Z] = $ZPercentage;

    asort($map);
    return key($map);    
}

编辑:修正了错误的sort(),你需要asort()来维护索引。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33786321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档