Skip to content

Limit memory retained by CAS inline file writers per transaction - #2980

Draft
k-morozov wants to merge 10 commits into
antalya-26.8from
cas-transaction-inline-memory-budget
Draft

k-morozov wants to merge 10 commits into
antalya-26.8from
cas-transaction-inline-memory-budget

Conversation

@k-morozov

@k-morozov k-morozov commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

A CAS disk transaction can finalize dozens of files smaller than 1 MiB before it publishes a part manifest. Limiting inline bytes at commit still leaves the transaction holding every writer's payload in RAM until then.

This change gives the inline file writers in one disk transaction a shared 16 MiB budget across open writers and finalized files. A writer that cannot reserve space for its next allocation transfers its buffered bytes to a local temporary file and continues writing there. At commit, the manifest budget still determines which files are stored inline. Once an inline entry owns its bytes, its path releases the staging source. An open reader or hardlink can keep that source alive independently. The writer budget covers new payload allocations. Manifest encoding and inline entries copied from an already published manifest can consume additional memory. Writer implementation · Release after inline placement

Merge #2969 first. Its changes are currently included in the release-branch diff. Review only the three follow-up commits.

Memory held before commit. One transaction wrote and finalized 0.idx through 63.idx sequentially, 600 KiB each, on local storage. Values are medians of six paired runs. The timer covered writing and finalizing all files and stopped before commit. Before the transaction budget · With the transaction budget

Writer measurement Before budget With budget
Memory retained just before commit 37.76 MiB 14.67 MiB
Peak memory during the writes 38.83 MiB 15.66 MiB
Time to write and finalize all 64 files 36.38 ms 31.39 ms
Local staging files before commit 0 39

The writer retained 23.09 MiB less before commit and staged 39 of the 64 files locally. This workload measures the effects of spilling on the writer.

Peak memory while rebuilding a text index. Test 05815 runs OPTIMIZE TABLE … FINAL DEDUPLICATE on two 50-row parts and MATERIALIZE INDEX idx_s on a separate 100-row part. Both operations write temporary dictionary files ending in .dct.idx. The table reports the operation peak from system.part_log.peak_memory_usage in MiB. “Before budget” is one reference run per cell on the #2969 implementation. “Budget” and “Budget + early release” are medians of four paired runs per storage configuration, with alternating run order. Time is system.part_log.duration_ms for those same pairs. 05815 workload · Early release commit

Storage / staging Operation Before budget, MiB Budget, MiB Budget + early release, MiB Time, budget → early release (ms)
Local disk Merge 153.73 164.78 151.81 436.5 → 441
Local disk Mutation 142.25 154.30 141.33 420.5 → 421.5
RustFS / local Merge 153.72 164.77 151.80 1,187.5 → 1,144.5
RustFS / local Mutation 142.24 154.29 141.32 1,043.5 → 1,035.5
RustFS / s3 Merge 187.65 198.71 185.74 4,819.5 → 5,047
RustFS / s3 Mutation 142.18 154.24 141.26 4,131 → 4,226.5

Early release saved 12.97 MiB in every paired comparison. Against the separate pre-budget reference, the final peaks were about 1.9 MiB lower for merge and 0.9 MiB lower for mutation. On RustFS with S3 staging, the paired change in merge time ranged from 1.97 s faster to 2.38 s slower, so these four pairs do not establish a latency improvement.

The local allocation profile explains where the memory went. At the peak of both operations, manifest encoding previously overlapped with 16 original dictionary buffers holding 14.75 MiB. Early release left two original buffers holding 1.84 MiB for pending blob uploads. In both versions the manifest entries contained the same 14 copies from RAM, totaling 12.91 MiB.

All 64 focused CAS unit tests passed, including a reader that outlives commit and a hardlink whose destination requires blob placement. All 24 paired-series runs of 05815 passed the indexed lookup and CHECK TABLE. Each merge and mutation wrote 20 temporary segments, spilled four writers under memory pressure, and selected three entries for blob placement at commit. Reader and hardlink regression test

Related: #2969

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

CAS disks use less memory when one transaction writes many small files, including during text-index rebuilds.

Documentation entry for user-facing changes

CAS automatically spills an inline candidate to local staging when its writer cannot reserve space in the transaction’s 16 MiB memory budget.

CI/CD Options

Exclude tests:

  • Fast test
  • Integration Tests
  • Stateless tests
  • Stateful tests
  • Unit tests
  • Performance tests
  • Aarch64 tests
  • All with ASAN
  • All with TSAN
  • All with MSAN
  • All with UBSAN
  • All with Coverage
  • All Regression
  • Disable CI Cache

Regression jobs to run:

  • Fast suites (mostly <1h)
  • Aggregate Functions (2h)
  • Alter (1.5h)
  • Benchmark (30m)
  • CAS (content-addressed storage; Antalya only)
  • ClickHouse Keeper (1h)
  • Iceberg (2h)
  • LDAP (1h)
  • OAuth (5m)
  • Parquet (1.5h)
  • RBAC (1.5h)
  • SSL Server (1h)
  • S3 (2h)
  • S3 Export (2h)
  • Swarms (30m)
  • Tiered Storage (2h)

k-morozov and others added 10 commits November 26, 2026 18:52
Text index rebuilds can produce hundreds of small files whose combined
inline data exceeds the 16 MiB manifest limit. Count inline bytes from
the final paths and store the largest added or replaced files as blobs
until the manifest fits. Include unchanged base entries in the budget
when updating a published part.

Use the existing precommit and blob upload path for spilled files.
Share their payload across upload requests and independent readers.

Co-authored-by: Codex (GPT-6) <[email protected]>
Signed-off-by: Konstantin Morozov <[email protected]>
Text index files can outgrow the 1 MiB per-file inline limit while a
merge is still writing them. Keeping the entire file in memory until
finalize makes the peak allocation grow with the file size.

Once a write crosses the limit, pass the buffered prefix and subsequent
blocks to the local staging writer. Release the prefix allocation after
the transfer. At finalize, register the staged file for publication
through the existing precommit and blob upload path.

Co-authored-by: Codex (GPT-6) <[email protected]>
Signed-off-by: Konstantin Morozov <[email protected]>
Stream new part files to the configured local or S3 staging backend.
Keep the size, content hash and staging handle when a writer closes.
At commit, read the files selected for inline placement and publish the
remaining blobs from staging. Hardlinks, moves and open readers retain
the source until their last reference is released.

Exercise the manifest budget with 100 rows and 20 temporary text index
segments in both a merge and a mutation. Cover multipart staging and
cleanup on RustFS after a successful insert or an injected failure.

Co-authored-by: Codex (GPT-6) <[email protected]>
Signed-off-by: Konstantin Morozov <[email protected]>
This reverts commit 6430520.

Co-authored-by: Codex (GPT-6) <[email protected]>
Signed-off-by: Konstantin Morozov <[email protected]>
An inline candidate written in chunks can retain substantially more memory than its payload when the transaction takes ownership. Request a smaller allocation before moving the finalized string into the manifest entry.

Co-authored-by: Codex (GPT-6) <[email protected]>
Signed-off-by: Konstantin Morozov <[email protected]>
Pin LZ4 for the text index rebuild so randomized server codecs cannot
make the segment dictionaries fit below the manifest limit. Generate
100 rows across 20 segments and check that both the merge and mutation
still select entries for blob placement.

Exclude the memory measurement at compile time under sanitizers, where
the allocation tracker cannot support the assertion. This avoids Clang's
unreachable-code diagnostic after `GTEST_SKIP`.

Co-authored-by: Codex (GPT-6) <[email protected]>
Signed-off-by: Konstantin Morozov <[email protected]>
…limit

Randomized dictionary settings can make the 100-row fixture fit within
the manifest budget. Keep the dictionary settings stable and report the
segment and blob-placement checks separately for the merge and mutation.

Use the same per-file inline limit in the writer and the manifest
validator. Include the namespace and ref in the retained-entry limit
error and document staging, the placement counter and failed commits.

CI: https://altinity-build-artifacts.s3.amazonaws.com/json.html?PR=2969&sha=a2b90dc739fc9e8169a4784148258164f8b191de&name_0=PR
PR: #2969

Co-authored-by: Codex (GPT-6) <[email protected]>
Signed-off-by: Konstantin Morozov <[email protected]>
Share payload reservations across writers in a disk transaction. When a writer cannot grow its reservation, spill the file to local staging and keep it eligible for inline placement at commit.

Co-authored-by: Codex (GPT-6) <[email protected]>
Signed-off-by: Konstantin Morozov <[email protected]>
Cover the last available byte and its reuse, hardlinks from published
parts under memory pressure, and placement when the same dictionary
files spill to local staging in different orders. Check that an open
reader keeps its S3 staging object through commit or abort.

Co-authored-by: Codex (GPT-6) <[email protected]>
Signed-off-by: Konstantin Morozov <[email protected]>
Once the manifest entry owns the file bytes, erase its path from the
transaction's staging map. This lets unshared payloads release their
memory before manifest encoding. An open reader or a hardlink can still
hold the source independently.

Check all three lifetimes during publication, including a hardlink
whose destination needs the same bytes for blob placement.

Co-authored-by: Codex (GPT-6) <[email protected]>
Signed-off-by: Konstantin Morozov <[email protected]>
@github-actions

Copy link
Copy Markdown

Workflow [PR], commit [291e935]

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