Skip to content

Add a regression test for a secondary table cache leak - #15767

Open
andpred wants to merge 1 commit into
meta:mainfrom
andpred:secondary-table-cache-leak-test
Open

andpred wants to merge 1 commit into
meta:mainfrom
andpred:secondary-table-cache-leak-test

Conversation

@andpred

@andpred andpred commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Summary:
Add two secondary tests for a table cache entry leak that 1192897
(#14914) fixed as a side effect. That commit added the eviction in
VersionBuilder::UnrefFile for a failed LogAndApply on a primary,
and its test detects a leaked entry only through
TEST_VerifyNoObsoleteFilesCached, which returns early unless
kMustFreeHeapAllocations, so no build without ASAN covers the
eviction.

The leak needs a secondary catch-up round that applies both the
MANIFEST record adding a file and the record deleting it. While
verify_sst_unique_id_in_manifest is set, the add puts a table reader
in the cache through VersionSet::VerifyFileMetadata and releases the
handle without erasing it, and the file never reaches a Version, so
FindObsoleteFiles and TableCache::ReleaseObsolete never see it.
With max_open_files=-1 nothing evicts it for capacity, so each such
file cost a secondary a TableReader and an fd for the life of the
process. A 10.x fork hit this in production, where files that live less
than one catch-up interval leak steadily.

Test Plan:
With the TableCache::Evict call in UnrefFile removed,
FilesAddedAndDeletedInOneCatchUp reports a table cache of
{13, 15, 16, 17} against a live set of {17}, and
NoGrowthOverRepeatedCatchUps, which repeats five rounds, ends with 16
entries for 1 live file. Nothing else in db_secondary_test (91 tests)
fails, and version_builder_test (31 tests) still passes in full,
including IngestExternalFileEvictsTableCacheOnLogAndApplyFailure.
Clearing verify_sst_unique_id_in_manifest in the fixture drops
NO_FILE_OPENS from 4 to 1 and fails the new guard, so the absent
cache entries cannot pass vacuously. make check and
ASSERT_STATUS_CHECKED=1 make check pass.

Summary:
Add two secondary tests for a table cache entry leak that 1192897
(meta#14914) fixed as a side effect. That commit added the eviction in
`VersionBuilder::UnrefFile` for a failed `LogAndApply` on a primary,
and its test detects a leaked entry only through
`TEST_VerifyNoObsoleteFilesCached`, which returns early unless
`kMustFreeHeapAllocations`, so no build without ASAN covers the
eviction.

The leak needs a secondary catch-up round that applies both the
MANIFEST record adding a file and the record deleting it. While
`verify_sst_unique_id_in_manifest` is set, the add puts a table reader
in the cache through `VersionSet::VerifyFileMetadata` and releases the
handle without erasing it, and the file never reaches a `Version`, so
`FindObsoleteFiles` and `TableCache::ReleaseObsolete` never see it.
With `max_open_files=-1` nothing evicts it for capacity, so each such
file cost a secondary a `TableReader` and an fd for the life of the
process. A 10.x fork hit this in production, where files that live less
than one catch-up interval leak steadily.

Test Plan:
With the `TableCache::Evict` call in `UnrefFile` removed,
`FilesAddedAndDeletedInOneCatchUp` reports a table cache of
{13, 15, 16, 17} against a live set of {17}, and
`NoGrowthOverRepeatedCatchUps`, which repeats five rounds, ends with 16
entries for 1 live file. Nothing else in `db_secondary_test` (91 tests)
fails, and `version_builder_test` (31 tests) still passes in full,
including `IngestExternalFileEvictsTableCacheOnLogAndApplyFailure`.
Clearing `verify_sst_unique_id_in_manifest` in the fixture drops
`NO_FILE_OPENS` from 4 to 1 and fails the new guard, so the absent
cache entries cannot pass vacuously. `make check` and
`ASSERT_STATUS_CHECKED=1 make check` pass.
@meta-cla meta-cla Bot added the CLA Signed label Nov 24, 2026
@github-actions

Copy link
Copy Markdown

✅ clang-tidy: No findings on changed lines

Completed in 264.2s.

@github-actions

Copy link
Copy Markdown

✅ Claude Code Review

Auto-triggered after CI passed — reviewing commit 833dacf


Summary

Well-structured regression tests for a real production leak. The test design is sound: it correctly triggers the leak scenario (secondary catch-up applying both add and delete for the same file) and verifies that table cache entries are properly cleaned up.

High-severity findings (0):

No high-severity findings.

Full review (click to expand)

Findings

🟡 MEDIUM

M1. ASSERT_EQ inside ApplyToAllEntries callback silently continues iteration on failure — db_secondary_test.cc:2981
  • Issue: ASSERT_EQ(sizeof(uint64_t), key.size()) is used inside a void lambda passed to ApplyToAllEntries. In gtest, ASSERT_* macros in a void function/lambda execute a return; on failure, which exits only the current lambda invocation — the cache continues calling the lambda for remaining entries. The failed assertion sets gtest's internal failure state but does NOT abort iteration. If a non-8-byte key were encountered (e.g., from a future code change introducing a different entry type in the shared cache), the GetUnaligned on the next line would still execute for subsequent entries, and the test would report a confusing failure rather than immediately stopping.
  • Root cause: ASSERT_* macros cannot propagate a fatal failure out of a lambda/non-TEST_F-method context in the standard gtest way. The lambda returns void, so ASSERT_EQ just returns from the lambda.
  • Suggested fix: This is acceptable for the current test since no blob files are created (enable_blob_files defaults to false) and the shared cache only has table entries. However, consider using EXPECT_EQ (which does not return) followed by an if guard to skip the GetUnaligned/insert, or add a brief comment explaining why only table cache entries are expected. The current code is functionally correct for this test scenario.
M2. NoGrowthOverRepeatedCatchUps does not check per-round eviction — db_secondary_test.cc:3089
  • Issue: The second test only checks the final state (live files == cached files) after all 5 rounds. It does not assert that deleted files from each round are evicted within that round. A hypothetical bug that delays eviction to a later round (e.g., evicting round N's files during round N+1) would pass this test but still indicate a problem.
  • Root cause: The test is designed to detect unbounded growth, not per-round correctness. This is a design tradeoff, not a bug.
  • Suggested fix: No action needed — FilesAddedAndDeletedInOneCatchUp already covers per-round eviction. The two tests are complementary.

🟢 LOW / NIT

L1. Consider documenting the deleted.size() + 1 threshold — db_secondary_test.cc:3074
  • Issue: The ASSERT_GE(TestGetTickerCount(..., NO_FILE_OPENS), deleted.size() + 1) check asserts at least 3 file opens (2 deleted inputs + 1 compaction output). The +1 accounts for the compaction output that remains live. A brief comment explaining the threshold would improve readability, though the PR description already explains this.
  • Suggested fix: Optional — add a brief inline comment like // +1 for the compaction output.

Cross-Component Analysis

Context Does code execute? Assumptions hold? Action needed?
BlobFileCache shared cache Test does not create blobs Yes — enable_blob_files=false by default None
WritePreparedTxnDB N/A (test-only) N/A None
ReadOnly DB N/A N/A None
User-defined timestamps N/A N/A None
MANIFEST rotation Primary uses default max_manifest_file_size (MAX) All records stay in one MANIFEST None

Assumption stress-test:

  • Claim: "Files added and deleted in one catch-up never reach a Version." Verified: the secondary hasn't caught up between the primary's flushes and compaction, so the VersionBuilder processes all MANIFEST records (flush adds + compaction add/delete) in one pass. The two flushed L0 files are added then deleted within the same LogAndApply sequence, so UnrefFile handles their cleanup. The default max_manifest_file_size is very large, so no MANIFEST rotation splits the records.

  • Claim: "ASSERT_EQ(2, deleted.size()) — exactly 2 files deleted." Verified: round 0 starts with no existing files. Two flushes create two L0 files. CompactRange with kForce compacts them into one L1 file. The two L0 files are deleted. No prior L1 file exists to be replaced.

  • Claim: "The final EXPECT_EQ also covers the intermediate compaction output." Verified: the compaction output is a live file and should appear in both LiveTableFileNumbers and TableCacheFileNumbers. If the eviction were too aggressive (evicting live files), this check would catch it.

Positive Observations

  • Non-vacuous testing guard: The NO_FILE_OPENS >= deleted.size() + 1 assertion is a clever guard against the test passing vacuously when verify_sst_unique_id_in_manifest is not actually causing file opens. The PR description confirms that setting the option to false drops the count and fails the guard.

  • Proper handle cleanup: The test correctly releases the cache handle when Lookup returns non-null (lines 3080-3082), preventing a handle leak that would mask the cache entry issue.

  • Good use of ASSERT_NO_FATAL_FAILURE: Setup and verification steps that use ASSERT_* internally are correctly wrapped in ASSERT_NO_FATAL_FAILURE to propagate fatal failures to the test body.

  • Complementary test coverage: The two tests cover different aspects — FilesAddedAndDeletedInOneCatchUp verifies per-entry eviction, while NoGrowthOverRepeatedCatchUps verifies no unbounded growth over time. Together they provide strong regression coverage.

  • SCOPED_TRACE usage: The second test uses SCOPED_TRACE to identify the failing round, which is good practice for loop-based tests.


ℹ️ About this response

Generated by Claude Code.
Review methodology: claude_md/ci_review_prompt.md

Limitations:

  • Claude may miss context from files not in the diff
  • Large PRs may be truncated
  • Always apply human judgment to AI suggestions

Commands:

  • /claude-review [context] — Request a code review
  • /claude-query <question> — Ask about the PR or codebase

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant