-
Notifications
You must be signed in to change notification settings - Fork 83
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
Show RoutingGroup info in query history #607
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.gateway.ha.handler; | ||
|
||
public record RoutingDestinationInfo(String group, String clusterUri) {} | ||
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. It would expand the scope of this change, but it seems like a good time to add some audibility on which rules were triggered. For example, you could have 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. Can |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,13 +66,16 @@ public RoutingTargetHandler( | |
cookiesEnabled = GatewayCookieConfigurationPropertiesProvider.getInstance().isEnabled(); | ||
} | ||
|
||
public String getRoutingDestination(HttpServletRequest request) | ||
public RoutingDestinationInfo getRoutingDestination(HttpServletRequest request) | ||
{ | ||
Optional<String> previousBackend = getPreviousBackend(request); | ||
String clusterHost = previousBackend.orElseGet(() -> getBackendFromRoutingGroup(request)); | ||
// This falls back on adhoc routing group if no routing group can be determined | ||
String routingGroup = routingGroupSelector.findRoutingGroup(request).orElse("adhoc"); | ||
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. moving this call out of the |
||
String user = request.getHeader(USER_HEADER); | ||
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 may not be always true, especially in case of bearer tokens (JWT). But I see that this logic is from before and it's not newly added as a part of the PR. |
||
String clusterHost = previousBackend.orElseGet(() -> routingManager.provideBackendForRoutingGroup(routingGroup, user)); | ||
logRewrite(clusterHost, request); | ||
|
||
return buildUriWithNewBackend(clusterHost, request); | ||
return new RoutingDestinationInfo(routingGroup, buildUriWithNewBackend(clusterHost, request)); | ||
} | ||
|
||
public boolean isPathWhiteListed(String path) | ||
|
@@ -87,17 +90,6 @@ public boolean isPathWhiteListed(String path) | |
|| extraWhitelistPaths.stream().anyMatch(pattern -> pattern.matcher(path).matches()); | ||
} | ||
|
||
private String getBackendFromRoutingGroup(HttpServletRequest request) | ||
{ | ||
String routingGroup = routingGroupSelector.findRoutingGroup(request); | ||
String user = request.getHeader(USER_HEADER); | ||
if (!isNullOrEmpty(routingGroup)) { | ||
// This falls back on adhoc backend if there is no cluster found for the routing group. | ||
return routingManager.provideBackendForRoutingGroup(routingGroup, user); | ||
} | ||
return routingManager.provideAdhocBackend(user); | ||
} | ||
|
||
private Optional<String> getPreviousBackend(HttpServletRequest request) | ||
{ | ||
Optional<String> queryId = extractQueryIdIfPresent(request, statementPaths, requestAnalyserClientsUseV2Format, requestAnalyserMaxBodySize); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
import io.trino.gateway.ha.config.GatewayCookieConfigurationPropertiesProvider; | ||
import io.trino.gateway.ha.config.HaGatewayConfiguration; | ||
import io.trino.gateway.ha.config.ProxyResponseConfiguration; | ||
import io.trino.gateway.ha.handler.RoutingDestinationInfo; | ||
import io.trino.gateway.ha.router.GatewayCookie; | ||
import io.trino.gateway.ha.router.OAuth2GatewayCookie; | ||
import io.trino.gateway.ha.router.QueryHistoryManager; | ||
|
@@ -118,49 +119,50 @@ public void shutdown() | |
public void deleteRequest( | ||
HttpServletRequest servletRequest, | ||
AsyncResponse asyncResponse, | ||
URI remoteUri) | ||
RoutingDestinationInfo routingDestinationInfo) | ||
{ | ||
Request.Builder request = prepareDelete(); | ||
performRequest(remoteUri, servletRequest, asyncResponse, request); | ||
performRequest(routingDestinationInfo, servletRequest, asyncResponse, request); | ||
} | ||
|
||
public void getRequest( | ||
HttpServletRequest servletRequest, | ||
AsyncResponse asyncResponse, | ||
URI remoteUri) | ||
RoutingDestinationInfo routingDestinationInfo) | ||
{ | ||
Request.Builder request = prepareGet(); | ||
performRequest(remoteUri, servletRequest, asyncResponse, request); | ||
performRequest(routingDestinationInfo, servletRequest, asyncResponse, request); | ||
} | ||
|
||
public void postRequest( | ||
String statement, | ||
HttpServletRequest servletRequest, | ||
AsyncResponse asyncResponse, | ||
URI remoteUri) | ||
RoutingDestinationInfo routingDestinationInfo) | ||
{ | ||
Request.Builder request = preparePost() | ||
.setBodyGenerator(createStaticBodyGenerator(statement, UTF_8)); | ||
performRequest(remoteUri, servletRequest, asyncResponse, request); | ||
performRequest(routingDestinationInfo, servletRequest, asyncResponse, request); | ||
} | ||
|
||
public void putRequest( | ||
String statement, | ||
HttpServletRequest servletRequest, | ||
AsyncResponse asyncResponse, | ||
URI remoteUri) | ||
RoutingDestinationInfo routingDestinationInfo) | ||
{ | ||
Request.Builder request = preparePut() | ||
.setBodyGenerator(createStaticBodyGenerator(statement, UTF_8)); | ||
performRequest(remoteUri, servletRequest, asyncResponse, request); | ||
performRequest(routingDestinationInfo, servletRequest, asyncResponse, request); | ||
} | ||
|
||
private void performRequest( | ||
URI remoteUri, | ||
RoutingDestinationInfo routingDestinationInfo, | ||
HttpServletRequest servletRequest, | ||
AsyncResponse asyncResponse, | ||
Request.Builder requestBuilder) | ||
{ | ||
URI remoteUri = URI.create(routingDestinationInfo.clusterUri()); | ||
requestBuilder.setUri(remoteUri); | ||
|
||
for (String name : list(servletRequest.getHeaderNames())) { | ||
|
@@ -181,6 +183,8 @@ private void performRequest( | |
ImmutableList.Builder<NewCookie> cookieBuilder = ImmutableList.builder(); | ||
cookieBuilder.addAll(getOAuth2GatewayCookie(remoteUri, servletRequest)); | ||
|
||
requestBuilder.addHeader("X-Routing-Group", routingDestinationInfo.group()); | ||
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. Can you pass the routing info directly to |
||
|
||
Request request = requestBuilder | ||
.setFollowRedirects(false) | ||
.build(); | ||
|
@@ -288,6 +292,7 @@ private ProxyResponse recordBackendForQueryId(Request request, ProxyResponse res | |
else { | ||
log.error("Non OK HTTP Status code with response [%s] , Status code [%s]", response.body(), response.statusCode()); | ||
} | ||
queryDetail.setRoutingGroup(request.getHeader("X-Routing-Group")); | ||
queryHistoryManager.submitQueryDetail(queryDetail); | ||
return response; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ALTER TABLE query_history | ||
ADD routing_group VARCHAR(255); | ||
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. nit: end the file with a newline. This goes for the other |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ALTER TABLE query_history | ||
ADD routing_group VARCHAR(255); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ALTER TABLE query_history | ||
ADD routing_group VARCHAR(255); |
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.
name
RoutingDestination
would be better.