Skip to content

Commit a99357f

Browse files
andythsuasu80
authored andcommitted
Convert byte array to string for logging in ProxyResponse
1 parent 531929a commit a99357f

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

docs/installation.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ serverConfig:
212212
http-server.process-forwarded: true
213213
```
214214

215+
## Configure larger proxy response size
216+
217+
Trino Gateway reads the response from Trino in bytes (up to 32MB by default).
218+
It can be configured by setting:
219+
220+
```yaml
221+
maxProxyResponseSize: 50MB
222+
```
223+
215224
## Running Trino Gateway
216225

217226
Start Trino Gateway with the following java command in the directory of the

gateway-ha/src/main/java/io/trino/gateway/ha/config/HaGatewayConfiguration.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515

1616
import com.google.common.collect.ImmutableList;
1717
import com.google.common.collect.Streams;
18+
import io.airlift.units.DataSize;
1819

1920
import java.util.ArrayList;
2021
import java.util.HashMap;
2122
import java.util.List;
2223
import java.util.Map;
2324

25+
import static io.airlift.units.DataSize.Unit.MEGABYTE;
2426
import static io.trino.gateway.ha.handler.HttpUtils.V1_STATEMENT_PATH;
2527

2628
public class HaGatewayConfiguration
@@ -42,6 +44,7 @@ public class HaGatewayConfiguration
4244
private GatewayCookieConfiguration gatewayCookieConfiguration = new GatewayCookieConfiguration();
4345
private List<String> statementPaths = ImmutableList.of(V1_STATEMENT_PATH);
4446
private boolean includeClusterHostInResponse;
47+
private DataSize maxProxyResponseSize = DataSize.of(32, MEGABYTE);
4548

4649
private RequestAnalyzerConfig requestAnalyzerConfig = new RequestAnalyzerConfig();
4750

@@ -255,6 +258,16 @@ public void setIncludeClusterHostInResponse(boolean includeClusterHostInResponse
255258
this.includeClusterHostInResponse = includeClusterHostInResponse;
256259
}
257260

261+
public DataSize getMaxProxyResponseSize()
262+
{
263+
return this.maxProxyResponseSize;
264+
}
265+
266+
public void setProxyResponseConfiguration(DataSize maxProxyResponseSize)
267+
{
268+
this.maxProxyResponseSize = maxProxyResponseSize;
269+
}
270+
258271
private void validateStatementPath(String statementPath, List<String> statementPaths)
259272
{
260273
if (statementPath.startsWith(V1_STATEMENT_PATH) ||

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.airlift.http.client.Request;
2323
import io.airlift.http.client.StaticBodyGenerator;
2424
import io.airlift.log.Logger;
25+
import io.airlift.units.DataSize;
2526
import io.airlift.units.Duration;
2627
import io.trino.gateway.ha.config.GatewayCookieConfigurationPropertiesProvider;
2728
import io.trino.gateway.ha.config.HaGatewayConfiguration;
@@ -87,6 +88,7 @@ public class ProxyRequestHandler
8788
private final List<String> statementPaths;
8889
private final boolean includeClusterInfoInResponse;
8990
private final TrinoRequestUser.TrinoRequestUserProvider trinoRequestUserProvider;
91+
private final DataSize maxProxyResponseSize;
9092

9193
@Inject
9294
public ProxyRequestHandler(
@@ -104,6 +106,7 @@ public ProxyRequestHandler(
104106
addXForwardedHeaders = haGatewayConfiguration.getRouting().isAddXForwardedHeaders();
105107
statementPaths = haGatewayConfiguration.getStatementPaths();
106108
this.includeClusterInfoInResponse = haGatewayConfiguration.isIncludeClusterHostInResponse();
109+
this.maxProxyResponseSize = requireNonNull(haGatewayConfiguration.getMaxProxyResponseSize(), "maxProxyResponseSize is null");
107110
}
108111

109112
@PreDestroy
@@ -246,7 +249,7 @@ private void setupAsyncResponse(AsyncResponse asyncResponse, ListenableFuture<Re
246249

247250
private FluentFuture<ProxyResponse> executeHttp(Request request)
248251
{
249-
return FluentFuture.from(httpClient.executeAsync(request, new ProxyResponseHandler()));
252+
return FluentFuture.from(httpClient.executeAsync(request, new ProxyResponseHandler(maxProxyResponseSize)));
250253
}
251254

252255
private static Response handleProxyException(Request request, ProxyException e)

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,24 @@
1818
import io.airlift.http.client.Request;
1919
import io.airlift.http.client.Response;
2020
import io.airlift.http.client.ResponseHandler;
21+
import io.airlift.units.DataSize;
2122
import io.trino.gateway.proxyserver.ProxyResponseHandler.ProxyResponse;
2223

2324
import java.io.IOException;
25+
import java.nio.charset.StandardCharsets;
2426

25-
import static com.google.common.io.ByteStreams.toByteArray;
2627
import static java.util.Objects.requireNonNull;
2728

2829
public class ProxyResponseHandler
2930
implements ResponseHandler<ProxyResponse, RuntimeException>
3031
{
32+
private final DataSize maxProxyResponseSize;
33+
34+
public ProxyResponseHandler(DataSize maxProxyResponseSize)
35+
{
36+
this.maxProxyResponseSize = requireNonNull(maxProxyResponseSize, "maxProxyResponseSize is null");
37+
}
38+
3139
@Override
3240
public ProxyResponse handleException(Request request, Exception exception)
3341
{
@@ -38,7 +46,7 @@ public ProxyResponse handleException(Request request, Exception exception)
3846
public ProxyResponse handle(Request request, Response response)
3947
{
4048
try {
41-
return new ProxyResponse(response.getStatusCode(), response.getHeaders(), toByteArray(response.getInputStream()));
49+
return new ProxyResponse(response.getStatusCode(), response.getHeaders(), new String(response.getInputStream().readNBytes((int) maxProxyResponseSize.toBytes()), StandardCharsets.UTF_8));
4250
}
4351
catch (IOException e) {
4452
throw new ProxyException("Failed reading response from remote Trino server", e);
@@ -48,7 +56,7 @@ public ProxyResponse handle(Request request, Response response)
4856
public record ProxyResponse(
4957
int statusCode,
5058
ListMultimap<HeaderName, String> headers,
51-
byte[] body)
59+
String body)
5260
{
5361
public ProxyResponse
5462
{

0 commit comments

Comments
 (0)