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

[Improvement] LockManager.java comparison using reference equality instead of value equality #2166

Closed
Tracked by #2225
justinmclean opened this issue Feb 9, 2024 · 10 comments · Fixed by #2511
Closed
Tracked by #2225
Labels
good first issue Good for newcomers improvement Improvements on everything

Comments

@justinmclean
Copy link
Member

What would you like to be improved?

In LockManager.java:230: warning: [ReferenceEquality] Comparison using reference equality instead of value equality
if (identifier == ROOT) {

See https://errorprone.info/bugpattern/ReferenceEquality

How should we improve?

Does it mean 'if (Objects.equals(identifier, ROOT)) {' or 'if (identifier.equals(ROOT)) {'?

@justinmclean justinmclean added improvement Improvements on everything good first issue Good for newcomers labels Feb 9, 2024
@YxAc
Copy link
Contributor

YxAc commented Feb 11, 2024

In most cases, it is better to use the Objects.equals() method. Because the Objects.equals() method is safer when dealing with null values, it can correctly handle cases where both parameters are null without throwing a NullPointerException.

In the LockManager class, logically speaking, identifier should not be null, so I think identifier.equals(ROOT) is sufficient.

What do you think? Can I fix it with this idea? @justinmclean

@justinmclean
Copy link
Member Author

Either is an improvement over the current code; I trust your judgment as to which is better.

@yuqi1129
Copy link
Contributor

@justinmclean @YxAc
I'm afraid we need to think twice about it, if we just use equals and the users create a name identifier that likes '/', the code may get an unexpected result.

@YxAc
Copy link
Contributor

YxAc commented Feb 12, 2024

@justinmclean @YxAc I'm afraid we need to think twice about it, if we just use equals and the users create a name identifier that likes '/', the code may get an unexpected result.

@yuqi1129 , I didn't get what you mean. Can you offer more details? thx

@yuqi1129
Copy link
Contributor

Can you offer more details? thx

What if a user creates a metalake with the name "/", obviously, it equals to the ROOT, but it's not the ROOT.

@YxAc
Copy link
Contributor

YxAc commented Feb 12, 2024

Can you offer more details? thx

What if a user creates a metalake with the name "/", obviously, it equals to the ROOT, but it's not the ROOT.

if a user creates a metalake with the name "/", we expect return the root TreeLock? isn't it?
if using equals, return the root TreeLock exactly, isn't it our expected result? @yuqi1129
i think we expected there is only one root treeLock in most cases. Maybe i was wrong.

@yuqi1129
Copy link
Contributor

yuqi1129 commented Feb 12, 2024

we expect return the root TreeLock?

No, it's not the ROOT node. ROOT stands for the real root of the tree. Currently, we haven't banned users from naming metalake as '/'.

@YxAc
Copy link
Contributor

YxAc commented Feb 12, 2024

we expect return the root TreeLock?

No, it's not the ROOT node. ROOT stands for the real root of the tree. Currently, we haven't banned users from naming metalake as '/'.

if we use ==, it indeed not return the ROOT node, but i thought we should return the ROOT node, otherwise it's confused here.

if ==(reference equality) is deliberately used here to distinguish whether it is the real ROOT node, so this problem can be turned off? @yuqi1129

@yuqi1129
Copy link
Contributor

if ==(reference equality) is deliberately used here to distinguish whether it is the real ROOT node, so this problem can be turned off?

Yes, I use == (reference equality) deliberately, but any improvement and optimization is welcome, maybe we can come up with a better solution here.

@justinmclean
Copy link
Member Author

If there was a good reason for using reference equality and it was deliberately done, then a comment should be added stating why this was done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers improvement Improvements on everything
Projects
None yet
3 participants