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

loadbalancer-experimental: add support for weights in round robin #2909

Merged
merged 5 commits into from
May 8, 2024

Conversation

bryce-anderson
Copy link
Contributor

Motivation:

We want to support weighted but don't in the round-robin
HostSelector.

Modifications:

Add weight support to round-robin using the static stride
algorithm common to the grpc libraries.

@bryce-anderson bryce-anderson requested review from daschl, mgodave, tkountis and idelpivnitskiy and removed request for daschl and idelpivnitskiy May 6, 2024 22:01

@Override
int nextHost() {
return (int) (Integer.toUnsignedLong(index.getAndIncrement()) % hostsSize);
Copy link
Member

Choose a reason for hiding this comment

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

  1. Consider keeping original (index.getAndIncrement() & Integer.MAX_VALUE) to avoid a cast from long.
  2. Pre-existing, but if we are here wanna ask: when index overflows we loose the natural index increment. For example, if there are 13 hosts it will jump from idx=10 to idx=0 on overflow instead of going to inx=11. Should we worry about it or is it considered "ok" to interrupt a real round-robin once in a while?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. I like the .toUnsignedLong because it doubles the range an int can represent. The cast is clearly safe since hostSize is bounded to int sizes.
  2. I'm not worried about a jump every ~4 billion requests.

Copy link
Member

Choose a reason for hiding this comment

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

  1. No worried about safety of the cast. If we see a trivial way to avoid unnecessary operation on the hot path, every small effort counts. I wouldn't say it's "premature optimization" bcz this is what we already had.
  2. Ok.

@Override
int nextHost() {
while (true) {
long counter = Integer.toUnsignedLong(index.getAndIncrement());
Copy link
Member

Choose a reason for hiding this comment

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

I would propose to use the same trick here to avoid long completely

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above: I think it's mostly subjective but I like the expanded range without switching to an AtomicLong.

Copy link
Member

Choose a reason for hiding this comment

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

My expectation is that we can stay in int without even going to long or AtomicLong

@bryce-anderson bryce-anderson merged commit f1e39d7 into apple:main May 8, 2024
15 checks passed
@bryce-anderson bryce-anderson deleted the bl_anderson/edf-wrr branch May 8, 2024 20:26
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