Repository navigation
feat: support zstd HTTP response compression between Druid services - #20945
Conversation
Replace the deprecated Jetty GzipHandler with Jetty's CompressionHandler, supporting zstd and gzip responses. Among the encodings a client accepts, the server uses the first one in druid.server.http.compression.codecs (default ["zstd", "gzip"]; [] disables response compression). Accept-Encoding is normalized before Jetty negotiates, so codec names match ignoring case and a "*" entry never selects a codec the client declined. Druid's internal HTTP client now sends "Accept-Encoding: zstd, gzip;q=0.5" by default, so services that both run this change use zstd, and fall back to gzip with older services. zstd responses are decoded by Netty's HttpContentDecompressor, which supports zstd when zstd-jni is on the classpath. druid.global.http.compressionCodec and druid.broker.http.compressionCodec still ask for a single codec, and are now validated at startup. deflate is no longer accepted, since Druid servers never sent deflate responses. gzip settings move under druid.server.http.compression.gzip. The old druid.server.http.compressionLevel and druid.server.http.inflateBufferSize properties still apply when the new ones are not set, and log a deprecation warning. inflateBufferSize=0 still disables request decompression.
FrankChen021
left a comment
There was a problem hiding this comment.
🟡 Changes recommended
Preserve method-independent request decompression before merging. The new allow-list omits GET, so gzip-compressed GET bodies can reach existing servlets still compressed, causing a backward-compatibility regression.
Reviewed 32 of 32 changed files; findings in 1 file.
| Severity | Findings |
|---|---|
| P0 | 0 |
| P1 | 0 |
| P2 | 1 |
| P3 | 0 |
| Total | 1 |
This is an automated review by Codex GPT-5.6-Luna(max)
After addressing the findings or replying to the comments, you can request another review from me to trigger a new automated review.
|
@maytasm you can review the change if you're interested in this feature |
FrankChen021
left a comment
There was a problem hiding this comment.
🟡 Changes recommended
Preserve method-independent request decompression for custom methods. The new allow-list omits methods outside Jetty's HttpMethod.values(), so an extension-defined method explicitly permitted by druid.server.http.allowedHttpMethods can reach its servlet with a gzip/zstd request body still compressed.
Reviewed 32 of 32 changed files; findings in 1 file.
Validation: git diff --check passed. No tests or builds were run.
| Severity | Findings |
|---|---|
| P0 | 0 |
| P1 | 0 |
| P2 | 1 |
| P3 | 0 |
| Total | 1 |
This is an automated review by Codex GPT-5.6-Luna(max)
After addressing the findings or replying to the comments, you can request another review from me to trigger a new automated review.
FrankChen021
left a comment
There was a problem hiding this comment.
🟢 Approval recommended
No actionable issues found in this review. The prior request-decompression gap for extension-defined methods is resolved in the current head: configured druid.server.http.allowedHttpMethods entries are now included alongside Jetty-known methods at every production compression-handler call site.
Reviewed 32 of 32 changed files (full PR scope); the 11-file incremental delta was reviewed first.
Validation: git diff --check passed for both the full and incremental ranges. No tests or builds were run.
This is an automated review by Codex GPT-5.6-Luna(max)
| ), | ||
| config.getReadTimeout(), | ||
| config.getCompressionCodec() | ||
| toAcceptEncoding( |
There was a problem hiding this comment.
nit: If zstd-jni can't load in this JVM, this still sends Accept-Encoding: zstd, gzip;q=0.5. Any peer that has zstd enabled will then answer with zstd, and Netty passes those bytes through undecoded. The documented workaround, codecs=["gzip"], only changes what this process's server sends, not what its client asks for (i.e. scenario where a client that can't decode zstd calling a server that offers it). Suggest offering zstd only when Zstd.isAvailable().
There was a problem hiding this comment.
Done in 797dfc0. If the zstd native library cannot be loaded, the client's default Accept-Encoding drops zstd and asks for gzip only, and logs a warning once (HttpClientInit.defaultCompressionCodecs). An explicitly configured compressionCodec=zstd is left as is.
| if (compressionConfig.getCodecs().contains(CompressionCodec.ZSTD)) { | ||
| final ZstandardCompression zstd = new ZstandardCompression(); | ||
| final ZstandardEncoderConfig zstdEncoderConfig = new ZstandardEncoderConfig(); | ||
| zstdEncoderConfig.setCompressionLevel(compressionConfig.getZstd().level()); |
There was a problem hiding this comment.
zstd costs much more memory per in-flight response than gzip, mostly on the client side. This isn't documented.
I measured RSS per open stream with zstd-jni 1.5.7-17. The decoder figures are the client side, where Netty's ZstdDecoder uses ZstdInputStreamNoFinalizer.
| gzip | zstd level 1 | zstd level 19 | |
|---|---|---|---|
| Decoder (client) | 42 KB | 0.4 MB after 64 KB read, 1.0 MB once past the 512 KB window | 5.3 MB after 4 MB read (8 MB window) |
| Encoder (server) | 233 KB (level 6) | 0.9 MB | ≥ 25 MB |
Each zstd decoder also allocates a 128 KB byte[] on the heap when it's created, even for tiny responses.
A Broker holds one decoder for every data-server response in flight, up to numConnections × servers. With the default 20 connections and 400 Historicals, that's up to about 8 GB at level 1, compared with about 340 MB with gzip. The memory is native, outside the heap and outside MaxDirectMemorySize, so JVM limits don't cover it. A Broker whose container is sized to heap plus direct memory can get OOM-killed.
There was a problem hiding this comment.
We should document this. (and that the memory per in-flight response on zstd.level)
There was a problem hiding this comment.
Thanks for measuring this. I re-measured with zstd-jni 1.5.7-17 and Netty 4.2.19, counting only native malloc memory across many concurrent streams, using the same encoder setup as Jetty and Netty's ZstdDecoder:
| Side | gzip (level 6) | zstd level 1 | zstd level 3 | zstd level 9 | zstd level 19 |
|---|---|---|---|---|---|
| Server (encoder) | 250 KB | 0.9 MB | 3 MB | 15 MB | 90 MB |
| Client (decoder) | 40 KB | 0.8 MB | 2.3 MB | 4.5 MB | 8.5 MB |
Level 1 matches your numbers. Level 19 is higher than your table: its match tables take about 80 MB up front, and the decoder grows to about 8.5 MB once the 8 MB window fills. Decoder memory is the window plus about 0.3 MB.
Given this, zstd is now opt-in in 797dfc0: druid.server.http.compression.codecs defaults to ["gzip"]. The docs have a "Memory usage" section with this table, and a new "Enabling zstd" section that recommends enabling it only on the Coordinator for Broker segment polling. If it is enabled on data servers, they suggest druid.broker.http.compressionCodec=gzip on the Broker.
| |`druid.server.http.compressionLevel`|The compression level. Value should be between [-1,9], -1 for default level, 0 for no compression.|-1 (default compression level)| | ||
| |`druid.server.http.inflateBufferSize`|The buffer size used by gzip decoder. Set to 0 to disable request decompression.|4096| | ||
| |`druid.server.http.compression.codecs`|Ordered list of response encodings the server may use, most preferred first. Supported values are `zstd` and `gzip`. Set to `["gzip"]` to use gzip only, as in earlier Druid versions. Set to `[]` to disable response compression.|`["zstd", "gzip"]`| | ||
| |`druid.server.http.compression.zstd.level`|zstd compression level, between 1 and 19. Higher levels produce smaller responses but use more CPU.|1| |
There was a problem hiding this comment.
Please mention memory here too: a higher level raises memory per concurrent response on both the server and the client (see the comment on JettyServerInitUtils).
There was a problem hiding this comment.
Done in 797dfc0. The zstd.level row now says that higher levels also use more memory per in-flight response on both the server and the client, and links to the "Memory usage" section.
| * {@link CompressionHandler} and the handler it wraps, then restores the header the client sent, so that servlets, | ||
| * including the proxy servlets that forward the header, see it unchanged. Use {@link #wrap} to set up both. | ||
| * | ||
| * This class can be removed once Jetty fixes these bugs. |
There was a problem hiding this comment.
Do you have the link or ticket to Jetty bug? Would be good to link it here otherwise it's hard to keep track
There was a problem hiding this comment.
I couldn't find a Jetty issue for either bug, so there is nothing to link yet. To keep track of it, 797dfc0 adds testJettyStillHasNegotiationBugs, which runs Jetty's CompressionHandler without the workaround and asserts that both bugs still occur. When a Jetty upgrade fixes either one, the test fails with a message to consider removing NormalizeAcceptEncodingHandler.
…t when unavailable
FrankChen021
left a comment
There was a problem hiding this comment.
🟢 Approval recommended
No actionable issues found in this review. The final changes keep server-side zstd opt-in, gate the default client advertisement on native zstd availability, and preserve HTTP negotiation and legacy configuration behavior.
Reviewed 32 of 32 changed files; the 9-file incremental delta was reviewed first.
This is an automated review by Codex GPT-5.6-Luna(max)
FrankChen021
left a comment
There was a problem hiding this comment.
🟢 Approval recommended
No actionable issues found in this review. The final head preserves zstd opt-in, native-library fallback to gzip, legacy configuration behavior, allowed-method request decompression, and the managed zstd-jni dependency.
Reviewed 32 of 32 changed files; the incremental delta was reviewed first, followed by the full PR scope.
Validation: git diff --check passed. No tests or builds were run.
This is an automated review by Codex GPT-5.6-Luna(max)
amaechler
left a comment
There was a problem hiding this comment.
A handful of bot findings to consider.
…ng, and keep a single client codec - Exclude zstd from request decompression, since Jetty's zstd decoder has no window limit. NormalizeAcceptEncodingHandler lowercases Content-Encoding so that the exclusion also applies to ZSTD. - Ignore a malformed Accept-Encoding header and respond uncompressed, as the old GzipHandler did, instead of 500. - Restore HttpClientConfig's single nullable compressionCodec, and send a fixed default Accept-Encoding.
FrankChen021
left a comment
There was a problem hiding this comment.
🟢 Approval recommended
No actionable issues found. The incremental scope was reviewed first, followed by the complete diff and surrounding code; the zstd HTTP response-compression changes remain coherent across configuration, request/response handling, and tests.
Reviewed 32 of 32 changed files; incremental scope covered 9 files.
Validation: git diff --check passed for both the full and incremental ranges. No tests or builds were run.
This is an automated review by Codex GPT-5.6-Luna(max)
|
Thanks for 05a2f14, the zstd request-body exclusion with the Two points from the review summary are still open: Deflater leak when a response is aborted (P2). Jetty 12.1's Release note (P2). The "Release note" section still leaves out the changes users will notice. Suggested addition:
|
|
@amaechler Thanks. Deflater leak: confirmed on the ee8 path. In a test that aborted 20 streamed gzip responses, 13 Deflaters stayed in use, and after a GC all were reclaimed as leaked. This is already tracked in Jetty as jetty/jetty.project#16064, with a fix in jetty/jetty.project#16196 that a later Jetty upgrade will pick up. The release note lists it as a known issue until then. Release note: added your suggested items, plus the |
|
@maytasm @amaechler Thanks for the review |
Fixes #20918.
Description
With
metadataSegmentCacheEnableon, every Broker downloads the full used-segment list from the Coordinator every minute. With ~77k segments that is ~137 MiB of JSON, and gzip at level 6 took ~41% of Coordinator CPU. zstd at level 1 compresses ~6× faster and produces ~27% smaller output.Server: Jetty
CompressionHandlerwith zstd and gzipGzipHandlerwith Jetty 12.1'sCompressionHandler, registeringzstdandgzip. Among the encodings the client accepts, the server uses the first one indruid.server.http.compression.codecs.codecsdefaults to["gzip"], because zstd uses much more native memory per response in flight than gzip, on both the server and the client (see the memory table below). zstd is registered only when it is in the list, so the default does not load the zstd native library on the server.NormalizeAcceptEncodingHandlerworks around two Jetty negotiation bugs: a*entry could pick a codec the client declined withq=0, and the gzip exclusion was case-sensitive. The handler normalizesAccept-Encodingbefore Jetty negotiates and restores the original header for servlets.jetty-compression-serverandjetty-compression-zstandard.zstd-jniis excluded fromjetty-compression-zstandard, so Druid keeps a single managed version of the native library, asparquet-extensionsdoes.Client: Netty HTTP client asks for zstd
Accept-Encoding: zstd, gzip;q=0.5by default, so enabling zstd on a server is enough to use it. This does not depend on the server's codec list. If the zstd native library cannot be loaded, the client asks for gzip only.HttpContentDecompressor, which supports zstd whenzstd-jniis on the classpath, so no custom decoder is needed.druid.global.http.compressionCodec/druid.broker.http.compressionCodecstill ask for a single codec. They are now parsed asHttpClientConfig.CompressionCodec(case-insensitive), so an invalid value fails at startup with a message listing the supported codecs (identity,gzip,zstd). Thedeflatecodec is removed: it was undocumented, and no Druid server ever sent deflate responses, so it behaved likeidentity.Configuration
druid.server.http.compression.codecs["gzip"]["zstd", "gzip"]enables zstd;[]disables response compression. Duplicates are ignored;identityis rejected.druid.server.http.compression.zstd.level1druid.server.http.compression.gzip.level-1(level 6)druid.server.http.compression.gzip.inflateBufferSize40960disables request decompression for all encodings, asinflateBufferSize=0did beforedruid.server.http.compressionLevelcompression.gzip.level; logs a warningdruid.server.http.inflateBufferSizecompression.gzip.inflateBufferSize; logs a warningThe deprecated properties apply only when the new ones are not set. Level validation lives in the
HttpServerCompression.ZstdandHttpServerCompression.Gziprecords.Memory
Native memory per response in flight, measured with zstd-jni 1.5.7-17 and Netty 4.2.19, for a response larger than the zstd window (512 KB at level 1, 8 MB at level 19). It is allocated by zstd's C code, outside the heap and
MaxDirectMemorySize.A Broker decodes one response per data-server request in flight, so zstd on Historicals could use several GB on a large cluster. The docs recommend enabling zstd only where it pays off, such as on the Coordinator for Broker segment polling.
Compatibility
Encoding is negotiated per request, so a rolling upgrade works in any order. zstd is used only between a new client and a new server with zstd enabled; every other combination uses gzip.
Other behavior changes:
Scope
zstd is used for traffic sent through Druid's internal HTTP client without its own
Accept-Encoding, when the server runs this change withzstdenabled indruid.server.http.compression.codecs. Examples: Broker → Historical/Peon query responses, Broker → Coordinator segment polling, and Coordinator → Historical/Peon inventory sync and load/drop.Unchanged:
Accept-Encoding: identity.compressionCodecoverride.Results on a test cluster
Measured on the Coordinator after upgrading the Coordinators and Brokers and enabling zstd on the Coordinators, so Broker segment polls use zstd:
Release note
Druid services can now compress HTTP responses with zstd. It is not enabled by default; set
druid.server.http.compression.codecsto["zstd", "gzip"]on a service to use zstd for clients that accept it, such as other Druid services. For example, enabling it on the Coordinator reduces the CPU spent compressing the segment lists that Brokers poll.druid.server.http.compressionLevelanddruid.server.http.inflateBufferSize. Usedruid.server.http.compression.gzip.levelanddruid.server.http.compression.gzip.inflateBufferSizeinstead.Accept-Encoding: zstd, gzip;q=0.5by default and loads the zstd native library at startup to do so. If the library cannot be loaded, it asks for gzip only.druid.global.http.compressionCodecanddruid.broker.http.compressionCodecnow accept onlyidentity,gzipandzstd. A value ofdeflatenow fails at startup; it never produced deflate responses, so replace it withidentityto keep the same behavior.Accept-Encodingwithidentity;q=0and no supported codec now get 415 instead of an uncompressed 200.EncoderSink.release()is called when the client closes the connection during a compressed servlet response jetty/jetty.project#16196, which a later Jetty upgrade will pick up.ServerConfig's constructor now takes anHttpServerCompressioninstead ofinflateBufferSizeandcompressionLevel, andJettyServerInitUtils.wrapWithDefaultGzipHandleris replaced bywrapWithCompressionHandler.HttpClientConfig.DEFAULT_COMPRESSION_CODECis removed, andHttpClientConfig.getCompressionCodec()returns null when no codec is set, in which case the client uses its defaultAccept-Encoding.Key changed/added classes in this PR
HttpServerCompressionServerConfigJettyServerInitUtilsNormalizeAcceptEncodingHandlerHttpClientConfigNettyHttpClientDruidHttpClientConfigHttpClientModuleThis PR has:
licenses.yaml