Skip to content

Commit 0fe96fc

Browse files
Chaho12Jaeho Yoo
authored andcommitted
Add support for request and response compression
1 parent 016c21f commit 0fe96fc

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

gateway-ha/src/main/java/io/trino/gateway/proxyserver/ProxyRequestHandler.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ private void performRequest(
201201
for (String name : list(servletRequest.getHeaderNames())) {
202202
for (String value : list(servletRequest.getHeaders(name))) {
203203
// TODO: decide what should and shouldn't be forwarded
204-
if (!name.equalsIgnoreCase("Accept-Encoding")
205-
&& !name.equalsIgnoreCase("Host")
204+
if (!name.equalsIgnoreCase("Host")
206205
&& (addXForwardedHeaders || !name.startsWith("X-Forwarded"))) {
207206
requestBuilder.addHeader(name, value);
208207
}
@@ -262,7 +261,7 @@ else if (servletRequest.getCookies() != null) {
262261

263262
private Response buildResponse(ProxyResponse response, ImmutableList<NewCookie> cookie)
264263
{
265-
Response.ResponseBuilder builder = Response.status(response.statusCode()).entity(response.body());
264+
Response.ResponseBuilder builder = Response.status(response.statusCode()).entity(response.getRawBody());
266265
response.headers().forEach((headerName, value) -> builder.header(headerName.toString(), value));
267266
cookie.forEach(builder::cookie);
268267
return builder.build();
@@ -287,26 +286,26 @@ private FluentFuture<ProxyResponse> executeHttp(Request request)
287286
private ProxyResponse recordBackendForQueryId(Request request, ProxyResponse response, Optional<String> username,
288287
RoutingDestination routingDestination)
289288
{
290-
log.debug("For Request [%s] got Response [%s]", request.getUri(), response.body());
289+
log.debug("For Request [%s] got Response [%s]", request.getUri(), response.getDecompressedBody());
291290

292291
QueryHistoryManager.QueryDetail queryDetail = getQueryDetailsFromRequest(request, username);
293292

294293
log.debug("Extracting proxy destination : [%s] for request : [%s]", queryDetail.getBackendUrl(), request.getUri());
295294

296295
if (response.statusCode() == OK.getStatusCode()) {
297296
try {
298-
HashMap<String, String> results = OBJECT_MAPPER.readValue(response.body(), HashMap.class);
297+
HashMap<String, String> results = OBJECT_MAPPER.readValue(response.getDecompressedBody(), HashMap.class);
299298
queryDetail.setQueryId(results.get("id"));
300299
routingManager.setBackendForQueryId(queryDetail.getQueryId(), queryDetail.getBackendUrl());
301300
routingManager.setRoutingGroupForQueryId(queryDetail.getQueryId(), routingDestination.routingGroup());
302301
log.debug("QueryId [%s] mapped with proxy [%s]", queryDetail.getQueryId(), queryDetail.getBackendUrl());
303302
}
304303
catch (IOException e) {
305-
log.error("Failed to get QueryId from response [%s] , Status code [%s]", response.body(), response.statusCode());
304+
log.error("Failed to get QueryId from response [%s] , Status code [%s]", response.getDecompressedBody(), response.statusCode());
306305
}
307306
}
308307
else {
309-
log.error("Non OK HTTP Status code with response [%s] , Status code [%s], user: [%s]", response.body(), response.statusCode(), username.orElse(null));
308+
log.error("Non OK HTTP Status code with response [%s] , Status code [%s], user: [%s]", response.getDecompressedBody(), response.statusCode(), username.orElse(null));
310309
}
311310
queryDetail.setRoutingGroup(routingDestination.routingGroup());
312311
queryDetail.setExternalUrl(routingDestination.externalUrl());

gateway-ha/src/main/java/io/trino/gateway/proxyserver/ProxyResponseHandler.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
import io.trino.gateway.ha.config.ProxyResponseConfiguration;
2323
import io.trino.gateway.proxyserver.ProxyResponseHandler.ProxyResponse;
2424

25+
import java.io.ByteArrayInputStream;
2526
import java.io.IOException;
27+
import java.io.InputStream;
2628
import java.nio.charset.StandardCharsets;
29+
import java.util.zip.GZIPInputStream;
2730

2831
import static java.util.Objects.requireNonNull;
2932

@@ -47,7 +50,9 @@ public ProxyResponse handleException(Request request, Exception exception)
4750
public ProxyResponse handle(Request request, Response response)
4851
{
4952
try {
50-
return new ProxyResponse(response.getStatusCode(), response.getHeaders(), new String(response.getInputStream().readNBytes((int) responseSize.toBytes()), StandardCharsets.UTF_8));
53+
// Store raw bytes to preserve compression
54+
byte[] responseBodyBytes = response.getInputStream().readNBytes((int) responseSize.toBytes());
55+
return new ProxyResponse(response.getStatusCode(), response.getHeaders(), responseBodyBytes);
5156
}
5257
catch (IOException e) {
5358
throw new ProxyException("Failed reading response from remote Trino server", e);
@@ -57,11 +62,53 @@ public ProxyResponse handle(Request request, Response response)
5762
public record ProxyResponse(
5863
int statusCode,
5964
ListMultimap<HeaderName, String> headers,
60-
String body)
65+
byte[] body)
6166
{
6267
public ProxyResponse
6368
{
6469
requireNonNull(headers, "headers is null");
70+
requireNonNull(body, "body is null");
71+
}
72+
73+
/**
74+
* Get the response body as raw bytes for sending to clients (preserves
75+
* compression)
76+
*/
77+
public byte[] getRawBody()
78+
{
79+
return body;
80+
}
81+
82+
/**
83+
* Get the response body as a decompressed string for JSON parsing and logging.
84+
* Only call this when you need to parse the content, not when passing through
85+
* to clients.
86+
*/
87+
public String getDecompressedBody()
88+
{
89+
// Check if the response is gzip-compressed
90+
String contentEncoding = null;
91+
for (HeaderName headerName : headers.keySet()) {
92+
if (headerName.toString().equalsIgnoreCase("Content-Encoding")) {
93+
contentEncoding = headers.get(headerName).iterator().next();
94+
break;
95+
}
96+
}
97+
98+
if ("gzip".equalsIgnoreCase(contentEncoding)) {
99+
try {
100+
try (InputStream inputStream = new GZIPInputStream(new ByteArrayInputStream(body))) {
101+
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
102+
}
103+
}
104+
catch (IOException e) {
105+
// If decompression fails, return the body as UTF-8 string
106+
return new String(body, StandardCharsets.UTF_8);
107+
}
108+
}
109+
110+
// Not compressed, convert bytes to string
111+
return new String(body, StandardCharsets.UTF_8);
65112
}
66113
}
67114
}

0 commit comments

Comments
 (0)