Skip to content

Comments

Fix an issue where destinations are compared against themselves#2065

Merged
MihaZupan merged 2 commits intodotnet:mainfrom
tiramisuflavor:main
Mar 9, 2023
Merged

Fix an issue where destinations are compared against themselves#2065
MihaZupan merged 2 commits intodotnet:mainfrom
tiramisuflavor:main

Conversation

@tiramisuflavor
Copy link
Contributor

The two calls to Random.Next may return the same index, so the comparison happens against the same destination. This behavior compromises the worst case protection this algorithm has because it might only use the worst destination, not offering opportunity of a better one. The fewer destinations we have, the more prominent this issue becomes.

@tiramisuflavor
Copy link
Contributor Author

@microsoft-github-policy-service agree

@tiramisuflavor tiramisuflavor marked this pull request as draft March 9, 2023 11:47
auto-merge was automatically disabled March 9, 2023 11:47

Pull request was converted to draft

Copy link
Member

@MihaZupan MihaZupan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a reasonable change to make, thanks!

Can you please also add a test that checks this specific concern?
E.g.:

  • you have 2 destinations, one with 1000 requests and the other with 0
  • if you do 100 calls to PickDestination, they should all give you the destination with less load

The failing test is https://github.com/microsoft/reverse-proxy/blob/4700674a85b0fe9b0fd16be1de4034f626ceb1b9/test/ReverseProxy.Tests/LoadBalancing/LoadBalancingPoliciesTests.cs#L75

@tiramisuflavor tiramisuflavor marked this pull request as ready for review March 9, 2023 16:54
Copy link
Member

@MihaZupan MihaZupan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@MihaZupan MihaZupan added this to the YARP 2.x milestone Mar 9, 2023
@MihaZupan MihaZupan merged commit 84787a6 into dotnet:main Mar 9, 2023
Comment on lines +40 to +43
do
{
secondIndex = random.Next(destinationCount);
} while (firstIndex == secondIndex);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix suffers from the same issue as the original, a high collision rate in small sets. I think it'd be better to do a deterministic shift in those cases.

int secondIndex = random.Next(destinationCount);
if (secondIndex == firstIndex)
{
    secondIndex = (irstIndex + 1) % destinationCount;
}

I'll send a PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MihaZupan convinced me that the +1 approach leads to non-trivial imbalance.

Even in the smallest case of two destinations where collisions will be common, the loop will only need to run a few times on average to break the tie. That's probably adequate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants