Repository navigation
tsdb: keep head chunk file numbering monotonic across truncation - #20149
Merged
Merged
Conversation
Huang-404-Q
requested review from
a team,
bwplotka,
codesome,
jesusvazquez and
krajorama
as code owners
October 22, 2026 10:49
Huang-404-Q
force-pushed
the
fix/ooo-chunk-sequence
branch
from
October 22, 2026 10:56
6c249a2 to
5a4d355
Compare
After a restart the ChunkDiskMapper has no active chunk file. If Truncate runs in that window on a low-volume server it deletes every chunk file and resets evtlPos.seq to 0. New files were then numbered from the files on disk, so the next OOO chunk got a ref below lastGarbageCollectedMmapRef or minOOOMmapRef; queries skipped it and head GC then dropped it, losing the samples permanently. Name new files after the sequence carried by their chunk reference instead of the files on disk, and stop resetting evtlPos.seq when all files have been deleted. Refs stay monotonic while the process is up. The head test that forces a chunk write panic used to rely on disk-derived numbering; it now occupies the next segment names with directories, since cut() takes the sequence from the reference. Adds the regression test from the issue: 80 late samples survive a restart and later compactions (48 without the fix). Fixes prometheus#20054. Signed-off-by: huang <[email protected]>
Huang-404-Q
force-pushed
the
fix/ooo-chunk-sequence
branch
from
October 22, 2026 11:09
5a4d355 to
13c6345
Compare
aknuds1
self-requested a review
November 3, 2026 15:50
1 of 4 tasks
aknuds1
added a commit
to grafana/mimir
that referenced
this pull request
Nov 6, 2026
…17195) #### What this PR does Adds a regression test, ported from prometheus/prometheus#20291, checking that out-of-order samples appended after an ingester restart stay queryable and survive head GC and OOO compaction, even if head truncation deleted every head chunk file in between. Before prometheus/prometheus#20149, which #17193 vendored, deleting every head chunk file restarted the chunk file numbering. An OOO compaction remembers the last m-mapped chunk ref it covered, and queries and head GC treat OOO chunks at or below that ref as already compacted. OOO chunks m-mapped after the reset got refs at or below that mark. Queries then hid them, and head GC dropped them, although they had never been compacted. The test covers both orders of in-order head compaction (which runs head GC) and OOO head compaction, since the ingester uses both: - Forced head compaction (`userTSDB.compactHead`) compacts the in-order head first. - Early compaction of non-owned series (`compactBlocksDueToNonOwnedSeries`) compacts the OOO head first. The test drives the TSDB directly. The upstream TSDB tests aren't vendored, so they don't run in Mimir's suite. With the vendored `tsdb/chunks` package reverted to its state before #17193, both subtests fail with only the last of the five samples returned right after appending. With the current vendored version, both pass. #### Which issue(s) this PR fixes or relates to Relates to prometheus/prometheus#20054, prometheus/prometheus#20149, prometheus/prometheus#20291 and #17193. #### Checklist - [x] Tests updated. - [ ] Documentation added. - [ ] `CHANGELOG.md` updated - the order of entries should be `[CHANGE]`, `[FEATURE]`, `[ENHANCEMENT]`, `[BUGFIX]`. If changelog entry is not needed, please add the `changelog-not-needed` label to the PR. - [ ] [`about-versioning.md`](https://github.com/grafana/mimir/blob/main/docs/sources/mimir/configure/about-versioning.md) updated with experimental features. Signed-off-by: Arve Knudsen <[email protected]>
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.
Which issue(s) does the PR fix:
Fixes #20054.
Problem
After a restart
ChunkDiskMapperhas no active chunk file until the next chunk is written. If head truncation runs in that window on a low-volume server, it deletes every chunk file and resetsevtlPos.seqto 0 (Truncate). New files were then numbered from the files currently on disk, so the next file was000001.OOO chunks written into that file get refs like
1:8, which are below the bookmarkslastGarbageCollectedMmapRefandminOOOMmapRef(2:8). The OOO query path ignores refs that are not greater than the bookmark, and head GC then drops them, so the samples are lost permanently.Fix
cut()takes the sequence carried by the chunk reference and names the file with it, instead of callingnextSequenceFile()on the directory. This also keeps the async write queue correct: the sharedevtlPosmay have advanced before a queued job runs.Truncate()no longer resetsevtlPos.seqwhen all files are deleted, so numbering stays monotonic while the process is up.head_chunks_test.go'semptyFilehelper advances the position before callingcut, matching the production call path.Verification
Without the fix:
after second late batch: 48,after compactions and restart: 48,expected 80, actual 48.With the fix: both report 80 and the test passes.
passes, including
TestChunkDiskMapper_Truncate_WriteQueueRaceConditionandTestChunkDiskMapper_Truncate_PreservesFileSequence.Release notes for end users (ALL commits must be considered).
Reviewers should verify clarity and quality.