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

KAFKA-3014: fix integer overflow problem in leastLoadedNode #696

Closed
wants to merge 1 commit into from

Conversation

hachikuji
Copy link
Contributor

No description provided.

@hachikuji
Copy link
Contributor Author

Should be noted that overflow is technically still possible in this patch, but the cluster size would have to be greater than Integer.MAX_VALUE/2 to see it.

for (int i = 0; i < nodes.size(); i++) {
int idx = Utils.abs((this.nodeIndexOffset + i) % nodes.size());
int idx = (offset + i) % nodes.size();
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be completely fixed by

int startIdx = Utils.abs(this.randOffset.nextInt(Integer.MAX_VALUE)) % nodes.size();
...
for (int i = 0; i < nodes.size; i++) {
    int idx = (startIdx + i) % nodes.size();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could be wrong, but that seems equivalent to what I have above. If we're really concerned about this overflow, the easiest thing would be to do the addition using longs and convert back to int after doing the modulus. Seems like overkill though since no one is likely to scale Kafka past a billion nodes. And the impact of overflow is benign anyway when you have so many nodes to choose from.

Copy link
Contributor

Choose a reason for hiding this comment

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

You are right, they are actually the same.

@asfgit asfgit closed this in fa4ffb8 Dec 21, 2015
asfgit pushed a commit that referenced this pull request Dec 21, 2015
Author: Jason Gustafson <jason@confluent.io>

Reviewers: Guozhang Wang

Closes #696 from hachikuji/KAFKA-3014

(cherry picked from commit fa4ffb8)
Signed-off-by: Guozhang Wang <wangguoz@gmail.com>
@guozhangwang
Copy link
Contributor

LGTM. Merged to both branches.

efeg pushed a commit to efeg/kafka that referenced this pull request Jan 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants