L
L
LINKeR UA2017-09-15 16:56:16
Algorithms
LINKeR UA, 2017-09-15 16:56:16

How to build a priority queue like weighted round robin?

For example, we have an array of servers, where the percentage is indicated (balancing weights).

<?php $server = [
   's01' => 10/100, // может обработать 10 % задач и текущего стека очереди
   's02' => 20/100,
   's03' => 25/100,
   's04' => 20/100,
   's05' => 25/100,
];?>

This means that each:
10th request (1/0.1) goes to s01
5th request (1/0.2) goes to s02
4th request (1/0.25) goes to s03
5th request (1/0.2) goes to s04
4th request (1/ 0.25) goes to s05
What should be the calculation to for example from a stack of 100 requests. redirect each request in proportion to the specified performance factors?
I watched how RR works to distribute tasks to the CPU, I have something similar, but "turned inside out". There is quite an important coefficient "conditional completion time", relative to which the most suitable (faster processed) queue for execution is built according to the Gantt chart. I don't have such as "conditional end time".
Help with writing either a formula to calculate "where to send each Nth request out of 100" or build an array that will fill evenly ['s1', 's2', 's3', 's4', 's5', 's2', 's3' ...], whose length === 100.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2017-09-16
@wataru

Well, building an array is very simple - first put N1 values ​​of s1 there, then N2 values ​​of s2, and so on.
Something like this:

vector<int> a(100);
for (int i = 0, cur = 0; i < k; ++i ) {
  for (int j = 0; j < n[i]; j++) {
    a[cur++] = i;
  }
}

True, it will be of the form {'s1', 's1', 's1', ... 's2', 's2', ...]. If this is not allowed, the array can be shuffled:
for (int i = 1; i < 100; i++) {
   int j = rand() % (i+1);
   swap(a[i], a[j]);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question