Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an issue where destinations are compared against themselves #2065

Merged
merged 2 commits into from Mar 9, 2023

Conversation

felipeogarcia
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.

@felipeogarcia
Copy link
Contributor Author

@microsoft-github-policy-service agree

@felipeogarcia felipeogarcia 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

public void PickDestination_PowerOfTwoChoices_Works()

@felipeogarcia felipeogarcia 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 microsoft: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.

None yet

3 participants