Skip to content

[Bug]: Repeated snapshot initialization duplicates entries and fills the export quota #25889

Closed

Description

@Ariznawlll

Summary

snapshotManager.Init appends every on-disk exported snapshot to the existing in-memory record each time it is called. It does not clear the previous view or deduplicate an equal snapshot index.

As a result, retrying replica initialization makes one physical snapshot appear many times in memory. After 20 retries, one snapshot is counted as 20 snapshots, which reaches the default MaxExportedSnapshot quota.

Affected version

Latest main tested at 862d9d30c2cd7a55995fd0bae0951af0809f5752.

Production path

Both store.startReplica and store.startNonVotingReplica call snapshotMgr.Init(shardID, replicaID) before NodeHost.StartReplica.

A repeated/retried bootstrap command therefore appends the same snapshots before Dragonboat can return ErrShardAlreadyExist. The inflated count is then used by shouldDoExport:

if l.snapshotMgr.Count(shardID, replicaID) >= l.cfg.MaxExportedSnapshot {
    return false
}

This can prematurely stop snapshot export and delay the export/import path used for log truncation and WAL compaction.

Root cause

snapshotManager.Init sorts the directory indexes and calls addLocked for each one, but it never replaces the existing in-memory record. snapshotRecord.add also permits an index equal to the current last index.

Expected behavior

Initialization should be idempotent for a (shardID, replicaID): the in-memory view should reflect the unique on-disk snapshot directories, regardless of how many times initialization is retried.

No matching open issue was found with searches for snapshot initialization idempotency, duplicate snapshot counts, bootstrap retry, and MaxExportedSnapshot quota exhaustion.

Activity

  1. added this to the MO-202607 milestone on Jul 20, 2026
  2. added
    kind/bug
    on Jul 20, 2026
  3. Ariznawlll commented on Jul 20, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 白盒验证

    • 测试基线:main@862d9d30c2cd7a55995fd0bae0951af0809f5752
    • 测试文件:临时白盒测试 pkg/logservice/snapshot_init_audit_test.go
    • 测试目标:验证同一 (shardID, replicaID) 重复初始化时,内存快照视图是否与磁盘唯一目录保持一致。

    测试用例

    TestAuditSnapshotManagerInitIsIdempotent 执行以下步骤:

    1. 使用严格内存文件系统创建一个真实快照目录,索引为 100;目录内包含 snapshot.metadata 和对应的 .gbsnap 文件。
    2. 对同一 shard/replica 连续调用 snapshotManager.Init 20 次;20 是当前默认 MaxExportedSnapshot。
    3. 断言磁盘只有一个快照时,Count 必须始终为 1,不能因重试达到导出配额。

    原始代码测试命令与结果

    普通模式稳定性测试:

    go test ./pkg/logservice \
      -run '^TestAuditSnapshotManagerInitIsIdempotent$' \
      -count=3

    结果:FAIL 3/3。每轮均得到:

    expected: 1
    actual  : 20
    one on-disk snapshot must not fill the export quota after retries
    

    Race 模式:

    go test -race ./pkg/logservice \
      -run '^TestAuditSnapshotManagerInitIsIdempotent$' \
      -count=1

    结果:FAIL 1/1,同样为 expected 1, actual 20;没有检测到 data race,说明这是确定性的状态重复问题,不是测试竞争导致。

    临时修复对照

    临时修改 snapshotManager.Init:在重新扫描磁盘目录后,先释放并移除该 shard/replica 的旧内存记录,再从磁盘重建唯一视图。

    go test ./pkg/logservice \
      -run '^TestAuditSnapshotManagerInitIsIdempotent$' \
      -count=100
    
    go test -race ./pkg/logservice \
      -run '^TestAuditSnapshotManagerInitIsIdempotent$' \
      -count=50

    结果:普通模式 PASS 100/100,race 模式 PASS 50/50。

    结论与清理

    • 最新 main 可以稳定复现:一个磁盘快照被重复计数为 20 个,达到默认导出配额。
    • 临时修复对照证明根因是 Init 追加旧内存状态而非重建/去重。
    • 临时产品代码修复已经回退;仅保留本地审计测试用于复现,没有 commit、push 或 PR。
  4. added theissue type on Jul 20, 2026
  5. added
    severity/s0
    ai-easy
    and removed on Jul 31, 2026
  6. gouhongshen commented on Jul 31, 2026

    @gouhongshen
    Contributor

    Draft PR opened: #26502

    Head: gouhongshen:agent/issue-25889 at 4c08d6076bfb5c311a83ac726251b36acec593a0; base: matrixorigin/matrixone:main. The PR is open and marked Draft, and its description links this issue with Fixes #25889.

  7. added a commit that references this issue on Aug 1, 2026
    8d48028
  8. gouhongshen commented on Aug 2, 2026

    @gouhongshen
    Contributor

    fxied

  9. added and removed
    severity/s0
    on Aug 2, 2026
  10. Ariznawlll commented on Aug 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:b18cdee19bad80a51ea54789a727aa1753fd0b08
    • 部署方式:官方 main checkout 的 Go 单元测试
    • 测试日期:2026-08-03

    原问题验证

    执行 snapshot manager 的初始化、目录扫描及导入/删除回归。普通模式连续执行 3 轮、-race 连续执行 3 轮,均 exit 0。

    Happy Path 覆盖

    1. 从磁盘快照目录初始化后,内存视图与磁盘索引一致。
    2. 同一 shard/replica 重复 Init 时,旧内存视图被替换而非追加,避免同一物理快照重复计数并提前耗尽 export quota。
    3. Add、Remove 和 import-snapshot 评估路径的快照记录及计数均正确。

    Unhappy Path 覆盖

    1. 非法 snapshot item/目录输入被拒绝,不写入有效快照记录。
    2. 重复初始化、删除后重新初始化均不会残留旧记录;-race 未报告数据竞争。

    白盒验证

    • snapshot_test.go
    • go test ./pkg/logservice -run '^(TestSnapshotItemValid|TestSnapshotItemInValid|TestInitSnapshotMgr|TestInitSnapshotMgrReplacesInMemoryView|TestAddSnapshot|TestRemoveSnapshot|TestEvalImportSnapshot)$' -count=3 -v
    • 对同一集合执行 go test -race ... -count=3

    回归测试

    该问题属于 logservice 的磁盘目录与内存索引同步,不具备稳定 SQL 黑盒入口;回归保留在 pkg/logservice Go UT,不加入 motr。

    测试证据

    上述命令于 2026-08-03 在 commit b18cdee19bad80a51ea54789a727aa1753fd0b08 执行,普通模式和 race 模式均为 exit 0。

    测试结论

    测试通过。

  11. added 3 commits that reference this issue on Sep 17, 2026
    765a463
    71b9e81
    54e3461
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-easykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions