Repository navigation
Description
Problem
The kafka cache storage layer's producer encode path needs to accumulate a variable number of header/trailer entries produced in the course of encoding a single message, then materialize them as real Kafka headers once encoding completes. Today this accumulation happens in a per-stream, heap-resident growable buffer (an expandable array plus parallel offset/length arrays) that is retained for the entire lifetime of the producing stream, growing to whatever high-water mark that stream's own trailer volume reaches and never released.
This is a self-imposed limitation of the current implementation, not a constraint of the storage format: the same storage layer already solves an equivalent problem — accumulating a variable amount of transform-produced output for a single message, without knowing its final size in advance — for the value itself, and does so with zero retained per-stream heap state.
Existing pattern (value's own transformed output)
KafkaCachePartition already implements a claim-per-message protocol for the transformed value:
- At message start, a worst-case size bound is computed (
pipeline.padding(...)) and a block of that size is reserved in a dedicated per-segment file (convertedFile, distinct from the main key+value+headers log file) by advancing the file's write position past the reservation immediately (KafkaCachePartition.java~lines 424-431, 757-764). - As the transform produces output, bytes are written directly into that already-claimed, non-overlapping block — no heap buffer of any kind is used for the value's transformed bytes; the write callback (
consumeTransformed, ~lines 592-616) tracks only a couple of file positions. - Because each message's block is reserved up front and never overlaps another message's block, multiple messages can have claims outstanding at the same time — each writes into its own region regardless of interleaving with other in-flight messages on other streams — while claim order still determines the blocks' final layout.
- The worst-case bound this depends on is not new or exotic: a statically configured max already exists for header/trailer bytes today (
cacheClientTrailersSizeMax), currently used to size the shared log file's segment-rollover calculation.
Proposed change
Apply the identical claim-per-message pattern to trailer-entry accumulation:
- Add a new dedicated per-segment file (sibling to
logFile/convertedFile/deltaFileinKafkaCacheSegment, sameconfig.segmentBytescapacity, same file-naming convention inKafkaCacheFile). - At message start, claim a block in that file sized from the existing
cacheClientTrailersSizeMaxbound (mirroring theconvertedFileclaim already made frompipeline.padding(...)). - Replace the current heap-resident accumulator's storage with direct writes into the claimed block, tracked by a claimed-position and write-cursor pair per stream (analogous to the stream's existing
entryMark/valueMarkposition-tracking fields) instead of a growable array. - Read-back operations (count-by-name, get-by-name-and-index) scan the claimed block directly instead of a heap arena — same comparison logic, different backing buffer.
Benefit
Eliminates the only remaining per-stream retained heap allocation on this path: today every producing stream keeps a heap buffer alive for its whole lifetime, sized to its own steady-state trailer volume. With this change, no heap buffer is retained per stream at all — only two small position fields, with actual storage backed by the same mmap'd segment file the cache already pays for.
Scope
Confined to runtime/binding-kafka's cache storage layer (KafkaCacheFile, KafkaCacheSegment, KafkaCachePartition, and the produce-path trailer accumulator). Per this repo's contribution workflow for significant behavior/format changes, filing this issue to discuss the design before implementation; a follow-up PR would need new k3po coverage for the claim/reservation and overflow-abort behavior, mirroring the existing coverage for convertedFile.
Activity
perf(binding-kafka): claim a block in the log file for produce-path t…
perf(binding-kafka): claim a block in the log file for produce-path t…