Skip to content

Commit 7d4280f

Browse files
EdenKikebyhr
andcommitted
Make externalHeaders optional in external service response
Co-authored-by: Yuya Ebihara <[email protected]>
1 parent 4c7367c commit 7d4280f

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

docs/routing-rules.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,26 @@ return a result with the following criteria:
9595
* Only one group can be returned
9696
* If errors is not null, then query would route to default routing group adhoc
9797

98+
#### Request headers modification
99+
100+
The external routing service can optionally return an `externalHeaders` map in its response
101+
to add or modify HTTP headers before the request is forwarded.
102+
103+
This enables dynamic customization of request behavior, such as injecting session properties
104+
or setting client tags before the request reaches the Trino cluster.
105+
98106
```json
99107
{
100108
"routingGroup": "test-group",
101109
"errors": [
102110
"Error1",
103111
"Error2",
104112
"Error3"
105-
]
113+
],
114+
"externalHeaders": {
115+
"x-trino-client-tags": "['etl']",
116+
"x-trino-session": "query_max_memory=50GB,optimize_metadata_queries=false"
117+
}
106118
}
107119
```
108120

gateway-ha/src/main/java/io/trino/gateway/ha/router/schema/ExternalRouterResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
public record ExternalRouterResponse(
2929
@Nullable String routingGroup,
3030
List<String> errors,
31-
Map<String, String> externalHeaders)
31+
@Nullable Map<String, String> externalHeaders)
3232
implements RoutingGroupResponse
3333
{
3434
public ExternalRouterResponse {
35-
externalHeaders = ImmutableMap.copyOf(externalHeaders);
35+
externalHeaders = externalHeaders == null ? ImmutableMap.of() : ImmutableMap.copyOf(externalHeaders);
3636
}
3737
}

gateway-ha/src/test/java/io/trino/gateway/ha/router/TestRoutingGroupSelectorExternal.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,23 @@ void testHeaderModificationWithErrors()
287287
assertThat(routingSelectorResponse.externalHeaders().get(headerKey)).isNull();
288288
}
289289

290+
@Test
291+
void testHeaderModificationWithNoExternalHeaders()
292+
{
293+
RoutingGroupSelector selector = RoutingGroupSelector.byRoutingExternal(httpClient, provideRoutingRuleExternalConfig(), requestAnalyzerConfig);
294+
HttpServletRequest mockRequest = prepareMockRequest();
295+
setMockHeaders(mockRequest);
296+
297+
ExternalRouterResponse mockResponse = new ExternalRouterResponse("test-group", ImmutableList.of(), null);
298+
299+
when(httpClient.execute(any(), any())).thenReturn(mockResponse);
300+
301+
RoutingSelectorResponse routingSelectorResponse = selector.findRoutingDestination(mockRequest);
302+
303+
assertThat(routingSelectorResponse.routingGroup()).isEqualTo("test-group");
304+
assertThat(routingSelectorResponse.externalHeaders()).isEmpty();
305+
}
306+
290307
@Test
291308
void testHeaderModificationWithEmptyRoutingGroup()
292309
throws Exception

0 commit comments

Comments
 (0)