Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP API calls hang with 'Accept-Encoding: zstd' #17408

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix exists queries on nested flat_object fields throws exception ([#16803](https://github.com/opensearch-project/OpenSearch/pull/16803))
- Add highlighting for wildcard search on `match_only_text` field ([#17101](https://github.com/opensearch-project/OpenSearch/pull/17101))
- Fix illegal argument exception when creating a PIT ([#16781](https://github.com/opensearch-project/OpenSearch/pull/16781))
- Fix HTTP API calls that hang with 'Accept-Encoding: zstd' ([#17408](https://github.com/opensearch-project/OpenSearch/pull/17408))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@

import java.net.InetSocketAddress;
import java.net.SocketOption;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.netty.bootstrap.ServerBootstrap;
Expand All @@ -77,6 +79,12 @@
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.nio.NioChannelOption;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.compression.Brotli;
import io.netty.handler.codec.compression.CompressionOptions;
import io.netty.handler.codec.compression.DeflateOptions;
import io.netty.handler.codec.compression.GzipOptions;
import io.netty.handler.codec.compression.StandardCompressionOptions;
import io.netty.handler.codec.compression.ZstdEncoder;
import io.netty.handler.codec.http.HttpContentCompressor;
import io.netty.handler.codec.http.HttpContentDecompressor;
import io.netty.handler.codec.http.HttpMessage;
Expand Down Expand Up @@ -440,7 +448,7 @@
pipeline.addAfter(
"aggregator",
"encoder_compress",
new HttpContentCompressor(handlingSettings.getCompressionLevel())
new HttpContentCompressor(defaultCompressionOptions(handlingSettings.getCompressionLevel()))
);
}
pipeline.addBefore("handler", "request_creator", requestCreator);
Expand All @@ -467,7 +475,10 @@
aggregator.setMaxCumulationBufferComponents(transport.maxCompositeBufferComponents);
pipeline.addLast("aggregator", aggregator);
if (handlingSettings.isCompression()) {
pipeline.addLast("encoder_compress", new HttpContentCompressor(handlingSettings.getCompressionLevel()));
pipeline.addLast(
"encoder_compress",
new HttpContentCompressor(defaultCompressionOptions(handlingSettings.getCompressionLevel()))
);
}
pipeline.addLast("request_creator", requestCreator);
pipeline.addLast("response_creator", responseCreator);
Expand Down Expand Up @@ -512,7 +523,10 @@

if (handlingSettings.isCompression()) {
childChannel.pipeline()
.addLast("encoder_compress", new HttpContentCompressor(handlingSettings.getCompressionLevel()));
.addLast(

Check warning on line 526 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4HttpServerTransport.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4HttpServerTransport.java#L526

Added line #L526 was not covered by tests
"encoder_compress",
new HttpContentCompressor(defaultCompressionOptions(handlingSettings.getCompressionLevel()))

Check warning on line 528 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4HttpServerTransport.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4HttpServerTransport.java#L528

Added line #L528 was not covered by tests
);
}

childChannel.pipeline()
Expand Down Expand Up @@ -563,4 +577,59 @@
protected ChannelInboundHandlerAdapter createDecompressor() {
return new HttpContentDecompressor();
}

/**
* Copy of {@link HttpContentCompressor} default compression options with ZSTD excluded:
* although zstd-jni is on the classpath, {@link ZstdEncoder} requires direct buffers support
* which by default {@link NettyAllocator} does not provide.
*
* @param compressionLevel
* {@code 1} yields the fastest compression and {@code 9} yields the
* best compression. {@code 0} means no compression. The default
* compression level is {@code 6}.
*
* @return default compression options
*/
private static CompressionOptions[] defaultCompressionOptions(int compressionLevel) {
return defaultCompressionOptions(compressionLevel, 15, 8);
}

/**
* Copy of {@link HttpContentCompressor} default compression options with ZSTD excluded:
* although zstd-jni is on the classpath, {@link ZstdEncoder} requires direct buffers support
* which by default {@link NettyAllocator} does not provide.
*
* @param compressionLevel
* {@code 1} yields the fastest compression and {@code 9} yields the
* best compression. {@code 0} means no compression. The default
* compression level is {@code 6}.
* @param windowBits
* The base two logarithm of the size of the history buffer. The
* value should be in the range {@code 9} to {@code 15} inclusive.
* Larger values result in better compression at the expense of
* memory usage. The default value is {@code 15}.
* @param memLevel
* How much memory should be allocated for the internal compression
* state. {@code 1} uses minimum memory and {@code 9} uses maximum
* memory. Larger values result in better and faster compression
* at the expense of memory usage. The default value is {@code 8}
*
* @return default compression options
*/
private static CompressionOptions[] defaultCompressionOptions(int compressionLevel, int windowBits, int memLevel) {
final List<CompressionOptions> options = new ArrayList<CompressionOptions>(4);
final GzipOptions gzipOptions = StandardCompressionOptions.gzip(compressionLevel, windowBits, memLevel);
final DeflateOptions deflateOptions = StandardCompressionOptions.deflate(compressionLevel, windowBits, memLevel);

options.add(gzipOptions);
options.add(deflateOptions);
options.add(StandardCompressionOptions.snappy());

if (Brotli.isAvailable()) {
options.add(StandardCompressionOptions.brotli());

Check warning on line 629 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4HttpServerTransport.java

View check run for this annotation

Codecov / codecov/patch

modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4HttpServerTransport.java#L629

Added line #L629 was not covered by tests
}

return options.toArray(new CompressionOptions[0]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,10 @@ public void dispatchBadRequest(final RestChannel channel, final ThreadContext th

try (Netty4HttpClient client = Netty4HttpClient.http()) {
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, url);
request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, randomFrom("deflate", "gzip"));
// ZSTD is not supported at the moment by NettyAllocator (needs direct buffers),
// and Brotly is not on classpath.
final String contentEncoding = randomFrom("deflate", "gzip", "snappy", "br", "zstd");
request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, contentEncoding);
long numOfHugeAllocations = getHugeAllocationCount();
final FullHttpResponse response = client.send(remoteAddress.address(), request);
try {
Expand Down
Loading