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

fix: raft join failed #181 #188

Open
wants to merge 3 commits into
base: unstable
Choose a base branch
from
Open

fix: raft join failed #181 #188

wants to merge 3 commits into from

Conversation

marsevilspirit
Copy link
Collaborator

@marsevilspirit marsevilspirit commented Feb 1, 2025

Fixed the issue of raft join failure.
#181

Summary by CodeRabbit

  • Refactor

    • Streamlined error handling in cluster operations to ensure consistent responses.
    • Improved TCP connection event management for enhanced reliability.
    • Simplified method implementations for better code clarity.
  • Documentation

    • Updated internal comments to clarify command targeting and periodic replication checks.
  • Chores

    • Removed unused dependencies and optimized internal data type definitions for better efficiency.

Co-authored-by: LHG41278 <[email protected]>
Co-authored-by: marsevilspirit <[email protected]>
Copy link

coderabbitai bot commented Feb 1, 2025

Walkthrough

The pull request includes several code cleanups and minor adjustments to control flow across multiple files. Unused include directives have been removed, and comments have been added for clarity. Error handling in RAFT-related commands has been reorganized to ensure responses are set before returning. The handling of TCP connections has been enhanced with an additional conditional event trigger. Furthermore, enumeration types in both the RAFT and replication modules have been updated to explicitly use int8_t, and simplifications in method return values and argument types have been made.

Changes

File(s) Change Summary
src/client.cc Removed unused include directives (env.h, slow_log.h, std/std_string.h) and added a comment clarifying the function of isClusterCmdTarget.
src/cmd_raft.cc Reordered error handling by ensuring responses are set before return statements in several methods (e.g., DoCmdRemove, DoCmd, DoCmdInit, DoCmdJoin).
src/net/thread_manager.h Enhanced TCP connection handling in DoTCPConnect by adding a check for write_thread_ and triggering an EVENT_NULL event when appropriate.
src/raft/raft.cc, src/raft/raft.h Simplified return values (e.g., using "" instead of std::string()), reduced type casting in AppendArrayLen calls, removed unused includes (braft/snapshot.h, <filesystem>, <tuple>), and updated the ClusterCmdType enum to use int8_t.
src/raft/snapshot.cc Removed the include directive for "config.h".
src/replication.cc, src/replication.h Added a comment block in PReplication::Cron to explain periodic PINGs and updated PSlaveState and PReplState enums to explicitly use int8_t.

Sequence Diagram(s)

sequenceDiagram
    participant TM as ThreadManager
    participant WT as WriteThread
    participant RT as ReadThread

    TM->>TM: Initiate TCP connection (DoTCPConnect)
    alt write_thread_ exists
        TM->>WT: Add EVENT_NULL to write thread
    end
    TM->>RT: Add event to read thread
Loading
sequenceDiagram
    participant CMD as RaftCommand Handler
    participant RES as Response Setter
    participant CL as Client

    CMD->>RES: Set error response (e.g., "Already cluster member")
    CMD-->>CL: Return after setting response
Loading

Possibly related PRs

Suggested reviewers

  • dingxiaoshuai123
  • luky116
  • AlexStocks

Poem

I'm a rabbit in the code garden, hopping through each line,
Clearing clutter from includes, making our code shine.
Comments and clean flows make my heart leap,
With each tidy refactor, joy runs deep.
Hopping along, nibbling bugs away—oh, what a day!
🥕🐇 Happy coding in our burrow of bytes!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 61dac94 and 909ea7b.

📒 Files selected for processing (5)
  • src/net/thread_manager.h (1 hunks)
  • src/raft/raft.cc (4 hunks)
  • src/raft/raft.h (1 hunks)
  • src/raft/snapshot.cc (0 hunks)
  • src/replication.cc (1 hunks)
💤 Files with no reviewable changes (1)
  • src/raft/snapshot.cc
🚧 Files skipped from review as they are similar to previous changes (4)
  • src/raft/raft.h
  • src/replication.cc
  • src/net/thread_manager.h
  • src/raft/raft.cc
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: build_on_ubuntu
  • GitHub Check: build_on_macos

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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 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 for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Collaborator

@FinnTew FinnTew left a comment

Choose a reason for hiding this comment

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

LGTM,同步修改到我本地后运行以下测试:

	for i, f := range followers {
		res, err := f.Do(ctx, "RAFT.CLUSTER", "JOIN", "127.0.0.1:12111").Result()
		if err != nil {
			fmt.Printf("[Consistency]RAFT.CLUSTER Follower %d JOIN fail: %v\n", i, err)
			continue
		}
		msg, ok := res.(string)
		fmt.Printf("[Consistency]RAFT.CLUSTER Follower %d: %v, %v\n", i, ok, msg)
		err = f.Close()
		if err != nil {
			fmt.Println("[Consistency]RAFT.CLUSTER CLOSE fail.", err.Error())
			return
		}
	}

得到如下结果:

image

src/cmd_raft.cc Outdated Show resolved Hide resolved
@@ -354,6 +354,9 @@ uint64_t ThreadManager<T>::DoTCPConnect(T &t, int fd, const std::shared_ptr<Conn
}

readThread_->AddNewEvent(connId, fd, BaseEvent::EVENT_READ);
if (writeThread_) {
writeThread_->AddNewEvent(connId, fd, BaseEvent::EVENT_NULL); // add null event to write_thread epoll
Copy link
Collaborator

Choose a reason for hiding this comment

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

想问一下向写线程添加空事件的目的是啥

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

raft join failed是因为当读写线程分离时,尝试通过写线程output,结果发现写事件里没有对应的Id,导致write错误,真正解决问题的就是这里

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants