OpenSIPS version you are running
(any version including current master)
Describe the bug
While reading the weight_based_sort() function in the drouting module, I noticed that the random gateway selection in the weight_sum == 0 branch is commented out:
} else {
/* randomly select index */
// i = (unsigned int)((size-first)*((double)rand()/((double)RAND_MAX)));
i = first;
}
https://github.com/OpenSIPS/opensips/blob/master/modules/drouting/drouting.c#L2974
This means that when all gateways in the current subset have weight 0, the algorithm always picks the first element (i = first), making the sort fully deterministic and always preferring whichever gateway appears first in the list. If the weighted sort algorithm is selected, one would expect gateways with equal weight (including weight 0) to be picked at random with uniform distribution, but that is not happening.
To Reproduce
- Configure a drouting rule with two or more gateways, all with weight 0 and the weight-based sort algorithm selected
- Send traffic through the rule repeatedly
- Notice that the first gateway in the list is always selected, regardless of how many gateways are configured
Expected behavior
When all gateways in a subset have weight 0, they should be treated as equal and one should be picked at random, providing uniform distribution across all of them. This is consistent with the general intent of weight-based sorting.
If the commented line were active, that would be the behavior. The corrected version of the line should also be consistent with the fix introduced in PR #3020, i.e. using double and RAND_MAX+1 to avoid rounding errors:
i = first + (unsigned int)((size - first) * ((double)rand() / ((double)RAND_MAX + 1.0)));
Relevant System Logs
No relevant logs, the behavior is observable purely from traffic distribution.
OS/environment information
- Operating System: any
- OpenSIPS installation: any
Additional context
I searched through the git log, related issues and PRs but could not find any explicit discussion explaining why this line was commented out. The most recent change to this function was PR #3020, which fixed a bug in the weight_sum > 0 branch but did not touch the zero-weight case. The older issue #108 addressed unbalanced random selection but also does not mention this case.
If i = first is actually the intended behavior (e.g. weight 0 meaning "no load balancing, always use the first configured gateway"), it would be helpful to have a short comment in the source explaining that, to avoid confusion. Thanks!
OpenSIPS version you are running
Describe the bug
While reading the
weight_based_sort()function in thedroutingmodule, I noticed that the random gateway selection in theweight_sum == 0branch is commented out:https://github.com/OpenSIPS/opensips/blob/master/modules/drouting/drouting.c#L2974
This means that when all gateways in the current subset have weight 0, the algorithm always picks the first element (
i = first), making the sort fully deterministic and always preferring whichever gateway appears first in the list. If the weighted sort algorithm is selected, one would expect gateways with equal weight (including weight 0) to be picked at random with uniform distribution, but that is not happening.To Reproduce
Expected behavior
When all gateways in a subset have weight 0, they should be treated as equal and one should be picked at random, providing uniform distribution across all of them. This is consistent with the general intent of weight-based sorting.
If the commented line were active, that would be the behavior. The corrected version of the line should also be consistent with the fix introduced in PR #3020, i.e. using
doubleandRAND_MAX+1to avoid rounding errors:Relevant System Logs
No relevant logs, the behavior is observable purely from traffic distribution.
OS/environment information
Additional context
I searched through the git log, related issues and PRs but could not find any explicit discussion explaining why this line was commented out. The most recent change to this function was PR #3020, which fixed a bug in the
weight_sum > 0branch but did not touch the zero-weight case. The older issue #108 addressed unbalanced random selection but also does not mention this case.If
i = firstis actually the intended behavior (e.g. weight 0 meaning "no load balancing, always use the first configured gateway"), it would be helpful to have a short comment in the source explaining that, to avoid confusion. Thanks!