Skip to content

fix(logservice): make snapshot initialization idempotent - #26502

Merged
XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-25889
Aug 1, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-25889

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #25889.

What this PR does / why we need it:

Root cause

snapshotManager.Init loaded sorted on-disk snapshot directories by appending them to the existing in-memory snapshotRecord. Retrying replica startup therefore counted the same physical snapshot repeatedly and could fill MaxExportedSnapshot, suppressing later exports.

There was a second interleaving in the export path: Dragonboat can finish writing a snapshot directory before snapshotManager.Add runs. If a retrying replica start calls Init in that window, Init loads the new index and the delayed Add appended it again because snapshotRecord.add accepted an index equal to the current latest index.

Changes

  • Rebuild each (shardID, replicaID) record from the current validated disk view and replace the old record atomically under the snapshot-manager lock.
  • Return replaced snapshotItem objects to their pool after they become inaccessible.
  • Treat adding the already-recorded latest index as an idempotent success, preserving the successful export flow without duplicating quota entries.
  • Add focused coverage for 20 repeated initializations, the Init then delayed equal-index Add ordering, changed disk contents, sorted reconstruction, and an empty disk view.

No public API, configuration default, or on-disk format changes.

Validation

Pre-fix, TestInitSnapshotMgrReplacesInMemoryView failed deterministically: the second initialization increased a two-directory view to three items, and a later disk refresh retained a removed directory and grew the view to five items.

Post-fix on main@2902d56d2:

  • .agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=120s -run '^TestInitSnapshotMgr(ReplacesInMemoryView)?$' ./pkg/logservice
  • Race test selection was enumerated with -list and contained TestAddSnapshot, TestInitSnapshotMgr, and TestInitSnapshotMgrReplacesInMemoryView.
  • Each selected test measured T=0s in go test -race -json output. With a 30-second stress budget, the adaptive rule selected N=100; all three exact-test -race -count=100 runs passed.
  • .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=10m -skip '^TestNewServiceClosesStoreOnReplicaStartFailure$' ./pkg/logservice (PASS, 128.778s)
  • .agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=15m -skip '^TestNewServiceClosesStoreOnReplicaStartFailure$' ./pkg/logservice (PASS, 133.148s)
  • CGo-configured go build -mod=readonly ./pkg/logservice
  • CGo-configured go vet -mod=readonly ./pkg/logservice
  • git diff --check origin/main...HEAD

Residual risks / test baseline

The complete owning-package command without -skip has one unrelated current-main failure: TestNewServiceClosesStoreOnReplicaStartFailure expects duplicate metadata to reach Dragonboat and return ErrShardAlreadyExist, while the newer zombie-replica membership check skips both starts. The identical focused command fails on clean HEAD with no changes applied. This PR does not modify that unrelated service-start test or behavior.

No residual correctness risk was identified in the changed snapshot-manager paths. Initialization is serialized with snapshot mutations so a concurrent completed export cannot be lost during record replacement; equal-index Add reconciles the directory-written-before-Add ordering.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH left a comment

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.

Deep review completed on head 6961ff1. No blocking findings.

The fix closes both duplication paths: repeated Init now atomically replaces the per-replica disk-derived view, and a delayed Add of the already-loaded latest index is idempotent. All manager readers/mutators are serialized by the same RWMutex; snapshotItem pointers do not escape the lock, so releasing the replaced record back to the pool is safe.

Q1 failure: Stat/List failures leave the published record untouched. Q2 success: the new record is published once and the inaccessible old items are released once. Q3 concurrency: both directory-before-Add and directory-after-Init-list interleavings converge without duplicate or lost entries.

Performance impact is confined to low-frequency replica initialization: O(number of on-disk snapshots) rebuild/release, with no truncation hot-path regression; duplicate Add is cheaper.

Validation performed locally:

  • focused snapshot regression under race, count=50
  • forced Init/list vs export/Add interleaving under race, count=100
  • full ./pkg/logservice under race, PASS (131.7s)
  • clean diff/check and latest-main merge compatibility

The red Ubuntu UT job contains broad unrelated lease/checkpoint/embedded-cluster failures; the owning package passes independently.

@XuPeng-SH
XuPeng-SH merged commit 8d48028 into matrixorigin:main Aug 1, 2026
12 of 16 checks passed
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…n#26502)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Fixes matrixorigin#25889.

## What this PR does / why we need it:

### Root cause

`snapshotManager.Init` loaded sorted on-disk snapshot directories by
appending them to the existing in-memory `snapshotRecord`. Retrying
replica startup therefore counted the same physical snapshot repeatedly
and could fill `MaxExportedSnapshot`, suppressing later exports.

There was a second interleaving in the export path: Dragonboat can
finish writing a snapshot directory before `snapshotManager.Add` runs.
If a retrying replica start calls `Init` in that window, `Init` loads
the new index and the delayed `Add` appended it again because
`snapshotRecord.add` accepted an index equal to the current latest
index.

### Changes

- Rebuild each `(shardID, replicaID)` record from the current validated
disk view and replace the old record atomically under the
snapshot-manager lock.
- Return replaced `snapshotItem` objects to their pool after they become
inaccessible.
- Treat adding the already-recorded latest index as an idempotent
success, preserving the successful export flow without duplicating quota
entries.
- Add focused coverage for 20 repeated initializations, the `Init` then
delayed equal-index `Add` ordering, changed disk contents, sorted
reconstruction, and an empty disk view.

No public API, configuration default, or on-disk format changes.

### Validation

Pre-fix, `TestInitSnapshotMgrReplacesInMemoryView` failed
deterministically: the second initialization increased a two-directory
view to three items, and a later disk refresh retained a removed
directory and grew the view to five items.

Post-fix on `main@2902d56`:

- `.agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=120s
-run '^TestInitSnapshotMgr(ReplacesInMemoryView)?$' ./pkg/logservice`
- Race test selection was enumerated with `-list` and contained
`TestAddSnapshot`, `TestInitSnapshotMgr`, and
`TestInitSnapshotMgrReplacesInMemoryView`.
- Each selected test measured `T=0s` in `go test -race -json` output.
With a 30-second stress budget, the adaptive rule selected `N=100`; all
three exact-test `-race -count=100` runs passed.
- `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=10m -skip
'^TestNewServiceClosesStoreOnReplicaStartFailure$' ./pkg/logservice`
(PASS, 128.778s)
- `.agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=15m
-skip '^TestNewServiceClosesStoreOnReplicaStartFailure$'
./pkg/logservice` (PASS, 133.148s)
- CGo-configured `go build -mod=readonly ./pkg/logservice`
- CGo-configured `go vet -mod=readonly ./pkg/logservice`
- `git diff --check origin/main...HEAD`

### Residual risks / test baseline

The complete owning-package command without `-skip` has one unrelated
current-`main` failure: `TestNewServiceClosesStoreOnReplicaStartFailure`
expects duplicate metadata to reach Dragonboat and return
`ErrShardAlreadyExist`, while the newer zombie-replica membership check
skips both starts. The identical focused command fails on clean `HEAD`
with no changes applied. This PR does not modify that unrelated
service-start test or behavior.

No residual correctness risk was identified in the changed
snapshot-manager paths. Initialization is serialized with snapshot
mutations so a concurrent completed export cannot be lost during record
replacement; equal-index `Add` reconciles the
directory-written-before-Add ordering.

Co-authored-by: XuPeng-SH <[email protected]>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…n#26502)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Fixes matrixorigin#25889.

## What this PR does / why we need it:

### Root cause

`snapshotManager.Init` loaded sorted on-disk snapshot directories by
appending them to the existing in-memory `snapshotRecord`. Retrying
replica startup therefore counted the same physical snapshot repeatedly
and could fill `MaxExportedSnapshot`, suppressing later exports.

There was a second interleaving in the export path: Dragonboat can
finish writing a snapshot directory before `snapshotManager.Add` runs.
If a retrying replica start calls `Init` in that window, `Init` loads
the new index and the delayed `Add` appended it again because
`snapshotRecord.add` accepted an index equal to the current latest
index.

### Changes

- Rebuild each `(shardID, replicaID)` record from the current validated
disk view and replace the old record atomically under the
snapshot-manager lock.
- Return replaced `snapshotItem` objects to their pool after they become
inaccessible.
- Treat adding the already-recorded latest index as an idempotent
success, preserving the successful export flow without duplicating quota
entries.
- Add focused coverage for 20 repeated initializations, the `Init` then
delayed equal-index `Add` ordering, changed disk contents, sorted
reconstruction, and an empty disk view.

No public API, configuration default, or on-disk format changes.

### Validation

Pre-fix, `TestInitSnapshotMgrReplacesInMemoryView` failed
deterministically: the second initialization increased a two-directory
view to three items, and a later disk refresh retained a removed
directory and grew the view to five items.

Post-fix on `main@2902d56`:

- `.agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=120s
-run '^TestInitSnapshotMgr(ReplacesInMemoryView)?$' ./pkg/logservice`
- Race test selection was enumerated with `-list` and contained
`TestAddSnapshot`, `TestInitSnapshotMgr`, and
`TestInitSnapshotMgrReplacesInMemoryView`.
- Each selected test measured `T=0s` in `go test -race -json` output.
With a 30-second stress budget, the adaptive rule selected `N=100`; all
three exact-test `-race -count=100` runs passed.
- `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=10m -skip
'^TestNewServiceClosesStoreOnReplicaStartFailure$' ./pkg/logservice`
(PASS, 128.778s)
- `.agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=15m
-skip '^TestNewServiceClosesStoreOnReplicaStartFailure$'
./pkg/logservice` (PASS, 133.148s)
- CGo-configured `go build -mod=readonly ./pkg/logservice`
- CGo-configured `go vet -mod=readonly ./pkg/logservice`
- `git diff --check origin/main...HEAD`

### Residual risks / test baseline

The complete owning-package command without `-skip` has one unrelated
current-`main` failure: `TestNewServiceClosesStoreOnReplicaStartFailure`
expects duplicate metadata to reach Dragonboat and return
`ErrShardAlreadyExist`, while the newer zombie-replica membership check
skips both starts. The identical focused command fails on clean `HEAD`
with no changes applied. This PR does not modify that unrelated
service-start test or behavior.

No residual correctness risk was identified in the changed
snapshot-manager paths. Initialization is serialized with snapshot
mutations so a concurrent completed export cannot be lost during record
replacement; equal-index `Add` reconciles the
directory-written-before-Add ordering.

Co-authored-by: XuPeng-SH <[email protected]>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…n#26502)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Fixes matrixorigin#25889.

## What this PR does / why we need it:

### Root cause

`snapshotManager.Init` loaded sorted on-disk snapshot directories by
appending them to the existing in-memory `snapshotRecord`. Retrying
replica startup therefore counted the same physical snapshot repeatedly
and could fill `MaxExportedSnapshot`, suppressing later exports.

There was a second interleaving in the export path: Dragonboat can
finish writing a snapshot directory before `snapshotManager.Add` runs.
If a retrying replica start calls `Init` in that window, `Init` loads
the new index and the delayed `Add` appended it again because
`snapshotRecord.add` accepted an index equal to the current latest
index.

### Changes

- Rebuild each `(shardID, replicaID)` record from the current validated
disk view and replace the old record atomically under the
snapshot-manager lock.
- Return replaced `snapshotItem` objects to their pool after they become
inaccessible.
- Treat adding the already-recorded latest index as an idempotent
success, preserving the successful export flow without duplicating quota
entries.
- Add focused coverage for 20 repeated initializations, the `Init` then
delayed equal-index `Add` ordering, changed disk contents, sorted
reconstruction, and an empty disk view.

No public API, configuration default, or on-disk format changes.

### Validation

Pre-fix, `TestInitSnapshotMgrReplacesInMemoryView` failed
deterministically: the second initialization increased a two-directory
view to three items, and a later disk refresh retained a removed
directory and grew the view to five items.

Post-fix on `main@2902d56`:

- `.agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=120s
-run '^TestInitSnapshotMgr(ReplacesInMemoryView)?$' ./pkg/logservice`
- Race test selection was enumerated with `-list` and contained
`TestAddSnapshot`, `TestInitSnapshotMgr`, and
`TestInitSnapshotMgrReplacesInMemoryView`.
- Each selected test measured `T=0s` in `go test -race -json` output.
With a 30-second stress budget, the adaptive rule selected `N=100`; all
three exact-test `-race -count=100` runs passed.
- `.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=10m -skip
'^TestNewServiceClosesStoreOnReplicaStartFailure$' ./pkg/logservice`
(PASS, 128.778s)
- `.agents/skills/mo-dev/scripts/mo-cgo-test -race -count=1 -timeout=15m
-skip '^TestNewServiceClosesStoreOnReplicaStartFailure$'
./pkg/logservice` (PASS, 133.148s)
- CGo-configured `go build -mod=readonly ./pkg/logservice`
- CGo-configured `go vet -mod=readonly ./pkg/logservice`
- `git diff --check origin/main...HEAD`

### Residual risks / test baseline

The complete owning-package command without `-skip` has one unrelated
current-`main` failure: `TestNewServiceClosesStoreOnReplicaStartFailure`
expects duplicate metadata to reach Dragonboat and return
`ErrShardAlreadyExist`, while the newer zombie-replica membership check
skips both starts. The identical focused command fails on clean `HEAD`
with no changes applied. This PR does not modify that unrelated
service-start test or behavior.

No residual correctness risk was identified in the changed
snapshot-manager paths. Initialization is serialized with snapshot
mutations so a concurrent completed export cannot be lost during record
replacement; equal-index `Add` reconciles the
directory-written-before-Add ordering.

Co-authored-by: XuPeng-SH <[email protected]>

This branch was previously deployed

1 inactive deployment
ci — d35ffeef Deployed Aug 1, 2026 by XuPeng-SH via Matrixone Utils CI / Coverage #43951
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/S Denotes a PR that changes [10,99] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants