Skip to content

tsdb: test chunk file numbering after truncation; don't overwrite head chunk files - #20291

Open
aknuds1 wants to merge 4 commits into
prometheus:mainfrom
aknuds1:arve/ooo-chunk-file-sequence-reset-test
Open

aknuds1 wants to merge 4 commits into
prometheus:mainfrom
aknuds1:arve/ooo-chunk-file-sequence-reset-test

Conversation

@aknuds1

@aknuds1 aknuds1 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Which issue(s) does the PR fix:

Follow-up to #20149, which fixed #20054.

Description

Adds regression tests for out-of-order samples appended after a restart in which head truncation deleted every m-mapped chunk file, and makes cutting a head chunk file refuse to overwrite an existing one.

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. If the chunk file numbering restarts once all files are deleted, new chunks reuse refs at or below that mark. They are then hidden from queries and dropped by head GC, although they were never compacted.

The PR includes:

  • TestOOOSamplesAfterChunkFileSequenceReset (tsdb) drives a DB:
    • Compacts the OOO head, restarts, and compacts again, so that every chunk file is deleted. It asserts that this precondition holds.
    • Appends OOO samples that fill a chunk, so that it is m-mapped into a new file.
    • Checks the samples right after they are appended, and again after compaction.
    • Registers cleanup immediately after each database opens, so the first database is also closed if setup assertions fail.
    • Covers both orderings of head GC and OOO compaction. The second ordering only hides the samples until the OOO compaction runs, so it is only caught by the check right after appending.
  • TestChunkDiskMapper_Truncate_DeletingAllFilesKeepsRefsMonotonic (tsdb/chunks) tests the underlying contract directly. After a restart, it truncates every chunk file and checks that the next chunk's ref is above the refs of the deleted files, and that the chunk can be read back through it. It runs with the write queue both disabled and enabled, like the rest of the package.
  • cutSegmentFileWithSeq now opens the new file with O_EXCL. Since tsdb: keep head chunk file numbering monotonic across truncation #20149 the name comes from the sequence in the chunk ref, so nothing else guarantees that it is free, and the old check that the cut file had the expected sequence can no longer fail. Without O_EXCL, a file already at that name is silently truncated, although it may hold chunks that series still refer to. The same flag keeps the deferred cleanup on error from removing a file the call did not create. TestChunkDiskMapper_Cut_DoesNotOverwriteExistingFile covers it. The block Writer shares the helper, but it picks a free name first, so it is unaffected.

The first test differs from the one added in #20149, which drives the DB with a compaction loop and only asserts the final sample count after a second restart.

Validation

  • go test ./tsdb -run '^TestOOOSamplesAfterChunkFileSequenceReset$'
  • make lint

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

Reviewers should verify clarity and quality.

NONE

@aknuds1
aknuds1 force-pushed the arve/ooo-chunk-file-sequence-reset-test branch from b34a1e1 to efe96e9 Compare November 3, 2026 14:51
Add a regression test for out-of-order samples appended after a restart
in which head truncation deleted every m-mapped chunk file (prometheus#20054).

An OOO compaction remembers the last m-mapped chunk ref it covered, and
queries and head GC treat OOO chunks at or below it as already
compacted. If the chunk file numbering restarts once all files are
deleted, new chunks reuse refs at or below that mark and are hidden from
queries and then dropped, although they were never compacted.

The test covers both orderings of head GC and OOO compaction, and checks
the samples right after they are appended as well as after compaction.
It fails until chunk refs stay monotonic across such a truncation.

Signed-off-by: Arve Knudsen <[email protected]>
Add a ChunkDiskMapper test that, after a restart, truncates every chunk
file and checks that the next chunk's ref is above the refs of the
deleted files, and that the chunk can be read back through it.

Chunk refs going backwards is what lets OOO compaction and head GC treat
new chunks as already compacted. The existing Truncate tests never
delete all files, so the sequence reset was untested at this level. The
test fails until the file numbering stops restarting.

Signed-off-by: Arve Knudsen <[email protected]>
Head chunk files are named after the sequence in the chunk ref rather
than after the highest file on disk, so nothing guarantees that the name
is free. Without O_EXCL, a file already there would be silently
truncated although it may still hold live chunks, where the old sequence
check failed the write.

Open the file with O_EXCL so that fails loudly, which also keeps the
cleanup on error from removing a file it did not create. Add a test, and
update the cutAndExpectRef doc comment.

Signed-off-by: Arve Knudsen <[email protected]>
@aknuds1
aknuds1 force-pushed the arve/ooo-chunk-file-sequence-reset-test branch from efe96e9 to 34c623e Compare November 3, 2026 16:30
@aknuds1 aknuds1 changed the title tsdb: test OOO samples after chunk file sequence reset tsdb: test chunk file numbering after truncation; don't overwrite head chunk files Nov 3, 2026
@aknuds1
aknuds1 marked this pull request as ready for review November 3, 2026 16:37
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]>
aknuds1 added a commit to grafana/mimir that referenced this pull request Nov 9, 2026
#### What this PR does

Registers cleanup in the `openDB` helper of
`TestOOOSamplesAfterChunkFileSequenceReset`, immediately after each
database opens. If first-run setup assertions fail, that database is now
closed too; previously cleanup was registered only for the second
instance.

Keeps the explicit first close required for the restart. `DB.Close`
supports repeated calls, so the registered cleanup can safely close that
instance again. Both compaction-order cases and their assertions are
unchanged.

The same cleanup is included in prometheus/prometheus#20291.

#### Which issue(s) this PR fixes or relates to

Follow-up to #17195. Relates to
prometheus/prometheus#20291.

#### Checklist

- [x] Tests updated.
- [ ] Documentation added.
- [ ] `CHANGELOG.md` updated — test cleanup only; `changelog-not-needed`
applies.
- [ ] `about-versioning.md` updated with experimental features.

Signed-off-by: Arve Knudsen <[email protected]>
@aknuds1
aknuds1 requested a balanced review from Copilot November 9, 2026 17:16

Copilot AI 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.

🟢 Approval recommended

No active review issues are reported for the chunk-file collision safeguard and added out-of-order sample regression coverage.

No open issues 🎉

What changed in this PR

This follow-up to #20149 strengthens regression coverage for out-of-order sample loss after restart and truncation, and prevents head chunk creation from overwriting existing files.

Changes:

  • Adds database-level tests checking that out-of-order samples remain queryable after all head chunk files are deleted and after subsequent compaction.
  • Adds mapper tests for monotonic chunk references and preservation of existing files.
  • Uses exclusive file creation to reject chunk filename collisions.
File Description
tsdb/​ooo_chunk_file_sequence_reset_test.go Tests sample visibility across restart, truncation, and compaction orderings.
tsdb/​chunks/​head_chunks.go Clarifies the comment describing reference-based file naming.
tsdb/​chunks/​head_chunks_test.go Tests monotonic references after truncation and rejection of existing filenames.
tsdb/​chunks/​chunks.go Adds O_EXCL to prevent overwriting existing chunk files.

Review effort: Balanced

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