Repository navigation
fix(logservice): make snapshot initialization idempotent - #26502
Conversation
4c08d60 to
6961ff1
Compare
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
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.
…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]>
…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]>
…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]>
What type of PR is this?
Which issue(s) this PR fixes:
Fixes #25889.
What this PR does / why we need it:
Root cause
snapshotManager.Initloaded sorted on-disk snapshot directories by appending them to the existing in-memorysnapshotRecord. Retrying replica startup therefore counted the same physical snapshot repeatedly and could fillMaxExportedSnapshot, suppressing later exports.There was a second interleaving in the export path: Dragonboat can finish writing a snapshot directory before
snapshotManager.Addruns. If a retrying replica start callsInitin that window,Initloads the new index and the delayedAddappended it again becausesnapshotRecord.addaccepted an index equal to the current latest index.Changes
(shardID, replicaID)record from the current validated disk view and replace the old record atomically under the snapshot-manager lock.snapshotItemobjects to their pool after they become inaccessible.Initthen delayed equal-indexAddordering, changed disk contents, sorted reconstruction, and an empty disk view.No public API, configuration default, or on-disk format changes.
Validation
Pre-fix,
TestInitSnapshotMgrReplacesInMemoryViewfailed 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-listand containedTestAddSnapshot,TestInitSnapshotMgr, andTestInitSnapshotMgrReplacesInMemoryView.T=0singo test -race -jsonoutput. With a 30-second stress budget, the adaptive rule selectedN=100; all three exact-test-race -count=100runs 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)go build -mod=readonly ./pkg/logservicego vet -mod=readonly ./pkg/logservicegit diff --check origin/main...HEADResidual risks / test baseline
The complete owning-package command without
-skiphas one unrelated current-mainfailure:TestNewServiceClosesStoreOnReplicaStartFailureexpects duplicate metadata to reach Dragonboat and returnErrShardAlreadyExist, while the newer zombie-replica membership check skips both starts. The identical focused command fails on cleanHEADwith 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
Addreconciles the directory-written-before-Add ordering.