-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Query Blocklist #19011
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
Open
mshahid6
wants to merge
4
commits into
apache:master
Choose a base branch
from
mshahid6:broker-config-query-blocklist
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Query Blocklist #19011
Changes from all commits
Commits
Show all changes
4 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
175 changes: 175 additions & 0 deletions
175
server/src/main/java/org/apache/druid/server/QueryBlocklistRule.java
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 |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.druid.server; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.google.common.base.Preconditions; | ||
| import com.google.common.base.Strings; | ||
| import com.google.common.collect.Sets; | ||
| import org.apache.druid.query.Query; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * A rule for matching queries against blocklist criteria. A query matches this rule if ALL | ||
| * specified criteria match (AND logic). Null or empty criteria match everything. | ||
| */ | ||
| public class QueryBlocklistRule | ||
| { | ||
| private final String ruleName; | ||
| @Nullable | ||
| private final Set<String> dataSources; | ||
| @Nullable | ||
| private final Set<String> queryTypes; | ||
| @Nullable | ||
| private final Map<String, String> contextMatches; | ||
|
|
||
| private final boolean hasDataSourceCriteria; | ||
| private final boolean hasQueryTypeCriteria; | ||
| private final boolean hasContextCriteria; | ||
|
|
||
| @JsonCreator | ||
| public QueryBlocklistRule( | ||
| @JsonProperty("ruleName") String ruleName, | ||
| @JsonProperty("dataSources") @Nullable Set<String> dataSources, | ||
| @JsonProperty("queryTypes") @Nullable Set<String> queryTypes, | ||
| @JsonProperty("contextMatches") @Nullable Map<String, String> contextMatches | ||
| ) | ||
| { | ||
| Preconditions.checkArgument( | ||
| !Strings.isNullOrEmpty(ruleName), | ||
| "ruleName must not be null or empty" | ||
| ); | ||
|
|
||
| // At least one criterion must be specified to prevent accidentally blocking all queries | ||
mshahid6 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.hasDataSourceCriteria = dataSources != null && !dataSources.isEmpty(); | ||
| this.hasQueryTypeCriteria = queryTypes != null && !queryTypes.isEmpty(); | ||
| this.hasContextCriteria = contextMatches != null && !contextMatches.isEmpty(); | ||
|
|
||
| Preconditions.checkArgument( | ||
| hasDataSourceCriteria || hasQueryTypeCriteria || hasContextCriteria, | ||
| "At least one criterion (dataSources, queryTypes, or contextMatches) must be specified. " | ||
| + "A rule with all null/empty criteria would block ALL queries." | ||
| ); | ||
|
|
||
| this.ruleName = ruleName; | ||
| this.dataSources = dataSources; | ||
| this.queryTypes = queryTypes; | ||
| this.contextMatches = contextMatches; | ||
| } | ||
|
|
||
| @JsonProperty | ||
| public String getRuleName() | ||
| { | ||
| return ruleName; | ||
| } | ||
|
|
||
| @JsonProperty | ||
| @Nullable | ||
| public Set<String> getDataSources() | ||
| { | ||
| return dataSources; | ||
| } | ||
|
|
||
| @JsonProperty | ||
| @Nullable | ||
| public Set<String> getQueryTypes() | ||
| { | ||
| return queryTypes; | ||
| } | ||
|
|
||
| @JsonProperty | ||
| @Nullable | ||
| public Map<String, String> getContextMatches() | ||
| { | ||
| return contextMatches; | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the query matches ALL specified criteria (AND logic). | ||
| * Null or empty criteria match everything. | ||
| * | ||
| * @param query the query to check | ||
| * @return true if the query matches this rule, false otherwise | ||
| */ | ||
| public boolean matches(Query<?> query) | ||
|
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. It might be worth caching this result for all 3 types. JVM might be smart and branch predict this every time but since this is called per-query on constant fields, it might be worth explicitly doing this to save ≥ 2 comparisons per query. |
||
| { | ||
| if (hasDataSourceCriteria) { | ||
| Set<String> queryDatasources = query.getDataSource().getTableNames(); | ||
| if (Sets.intersection(dataSources, queryDatasources).isEmpty()) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| if (hasQueryTypeCriteria) { | ||
| if (!queryTypes.contains(query.getType())) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| if (hasContextCriteria) { | ||
| for (Map.Entry<String, String> entry : contextMatches.entrySet()) { | ||
| Object contextValue = query.getContext().get(entry.getKey()); | ||
| if (!entry.getValue().equals(String.valueOf(contextValue))) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) | ||
| { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| QueryBlocklistRule that = (QueryBlocklistRule) o; | ||
| return Objects.equals(ruleName, that.ruleName) | ||
| && Objects.equals(dataSources, that.dataSources) | ||
| && Objects.equals(queryTypes, that.queryTypes) | ||
| && Objects.equals(contextMatches, that.contextMatches); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() | ||
| { | ||
| return Objects.hash(ruleName, dataSources, queryTypes, contextMatches); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() | ||
| { | ||
| return "QueryBlocklistRule{" + | ||
| "ruleName='" + ruleName + '\'' + | ||
| ", dataSources=" + dataSources + | ||
| ", queryTypes=" + queryTypes + | ||
| ", contextMatches=" + contextMatches + | ||
| '}'; | ||
| } | ||
| } | ||
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.
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.