Skip to content

drouting: intentional behavior or dead code? random GW selection commented out in weight_based_sort() when weight_sum==0 #3863

@wlp2s0

Description

@wlp2s0

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

  1. Configure a drouting rule with two or more gateways, all with weight 0 and the weight-based sort algorithm selected
  2. Send traffic through the rule repeatedly
  3. 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!

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions