db/snapshotsync: make index building and opening segments safe with other files building events#21571
Merged
Merged
Conversation
…d-index) Running the block merge off the build semaphore exposed a race: the merge writes a merged .seg to its final path before building its index, so a concurrent OpenFolder picks up the bare .seg and BuildMissedIndices rebuilds the same index on the build semaphore — colliding with the merge's own buildIdx (the -to-block.idx.tmp rename fails) and stalling state collation for the duration. Add a per-(type,from,to) in-flight registry on RoSnapshots: TryAcquireRange/ ReleaseRange (atomic claim) and isInProgress (read check). Producers (merge, dump) and missed-index recovery claim the files they build; openSegments skips in-progress ranges and BuildMissedIndices skips/serializes on them, so a given index always has a single builder. Also makes concurrent BuildMissedIndices calls safe.
ac3018d to
d96b16d
Compare
awskii
reviewed
Jun 2, 2026
added 2 commits
June 5, 2026 04:55
…tric acquire/release keys - dumpRange: a claimed (type,range) is now an explicit error instead of a silent unprotected build (review: a function creating a fresh file must own it) - BuildMissedIndices: acquire and release now use the same (t, from, to) values instead of keying release off segment.FileInfo
…mp error A claimed range during dump is benign (another builder owns it), but the plain error reached RetireBlocksInBackground's Error log on every retire attempt. Follow the ErrHeimdallDataIsNotReady pattern: wrap a sentinel, log at Debug and retry on the next retire cycle.
awskii
reviewed
Jun 5, 2026
awskii
left a comment
Member
There was a problem hiding this comment.
Registry logic is correct and both earlier comments are addressed. Two notes:
- Land this before #21526 — it's the guard that makes the off-semaphore merge safe, and they're siblings off
main, not stacked. Merge's claim/skip/rollback (merger.go:181-199) has no test (no caller with a concurrent claimant) — that's the path #21526 relies on. Worth a test that pre-claims a range and assertsMergeskips it and leaves the claim intact.
Pre-claims one type of a merge range and asserts Merge skips the whole range, rolls back its partial claims, keeps the pre-claim intact, and still merges (and releases) the unclaimed range. Verified the test fails against the pre-registry Merge.
AskAlexSharov
approved these changes
Jun 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
BuildMissedIndices, it'll try to build the idx (even though merge goroutine is already building one).Fix
A per-
(type, from, to)in-flight registry onRoSnapshots:TryAcquireRange/ReleaseRange— atomic claim (LoadOrStore);false⇒ another builder owns it.isInProgress— read-only check.Wired in:
(type,range)it produces before writing; a range it can't fully claim is being built elsewhere → skipped this pass; released after integrate.dumpRange) claims its(type,range)across.seg+.idxbuild.BuildMissedIndicesTryAcquires per segment, skips if held — so it never rebuilds an in-flight file and concurrent recovery calls can't double-build.openSegmentsskips in-progress ranges (read-only), so it never creates a duplicateDirtySegmentfor a half-built file. Once the owning builder is done processing, it can readd todirtyFilesand doOpenFolder, so we don't miss the new file.