-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[DRAFT] Request labeling service in opensearch #14282
Draft
ansjcy
wants to merge
5
commits into
opensearch-project:main
Choose a base branch
from
ansjcy:RequestLabelingService-in-opensearch
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f4e8173
Support customized and rule based labeling for search queries
ansjcy 56246bf
improve the overall logic and fix several bugs based on comments
ansjcy fb6df67
remove label field from search source and use header for labeling
ansjcy 3b6fd30
refactor code based on comments
ansjcy cf49d74
draft commit for generic request labeling service
ansjcy 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 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 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 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 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 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 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 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 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
81 changes: 81 additions & 0 deletions
81
server/src/main/java/org/opensearch/search/labels/RequestLabelingService.java
This file contains 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,81 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.search.labels; | ||
|
||
import org.opensearch.action.search.SearchRequest; | ||
import org.opensearch.search.labels.rules.Rule; | ||
import org.opensearch.tasks.Task; | ||
import org.opensearch.threadpool.ThreadPool; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Service to attach labels to a search request based on pre-defined rules | ||
* It evaluate all available rules and generate labels into the thread context. | ||
*/ | ||
|
||
// Rule: allowed user provided labels, internally supported labels. -> do we allow conflicts? | ||
// take json string (or one key-value pair for each label?), parse and check if user-provided labels are valid based on allowed labels from different rules, if valid, put into all_labels. | ||
// evaluate all rules, put into all_labels thread context | ||
// for user defined labels, do we want to keep in another thread context? | ||
|
||
// or add a label for each separate key? | ||
|
||
// each rule define their own "allowed labels", each rule should only use their own defined "allowed labels" | ||
// This service should be responsible to check conflicts. | ||
public class RequestLabelingService { | ||
/** | ||
* Field name for computed labels | ||
*/ | ||
public static final String RULE_BASED_LABELS = "rule_based_labels"; | ||
private final ThreadPool threadPool; | ||
private final List<Rule> rules; | ||
|
||
public RequestLabelingService(final ThreadPool threadPool, final List<Rule> rules) { | ||
this.threadPool = threadPool; | ||
this.rules = rules; | ||
} | ||
|
||
public void parseUserLabels() { | ||
// parse user provided labels into a map, also validate them based on allowed_user_labels from each rule | ||
|
||
threadPool.getThreadContext().putTransient(USER_PROVIDED_LABELS, userLabels); | ||
} | ||
|
||
/** | ||
* Evaluate all labeling rules and store the computed rules into thread context | ||
* | ||
* @param searchRequest {@link SearchRequest} | ||
*/ | ||
public void applyAllRules(final SearchRequest searchRequest) { | ||
Map<String, Object> computedLabels = rules.stream() | ||
.map(rule -> rule.evaluate(threadPool.getThreadContext(), searchRequest)) | ||
.flatMap(m -> m.entrySet().stream()) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (existing, replacement) -> replacement)); | ||
// String userProvidedTag = getUserProvidedTag(threadPool); | ||
|
||
threadPool.getThreadContext().putTransient(RULE_BASED_LABELS, computedLabels); | ||
} | ||
|
||
/** | ||
* Get the user provided tag from the X-Opaque-Id header | ||
* | ||
* @return user provided tag | ||
*/ | ||
public static String getUserProvidedTag(ThreadPool threadPool) { | ||
return threadPool.getThreadContext().getTransient(USER_PROVIDED_LABELS); | ||
} | ||
|
||
public static Map<String, Object> getRuleBasedLabels(ThreadPool threadPool) { | ||
return threadPool.getThreadContext().getTransient(RULE_BASED_LABELS); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
server/src/main/java/org/opensearch/search/labels/SearchRequestLabelingListener.java
This file contains 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,32 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.search.labels; | ||
|
||
import org.opensearch.action.search.SearchRequestContext; | ||
import org.opensearch.action.search.SearchRequestOperationsListener; | ||
|
||
/** | ||
* SearchRequestOperationsListener subscriber for labeling search requests | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public final class SearchRequestLabelingListener extends SearchRequestOperationsListener { | ||
final private RequestLabelingService requestLabelingService; | ||
|
||
public SearchRequestLabelingListener(final RequestLabelingService requestLabelingService) { | ||
this.requestLabelingService = requestLabelingService; | ||
} | ||
|
||
@Override | ||
public void onRequestStart(SearchRequestContext searchRequestContext) { | ||
// add tags to search request | ||
requestLabelingService.applyAllRules(searchRequestContext.getRequest()); | ||
requestLabelingService.parseUserLabels(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
server/src/main/java/org/opensearch/search/labels/package-info.java
This file contains 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,10 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/** Search labeling service. */ | ||
package org.opensearch.search.labels; |
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.
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.
Check if labels are valid here