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 backOff API in UniformRandomBackOffPolicy #465

Merged
merged 4 commits into from
Sep 9, 2024

Conversation

LokeshAlamuri
Copy link
Contributor

When maxBackOffPeriod is less than minBackOffPeriod, delta is taken taken as zero in UniformRandomBackOffPolicy backOff method.

When maxBackOffPeriod is less than minBackOffPeriod, delta is taken
taken as zero in UniformRandomBackOffPolicy backOff method.
@@ -134,7 +134,7 @@ public long getMaxBackOffPeriod() {
protected void doBackOff() throws BackOffInterruptedException {
try {
Long min = this.minBackOffPeriod.get();
long delta = this.maxBackOffPeriod.get() == this.minBackOffPeriod.get() ? 0
long delta = this.maxBackOffPeriod.get() <= this.minBackOffPeriod.get() ? 0
: this.random.nextInt((int) (this.maxBackOffPeriod.get() - min));
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to extract both Supplier.get() into local variables and use them afterwards.
I think the logical way is possibility to return different values for every get() call.
So, it is OK to have them different for each backOff() call, but not within a single one.
Otherwise it may lead to unexpected behavior.
I mean we have that Long min = this.minBackOffPeriod.get(); variable.
So, let's use that in the following code!
Plus let's extract Long max = this.maxBackOffPeriod.get();

@LokeshAlamuri
Copy link
Contributor Author

LokeshAlamuri commented Sep 7, 2024

I have identified one more issue in UniformRandomBackOffPolicy. Please let me know if i have to raise another issue or should fix along with PR itself.

Issue: With UniformRandomBackOffPolicy there are no chances of getting maxBackOffPeriod as per the configuration. Maximum possible value is maxBackOffPeriod -1.

Reason:
In doBackOff please find the following code.

long delta = max <= min ? 0 : this.random.nextInt((int) (max - min));

Random nextInt upper bound value is always less than the provided argument. So, backoffValue computated as min + delta will never touches maxBackOffPeriod.

Possible Fix (need verify one more time on this):
long delta = max <= min ? 0 : this.random.nextInt((int) (max - min + 1));

@artembilan
Copy link
Member

According to its JavaDocs:

uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)

So, your observation is correct.
However we never advertised that maxBackOffPeriod is an inclusive max value.
Looks like the logic is to get delta somewhere between 0 and max and that is aligned with whatever nextInt() does.
Therefore, what you suggest is a behavior change where normal impression is to follow whatever nextInt() does.
At least that is what makes sense for me.

@artembilan artembilan merged commit c0a4941 into spring-projects:main Sep 9, 2024
2 checks passed
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.

2 participants