Skip to content

Support length-changing model transforms in kafka cache storage's produce path #1688

Closed

Description

@jfallows

Describe the desired outcome from the user's perspective

As a binding author composing a ModelTransform (#2325/#2326) into the value pipeline, I want the kafka cache storage layer's produce path to accept a transform whose output is a different length than the client's declared input, so that a transform is not structurally restricted to same-length rewrites.

What actually shipped, and a real gap it leaves open. The original plan below proposed a dedicated staging mechanism, explicitly diverging from the existing view: json conversion pattern so that only the transformed output would ever exist in the log. What shipped instead reuses that existing pattern: KafkaCachePartition.writeProduceEntryStart calls the transform's padding() (a statically-known upper bound on how much larger the output can get — computable exactly for a fixed per-layer overhead) and reserves that much extra space in segment.convertedFile(), the same side-file mechanism view: json already uses; if the actual output still exceeds the reserved padding, the entry is marked CACHE_ENTRY_FLAGS_ABORTED. This works and needed no new mmap staging area — but it means the untransformed input is also persisted, in the convertedFile() companion, for as long as that pattern's existing retention already keeps it. For a length-changing transform whose whole point is protecting the original bytes (rather than offering an alternate rendering the way view: json does), that's a real, unresolved question: does the untransformed input persisting anywhere at rest undermine the guarantee such a transform is meant to provide? This needs a real answer, not an assumption either way, before this issue is considered settled.

Acceptance criteria

Implementation layer

This lives in the shared kafka cache storage implementation — concretely KafkaCachePartition (the storage class) plus KafkaCacheModel (the ModelHandler/ModelPipeline wrapper), invoked from KafkaCacheClientProduceFactory's KafkaCacheClientProduceFan. Any binding that produces through this storage layer gets this behavior automatically, with no separate implementation of its own.

Why today's produce write path can't be reused unmodified

KafkaCachePartition.writeProduceEntryStart commits the value's length prefix and reserves its space eagerly, from the client's declared (deferred) length, before later fragments arrive — this is what makes concurrent multi-producer writes to one partition cheap: each stream's region is carved out, non-overlapping, at INIT time, with no separate per-stream buffer needed.

A length-changing transform breaks this structurally, not as an edge case: its output size is not known until the transform runs, and may differ from the declared length.

Resolution actually shipped

  • A record whose transform is length-preserving (or absent) is unaffected — today's direct-to-mmap-log mechanism is untouched, at no cost and no behavior change.
  • A record subject to a length-changing transform reserves padding() extra bytes in the partition's convertedFile() side file (the same mechanism view: json uses for its converted rendering) and writes the transformed output there. If the transform's actual output still exceeds the reserved padding, the entry is marked CACHE_ENTRY_FLAGS_ABORTED rather than attempting to grow the reservation.
  • This depends on padding() being a real, exact upper bound for the installed transform — a transform that cannot state a static bound cannot use this path safely, and should be rejected at schema-resolution time rather than silently risking abort under load.

Still open — needs resolving

  • Whether persisting the untransformed input in convertedFile() is acceptable for a transform whose purpose is protecting those bytes, or whether the storage layer needs an explicit "output-only, no side-by-side original" mode distinct from view: json's deliberate keep-both behavior.
  • Whether CACHE_ENTRY_FLAGS_ABORTED is the right outcome for a padding under-estimate, or whether that should be a startup-time rejection of the transform's declared padding() instead, so it never occurs on the data path at all.

Coverage

Unit and k3po coverage for: a record whose transform is absent or length-preserving following today's direct-to-mmap path unchanged; a record subject to a length-changing transform correctly reserving and using padding() space in convertedFile(); a transform whose actual output exceeds its declared padding() producing CACHE_ENTRY_FLAGS_ABORTED; and coverage for whichever resolution the two open questions above receive.

Additional context

Depends on #2325/#2326 (ModelTransform, the mechanism that can make a value's output length differ from its declared input length).

Activity

  1. added theissue type on Mar 30, 2026
  2. jfallows commented on Aug 4, 2026

    @jfallows
    ContributorAuthor

    Two changes to the header composition this issue owns, from decisions taken since it was filed.

    1. Three headers, not four — zilla:efields is dropped.

    Header Purpose
    zilla:key vault key identifier; its presence is also the "is this encrypted" discriminator
    zilla:edek the EDEK
    zilla:edata ciphertext region descriptors, [block-count][offset|length] × N

    A field id is only interpretable with the schema that defines it, so zilla:efields was either redundant (a consumer holding the schema derives the mapping by traversal) or unreadable (a consumer without it cannot interpret the ids). Neither the gateway nor the client deserializer consumed it — see #1680. options.encryption.headers therefore has three entries.

    2. Descriptors arrive via ModelVisitor.onBlock(int offset, int length), a default method added in #1680. The binding accumulates regions as the transform writes them and composes zilla:edata once per message. Offsets are message-relative, so they remain valid across an OVERFLOW resume where the output buffer drains and transform() is called again.

    Also relevant to this issue: the produce path is parse → validate → encrypt → sink, with validation on plaintext (see #1689). And ModelPipeline.padding(...) is the existing hook for reserving ciphertext expansion — 28 bytes per region, plus the header bytes — which the binding must call before transforming.


    Generated by Claude Code

  3. jfallows commented on Aug 4, 2026

    @jfallows
    ContributorAuthor

    Padding reservation cannot be 28 × fieldCount — that under-reserves badly and will overflow the output buffer.

    28 (12-byte IV + 16-byte GCM tag) is correct only for bytes fields, which carry raw ciphertext. string fields carry base64, verified against Confluent's EncryptionExecutor — standard RFC 4648 alphabet with = padding — so growth is multiplicative, not additive:

    plaintext on-wire factor
    10 chars 52 chars ~5×
    30 chars 80 chars ~2.7×
    100 chars 172 chars ~1.7×

    Per encrypted string field the on-wire length is 4·ceil((p + 28)/3), where p is the plaintext length — plus 5 more inside the base64 if the DEK-version prefix is present (registry mode with rotation enabled, see #1680).

    The difficulty is that ModelPipeline.padding(data, index, length) is called before transforming, so per-field plaintext lengths are not yet known. The bound has to be derived from the payload length and the field count rather than measured:

    • the base64 expansion term is bounded by the whole payload, contributing at most ceil(length / 3) beyond the original bytes
    • each encrypted field adds the IV, the tag, base64 rounding and padding, and the optional version prefix — around 44 bytes worst case
    • so ceil(length / 3) + 44 × fieldCount + headerBytes is a safe conservative bound

    Over-reserving is cheap; under-reserving means an OVERFLOW the transform cannot recover from because the field no longer fits. The exact constant should be derived from the per-field formula above rather than copied from this comment.

    fieldCount comes from the policy, which names the maximum number of encryptable fields for the resolved schema — so it is known at padding(...) time without parsing.

    Also note the header bytes are now three, not four — zilla:efields is dropped (#1680) — and zilla:edek is base64 of the EDEK, so its size depends on the KEK's wrap output rather than being fixed.


    Generated by Claude Code

  4. self-assigned this
    on Aug 5, 2026
  5. changed the title [-]Implement produce-path encryption in kafka-cache-client[/-] [+]Implement produce-path encryption in kafka cache storage[/+] on Aug 6, 2026
  6. changed the title [-]Implement produce-path encryption in kafka cache storage[/-] [+]Support length-changing model transforms in kafka cache storage's produce path[/+] on Aug 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions