-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
NPE in NodeImpl #1153
NPE in NodeImpl #1153
Conversation
WalkthroughThe changes introduce error handling in the Changes
Assessment against linked issues
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
jraft-core/src/main/java/com/alipay/sofa/jraft/storage/impl/LogManagerImpl.java
Outdated
Show resolved
Hide resolved
cc562b6
to
09f1523
Compare
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.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (5)
jraft-core/src/test/java/com/alipay/sofa/jraft/storage/impl/LogManagerTest.java (3)
420-428
: LGTM! Consider adding a test forgetLastLogId(false)
.The test case effectively verifies that
getLastLogId(true)
throws anIllegalStateException
with the correct message when theLogManager
is shut down. This aligns well with the PR objectives of addressing potential NPEs during shutdown.Consider adding an additional assertion to test
getLastLogId(false)
as well, to ensure complete coverage of thegetLastLogId
method:assertThrows(IllegalStateException.class, () -> this.logManager.getLastLogId(false));
430-438
: LGTM! Consider adding a test forgetLastLogIndex(false)
.The test case effectively verifies that
getLastLogIndex(true)
throws anIllegalStateException
with the correct message when theLogManager
is shut down. This aligns well with the PR objectives of addressing potential NPEs during shutdown.Consider adding an additional assertion to test
getLastLogIndex(false)
as well, to ensure complete coverage of thegetLastLogIndex
method:assertThrows(IllegalStateException.class, () -> this.logManager.getLastLogIndex(false));
420-438
: Great addition of shutdown behavior tests!These new test methods effectively address the PR objectives by verifying that
LogManager
throws appropriate exceptions when attempting to access log information during shutdown. This helps prevent potential NPEs in theNodeImpl
class during the stopping phase.To further improve the robustness of the SOFA JRaft project:
- Consider implementing similar shutdown behavior checks in other critical components that might be accessed during the node stopping process.
- Ensure that all public methods in
LogManager
and similar classes have appropriate shutdown checks to maintain consistency across the codebase.- Document the expected behavior during shutdown in the class-level JavaDoc for
LogManager
and related classes to guide future development and prevent regression.jraft-core/src/main/java/com/alipay/sofa/jraft/storage/impl/LogManagerImpl.java (2)
849-853
: Approve the null check and exception handling.The added null check for
c.lastLogId
and the subsequent exception handling address the NPE issue mentioned in the PR objectives. This change improves the robustness of thegetLastLogIndex
method when the node is shutting down.Consider extracting the common null check and exception throwing logic into a private method to reduce code duplication, as this pattern is repeated in the
getLastLogId
method as well. For example:private void checkLastLogIdNotNull(LogId lastLogId) { if (lastLogId == null) { assert stopped : "Last log id can be null only when node is stopping."; throw new IllegalStateException("Node is shutting down"); } }Then you can call this method in both
getLastLogIndex
andgetLastLogId
.
Line range hint
849-906
: Summary of changesThe modifications in both
getLastLogIndex
andgetLastLogId
methods effectively address the NPE issue described in the PR objectives and linked issue #1152. By adding null checks forc.lastLogId
and throwingIllegalStateException
when the node is shutting down, the code now handles the scenario wheregetLastLogId
returns null during the node stopping process.These changes improve the robustness of the
LogManagerImpl
class and prevent potential NPEs in theNodeImpl#handleRequestVoteRequest
method.While the current implementation solves the immediate issue, consider the following architectural improvements for future iterations:
Error handling strategy: Evaluate if throwing an
IllegalStateException
is the best approach for handling this scenario. Consider if returning an optional or a special value might be more appropriate, depending on how callers are expected to handle this situation.Lifecycle management: The need for these checks suggests that the lifecycle management of the
LogManagerImpl
might benefit from a more explicit state machine approach. This could make it clearer when certain operations are valid or invalid based on the current state of the node.Concurrency considerations: Ensure that the
stopped
flag is properly synchronized or made volatile if it's accessed from multiple threads.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
- jraft-core/src/main/java/com/alipay/sofa/jraft/storage/impl/LogManagerImpl.java (2 hunks)
- jraft-core/src/test/java/com/alipay/sofa/jraft/storage/impl/LogManagerTest.java (2 hunks)
🔇 Additional comments (1)
jraft-core/src/main/java/com/alipay/sofa/jraft/storage/impl/LogManagerImpl.java (1)
902-906
: Approve the null check and exception handling.The added null check for
c.lastLogId
and the subsequent exception handling address the NPE issue mentioned in the PR objectives. This change improves the robustness of thegetLastLogId
method when the node is shutting down.As mentioned in the previous comment, consider extracting this common logic into a private method to reduce code duplication.
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.
LGTM
@alievmirza Thank you. |
Motivation:
We want to make sure that
com.alipay.sofa.jraft.storage.impl.LogManagerImpl#getLastLogId
won't returnnull
, that can lead to NPEs when a Node is stopping#1152
Modification:
Describe the idea and modifications you've done.
Result:
Fixes #1152.
Summary by CodeRabbit
New Features
Tests