Skip to content

C0-1: Restore CargoShip throughput on the compressed write path #153

Closed

Description

@scttfrdmn

Summary

PutObject compresses object bodies but the CargoShip upload path stores the
Content-Encoding value as user metadata (x-amz-meta-content-encoding) instead of the
real HTTP header. GetObject keys decompression off the real header only, so objects
uploaded through that path are returned to callers as raw zstd bytes.

Background

In internal/storage/s3/backend.go PutObject, the standard path sets the header correctly:

if contentEncoding != "" {
    input.ContentEncoding = aws.String(contentEncoding)
}

So does initiateMultipartUpload in internal/storage/s3/multipart_upload.go. The CargoShip
branch does not — it puts the value in the metadata map that becomes x-amz-meta-*:

if contentEncoding != "" {
    cargoMeta["content-encoding"] = contentEncoding
}

This is a limitation of the dependency, not an oversight that can be worked around in the
archive struct: in [email protected]/pkg/aws/s3/transporter.go the Archive struct has no
ContentEncoding field, and the PutObjectInput it builds sets only Bucket, Key, Body,
StorageClass, and Metadata.

The read path confirms the data is unrecoverable. GetObject decompresses only when the
real header came back:

if result.ContentEncoding != nil {
    contentEncoding = aws.ToString(result.ContentEncoding)
}
// ...
if b.compressor != nil && contentEncoding != "" {
    decompressed, decompErr := b.compressor.Decompress(data, contentEncoding)
    // ...
}

Nothing consults user metadata. Two consequences:

  1. A full read returns raw compressed bytes to the application.
  2. Worse, the range slice that runs after the decompress step then slices compressed
    bytes at uncompressed offsets — so every partial read of such an object returns
    silent garbage from the wrong position.

Implementation

Hoist the wasCompressed result out of the compression block and skip the CargoShip
transporter when it is true, taking the standard PutObject path that sets the header
correctly. This is correct-by-construction and needs no upstream change. Log at INFO when
CargoShip is bypassed for this reason so the throughput trade-off stays visible.

Upstreaming a ContentEncoding field to CargoShip's Archive is the longer-term fix and
should be filed separately; it does not block this one.

Note while in this code: the CargoShip branch is unreachable for objects at or above
MultipartThreshold, because the multipart early-return precedes it. The transporter only
ever handles objects smaller than 32 MB by default, so the "4.6x performance" comment above
it overstates its reach. Correct the comment.

Acceptance Criteria

  • When compression compresses an object, the upload takes the standard path and
    HeadObject reports ContentEncoding: "zstd" — not x-amz-meta-content-encoding
  • A full GetObject returns bytes byte-identical to the input
  • A partial GetObject at a non-zero offset returns the correct slice
  • Unit test asserts the CargoShip path is not selected when wasCompressed is true
  • The misleading throughput comment is corrected
  • go test -race ./internal/storage/s3/... passes
  • AWS_PROFILE=aws AWS_REGION=us-west-2 go test -race -tags=integration ./internal/storage/s3/... passes

The reproducing case needs an object between MinSize (4KB) and MultipartThreshold
(32MB): below 4KB nothing is compressed, and at or above 32MB the CargoShip branch is
unreachable.

Dependencies

None. Must land before the issue that plumbs EnableCargoShipOptimization through
buildS3Config — today that flag never reaches the backend on the mount path, so the
CargoShip branch is dead code there. Plumbing it first would activate this corruption.

Activity

  1. added
    type: bug
    priority: critical
    area: s3
    on Jul 29, 2026
  2. scttfrdmn commented on Aug 2, 2026

    @scttfrdmn
    OwnerAuthor

    Moved to v0.11.0. Not fixed in v0.10.1, and it is worth being precise about why, because the release did touch this code.

    internal/storage/s3/backend.go:830-885 now bypasses CargoShip entirely for compressed objects and takes the plain SDK put path instead. That closes the corruption — a Content-Encoding cannot be turned into user metadata by a path that is no longer taken — but it does so by routing around the defect, not by fixing it. The CargoShip upload path still cannot carry Content-Encoding correctly, so the moment anything else needs that path for an encoded object, the bug is live again.

    So the fix in the release is a containment, and this issue stays open for the real one: make the CargoShip path preserve Content-Encoding as a header, and then remove the bypass.

  3. scttfrdmn commented on Aug 3, 2026

    @scttfrdmn
    OwnerAuthor

    The corruption this issue describes is already prevented, though not in the way the title proposes — worth recording, because the remaining work is smaller than the issue reads and the ordering hazard it created is gone.

    Measured

    CargoShip enabled and compression enabled, zstd, min_size: 1B — the configuration this issue says corrupts:

    ROUND-TRIP OK: 98304 bytes, stored 37
    

    98,304 bytes in, 98,304 identical bytes out, 37 bytes on the wire. No corruption.

    Why

    internal/storage/s3/backend.go:874 bypasses the transporter entirely when the object carries an encoding:

    transporter := b.clientManager.GetTransporter()
    if contentEncoding != "" && transporter != nil {
        b.logger.Debug("Bypassing CargoShip for a compressed object: the transporter cannot set Content-Encoding", ...)
        transporter = nil
    }

    So a compressed object takes the direct path, which does set the header. The fix chosen was "do not use the transporter for objects it cannot represent" rather than "teach the transporter to carry the header" — which is why this issue is still open while the defect is not reachable. The same pattern was applied a few lines down for encryption (cargoShipCanEncrypt), where the transporter hardcodes aws:kms and has no bucket-key field.

    What that changes about scheduling

    The v0.10.0 audit plan listed this as an ordering hazard: plumbing config before fixing the encoding path would activate EnableCargoShipOptimization and thereby activate the corruption. That constraint no longer holds. The flag is now plumbed — internal/adapter/adapter.go:447 maps it from s3cfg.UseCargoShip, so it is reachable on the real mount path, and the round-trip above is that path. Config plumbing work no longer needs to wait on this.

    What is actually left

    The title's literal ask: make the transporter able to carry Content-Encoding, so compressed objects can use CargoShip's throughput path instead of falling back. That is an optimization, not a correctness fix, and it belongs upstream in cargoship (transporter.go:184 puts the encoding in user metadata and nothing sets the HTTP header) before it can be used here.

    Suggest relabeling from priority: critical to something lower and rewriting the summary, since the current text says compressed objects are stored corrupt and they are not. Leaving the label change to @scttfrdmn rather than making it unilaterally — but a critical-priority issue whose defect is unreachable will keep pulling attention away from ones that are live, which is the same failure mode as a green check for work that did not run.

    Whichever way it goes, the bypass needs a regression test pinning it: the probe above was thrown away, and nothing currently fails if backend.go:874 is deleted.

  4. scttfrdmn commented on Aug 3, 2026

    @scttfrdmn
    OwnerAuthor

    Correcting the last paragraph of my previous comment: I said the bypass has no regression test and that nothing fails if backend.go:874 is deleted. Both are wrong.

    TestDefaultConfigDoesNotCorruptCompressedObjects in internal/storage/s3/compression_integrity_test.go:116 already pins it, and it pins the right thing — the stored Content-Encoding header read back through a raw client, not just the round-trip, so it catches the metadata-instead-of-header failure specifically.

    Verified by mutation rather than by reading it. Disabling the bypass so compressed objects route back through the transporter:

    --- FAIL: TestDefaultConfigDoesNotCorruptCompressedObjects
        compression_integrity_test.go:145: stored Content-Encoding header = "", want "zstd".
          The encoding is only recoverable from the header; storing it in user metadata makes the
          object undecodable by the read path and by every other S3 client.
    

    backend.go restored, confirmed with git diff.

    The test's own doc comment also already records the ordering point I made above, and more precisely than I did — including that the audit's "latent" classification was wrong twice over, and that use_cargoship maps straight to the flag as of v0.10.1. Everything else in my previous comment stands: the corruption is unreachable, the ordering hazard is gone, and what remains is the upstream optimization.

  5. added
    priority: medium
    and removed
    priority: critical
    on Aug 3, 2026
  6. changed the title [-]C0-1: Preserve Content-Encoding on the CargoShip upload path[/-] [+]C0-1: Restore CargoShip throughput on the compressed write path[/+] on Aug 3, 2026
  7. scttfrdmn commented on Aug 3, 2026

    @scttfrdmn
    OwnerAuthor

    Relabelled priority: critical → priority: medium and retitled, because the defect this was filed for is not reachable.

    backend.go:874 bypasses the CargoShip transporter whenever contentEncoding != "", so a compressed object never travels the path that would have stored Content-Encoding as user metadata. Measured: a 98,304-byte round trip with EnableCargoShipOptimization and compression both on stored 37 bytes and read back byte-identical.

    That bypass is pinned by a test with teeth — TestDefaultConfigDoesNotCorruptCompressedObjects (internal/storage/s3/compression_integrity_test.go:116), which asserts the stored Content-Encoding header through a raw client. Verified by mutation: changing the guard to if false && contentEncoding != "" fails it with stored Content-Encoding header = "", want "zstd".

    So there is no correctness work left here. What remains is that compressed writes forgo CargoShip's throughput optimization entirely — a performance gap, and the fix belongs upstream in cargoship (teaching the transporter to carry Content-Encoding as a real header) rather than here. Retitled to name that.

    Also note this removes the ordering hazard the audit plan recorded: "plumbing config first would activate CargoShip corruption" no longer applies. adapter.go:447 already maps the flag from s3cfg.UseCargoShip and the round trip is clean.

  8. added
    effort: medium
    on Aug 3, 2026
  9. scttfrdmn commented on Aug 3, 2026

    @scttfrdmn
    OwnerAuthor

    Closed by 851af8e. The corruption itself was fixed in v0.10.1; what remained was the fourth acceptance criterion — a test asserting CargoShip is not selected when wasCompressed is true.

    Why that criterion was worth keeping separate. TestDefaultConfigDoesNotCorruptCompressedObjects already asserted the stored object carries a real Content-Encoding header, which is the property users need. But it is one step removed from the mechanism: it would also pass if the transporter had acquired header support, and it would keep passing if the bypass were replaced by anything else that happened to produce a correct object. The comment in PutObject explaining why a compressed body must not go through the transporter had nothing asserting the branch it describes.

    What the new test uses. cargoship-created-by, which buildMetadata stamps on everything the transporter uploads. It is the only observable that distinguishes the two paths — both end in a PutObject to the same endpoint with the same body, so asserting on the object alone cannot tell them apart, and asserting on a log line would test the logging rather than the routing.

    The control half is equally load-bearing. A 1 KiB object, below the 4 KB compression threshold, must carry the stamp. Without that, the test would pass on a build where the transporter never runs at all — silently measuring a disabled feature instead of the bypass.

    Both halves verified by mutation. Neutering the bypass condition fails the assertion, and the metadata in the failure output shows the defect exactly:

    cargoship-created-by:cargoship  content-encoding:zstd  objectfs-original-size:8192
    

    content-encoding as user metadata beside an empty real header. Setting EnableCargoShipOptimization false fails the control with its reason.

    Status of the other criteria, all already met:

    • HeadObject reports ContentEncoding: "zstd" — TestDefaultConfigDoesNotCorruptCompressedObjects
    • Full GetObject is byte-identical — same test
    • Partial GetObject at a non-zero offset returns the correct slice — TestPartialReadSurvivesAnAlgorithmChange, at offset 9001 of a 22000-byte object, deliberately away from any boundary so a skipped decode cannot coincide with plaintext
    • Unit test asserts CargoShip is not selected when compressed — this commit
    • The misleading throughput comment corrected — repo-wide; "4.6x" appeared 24 times across 10 files and each site now names its mechanism or cites benchmarks/
    • go test -race ./internal/storage/s3/... passes

    The ordering constraint this issue named held: use_cargoship was plumbed through buildS3Config only after the bypass existed, so the plumbing never activated the corruption.

    Upstream follow-up: scttfrdmn/cargoship#353. Archive still has no field that maps to Content-Encoding and neither transporter sets the header, as of v0.20.0 (we pin v0.13.0). CompressionType looks like the field and is not — buildMetadata puts it in user metadata, and CargoShip's own tests use "none" for it, which is not a valid HTTP content coding, so deriving the header from it would not work either. Until that lands, ObjectFS gives up the throughput optimization for exactly the objects that compressed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: s3effort: mediumpriority: mediumtype: bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions