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

Time used fetching should never be negative #878

Merged
merged 2 commits into from
Apr 5, 2023
Merged

Time used fetching should never be negative #878

merged 2 commits into from
Apr 5, 2023

Conversation

liu-jianyang
Copy link
Contributor

If device system time changes into the past while fetching from nats, we should not allow the difference in time to be negative. This would cause the call to nextMessageInternal to hang for a long time.

@@ -149,7 +149,7 @@ private List<Message> _fetch(int batchSize, long maxWaitMillis) {
batchLeft--;
}
// try again while we have time
timeLeft = maxWaitMillis - (System.currentTimeMillis() - start);
timeLeft = maxWaitMillis - Math.max(0, System.currentTimeMillis() - start);
Copy link
Contributor

@scottf scottf Apr 4, 2023

Choose a reason for hiding this comment

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

The right side will always be zero, which also doesn't work, timeLeft will never decrease below maxWaitMillis.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is my proposal.

timeLeft = maxWaitMillis - Math.abs(System.currentTimeMillis() - start);

Copy link
Contributor

Choose a reason for hiding this comment

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

The Math.abs solution will make timeLeft < 0 if the clock changes backwards, stopping the loop. Messages that are missed will get put in a buffer and dealt with the next time fetch is called again, see lines 123-127. The new underlying pull will be adjusted.
Just a note regarding what we are calling simplification. I'm making a new "fetch" (really an iterate, it does not block and also making a pull based consumer that is endless, which means it looks like a push subscription but uses pull under the covers. These may or may not be of interest to you, either way I should have a beta by end of week.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, definitely not ideal. Alternatively we could reset and let the user handle it? Since having the current time be less than the original start time is invalid anyway.

Copy link
Contributor

@scottf scottf Apr 5, 2023

Choose a reason for hiding this comment

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

The Math.abs version will do the trick. If the current time is less than start, the loop will stop. If the time changes forward, the right side is still valid and makes timeLeft less than 0, also stopping.

If you would like to make the changes, please go ahead, otherwise I will make a PR. If you can, please verify your commit. https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits

Choose a reason for hiding this comment

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

Should we not move away from using System.currentTimeMillis() and use something like System.nanoTime()?

@liu-jianyang liu-jianyang requested a review from scottf April 5, 2023 14:23
@liu-jianyang liu-jianyang marked this pull request as ready for review April 5, 2023 14:23
Copy link
Contributor

@scottf scottf left a comment

Choose a reason for hiding this comment

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

Approved

@scottf scottf merged commit af8415a into nats-io:main Apr 5, 2023
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