Skip to content

Commit 3781459

Browse files
Chaho12ebyhr
authored andcommitted
Support HttpClient options for external routing group selector
1 parent e2a4fd5 commit 3781459

File tree

7 files changed

+60
-11
lines changed

7 files changed

+60
-11
lines changed

docs/routing-rules.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ If there is error parsing the routing rules configuration file, an error is
5050
logged, and requests are routed using the routing group header
5151
`X-Trino-Routing-Group` as default.
5252

53+
### Configuring API requests with HTTP client config
54+
55+
You can configure the HTTP client by adding the following configuration to
56+
the `serverConfig:` section with the `router` prefix.
57+
58+
```yaml
59+
serverConfig:
60+
router.http-client.request-timeout: 1s
61+
```
62+
63+
Please refer to the [Trino HTTP client properties](
64+
https://trino.io/docs/current/admin/properties-http-client.html)
65+
documentation for more.
66+
5367
### Use an external service for routing rules
5468

5569
You can use an external service for processing your routing by setting the

gateway-ha/src/main/java/io/trino/gateway/baseapp/BaseApp.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.trino.gateway.ha.resource.LoginResource;
3232
import io.trino.gateway.ha.resource.PublicResource;
3333
import io.trino.gateway.ha.resource.TrinoResource;
34+
import io.trino.gateway.ha.router.ForRouter;
3435
import io.trino.gateway.ha.security.AuthorizedExceptionMapper;
3536
import io.trino.gateway.proxyserver.ForProxy;
3637
import io.trino.gateway.proxyserver.ProxyRequestHandler;
@@ -170,5 +171,6 @@ private static void registerProxyResources(Binder binder)
170171
jaxrsBinder(binder).bind(ProxyRequestHandler.class);
171172
httpClientBinder(binder).bindHttpClient("proxy", ForProxy.class);
172173
httpClientBinder(binder).bindHttpClient("monitor", ForMonitor.class);
174+
httpClientBinder(binder).bindHttpClient("router", ForRouter.class);
173175
}
174176
}

gateway-ha/src/main/java/io/trino/gateway/ha/module/HaGatewayProviderModule.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.google.inject.AbstractModule;
1818
import com.google.inject.Provides;
1919
import com.google.inject.Singleton;
20+
import io.airlift.http.client.HttpClient;
2021
import io.trino.gateway.ha.config.AuthenticationConfiguration;
2122
import io.trino.gateway.ha.config.AuthorizationConfiguration;
2223
import io.trino.gateway.ha.config.GatewayCookieConfigurationPropertiesProvider;
@@ -26,6 +27,7 @@
2627
import io.trino.gateway.ha.config.RulesExternalConfiguration;
2728
import io.trino.gateway.ha.config.UserConfiguration;
2829
import io.trino.gateway.ha.router.BackendStateManager;
30+
import io.trino.gateway.ha.router.ForRouter;
2931
import io.trino.gateway.ha.router.RoutingGroupSelector;
3032
import io.trino.gateway.ha.security.ApiAuthenticator;
3133
import io.trino.gateway.ha.security.AuthorizationManager;
@@ -176,7 +178,7 @@ public BackendStateManager getBackendStateConnectionManager()
176178

177179
@Provides
178180
@Singleton
179-
public RoutingGroupSelector getRoutingGroupSelector()
181+
public RoutingGroupSelector getRoutingGroupSelector(@ForRouter HttpClient httpClient)
180182
{
181183
RoutingRulesConfiguration routingRulesConfig = configuration.getRoutingRules();
182184
if (routingRulesConfig.isRulesEngineEnabled()) {
@@ -188,7 +190,7 @@ public RoutingGroupSelector getRoutingGroupSelector()
188190
}
189191
case EXTERNAL -> {
190192
RulesExternalConfiguration rulesExternalConfiguration = routingRulesConfig.getRulesExternalConfiguration();
191-
yield RoutingGroupSelector.byRoutingExternal(rulesExternalConfiguration, configuration.getRequestAnalyzerConfig());
193+
yield RoutingGroupSelector.byRoutingExternal(httpClient, rulesExternalConfiguration, configuration.getRequestAnalyzerConfig());
192194
}
193195
};
194196
}

gateway-ha/src/main/java/io/trino/gateway/ha/router/ExternalRoutingGroupSelector.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
import com.google.common.collect.ImmutableSet;
1919
import com.google.common.collect.Multimap;
2020
import io.airlift.http.client.HttpClient;
21-
import io.airlift.http.client.HttpClientConfig;
2221
import io.airlift.http.client.JsonBodyGenerator;
2322
import io.airlift.http.client.JsonResponseHandler;
2423
import io.airlift.http.client.Request;
25-
import io.airlift.http.client.jetty.JettyHttpClient;
2624
import io.airlift.json.JsonCodec;
2725
import io.airlift.log.Logger;
2826
import io.trino.gateway.ha.config.RequestAnalyzerConfig;
@@ -59,8 +57,9 @@ public class ExternalRoutingGroupSelector
5957
createJsonResponseHandler(jsonCodec(RoutingGroupExternalResponse.class));
6058

6159
@VisibleForTesting
62-
ExternalRoutingGroupSelector(RulesExternalConfiguration rulesExternalConfiguration, RequestAnalyzerConfig requestAnalyzerConfig)
60+
ExternalRoutingGroupSelector(HttpClient httpClient, RulesExternalConfiguration rulesExternalConfiguration, RequestAnalyzerConfig requestAnalyzerConfig)
6361
{
62+
this.httpClient = requireNonNull(httpClient, "httpClient is null");
6463
this.excludeHeaders = ImmutableSet.<String>builder()
6564
.add("Content-Length")
6665
.addAll(rulesExternalConfiguration.getExcludeHeaders())
@@ -76,7 +75,6 @@ public class ExternalRoutingGroupSelector
7675
throw new RuntimeException("Invalid URL provided, using "
7776
+ "routing group header as default.", e);
7877
}
79-
httpClient = new JettyHttpClient(new HttpClientConfig());
8078
}
8179

8280
@Override
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.trino.gateway.ha.router;
15+
16+
import com.google.inject.BindingAnnotation;
17+
18+
import java.lang.annotation.Retention;
19+
import java.lang.annotation.Target;
20+
21+
import static java.lang.annotation.ElementType.FIELD;
22+
import static java.lang.annotation.ElementType.METHOD;
23+
import static java.lang.annotation.ElementType.PARAMETER;
24+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
25+
26+
@Retention(RUNTIME)
27+
@Target({FIELD, PARAMETER, METHOD})
28+
@BindingAnnotation
29+
public @interface ForRouter {}

gateway-ha/src/main/java/io/trino/gateway/ha/router/RoutingGroupSelector.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
package io.trino.gateway.ha.router;
1515

16+
import io.airlift.http.client.HttpClient;
1617
import io.trino.gateway.ha.config.RequestAnalyzerConfig;
1718
import io.trino.gateway.ha.config.RulesExternalConfiguration;
1819
import jakarta.servlet.http.HttpServletRequest;
@@ -46,9 +47,12 @@ static RoutingGroupSelector byRoutingRulesEngine(String rulesConfigPath, Request
4647
* Routing group selector that uses RESTful API
4748
* to determine the right routing group.
4849
*/
49-
static RoutingGroupSelector byRoutingExternal(RulesExternalConfiguration rulesExternalConfiguration, RequestAnalyzerConfig requestAnalyzerConfig)
50+
static RoutingGroupSelector byRoutingExternal(
51+
HttpClient httpClient,
52+
RulesExternalConfiguration rulesExternalConfiguration,
53+
RequestAnalyzerConfig requestAnalyzerConfig)
5054
{
51-
return new ExternalRoutingGroupSelector(rulesExternalConfiguration, requestAnalyzerConfig);
55+
return new ExternalRoutingGroupSelector(httpClient, rulesExternalConfiguration, requestAnalyzerConfig);
5256
}
5357

5458
/**

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void testApiFailure()
133133
{
134134
RulesExternalConfiguration rulesExternalConfiguration = provideRoutingRuleExternalConfig();
135135
RoutingGroupSelector routingGroupSelector =
136-
RoutingGroupSelector.byRoutingExternal(rulesExternalConfiguration, requestAnalyzerConfig);
136+
RoutingGroupSelector.byRoutingExternal(httpClient, rulesExternalConfiguration, requestAnalyzerConfig);
137137

138138
HttpServletRequest mockRequest = prepareMockRequest();
139139
setMockHeaders(mockRequest);
@@ -162,7 +162,7 @@ void testNullUri()
162162
rulesExternalConfiguration.setUrlPath(null);
163163

164164
// Assert that a RuntimeException is thrown with message
165-
assertThatThrownBy(() -> RoutingGroupSelector.byRoutingExternal(rulesExternalConfiguration, requestAnalyzerConfig))
165+
assertThatThrownBy(() -> RoutingGroupSelector.byRoutingExternal(httpClient, rulesExternalConfiguration, requestAnalyzerConfig))
166166
.isInstanceOf(RuntimeException.class)
167167
.hasMessage("Invalid URL provided, using routing group header as default.");
168168
}
@@ -175,7 +175,7 @@ void testExcludeHeader()
175175
rulesExternalConfiguration.setExcludeHeaders(List.of("test-exclude-header"));
176176

177177
RoutingGroupSelector routingGroupSelector =
178-
RoutingGroupSelector.byRoutingExternal(rulesExternalConfiguration, requestAnalyzerConfig);
178+
RoutingGroupSelector.byRoutingExternal(httpClient, rulesExternalConfiguration, requestAnalyzerConfig);
179179

180180
// Mock headers to be read by mockRequest
181181
HttpServletRequest mockRequest = mock(HttpServletRequest.class);

0 commit comments

Comments
 (0)