-
Notifications
You must be signed in to change notification settings - Fork 123
Extract query ID from all kill_query procedure variations #425
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
Merged
mosabua
merged 3 commits into
trinodb:main
from
willmostly:will/test_extract_query_id_from_system_runtime_kill_query
Oct 30, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,8 +15,11 @@ | |
|
|
||
| import com.google.common.io.CharStreams; | ||
| import io.airlift.log.Logger; | ||
| import io.trino.gateway.ha.router.TrinoQueryProperties; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.ws.rs.HttpMethod; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
@@ -49,51 +52,50 @@ public final class ProxyUtils | |
| * capitalization. | ||
| */ | ||
| private static final Pattern QUERY_ID_PARAM_PATTERN = Pattern.compile(".*(?:%2F|(?i)query_?id(?-i)=|^)(\\d+_\\d+_\\d+_\\w+).*"); | ||
| private static final Pattern EXTRACT_BETWEEN_SINGLE_QUOTES = Pattern.compile("'([^\\s']+)'"); | ||
|
|
||
| private ProxyUtils() {} | ||
|
|
||
| public static String extractQueryIdIfPresent(HttpServletRequest request, List<String> statementPaths) | ||
| public static Optional<String> extractQueryIdIfPresent( | ||
| HttpServletRequest request, | ||
| List<String> statementPaths, | ||
| boolean requestAnalyserClientsUseV2Format, | ||
| int requestAnalyserMaxBodySize) | ||
| { | ||
| String path = request.getRequestURI(); | ||
| String queryParams = request.getQueryString(); | ||
| if (!request.getMethod().equals(HttpMethod.POST)) { | ||
| return extractQueryIdIfPresent(path, queryParams, statementPaths); | ||
| } | ||
| String queryText; | ||
| try { | ||
| String queryText = CharStreams.toString(new InputStreamReader(request.getInputStream(), UTF_8)); | ||
| if (!isNullOrEmpty(queryText) | ||
| && queryText.toLowerCase(ENGLISH).contains("system.runtime.kill_query")) { | ||
| // extract and return the queryId | ||
| String[] parts = queryText.split(","); | ||
| for (String part : parts) { | ||
| if (part.contains("query_id")) { | ||
| Matcher matcher = EXTRACT_BETWEEN_SINGLE_QUOTES.matcher(part); | ||
| if (matcher.find()) { | ||
| String queryQuoted = matcher.group(); | ||
| if (!isNullOrEmpty(queryQuoted) && queryQuoted.length() > 0) { | ||
| return queryQuoted.substring(1, queryQuoted.length() - 1); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| queryText = CharStreams.toString(new InputStreamReader(request.getInputStream(), UTF_8)); | ||
| } | ||
| catch (Exception e) { | ||
| log.error(e, "Error extracting query payload from request"); | ||
| catch (IOException e) { | ||
ebyhr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new RuntimeException("Error reading request body", e); | ||
willmostly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return extractQueryIdIfPresent(path, queryParams, statementPaths); | ||
| if (!isNullOrEmpty(queryText) && queryText.toLowerCase(ENGLISH).contains("kill_query")) { | ||
| TrinoQueryProperties trinoQueryProperties = new TrinoQueryProperties(request, requestAnalyserClientsUseV2Format, requestAnalyserMaxBodySize); | ||
| return trinoQueryProperties.getQueryId(); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| public static String extractQueryIdIfPresent(String path, String queryParams, List<String> statementPaths) | ||
| public static Optional<String> extractQueryIdIfPresent(String path, String queryParams, List<String> statementPaths) | ||
| { | ||
| if (path == null) { | ||
| return null; | ||
| return Optional.empty(); | ||
| } | ||
| String queryId = null; | ||
| log.debug("Trying to extract query id from path [%s] or queryString [%s]", path, queryParams); | ||
| // matchingStatementPath should match paths such as /v1/statement/executing/query_id/nonce/sequence_number, | ||
| // and if custom paths are supplied using the statementPaths configuration, paths such as | ||
| // /custom/statement/path/executing/query_id/nonce/sequence_number | ||
| Optional<String> matchingStatementPath = statementPaths.stream().filter(path::startsWith).findAny(); | ||
| if (!isNullOrEmpty(queryParams)) { | ||
| Matcher matcher = QUERY_ID_PARAM_PATTERN.matcher(queryParams); | ||
| if (matcher.matches()) { | ||
| return Optional.of(matcher.group(1)); | ||
| } | ||
| } | ||
| if (matchingStatementPath.isPresent() || path.startsWith(V1_QUERY_PATH)) { | ||
| path = path.replace(matchingStatementPath.orElse(V1_QUERY_PATH), ""); | ||
| String[] tokens = path.split("/"); | ||
|
|
@@ -102,27 +104,20 @@ public static String extractQueryIdIfPresent(String path, String queryParams, Li | |
| || tokens[1].equals("scheduled") | ||
| || tokens[1].equals("executing") | ||
| || tokens[1].equals("partialCancel")) { | ||
| queryId = tokens[2]; | ||
| return Optional.of(tokens[2]); | ||
| } | ||
| else { | ||
| queryId = tokens[1]; | ||
| return Optional.of(tokens[1]); | ||
|
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.
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. |
||
| } | ||
| } | ||
| } | ||
| else if (path.startsWith(TRINO_UI_PATH)) { | ||
| Matcher matcher = QUERY_ID_PATH_PATTERN.matcher(path); | ||
| if (matcher.matches()) { | ||
| queryId = matcher.group(1); | ||
| } | ||
| } | ||
| if (!isNullOrEmpty(queryParams)) { | ||
| Matcher matcher = QUERY_ID_PARAM_PATTERN.matcher(queryParams); | ||
| if (matcher.matches()) { | ||
| queryId = matcher.group(1); | ||
| return Optional.of(matcher.group(1)); | ||
| } | ||
| } | ||
| log.debug("Query id in URL [%s]", queryId); | ||
| return queryId; | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| public static String buildUriWithNewBackend(String backendHost, HttpServletRequest request) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.