sofastack / sofa-jraft

A production-grade java implementation of RAFT consensus algorithm.
https://www.sofastack.tech/projects/sofa-jraft/
Apache License 2.0
3.52k stars 1.12k forks source link

feat: 添加节点参与投票异常监听器 #1100

Open zxuanhong opened 2 months ago

zxuanhong commented 2 months ago

添加节点投票异常监听器,以便为用户提供处理自动加入集群的时机;

Motivation:

  1. 在新节点加入集群或历史节点被移除后再次启动加入集群时,由于缺少触发参与集群投票异常的时机,使得无法做自动加入集群处理。当前pr在新加入集群(参与者不在配置中),或投票提示么有权限(节点被移除后再次启动时);

  2. 对于为什么会出现历史节点移除后再次加入集群。目前出现的原因是,一般系统可能会添加一定心跳时间无法响应时做出自动移除节点(一般是节点下线),后系统再次回复。

    Modification:

  3. 在NodeImpl preVote方法配置检查异常时添加peerNotInConf回调方法,处理当前参与者不在配置中(没有加入);

  4. 在NodeImpl handlePreVoteResponse响应提示没有投票权时添加noVotingGranted回调方法,处理当前参与者虽然在本地配置中,但是不在集群配置中,也就是没有投票权(节点被自动移除然后恢复时)。

Summary by CodeRabbit

coderabbitai[bot] commented 2 months ago

Walkthrough

The updates to the JRaft library introduce a new NodeStateListener interface and associated management methods in the Node class. This enhancement allows users to add, remove, and manage listeners that can execute custom logic in response to specific node state changes, such as pre-vote failures and configuration discrepancies. These changes are primarily implemented in the core node handling classes, enhancing the library's extensibility and monitoring capabilities.

Changes

File Path Change Summary
.../jraft/Node.java Added methods for managing NodeStateListener instances and introduced NodeStateListener interface.
.../jraft/core/NodeImpl.java Implemented listener management methods and integrated state listener triggers in node state handling.

🐇✨ In the land of code where the data nodes play, A new listener sprouts, hip hip hooray! It watches, it triggers, without any delay, On state changes it dances, in JRaft's ballet. So here's to the devs, with their code so bright, May your clusters run smoothly, by day and by night. 🌟🐰


Recent Review Status **Configuration used: CodeRabbit UI**
Commits Files that changed from the base of the PR and between 890033a64d8ed5c8838463f278b940355553e413 and 285715a0bf5405644ccbc687731bc5d15bd19c62.
Files selected for processing (2) * jraft-core/src/main/java/com/alipay/sofa/jraft/Node.java (2 hunks) * jraft-core/src/main/java/com/alipay/sofa/jraft/core/NodeImpl.java (7 hunks)
Additional comments not posted (4)
jraft-core/src/main/java/com/alipay/sofa/jraft/Node.java (2)
`353-371`: Ensure thread safety for `NodeStateListener` management methods.
Verification successful --- The verification process has confirmed that the `NodeStateListener` management methods (`addNodeStateListener`, `removeNodeStateListener`, `clearNodeStateListener`, and `getNodeStateListeners`) utilize a `CopyOnWriteArrayList` for storing listeners. This choice of data structure ensures thread safety for the operations performed on the list of `NodeStateListener` instances, as `CopyOnWriteArrayList` is designed to handle concurrent modifications safely by making a fresh copy of the underlying array upon modification. Thus, the implementation of these methods in the `NodeImpl` class is thread-safe, which aligns with the requirements for concurrent access in a multi-threaded environment like that of SOFAJRaft.
Scripts Executed The following scripts were executed for the analysis: --- Script: ```shell #!/bin/bash # Verify if the methods are synchronized or if the underlying data structure is thread-safe. rg --type java 'NodeStateListener' -C 10 ``` Length of output: 17157
--- `418-438`: Consider adding documentation for the default methods in `NodeStateListener` to explain when they should be called and what the expected actions might be.
jraft-core/src/main/java/com/alipay/sofa/jraft/core/NodeImpl.java (2)
`2681-2683`: Ensure that the `peerNotInConf` method in the `NodeStateListener` interface handles exceptions gracefully to prevent potential disruptions in the node's operations if a listener implementation throws an exception. --- `3458-3478`: The methods for managing `NodeStateListener` instances (add, remove, clear, get) are implemented correctly. Ensure that these methods are thread-safe if `NodeImpl` instances can be accessed by multiple threads concurrently.
Verification successful --- The methods for managing `NodeStateListener` instances in `NodeImpl` are thread-safe as they utilize a `CopyOnWriteArrayList` for `nodeStateListeners`. This ensures that all modifications and access to the list are handled in a thread-safe manner. Therefore, the implementation aligns with the requirements for thread safety in a concurrent environment.
Scripts Executed The following scripts were executed for the analysis: --- Script: ```shell #!/bin/bash # Check for any additional synchronization mechanisms around nodeStateListeners usage across the codebase. rg --type java "nodeStateListeners" ``` Length of output: 1065
--- 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?
Share - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `Generate unit testing code for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit testing code for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai generate interesting stats about this repository and render them as a table.` - `@coderabbitai show all the console.log statements in this repository.` - `@coderabbitai read src/utils.ts and generate unit testing code.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` 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 as PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger a review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai help` to get help. Additionally, you can add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Configration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information. - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json` ### Documentation and Community - Visit our [Documentation](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.