-
Notifications
You must be signed in to change notification settings - Fork 181
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
Conversation
...-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/RoundRobinSelector.java
Outdated
Show resolved
Hide resolved
|
||
@Override | ||
int nextHost() { | ||
return (int) (Integer.toUnsignedLong(index.getAndIncrement()) % hostsSize); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Consider keeping original
(index.getAndIncrement() & Integer.MAX_VALUE)
to avoid a cast fromlong
. - 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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I like the
.toUnsignedLong
because it doubles the range an int can represent. The cast is clearly safe sincehostSize
is bounded to int sizes. - I'm not worried about a jump every ~4 billion requests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 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.
- Ok.
@Override | ||
int nextHost() { | ||
while (true) { | ||
long counter = Integer.toUnsignedLong(index.getAndIncrement()); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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
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.