-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compress large prepared statements in HTTP headers
This allows working with larger prepared statements before hitting header size limits (on either server, or intermediate proxy). Since the client should not make assumption on any specific format of prepared statements in the headers, the compression is applied unconditionally.
- Loading branch information
Showing
11 changed files
with
182 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
core/trino-main/src/main/java/io/trino/server/protocol/PreparedStatementEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.server.protocol; | ||
|
||
import io.airlift.compress.zstd.ZstdCompressor; | ||
import io.airlift.compress.zstd.ZstdDecompressor; | ||
import io.trino.server.ProtocolConfig; | ||
|
||
import javax.inject.Inject; | ||
|
||
import static com.google.common.io.BaseEncoding.base64Url; | ||
import static java.lang.Math.toIntExact; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class PreparedStatementEncoder | ||
{ | ||
// No valid SQL statement starts with $ | ||
private static final String PREFIX = "$zstd:"; | ||
|
||
private final int compressionThreshold; | ||
private final int compressionMinGain; | ||
|
||
@Inject | ||
public PreparedStatementEncoder(ProtocolConfig protocolConfig) | ||
{ | ||
requireNonNull(protocolConfig, "protocolConfig is null"); | ||
this.compressionThreshold = protocolConfig.getPreparedStatementCompressionThreshold(); | ||
this.compressionMinGain = protocolConfig.getPreparedStatementCompressionMinimalGain(); | ||
} | ||
|
||
public String encodePreparedStatementForHeader(String preparedStatement) | ||
{ | ||
if (preparedStatement.length() < compressionThreshold) { | ||
return preparedStatement; | ||
} | ||
|
||
ZstdCompressor compressor = new ZstdCompressor(); | ||
byte[] inputBytes = preparedStatement.getBytes(UTF_8); | ||
byte[] compressed = new byte[compressor.maxCompressedLength(inputBytes.length)]; | ||
int outputSize = compressor.compress(inputBytes, 0, inputBytes.length, compressed, 0, compressed.length); | ||
String encoded = base64Url().encode(compressed, 0, outputSize); | ||
|
||
if (encoded.length() + PREFIX.length() + compressionMinGain > preparedStatement.length()) { | ||
return preparedStatement; | ||
} | ||
return PREFIX + encoded; | ||
} | ||
|
||
public String decodePreparedStatementFromHeader(String headerValue) | ||
{ | ||
if (!headerValue.startsWith(PREFIX)) { | ||
return headerValue; | ||
} | ||
|
||
String encoded = headerValue.substring(PREFIX.length()); | ||
byte[] compressed = base64Url().decode(encoded); | ||
|
||
byte[] preparedStatement = new byte[toIntExact(ZstdDecompressor.getDecompressedSize(compressed, 0, compressed.length))]; | ||
new ZstdDecompressor().decompress(compressed, 0, compressed.length, preparedStatement, 0, preparedStatement.length); | ||
return new String(preparedStatement, UTF_8); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.