-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
HBASE-26555 Fix findbugs/spotbugs findings #3936
base: master
Are you sure you want to change the base?
Conversation
843ea7e
to
c2dc14b
Compare
🎊 +1 overall
This message was automatically generated. |
🎊 +1 overall
This message was automatically generated. |
🎊 +1 overall
This message was automatically generated. |
🎊 +1 overall
This message was automatically generated. |
💔 -1 overall
This message was automatically generated. |
💔 -1 overall
This message was automatically generated. |
Fix LoadTestKVGenerator related failures. |
🎊 +1 overall
This message was automatically generated. |
🎊 +1 overall
This message was automatically generated. |
🎊 +1 overall
This message was automatically generated. |
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.
The patch is too big to review, I suggest that we split it into several pieces, for fixing different type of spotbugs warnings.
WDYT? @apurtell
Thanks.
private final long clientId; | ||
|
||
private Random rng; |
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 do not see any real differences here? What is the spotbugs issue here?
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.
Spotbugs wants you to allocate a Random object and reuse it as often as possible. This is because if you do not reuse Random, if you just do like so in a method
Random r = new Random();
// do something with 'r'
the quality of the generated random numbers is poor.
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 say let's just use ThreadLocalRandom, unless SecureRandom is required, then we could create a static SecureRandom instance and use it.
// Ensure priority is a positive integer | ||
int priority = className.hashCode(); | ||
if (priority < 0) { | ||
priority = -priority; |
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.
This is not the correct fix... You are just repeating the implementation of Math.abs here...
If priority is Integer.MIN_VALUE, then -priority will still be Integer.MIN_VALUE...
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.
Thanks for pointing that out. As explained by the help text for the spotbugs finding the issue is Math.abs(INT_MIN) can return INT_MIN. So if ABS is supposed to ensure a positive number, it will not always do so. The fix is to change the sign of the value if it is negative and should not be negative. Needs more here, then.
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.
The main problem is not Math.abs, is INT_MIN. As the absolute value of INT_MIN is greater than INT_MAX, there is no positive int value can represent the absolute value of INT_MIN... You could try the piece of code here, let priority to be INT_MIN, you will still get a INT_MIN with -priority...
byte[] testBytes = new byte[10]; | ||
rand.nextBytes(testBytes); | ||
RNG.nextBytes(testBytes); |
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.
What is the real gain here? We just the the random once, and I suggest that we just use ThreadLocalRandom instead...
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.
Spotbugs wants you to allocate a Random object and reuse it as often as possible. This is because if you do not reuse Random, if you just do like so in a method
Random r = new Random();
// do something with 'r'
the quality of the generated random numbers is poor.
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.
Sure, let's use ThreadLocalRandom.current() instead of the static Random instance.
for (int i = 0; i < NELEM; ++i) { | ||
int key = rand.nextInt(MAX_KEY); | ||
int key = RNG.nextInt(MAX_KEY); |
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.
Ditto, since we do not have a fixed seed here, let's just use ThreadLocalRandom.
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 don't care really the objective is reuse of the Random object
assertEquals(0, props.size()); | ||
testBase.loadMonkeyProperties(props, conf); | ||
new IntegrationTestDDLMasterFailover(); |
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.
Why a new without assignment?
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.
Because the assignment is flagged as a dead store. This code still works. The constructor is called. The warning about a dead store is avoided. It is a trivial issue and I don't care much either way.
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.
Then do we really need to create this instance? It has other side effects?
long chainId = Math.abs(new Random().nextLong()); | ||
// ensure chainId is positive | ||
long chainId = RNG.nextLong(); | ||
if (chainId < 0) { |
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.
Ditto, not the correct way for fixing the abs problem...
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.
Ack
Each module has its own commit in the PR branch @Apache9 . I think that is the best we can do as far as keeping each review item small. In other words I am aware the patch is big over all. So there are multiple commits on this PR. Find them here: https://github.com/apache/hbase/pull/3936/commits Each commit has the changes just for one maven module. |
We could just use multiple PRs to get them in? A problem for a big PR is that, we could have a lot of small nits every time when reviewing, on different part of the big PR, then we can not land the whole PR, which will lead to a very long review time and easy to introduce conflicts with other commits... |
You want me to close this and open multiple PRs? How many PRs? How would you divide this? One PR per module? One PR per file? There are not THAT many changes here. I think it is not the hassle you describe. I would like to contribute spotbugs changes but it is hardly a priority relative to other things. This PR is ready. I hope it can be used. I do not plan to open multiple PRs for this. If that is a problem, feel free to just close this and resolve the JIRA. |
I am going to split the Random changes into a separate JIRA and PR because that part has taken on a life of its own. Edit: It is HBASE-26582 |
c80ecfd
to
7eba7d7
Compare
🎊 +1 overall
This message was automatically generated. |
💔 -1 overall
This message was automatically generated. |
💔 -1 overall
This message was automatically generated. |
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.
Commented on the branch-2 backport. This seems fine to me!
Commits in the PR branch break down changes per module.