Skip to content

tsdb: keep head chunk file numbering monotonic across truncation - #20149

Merged
aknuds1 merged 1 commit into
prometheus:mainfrom
Huang-404-Q:fix/ooo-chunk-sequence
Nov 3, 2026
Merged

aknuds1 merged 1 commit into
prometheus:mainfrom
Huang-404-Q:fix/ooo-chunk-sequence

Conversation

@Huang-404-Q

Copy link
Copy Markdown
Contributor

Which issue(s) does the PR fix:

Fixes #20054.

Problem

After a restart ChunkDiskMapper has 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 resets evtlPos.seq to 0 (Truncate). New files were then numbered from the files currently on disk, so the next file was 000001.

OOO chunks written into that file get refs like 1:8, which are below the bookmarks lastGarbageCollectedMmapRef and minOOOMmapRef (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 calling nextSequenceFile() on the directory. This also keeps the async write queue correct: the shared evtlPos may have advanced before a queued job runs.
  • Truncate() no longer resets evtlPos.seq when all files are deleted, so numbering stays monotonic while the process is up.
  • head_chunks_test.go's emptyFile helper advances the position before calling cut, matching the production call path.
  • Adds the regression test from the issue.

Verification

go test ./tsdb -run '^TestOOOSamplesLostAfterRestart$' -count=1 -v

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.

go test ./tsdb/chunks -count=1

passes, including TestChunkDiskMapper_Truncate_WriteQueueRaceCondition and TestChunkDiskMapper_Truncate_PreservesFileSequence.

Release notes for end users (ALL commits must be considered).

Reviewers should verify clarity and quality.

[BUGFIX] TSDB: Keep head chunk file numbering monotonic across truncation, so out-of-order samples are not lost after a restart on a low-volume server.

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]>

@aknuds1 aknuds1 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.

LGTM, thanks!

@aknuds1
aknuds1 merged commit 4a933f8 into prometheus:main Nov 3, 2026
39 checks passed
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]>
@roidelapluie roidelapluie mentioned this pull request Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TSDB: late out-of-order samples lost after restart on a low-volume server

2 participants