GeezerVison is showing github.com — Fix Zstd compression for large data (#15861) (#15900) · netty/netty@4647500
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty/handler/codec/compression test/java/io/netty/handler/codec/compression
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: codec/src/main/java/io/netty/handler/codec/compression/StandardCompressionOptions.java +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -71,7 +71,7 @@ public static BrotliOptions brotli(int quality, int window, BrotliMode mode) { | 71 | 71 | /** | 72 | 72 | * Default implementation of {@link ZstdOptions} with{compressionLevel(int)} set to | 73 | 73 | * {@link ZstdConstants#DEFAULT_COMPRESSION_LEVEL},{@link ZstdConstants#DEFAULT_BLOCK_SIZE}, | 74 | | - * {@link ZstdConstants#MAX_BLOCK_SIZE} | | 74 | + * {@link ZstdConstants#DEFAULT_MAX_ENCODE_SIZE} | 75 | 75 | */ | 76 | 76 | public static ZstdOptions zstd() { | 77 | 77 | return ZstdOptions.DEFAULT; | |
Collapse file Copy file name to clipboardExpand all lines: codec/src/main/java/io/netty/handler/codec/compression/ZstdConstants.java +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,9 +35,9 @@ final class ZstdConstants { | 35 | 35 | static final int MAX_COMPRESSION_LEVEL = Zstd.maxCompressionLevel(); | 36 | 36 |
| 37 | 37 | /** | 38 | | - * Max block size | | 38 | + * Max encode size | 39 | 39 | */ | 40 | | - static final int MAX_BLOCK_SIZE = 1 << (DEFAULT_COMPRESSION_LEVEL + 7) + 0x0F; // 32 M | | 40 | + static final int DEFAULT_MAX_ENCODE_SIZE = Integer.MAX_VALUE; | 41 | 41 | /** | 42 | 42 | * Default block size | 43 | 43 | */ | |
Collapse file Copy file name to clipboardExpand all lines: codec/src/main/java/io/netty/handler/codec/compression/ZstdEncoder.java +6-4Lines changed: 6 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 28 | 28 | import static io.netty.handler.codec.compression.ZstdConstants.MIN_COMPRESSION_LEVEL; | 29 | 29 | import static io.netty.handler.codec.compression.ZstdConstants.MAX_COMPRESSION_LEVEL; | 30 | 30 | import static io.netty.handler.codec.compression.ZstdConstants.DEFAULT_BLOCK_SIZE; | 31 | | -import static io.netty.handler.codec.compression.ZstdConstants.MAX_BLOCK_SIZE; | | 31 | +import static io.netty.handler.codec.compression.ZstdConstants.DEFAULT_MAX_ENCODE_SIZE; | 32 | 32 |
| 33 | 33 | /** | 34 | 34 | * Compresses a {@link ByteBuf} using the Zstandard algorithm. | @@ -56,7 +56,7 @@ public final class ZstdEncoder extends MessageToByteEncoder<ByteBuf> { | 56 | 56 | * please use {@link ZstdEncoder(int,int)} constructor | 57 | 57 | */ | 58 | 58 | public ZstdEncoder() { | 59 | | - this(DEFAULT_COMPRESSION_LEVEL, DEFAULT_BLOCK_SIZE, MAX_BLOCK_SIZE); | | 59 | + this(DEFAULT_COMPRESSION_LEVEL, DEFAULT_BLOCK_SIZE, DEFAULT_MAX_ENCODE_SIZE); | 60 | 60 | } | 61 | 61 |
| 62 | 62 | /** | @@ -65,7 +65,7 @@ public ZstdEncoder() { | 65 | 65 | * specifies the level of the compression | 66 | 66 | */ | 67 | 67 | public ZstdEncoder(int compressionLevel) { | 68 | | - this(compressionLevel, DEFAULT_BLOCK_SIZE, MAX_BLOCK_SIZE); | | 68 | + this(compressionLevel, DEFAULT_BLOCK_SIZE, DEFAULT_MAX_ENCODE_SIZE); | 69 | 69 | } | 70 | 70 |
| 71 | 71 | /** | @@ -113,7 +113,9 @@ protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean | 113 | 113 | while (remaining > 0) { | 114 | 114 | int curSize = Math.min(blockSize, remaining); | 115 | 115 | remaining -= curSize; | 116 | | - bufferSize += Zstd.compressBound(curSize); | | 116 | + // calculate the max compressed size with Zstd.compressBound since | | 117 | + // it returns the maximum size of the compressed data | | 118 | + bufferSize = Math.max(bufferSize, Zstd.compressBound(curSize)); | 117 | 119 | } | 118 | 120 |
| 119 | 121 | if (bufferSize > maxEncodeSize || 0 > bufferSize) { | |
Collapse file Copy file name to clipboardExpand all lines: codec/src/main/java/io/netty/handler/codec/compression/ZstdOptions.java +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | import static io.netty.handler.codec.compression.ZstdConstants.MIN_COMPRESSION_LEVEL; | 22 | 22 | import static io.netty.handler.codec.compression.ZstdConstants.MAX_COMPRESSION_LEVEL; | 23 | 23 | import static io.netty.handler.codec.compression.ZstdConstants.DEFAULT_BLOCK_SIZE; | 24 | | -import static io.netty.handler.codec.compression.ZstdConstants.MAX_BLOCK_SIZE; | | 24 | +import static io.netty.handler.codec.compression.ZstdConstants.DEFAULT_MAX_ENCODE_SIZE; | 25 | 25 |
| 26 | 26 | /** | 27 | 27 | * {@link ZstdOptions} holds compressionLevel for | @@ -36,9 +36,10 @@ public class ZstdOptions implements CompressionOptions { | 36 | 36 | /** | 37 | 37 | * Default implementation of {@link ZstdOptions} with{compressionLevel(int)} set to | 38 | 38 | * {@link ZstdConstants#DEFAULT_COMPRESSION_LEVEL},{@link ZstdConstants#DEFAULT_BLOCK_SIZE}, | 39 | | - * {@link ZstdConstants#MAX_BLOCK_SIZE} | | 39 | + * {@link ZstdConstants#DEFAULT_MAX_ENCODE_SIZE} | 40 | 40 | */ | 41 | | - static final ZstdOptions DEFAULT = new ZstdOptions(DEFAULT_COMPRESSION_LEVEL, DEFAULT_BLOCK_SIZE, MAX_BLOCK_SIZE); | | 41 | + static final ZstdOptions DEFAULT = new ZstdOptions(DEFAULT_COMPRESSION_LEVEL, DEFAULT_BLOCK_SIZE, | | 42 | + DEFAULT_MAX_ENCODE_SIZE); | 42 | 43 |
| 43 | 44 | /** | 44 | 45 | * Create a new {@link ZstdOptions} | |
Collapse file Copy file name to clipboardExpand all lines: codec/src/test/java/io/netty/handler/codec/compression/ZstdEncoderTest.java +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -49,6 +49,14 @@ public void setup() { | 49 | 49 | when(ctx.alloc()).thenReturn(ByteBufAllocator.DEFAULT); | 50 | 50 | } | 51 | 51 |
| | 52 | + public static ByteBuf[] hugeData() { | | 53 | + final byte[] bytesHuge = new byte[36 * 1024 * 1024]; | | 54 | + ByteBuf heap = Unpooled.wrappedBuffer(bytesHuge); | | 55 | + ByteBuf direct = Unpooled.directBuffer(bytesHuge.length); | | 56 | + direct.writeBytes(bytesHuge); | | 57 | + return new ByteBuf[] {heap, direct}; | | 58 | + } | | 59 | + | 52 | 60 | @Override | 53 | 61 | public EmbeddedChannel createChannel() { | 54 | 62 | return new EmbeddedChannel(new ZstdEncoder()); | @@ -57,6 +65,16 @@ public EmbeddedChannel createChannel() { | 57 | 65 | @ParameterizedTest | 58 | 66 | @MethodSource("largeData") | 59 | 67 | public void testCompressionOfLargeBatchedFlow(final ByteBuf data) throws Exception { | | 68 | + testCompressionOfLargeDataBatchedFlow(data); | | 69 | + } | | 70 | + | | 71 | + @ParameterizedTest | | 72 | + @MethodSource("hugeData") | | 73 | + public void testCompressionOfHugeBatchedFlow(final ByteBuf data) throws Exception { | | 74 | + testCompressionOfLargeDataBatchedFlow(data); | | 75 | + } | | 76 | + | | 77 | + private void testCompressionOfLargeDataBatchedFlow(final ByteBuf data) throws Exception { | 60 | 78 | final int dataLength = data.readableBytes(); | 61 | 79 | int written = 0; | 62 | 80 |
| |
Collapse file Copy file name to clipboardExpand all lines: codec/src/test/java/io/netty/handler/codec/compression/ZstdIntegrationTest.java +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 17 | 17 |
| 18 | 18 | import io.netty.channel.embedded.EmbeddedChannel; | 19 | 19 |
| 20 | | -import static io.netty.handler.codec.compression.ZstdConstants.MAX_BLOCK_SIZE; | | 20 | +import static io.netty.handler.codec.compression.ZstdConstants.DEFAULT_MAX_ENCODE_SIZE; | 21 | 21 |
| 22 | 22 | public class ZstdIntegrationTest extends AbstractIntegrationTest { | 23 | 23 |
| 24 | 24 | private static final int BLOCK_SIZE = 1 << 20; | 25 | 25 |
| 26 | 26 | @Override | 27 | 27 | protected EmbeddedChannel createEncoder() { | 28 | | - return new EmbeddedChannel(new ZstdEncoder(BLOCK_SIZE, MAX_BLOCK_SIZE)); | | 28 | + return new EmbeddedChannel(new ZstdEncoder(BLOCK_SIZE, DEFAULT_MAX_ENCODE_SIZE)); | 29 | 29 | } | 30 | 30 |
| 31 | 31 | @Override | |
|