Skip to content

Conversation

@shangm2
Copy link
Contributor

@shangm2 shangm2 commented Oct 21, 2025

Description

  1. add support to decompress thrift request with zstd and compress thrift response for http server

Motivation and Context

  1. compressed data can be sent over wire faster

Differential Revision: D85150330

== RELEASE NOTES ==

General Changes
* Add compression support for http2 protocol on cpp worker

@shangm2 shangm2 requested review from a team as code owners October 21, 2025 14:46
@prestodb-ci prestodb-ci added the from:Meta PR from Meta label Oct 21, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Oct 21, 2025

Reviewer's Guide

This PR adds support for ZSTD-based decompression of incoming Thrift requests and enables HTTP/2 response compression (including Thrift content types) in the Presto native execution engine, by updating request handlers, utilities, and server configuration.

Sequence diagram for handling compressed Thrift requests

sequenceDiagram
    participant Client
    participant "HTTPServer"
    participant "TaskResource"
    participant "Utils (decompressMessageBody)"
    participant "TaskManager"
    Client->>HTTPServer: Send Thrift request (Content-Encoding: zstd)
    HTTPServer->>TaskResource: Forward request with headers
    TaskResource->>Utils (decompressMessageBody): Decompress request body if isCompressed
    Utils (decompressMessageBody)-->>TaskResource: Return decompressed body
    TaskResource->>TaskManager: Process decompressed request
    TaskManager-->>TaskResource: Return response
    TaskResource->>HTTPServer: Send response
    HTTPServer->>Client: Return compressed response (if applicable)
Loading

Class diagram for updated Utils functions

classDiagram
    class Utils {
        +std::string extractMessageBody(const std::vector<std::unique_ptr<folly::IOBuf>>& body)
        +std::string decompressMessageBody(const std::vector<std::unique_ptr<folly::IOBuf>>& body, const std::string& contentEncoding)
    }
Loading

File-Level Changes

Change Details Files
Support request body decompression in TaskResource
  • Extract Content-Encoding header and determine compression flag
  • Pass contentEncoding and isCompressed into the request handler lambda
  • Branch on isCompressed to call util::decompressMessageBody or extractMessageBody
presto_cpp/main/TaskResource.cpp
Add ZSTD decompression utility
  • Implement decompressMessageBody() using folly::compression to uncompress IOBuf chains
  • Handle unsupported encodings and exceptions with fallbacks
  • Declare function in Utils.h and include necessary headers
presto_cpp/main/common/Utils.cpp
presto_cpp/main/common/Utils.h
Enable and configure HTTP/2 response compression
  • Set enableContentCompression, compression level, and minimum size options
  • Add Thrift MIME types to contentCompressionTypes for response compression
presto_cpp/main/http/HttpServer.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • decompressMessageBody uses prependChain which reverses buffer order and may incur extra copies—consider using appendChain or folly's coalescing utilities to preserve ordering and reduce overhead.
  • the contentEncoding check is exact-match and case-sensitive for "zstd" only—normalize the header and support common variants (e.g. uppercase, "x-zstd", or even gzip) to be more robust.
  • response compression is enabled only for gzip and thrift mime types—if you want end-to-end zstd support, consider adding zstd to the HTTP/2 response compression codecs or exposing negotiation of the response codec.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- decompressMessageBody uses prependChain which reverses buffer order and may incur extra copies—consider using appendChain or folly's coalescing utilities to preserve ordering and reduce overhead.
- the contentEncoding check is exact-match and case-sensitive for "zstd" only—normalize the header and support common variants (e.g. uppercase, "x-zstd", or even gzip) to be more robust.
- response compression is enabled only for gzip and thrift mime types—if you want end-to-end zstd support, consider adding zstd to the HTTP/2 response compression codecs or exposing negotiation of the response codec.

## Individual Comments

### Comment 1
<location> `presto-native-execution/presto_cpp/main/common/Utils.cpp:97-101` </location>
<code_context>
+  try {
+    // Combine all IOBufs into a single chain
+    std::unique_ptr<folly::IOBuf> combined;
+    for (const auto& buf : body) {
+      if (!combined) {
+        combined = buf->clone();
+      } else {
+        combined->prependChain(buf->clone());
+      }
+    }
</code_context>

<issue_to_address>
**issue (bug_risk):** Prepending chains may reverse buffer order, potentially altering request body.

Using prependChain here reverses the buffer order. If maintaining the original order is important, use appendChain instead.
</issue_to_address>

### Comment 2
<location> `presto-native-execution/presto_cpp/main/common/Utils.cpp:116` </location>
<code_context>
+    }
+
+    // Decompress the data
+    auto codec = folly::compression::getCodec(codecType);
+    auto decompressed = codec->uncompress(combined.get());
+
</code_context>

<issue_to_address>
**issue (bug_risk):** Potential null pointer dereference if getCodec returns nullptr.

Add a nullptr check for codec before calling uncompress to prevent a crash if no codec is found.
</issue_to_address>

### Comment 3
<location> `presto-native-execution/presto_cpp/main/common/Utils.cpp:128-134` </location>
<code_context>
+
+    return ret;
+  } catch (const std::exception& e) {
+    LOG(ERROR) << "Failed to decompress request body with " << contentEncoding
+               << ": " << e.what() << ". Treating as uncompressed.";
+    // Fall back to treating it as uncompressed
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Falling back to uncompressed extraction on decompression failure may mask client errors.

Returning an error response instead of accepting uncompressed data may prevent processing invalid input when decompression fails.

```suggestion
  } catch (const std::exception& e) {
    LOG(ERROR) << "Failed to decompress request body with " << contentEncoding
               << ": " << e.what() << ". Returning error response.";
    throw std::runtime_error(
        "Failed to decompress request body: " + std::string(e.what()));
  }
}
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +97 to +102
for (const auto& buf : body) {
if (!combined) {
combined = buf->clone();
} else {
combined->prependChain(buf->clone());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Prepending chains may reverse buffer order, potentially altering request body.

Using prependChain here reverses the buffer order. If maintaining the original order is important, use appendChain instead.

}

// Decompress the data
auto codec = folly::compression::getCodec(codecType);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Potential null pointer dereference if getCodec returns nullptr.

Add a nullptr check for codec before calling uncompress to prevent a crash if no codec is found.

shangm2 added a commit to shangm2/presto that referenced this pull request Oct 21, 2025
Summary:

1. add support to decompress thrift request with zstd and compress thrift response for http server

Differential Revision: D85150330
shangm2 added a commit to shangm2/presto that referenced this pull request Oct 21, 2025
Summary:

1. add support to decompress thrift request with zstd and compress thrift response for http server

Differential Revision: D85150330
@shangm2 shangm2 changed the title add http2 data compression for worker Add http2 data compression for worker Oct 21, 2025
@shangm2 shangm2 changed the title Add http2 data compression for worker Add http2 data compression for cpp worker Oct 21, 2025
shangm2 added a commit to shangm2/presto that referenced this pull request Oct 21, 2025
Summary:

1. add support to decompress thrift request with zstd and compress thrift response for http server

Differential Revision: D85150330
shangm2 added a commit to shangm2/presto that referenced this pull request Oct 21, 2025
Summary:

1. add support to decompress thrift request with zstd and compress thrift response for http server

Differential Revision: D85150330
Summary:

1. add support to decompress thrift request with zstd and compress thrift response for http server

Differential Revision: D85150330
@shangm2 shangm2 changed the title Add http2 data compression for cpp worker [chore] Add http2 data compression for cpp worker Oct 24, 2025
@shangm2 shangm2 changed the title [chore] Add http2 data compression for cpp worker chore: Add http2 data compression for cpp worker Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

from:Meta PR from Meta

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants