-
Notifications
You must be signed in to change notification settings - Fork 129
Implement Strict Routing Semantics for Routing Rules #804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d7fe5fc
2810a25
ac28ac5
54d3c37
a4159fc
eb736ee
88479d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -91,11 +91,14 @@ private RoutingTargetResponse getRoutingTargetResponse(HttpServletRequest reques | |||||
| RoutingSelectorResponse routingDestination = routingGroupSelector.findRoutingDestination(request); | ||||||
| String user = request.getHeader(USER_HEADER); | ||||||
|
|
||||||
| // This falls back on default routing group backend if there is no cluster found for the routing group. | ||||||
| // When no cluster is found: | ||||||
| // - If strictRouting is false, fall back to the default routing group backend. | ||||||
| // - If strictRouting is true, return a 404 response. | ||||||
| String routingGroup = !isNullOrEmpty(routingDestination.routingGroup()) | ||||||
| ? routingDestination.routingGroup() | ||||||
| : defaultRoutingGroup; | ||||||
| ProxyBackendConfiguration backendConfiguration = routingManager.provideBackendConfiguration(routingGroup, user); | ||||||
| boolean strictRouting = Optional.ofNullable(routingDestination.strictRouting()).orElse(false); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ProxyBackendConfiguration backendConfiguration = routingManager.provideBackendConfiguration(routingGroup, user, strictRouting); | ||||||
| String clusterHost = backendConfiguration.getProxyTo(); | ||||||
| String externalUrl = backendConfiguration.getExternalUrl(); | ||||||
| // Apply headers from RoutingDestination if there are any | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,8 @@ | |
| import jakarta.annotation.Nullable; | ||
| import jakarta.annotation.PreDestroy; | ||
| import jakarta.ws.rs.HttpMethod; | ||
| import jakarta.ws.rs.WebApplicationException; | ||
| import jakarta.ws.rs.core.Response; | ||
|
|
||
| import java.net.HttpURLConnection; | ||
| import java.net.URI; | ||
|
|
@@ -42,6 +44,8 @@ | |
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static jakarta.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE; | ||
|
|
||
| /** | ||
| * This class performs health check, stats counts for each backend and provides a backend given | ||
| * request object. Default implementation comes here. | ||
|
|
@@ -99,15 +103,21 @@ public ProxyBackendConfiguration provideDefaultBackendConfiguration(String user) | |
| } | ||
|
|
||
| /** | ||
| * Performs routing to a given cluster group. This falls back to a default backend, if no scheduled | ||
| * backend is found. | ||
| * Performs routing to a given cluster group. This falls back to a default backend if the target group | ||
| * has no suitable backend unless {@code strictRouting} is true, in which case a 503 is returned. | ||
| */ | ||
| @Override | ||
| public ProxyBackendConfiguration provideBackendConfiguration(String routingGroup, String user) | ||
| public ProxyBackendConfiguration provideBackendConfiguration(String routingGroup, String user, boolean strictRouting) | ||
| { | ||
| List<ProxyBackendConfiguration> backends = gatewayBackendManager.getActiveBackends(routingGroup).stream() | ||
| .filter(backEnd -> isBackendHealthy(backEnd.getName())) | ||
| .toList(); | ||
| if (strictRouting && backends.isEmpty()) { | ||
| throw new WebApplicationException( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why WebApplicationException and not IllegalStateException like in IllegalStateException("Number of active backends found zero")) or maybe a custom Explicit Exception inherited from RouterException?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can add a WARN or ERROR with context when the 404 is triggered? Also +1 on maybe making it a |
||
| Response.status(SERVICE_UNAVAILABLE) | ||
| .entity(String.format("No healthy backends available for routing group '%s' under strict routing for user '%s'", routingGroup, user)) | ||
| .build()); | ||
| } | ||
| return selectBackend(backends, user).orElseGet(() -> provideDefaultBackendConfiguration(user)); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ | |
| import static io.trino.gateway.ha.handler.HttpUtils.TRINO_REQUEST_USER; | ||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static java.util.Collections.sort; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class FileBasedRoutingGroupSelector | ||
| implements RoutingGroupSelector | ||
|
|
@@ -72,13 +73,15 @@ public RoutingSelectorResponse findRoutingDestination(HttpServletRequest request | |
| data = ImmutableMap.of("request", request); | ||
| } | ||
|
|
||
| rules.get().forEach(rule -> { | ||
| boolean strictRouting = false; | ||
| for (RoutingRule rule : requireNonNull(rules.get())) { | ||
| if (rule.evaluateCondition(data, state)) { | ||
| log.debug("%s evaluated to true on request: %s", rule, request); | ||
| rule.evaluateAction(result, data, state); | ||
| strictRouting = rule.isStrictRouting(); | ||
| } | ||
| }); | ||
| return new RoutingSelectorResponse(result.get(RESULTS_ROUTING_GROUP_KEY)); | ||
| } | ||
| return new RoutingSelectorResponse(result.get(RESULTS_ROUTING_GROUP_KEY), ImmutableMap.of(), strictRouting); | ||
|
Comment on lines
+76
to
+84
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic is flawed. |
||
| } | ||
|
|
||
| public List<RoutingRule> readRulesFromPath(Path rulesPath) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| name: "airflow" | ||
| description: "if query from airflow, route to adhoc" | ||
| priority: 0 | ||
| actions: | ||
| - "result.put(\"routingGroup\", \"adhoc\")" | ||
| condition: "request.getHeader(\"X-Trino-Source\") == \"datagrip\"" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the comment to
provideBackendConfiguration().Also, we return 503 now, not 404.