Skip to content

[Bug]: Data Branch DAG construction does not stop cyclic parent metadata #26205

Closed

Description

@Ariznawlll

Description

databranchutils.NewDAG calculates every node depth recursively, but the depth walk does not track nodes already visited on the current parent chain. A two-row circular parent relation therefore does not return from DAG construction.

The same file already applies a bounded parent walk in PathFromAncestor, but the construction-time depth calculation does not use an equivalent check.

Tested at origin/main commit 3f3cdcdda64fddb93b6668a301ff38d41e73fb76.

Reproduction

Run this minimal program three times in separate processes:

databranchutils.NewDAG([]databranchutils.DataBranchMetadata{
    {TableID: 101, PTableID: 102},
    {TableID: 102, PTableID: 101},
})

Each run exits with status 2 after repeating calculateDepth(101) and calculateDepth(102).

Actual result

NewDAG does not finish normally when the metadata parent chain loops.

Expected result

The DAG constructor should detect the repeated node in the current depth walk and return or construct a bounded invalid-DAG representation instead of recursively walking forever.

Code path

  • pkg/frontend/databranchutils/branch_dag.go: NewDAG
  • pkg/frontend/databranchutils/branch_dag.go: DataBranchDAG.calculateDepth

Related closed report

This is a fresh report because closed issue #26083 describes the same path, while the behavior remains present at the commit above.

Activity

  1. added this to the MO-202607 milestone on Jul 27, 2026
  2. added
    kind/bug
    on Jul 27, 2026
  3. added theissue type on Jul 27, 2026
  4. added
    severity/s0
    and removed on Jul 27, 2026
  5. VioletQwQ-0 commented on Jul 28, 2026

    @VioletQwQ-0
    Collaborator

    This was already fixed by #26139, merged as 7b4c81a. The reported origin/main commit 3f3cdcd predates that fix.

    Verified again against the current main implementation at 6004bec:

    • TestNewDAGCycle passed under -race -count=3.
    • The complete pkg/frontend/databranchutils package passed under -race.
    • Coverage includes a two-node cycle, a self-cycle, a non-cyclic descendant of a cycle, and a 4,096-node acyclic chain.

    calculateDepth now uses an iterative parent walk, detaches only repeated cycle members, and preserves valid descendant edges. Closing this duplicate as completed.

  6. added
    no-pr-linked
    on Jul 28, 2026
  7. Ariznawlll commented on Jul 29, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在最新 main 分支完成验证。

    测试版本:52ea9f874f492c368a20efef16b46ab8ced2de37(fix: support cyclic foreign keys in database branch (#26292))

    测试过程:

    1. 执行 Data Branch DAG 循环依赖专项测试,覆盖两节点循环、自循环、循环节点下仍存在合法子节点,以及长链无环依赖。

    2. 普通模式连续执行 3 轮:

      go test ./pkg/frontend/databranchutils \
        -run 'Test(NewDAGCycle|DAGFunctionality)$' -count=3
      
    3. 使用 race detector 再连续执行 3 轮:

      go test -race ./pkg/frontend/databranchutils \
        -run '^TestNewDAGCycle$' -count=3
      

    测试结果:

    • 所有循环依赖均能终止处理,没有出现无限递归、hang 或 panic。
    • 循环节点能够被正确识别,不会错误删除循环节点之外的合法子依赖。
    • 长无环链仍能正常计算深度。
    • 普通测试和 -race 测试全部通过,未发现数据竞争。

    结论:该问题在上述最新 main commit 上验证通过。

  8. Ariznawlll commented on Jul 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在 main@0ebba1c0c96bbcf0ff4091236e10df7b97e24a0c 补充完成黑盒和白盒验证。

    白盒验证原始缺陷:

    go test ./pkg/frontend/databranchutils \
      -run '^TestNewDAGCycle$' -count=1
    ok github.com/matrixorigin/matrixone/pkg/frontend/databranchutils
    

    该 UT 在子进程中构造循环 parent metadata,验证 NewDAG 能有界返回、循环节点被安全断开,且不会递归至 stack overflow。

    黑盒验证合法 happy path:

    • MOTR 用例:issue_26205_data_branch_deep_lineage.test
    • 仅使用公开 SQL 创建 16 层合法 branch lineage,不直接篡改系统表。
    • 在多层执行 UPDATE、INSERT、DELETE,并创建 sibling branch。
    • 执行 ancestor/sibling DIFF 和 MERGE,检查 summary、最终精确行集以及 16 个 active lineage node。

    结果:该 case 38/38 条 SQL/断言通过;本轮 Data Branch 全集 13/13 case、280/280 条 SQL/断言通过。

    Unhappy path / 防护边界:

    • 白盒直接构造 101 → 102 → 101 的非法 parent cycle,要求 NewDAG 有界返回,不能无限递归或 stack overflow。
    • 循环节点必须从 parent cycle 安全断开,并且不能错误报告公共祖先。
    • 黑盒使用 16 层合法 DAG 作反向控制,防止 cycle protection 误伤深层合法 lineage。
    • 深层路径同时执行 sibling/ancestor DIFF 和 MERGE,检查修复没有退化正常遍历结果。

    结论:循环 catalog metadata 的内部失败路径由白盒 UT 覆盖,深层合法 DAG 的公开路径由 MOTR 黑盒覆盖;回归已加入 motr#69。

  9. Ariznawlll commented on Jul 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    Unhappy Path / 循环 DAG 专项验证

    测试版本:main@0ebba1c0c96bbcf0ff4091236e10df7b97e24a0c

    白盒直接构造非法 metadata:

    101 → 102 → 101
    

    验证要求:

    1. NewDAG 必须有界返回,不能无限递归或 stack overflow。
    2. 循环节点必须从 parent cycle 安全断开。
    3. 已断开的循环节点不能错误报告公共祖先。
    TestNewDAGCycle
    ok github.com/matrixorigin/matrixone/pkg/frontend/databranchutils
    

    反向 happy-path 控制:

    • 使用公开 SQL 创建 16 层合法 lineage;
    • 执行 sibling/ancestor DIFF 和 MERGE;
    • 检查合法深层 DAG 不会被 cycle protection 误伤。

    结果:非法 cycle 白盒测试通过;合法 16 层黑盒用例 38/38 条 SQL/断言通过。回归见 motr#69。

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

Metadata

Metadata

Assignees

Labels

kind/bugno-pr-linkedphase/testingseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions