What is the best way to implement weighted random selection based on two types of variables (in php)?

In principle, my dilemma is this. I have a list of x servers that store files. There is another server hosting the mysql db and application site. When a file is downloaded (to an external server), the application checks which server has more free space on it and moves the file there. This works great if you started with 2+ empty servers with the same amount of free space. If you add another server to the mix at a later time ... which will have more free space than the current servers, this method is not so effective because all new files will be downloaded elusive to the new server, which will overload as it will handle most of the new traffic until it catches the rest of the boxes in terms of free space.

Therefore, I thought to introduce a weighing system that will help normalize the distribution of files. Therefore, if 3 servers are installed at 33%, and 1 server has significantly more free space, it will receive more downloads than other servers (although it has the same weight), but the load will be distributed across all servers.

Can anyone suggest a good php-only implementation?

+3
source share
4 answers

- , ( , , , ). , ( ). , , .

, :

Server 1:   2048MB
Server 2:  51400MB
Server 3:   1134MB
Server 4: 140555MB

1500 . Server 3 - , 194003MB.

Server 1:  1.0%
Server 2: 26.5%
Server 4: 72.5%

0 100: 40

Numbers between 0 and 1 (inclusive) would go to Server 1
Numbers > 1 and <= 26.5 would go to Server 2
Numbers > 26.5 and <= 100 would go to Server 4

, 40 , 4.

+4

. - , (, , - , ) - , , .

, , , , , , / .

php-only, .

+1

:

  • , , {0.5, 0.5, 1.0}
  • - , , - {0,25, 0,25, 0,5}
  • , (0.0,1.0), 1.0 * mt_rand()/mt_getmaxrand()
  • :

    $total_weight = 0.0;
    for ( $i = 10; $i <= sizeof($weights); $i++) {
      $total_weight += #weights[$i];
      if($rand <= $total_weight) {
    return $i;
      }
    }
    

-

+1

- , .

/. , MogileFS, , , , , ( , )

, ", ": 2 , , , ? , , , ? , , , , , .. .....

. , . , , , .

+1

Source: https://habr.com/ru/post/1710710/


All Articles