Skip to content

ovsdb: exit clustered ovsdb server if multiple raft leaders found#6065

Merged
zhangzujian merged 2 commits intokubeovn:masterfrom
zhangzujian:leader-check
Dec 18, 2025
Merged

ovsdb: exit clustered ovsdb server if multiple raft leaders found#6065
zhangzujian merged 2 commits intokubeovn:masterfrom
zhangzujian:leader-check

Conversation

@zhangzujian
Copy link
Copy Markdown
Member

Pull Request

What type of this PR

Examples of user facing changes:

  • Features
  • Bug fixes
  • Docs
  • Tests

Which issue(s) this PR fixes

Fixes #(issue-number)

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @zhangzujian, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the stability of OVN deployments by implementing a robust mechanism to detect and recover from split-brain scenarios in clustered OVSDB. By enabling the kube-ovn-leader-checker to identify multiple active Raft leaders across the cluster and trigger a restart, it ensures that the OVN control plane maintains a single, consistent source of truth, thereby preventing potential network inconsistencies and improving overall system resilience.

Highlights

  • Split-Brain Detection: The kube-ovn-leader-checker now actively monitors for multiple Raft leaders in clustered OVSDB (Northbound and Southbound) and OVN-IC databases.
  • Graceful Exit on Conflict: If a local OVSDB server detects another OVSDB server acting as a leader for the same database, it will log a fatal error and exit, prompting a pod restart to resolve the split-brain condition.
  • Centralized OVSDB Client Utilities: Introduced new helper functions OvsdbServerAddress and Query in pkg/ovs/ovsdb-client.go to standardize and simplify OVSDB client interactions, leveraging libovsdb.
  • Configuration for Remote Addresses: The kube-ovn-leader-checker now accepts a --remoteAddresses argument, allowing it to probe other OVSDB instances in the cluster.
  • Refactored Leader Check Logic: The isDBLeader function in pkg/ovn_leader_checker/ovn.go has been updated to use the new ovs.Query utility, improving its robustness and readability.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a critical stability improvement by detecting and handling split-brain scenarios in the clustered OVSDB setup. When multiple raft leaders are found, the kube-ovn-leader-checker process will now exit to allow for a clean restart. The implementation also includes a valuable refactoring that centralizes the ovsdb-client execution logic into a new ovs.Query helper function, which enhances code quality and maintainability across the ovn_leader_checker and pinger packages. My feedback focuses on further improving maintainability by using constants for hardcoded values, ensuring test stability, and clarifying code comments.

Comment on lines +176 to +179
case "OVN_IC_Northbound":
dbAddr = ovs.OvsdbServerAddress(address, intstr.FromInt32(6645))
case "OVN_IC_Southbound":
dbAddr = ovs.OvsdbServerAddress(address, intstr.FromInt32(6646))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The database names "OVN_IC_Northbound" and "OVN_IC_Southbound" are hardcoded. To improve maintainability and avoid typos, it's recommended to define them as constants, similar to ovnnb.DatabaseName and ovnsb.DatabaseName. These constants could be placed in pkg/util/const.go or another appropriate shared package.

For example, in pkg/util/const.go:

const (
    // ...
    OvnICNB = "OVN_IC_Northbound"
    OvnICSB = "OVN_IC_Southbound"
)

Then you could use util.OvnICNB and util.OvnICSB here and in other places where these strings are used.

}

output, err := exec.Command("ovsdb-client", cmd...).CombinedOutput() // #nosec G204
result, err := ovs.Query(dbAddr, serverdb.DatabaseName, 1, ovsdb.Operation{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The timeout for ovs.Query is hardcoded to 1 second. This might be too short for a loaded system, potentially causing false negatives. It's better to define this as a constant and consider a slightly larger value, for example, 3 or 5 seconds, to improve robustness.

For example, you could add a constant:

const (
    // ...
    DBLeaderCheckTimeout = 3
)

And use it in the function call.

Suggested change
result, err := ovs.Query(dbAddr, serverdb.DatabaseName, 1, ovsdb.Operation{
result, err := ovs.Query(dbAddr, serverdb.DatabaseName, 3, ovsdb.Operation{


args := []string{"--timeout", strconv.Itoa(timeout), "query", address, string(query)}
if strings.HasPrefix(address, "ssl:") {
args = slices.Insert(args, 0, "-p", "/var/run/tls/key", "-c", "/var/run/tls/cert", "-C", "/var/run/tls/cacert")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The SSL certificate paths (/var/run/tls/key, /var/run/tls/cert, /var/run/tls/cacert) are hardcoded. It would be better to define these as constants in a central place (e.g., pkg/util/const.go) to improve maintainability and consistency across the codebase.

@coveralls
Copy link
Copy Markdown

coveralls commented Dec 17, 2025

Pull Request Test Coverage Report for Build 20329458233

Details

  • 6 of 107 (5.61%) changed or added relevant lines in 3 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage remained the same at 22.355%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pkg/pinger/ovn.go 0 7 0.0%
pkg/ovs/ovsdb-client.go 6 25 24.0%
pkg/ovn_leader_checker/ovn.go 0 75 0.0%
Files with Coverage Reduction New Missed Lines %
pkg/ovn_leader_checker/ovn.go 1 0.0%
Totals Coverage Status
Change from base Build 20323768490: 0.0%
Covered Lines: 11788
Relevant Lines: 52732

💛 - Coveralls

@zhangzujian
Copy link
Copy Markdown
Member Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a crucial high-availability feature to detect and handle split-brain scenarios in a clustered OVSDB setup. When multiple raft leaders are found, the ovsdb-server process will now exit, allowing for a restart and potential recovery. This is implemented by passing remote node addresses to kube-ovn-leader-checker and having it query the leader status on other nodes.

The changes also include a significant and very welcome refactoring of how OVSDB is queried. The logic is now centralized in a new pkg/ovs/ovsdb-client.go file, which wraps ovsdb-client calls. This greatly improves code clarity, maintainability, and robustness.

However, I've found a critical compilation error due to the use of a non-standard function slices.Values. I've also included a medium-severity suggestion to improve the consistency of the Configuration struct. Please see the detailed comments.

}
}

for addr := range slices.Values(cfg.remoteAddresses) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

critical

The function slices.Values is not part of the standard Go slices package and will cause a compilation error. To iterate over the slice of remote addresses, you should use a standard for...range loop. The range over func feature is experimental in Go 1.22 and slices.Values is not a standard iterator function.

Suggested change
for addr := range slices.Values(cfg.remoteAddresses) {
for _, addr := range cfg.remoteAddresses {

}
}

for addr := range slices.Values(cfg.remoteAddresses) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

critical

Similar to the loop above, slices.Values is not a valid function and will cause a compilation error. Please replace it with a standard for...range loop to iterate over the slice.

Suggested change
for addr := range slices.Values(cfg.remoteAddresses) {
for _, addr := range cfg.remoteAddresses {

Comment on lines +58 to +59
localAddress string
remoteAddresses []string
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The new fields localAddress and remoteAddresses are unexported, which is inconsistent with other fields in the Configuration struct. It's a good practice to export fields of configuration structs for consistency and reusability. Please consider making them LocalAddress and RemoteAddresses and updating their usage accordingly in ParseFlags.

Suggested change
localAddress string
remoteAddresses []string
LocalAddress string
RemoteAddresses []string

@zhangzujian zhangzujian marked this pull request as ready for review December 17, 2025 09:32
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Dec 17, 2025
@dosubot dosubot bot added bug Something isn't working go Pull requests that update Go code labels Dec 17, 2025
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Dec 17, 2025
Signed-off-by: zhangzujian <zhangzujian.7@gmail.com>
Signed-off-by: zhangzujian <zhangzujian.7@gmail.com>
@zhangzujian zhangzujian force-pushed the leader-check branch 2 times, most recently from 04b4f1f to 743c5cc Compare December 18, 2025 07:31
@zhangzujian zhangzujian merged commit 14de682 into kubeovn:master Dec 18, 2025
74 of 75 checks passed
@zhangzujian zhangzujian deleted the leader-check branch December 18, 2025 08:26
zhangzujian added a commit that referenced this pull request Dec 18, 2025
)

Signed-off-by: zhangzujian <zhangzujian.7@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update Go code lgtm This PR has been approved by a maintainer need backport size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants