Skip to content

Conversation

@wlwilliamx
Copy link
Collaborator

@wlwilliamx wlwilliamx commented Jan 8, 2026

What problem does this PR solve?

TiCDC does not support using the same TiDB logical cluster as both the upstream and downstream, because it can easily lead to self-replication loops or unexpected data behaviors.

In TiDB Next-Gen, a physical cluster can contain multiple logical clusters (keyspaces). Different keyspaces should be treated as different clusters from TiCDC's perspective. So this PR keeps rejecting same upstream/downstream for the same keyspace, while allowing cross-keyspace replication within the same physical cluster.

Issue Number: close #3949

What is changed and how it works?

  • Add a pre-check for changefeed create/update/resume to detect “same upstream & downstream” for MySQL-compatible sinks.
    • The downstream TiDB cluster ID is read from mysql.tidb (cluster_id).
    • If downstream is not TiDB or the cluster ID is unavailable, the check is skipped (legacy/compat behavior).
  • Next-Gen compatibility: when the upstream and downstream cluster_id are the same, query the downstream keyspace via:
    • show config where type = 'tidb' and name = 'keyspace-name'
    • Treat (cluster_id, keyspace) as the cluster identity, so cross-keyspace is allowed but same keyspace is rejected.
    • If downstream keyspace cannot be determined, keep the conservative legacy behavior (treat as the same physical cluster and reject) to avoid accidental self-replication.
  • Add unit tests and an integration test case same_upstream_downstream.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test

Questions

Will it cause performance regression or break compatibility?
  • No steady-state overhead: the check only runs when creating/updating/resuming a changefeed.
  • Classic mode keeps the existing behavior (same physical cluster is rejected).
  • Next-Gen allows cross-keyspace replication within the same physical cluster, while still rejecting same keyspace.
Do you need to update user documentation, design documentation or monitoring documentation?

N/A

Release note

 Reject creating/updating/resuming a changefeed when downstream is the same TiDB logical cluster as upstream (keyspace-aware for TiDB Next-Gen).

@ti-chi-bot ti-chi-bot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. do-not-merge/needs-triage-completed labels Jan 8, 2026
@ti-chi-bot
Copy link

ti-chi-bot bot commented Jan 8, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign wlwilliamx for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@gemini-code-assist
Copy link

Summary of Changes

Hello @wlwilliamx, 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 enhances TiCDC's robustness by implementing a critical safeguard against self-replication. It ensures that a TiDB cluster cannot serve as both the data source and the replication target for a changefeed, thereby preventing potential data inconsistencies and operational issues. The change integrates a new cluster identification and comparison logic into the changefeed lifecycle management, providing immediate feedback and preventing misconfigurations.

Highlights

  • Preventing Self-Replication: Introduced a new validation mechanism to prevent a TiDB cluster from being configured as both the upstream source and downstream target for a TiCDC changefeed.
  • Validation Scope: The upstream/downstream cluster identity check is now enforced during changefeed creation, update, and resume operations to ensure data integrity and prevent circular replication.
  • New Cluster Check Utility: A new utility function UpstreamDownstreamNotSame has been added to pkg/check to determine if the upstream and downstream TiDB clusters are distinct by comparing their cluster IDs.
  • Error Handling: A specific error, ErrSameUpstreamDownstream, is now returned when this self-replication condition is detected.
  • Comprehensive Testing: New unit tests for the cluster ID checking logic and an integration test script have been added to validate the new behavior across create, update, and resume scenarios.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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.

@wlwilliamx wlwilliamx marked this pull request as draft January 8, 2026 06:26
@ti-chi-bot ti-chi-bot bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. labels Jan 8, 2026
Copy link

@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 valuable feature to prevent a changefeed from using the same TiDB cluster as both upstream and downstream, which could lead to replication loops. The implementation correctly adds checks at changefeed creation, update, and resume. The core logic for comparing cluster IDs is well-encapsulated in the new pkg/check package and is accompanied by good unit and integration tests.

My main feedback is regarding code duplication in api/v2/changefeed.go, where the check logic is repeated in three different API handlers. Refactoring this into a shared helper function would improve maintainability. I've also included a couple of minor suggestions for improving code clarity and error handling in pkg/check/cluster.go.

Overall, this is a solid contribution that enhances the robustness of TiCDC.

Comment on lines +253 to +264
// Check whether the upstream and downstream are the different cluster.
notSame, err := check.UpstreamDownstreamNotSame(ctx, pdClient, cfConfig)
if err != nil {
_ = c.Error(err)
return
}
if !notSame {
_ = c.Error(errors.ErrSameUpstreamDownstream.GenWithStack(
"TiCDC does not support creating a changefeed with the same TiDB cluster " +
"as both the source and the target for the changefeed."))
return
}

Choose a reason for hiding this comment

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

medium

The logic to check if upstream and downstream clusters are the same is duplicated across CreateChangefeed, ResumeChangefeed (lines 694-705), and UpdateChangefeed (lines 888-899). This makes the code harder to maintain.

To improve this, you could extract the duplicated logic into a helper method on *OpenAPIV2. This method can be parameterized to handle the slightly different error messages for each context (creating, resuming, updating).

Here's an example of what the helper method could look like:

func (h *OpenAPIV2) verifyUpstreamDownstreamNotSame(
	c *gin.Context,
	pdClient pd.Client,
	cfConfig *config.ChangefeedConfig,
	action string,
) bool {
	ctx := c.Request.Context()
	notSame, err := check.UpstreamDownstreamNotSame(ctx, pdClient, cfConfig)
	if err != nil {
		_ = c.Error(err)
		return false
	}
	if !notSame {
		errMsg := fmt.Sprintf(
			"TiCDC does not support %s a changefeed with the same TiDB cluster "+
				"as both the source and the target for the changefeed.", action)
		_ = c.Error(errors.ErrSameUpstreamDownstream.GenWithStack(errMsg))
		return false
	}
	return true
}

Then, you can replace the duplicated blocks with a single call to this helper, for instance in CreateChangefeed:

if !h.verifyUpstreamDownstreamNotSame(c, pdClient, cfConfig, "creating") {
	return
}

This refactoring will make the code more DRY and easier to manage in the future.

Comment on lines +29 to +34
if upPD == nil {
return false, cerrors.New("pd client is nil")
}
if changefeedCfg == nil {
return false, cerrors.New("changefeed config is nil")
}

Choose a reason for hiding this comment

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

medium

For better error handling and consistency, consider using a structured error from the cerrors package instead of cerrors.New(). cerrors.ErrInternalCheckFailed seems appropriate for these nil checks.

For example:

if upPD == nil {
    return false, cerrors.ErrInternalCheckFailed.GenWithStack("pd client is nil")
}
if changefeedCfg == nil {
    return false, cerrors.ErrInternalCheckFailed.GenWithStack("changefeed config is nil")
}

This provides more context to the error and aligns with the error handling patterns used elsewhere in the codebase.

Suggested change
if upPD == nil {
return false, cerrors.New("pd client is nil")
}
if changefeedCfg == nil {
return false, cerrors.New("changefeed config is nil")
}
if upPD == nil {
return false, cerrors.ErrInternalCheckFailed.GenWithStack("pd client is nil")
}
if changefeedCfg == nil {
return false, cerrors.ErrInternalCheckFailed.GenWithStack("changefeed config is nil")
}


_, db, err := newMySQLConfigAndDBFn(ctx, changefeedCfg.ChangefeedID, uri, changefeedCfg)
if err != nil {
return 0, true, cerrors.Trace(err)

Choose a reason for hiding this comment

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

medium

When newMySQLConfigAndDBFn returns an error, you are returning isTiDB as true. This is slightly counter-intuitive, as a connection failure means we cannot determine if the downstream is a TiDB cluster.

While the calling function UpstreamDownstreamNotSame correctly handles the error and ignores the boolean value, returning false for isTiDB in this error case would make the function's contract clearer and less surprising to future readers.

Suggested change
return 0, true, cerrors.Trace(err)
return 0, false, cerrors.Trace(err)

@ti-chi-bot ti-chi-bot bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. do-not-merge/needs-linked-issue do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/needs-triage-completed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. release-note Denotes a PR that will be considered when it comes time to generate release notes. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Jan 14, 2026
@wlwilliamx wlwilliamx marked this pull request as ready for review January 14, 2026 11:47
@ti-chi-bot ti-chi-bot bot removed do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. do-not-merge/needs-linked-issue labels Jan 14, 2026
@wlwilliamx
Copy link
Collaborator Author

CC @tenfyzhong

@wlwilliamx
Copy link
Collaborator Author

CC @asddongmen

// verify sinkURI
cfConfig := info.ToChangefeedConfig()
// Check whether the upstream and downstream are the different cluster.
notSame, err := check.UpstreamDownstreamNotSame(ctx, pdClient, cfConfig)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Confusing Naming and Logic: The function name UpstreamDownstreamNotSame with double negative logic (!notSame) makes the code harder to understand.

Consider renaming to use positive semantics for better readability:

// Rename to IsSameCluster
isSame, err := check.IsSameCluster(ctx, pdClient, cfConfig)
if err != nil {
    _ = c.Error(err)
    return
}
if isSame {
    _ = c.Error(errors.ErrSameUpstreamDownstream.GenWithStack(...))
    return
}

Either naming would be clearer than the current notSame variable combined with !notSame check. The current double negative makes the logic harder to follow.

if !hasRow {
keyspace = value
hasRow = true
continue
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optimization: You can directly break here instead of continue. Within the same TiDB cluster, all instances should report the same keyspace-name value.

Since we're just checking consistency across rows, we could simplify this to:

if !hasRow {
    keyspace = value
    hasRow = true
    break  // We have the first value, no need to continue
}

if err := rows.Err(); err != nil {
return "", cerrors.Trace(err)
}
if !hasRow {
Copy link
Collaborator

@tenfyzhong tenfyzhong Jan 15, 2026

Choose a reason for hiding this comment

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

Classic Architecture Compatibility: When no keyspace-name is found, we should return common.DefaultKeyspaceName instead of returning an error. This ensures compatibility with TiDB Classic architecture.

In TiDB Classic (non-Next-Gen), the SHOW CONFIG command may not return the keyspace-name row at all, or the configuration may not exist. Returning an error here would break the check for Classic deployments.

Suggested fix:

if !hasRow {
    // TiDB Classic doesn't expose keyspace-name, use default
    return common.DefaultKeyspaceNamme, nil
}

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

Labels

release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prevent TiCDC from Using the Same TiDB Cluster as Both Upstream and Downstream

2 participants