-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[Rule based auto tagging] Add in-memory rule processing service #17365
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
jainankitk
merged 19 commits into
opensearch-project:main
from
kaushalmahi12:wlm/rule-matching
Apr 3, 2025
Merged
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a186749
[rule based autotagging] add attribute value store
kaushalmahi12 5c6d37f
add in-memory rule processing service
kaushalmahi12 5f33006
add missing javadoc
kaushalmahi12 daff632
merge the in-memory store changes:
kaushalmahi12 e5c98fe
fix generics error
kaushalmahi12 c5b2766
add CHANGELOG entry
kaushalmahi12 5d59a72
remove stubs
kaushalmahi12 a7e9de7
move generic logic to lib
kaushalmahi12 6eb15f5
fix javadoc error
kaushalmahi12 4b43025
fix javadoc error
kaushalmahi12 b7de765
delete licenses from the wlm plugin
kaushalmahi12 f6bae1d
expose feature level attribute value store init method
kaushalmahi12 0ad6a09
add extra space to remove unwanted entry from the changelog diff
kaushalmahi12 1e90840
address comments
kaushalmahi12 627efa6
use constructors over static methods
kaushalmahi12 e502a38
make member var final in InMemoryRuleProcessingService
kaushalmahi12 cfeaace
make concurrency checks more granular
kaushalmahi12 512b0ca
add concurrent test
kaushalmahi12 8a9aeb8
remove forbidden api usage
kaushalmahi12 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 |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| dependencies { | ||
| api 'org.apache.commons:commons-collections4:4.4' | ||
| api project(":server") | ||
|
|
||
| testImplementation(project(":test:framework")) { | ||
| exclude group: 'org.opensearch', module: 'opensearch-core' | ||
| } | ||
| } | ||
|
|
||
| tasks.named("dependencyLicenses").configure { | ||
| mapping from: /commons-collections.*/, to: 'commons-collections' | ||
| } |
File renamed without changes.
File renamed without changes.
File renamed without changes.
147 changes: 147 additions & 0 deletions
147
.../autotagging-commons/src/main/java/org/opensearch/rule/InMemoryRuleProcessingService.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,147 @@ | ||
| /* | ||
| * 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.rule; | ||
|
|
||
| import org.opensearch.autotagging.Attribute; | ||
| import org.opensearch.autotagging.FeatureType; | ||
| import org.opensearch.autotagging.Rule; | ||
| import org.opensearch.rule.attribute_extractor.AttributeExtractor; | ||
| import org.opensearch.rule.storage.AttributeValueStore; | ||
| import org.opensearch.rule.storage.AttributeValueStoreFactory; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * This class is responsible for managing in-memory view of Rules and Find matching Rule for the request | ||
| * Each auto-tagging feature should use a separate instance of this class as this avoid potential concurrency overhead | ||
| * in case of dynamic updates and attribute sharing scenarios | ||
| */ | ||
| public class InMemoryRuleProcessingService { | ||
|
|
||
| private final AttributeValueStoreFactory attributeValueStoreFactory; | ||
|
|
||
| /** | ||
| * Constrcutor | ||
| * @param featureType | ||
| * @param attributeValueStoreSupplier | ||
| */ | ||
| public InMemoryRuleProcessingService( | ||
| FeatureType featureType, | ||
| Supplier<AttributeValueStore<String, String>> attributeValueStoreSupplier | ||
| ) { | ||
| attributeValueStoreFactory = new AttributeValueStoreFactory(featureType, attributeValueStoreSupplier); | ||
| } | ||
|
|
||
| /** | ||
| * Adds the rule to in-memory view | ||
| * @param rule to be added | ||
| */ | ||
| public synchronized void add(final Rule rule) { | ||
| new AddRuleOperation(rule, attributeValueStoreFactory).perform(); | ||
| } | ||
|
|
||
| /** | ||
| * Removes the rule from in-memory view | ||
| * @param rule to be removed | ||
| */ | ||
| public synchronized void remove(final Rule rule) { | ||
| new DeleteRuleOperation(rule, attributeValueStoreFactory).perform(); | ||
| } | ||
|
|
||
| /** | ||
| * Evaluates the label for the current request. It finds the matches for each attribute value and then it is an | ||
| * intersection of all the matches | ||
| * @param attributeExtractors list of extractors which are used to get the attribute values to find the | ||
| * matching rule | ||
| * @return a label if there is unique label otherwise empty | ||
| */ | ||
| public Optional<String> evaluateLabel(List<AttributeExtractor<String>> attributeExtractors) { | ||
| assert attributeValueStoreFactory != null; | ||
| Optional<String> result = Optional.empty(); | ||
| for (AttributeExtractor<String> attributeExtractor : attributeExtractors) { | ||
| AttributeValueStore<String, String> valueStore = attributeValueStoreFactory.getAttributeValueStore( | ||
| attributeExtractor.getAttribute() | ||
| ); | ||
jainankitk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for (String value : attributeExtractor.extract()) { | ||
| Optional<String> possibleMatch = valueStore.get(value); | ||
|
|
||
| if (possibleMatch.isEmpty()) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| if (result.isEmpty()) { | ||
| result = possibleMatch; | ||
| } else { | ||
| boolean isThePossibleMatchEqualResult = possibleMatch.get().equals(result.get()); | ||
| if (!isThePossibleMatchEqualResult) { | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static abstract class RuleProcessingOperation { | ||
| protected final Rule rule; | ||
| protected final AttributeValueStoreFactory attributeValueStoreFactory; | ||
|
|
||
| public RuleProcessingOperation(Rule rule, AttributeValueStoreFactory attributeValueStoreFactory) { | ||
| this.rule = rule; | ||
| this.attributeValueStoreFactory = attributeValueStoreFactory; | ||
| } | ||
|
|
||
| void perform() { | ||
| final FeatureType feature = rule.getFeatureType(); | ||
| final String label = rule.getFeatureValue(); | ||
|
|
||
| for (Map.Entry<Attribute, Set<String>> attributeEntry : rule.getAttributeMap().entrySet()) { | ||
| processAttributeEntry(attributeEntry); | ||
| } | ||
| } | ||
|
|
||
| protected AttributeValueStore<String, String> getAttributeValueStore(final Attribute attribute) { | ||
| return this.attributeValueStoreFactory.getAttributeValueStore(attribute); | ||
| } | ||
|
|
||
| protected abstract void processAttributeEntry(Map.Entry<Attribute, Set<String>> attributeEntry); | ||
| } | ||
|
|
||
| private static class DeleteRuleOperation extends RuleProcessingOperation { | ||
| public DeleteRuleOperation(Rule rule, AttributeValueStoreFactory attributeValueStoreFactory) { | ||
| super(rule, attributeValueStoreFactory); | ||
| } | ||
|
|
||
| @Override | ||
| protected void processAttributeEntry(Map.Entry<Attribute, Set<String>> attributeEntry) { | ||
| AttributeValueStore<String, String> valueStore = getAttributeValueStore(attributeEntry.getKey()); | ||
| for (String value : attributeEntry.getValue()) { | ||
| valueStore.remove(value); | ||
| } | ||
jainankitk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| private static class AddRuleOperation extends RuleProcessingOperation { | ||
| public AddRuleOperation(Rule rule, AttributeValueStoreFactory attributeValueStoreFactory) { | ||
| super(rule, attributeValueStoreFactory); | ||
| } | ||
|
|
||
| @Override | ||
| protected void processAttributeEntry(Map.Entry<Attribute, Set<String>> attributeEntry) { | ||
| AttributeValueStore<String, String> valueStore = getAttributeValueStore(attributeEntry.getKey()); | ||
| for (String value : attributeEntry.getValue()) { | ||
| valueStore.put(value, this.rule.getFeatureValue()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
29 changes: 29 additions & 0 deletions
29
...ing-commons/src/main/java/org/opensearch/rule/attribute_extractor/AttributeExtractor.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,29 @@ | ||
| /* | ||
| * 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.rule.attribute_extractor; | ||
|
|
||
| import org.opensearch.autotagging.Attribute; | ||
|
|
||
| /** | ||
| * This interface defines the contract for extracting the attributes for Rule based auto-tagging feature | ||
| * @param <V> | ||
| */ | ||
| public interface AttributeExtractor<V> { | ||
| /** | ||
| * This method returns the Attribute which it is responsible for extracting | ||
| * @return attribute | ||
| */ | ||
| Attribute getAttribute(); | ||
|
|
||
| /** | ||
| * This method returns the attribute values in context of the current request | ||
| * @return attribute value | ||
| */ | ||
| Iterable<V> extract(); | ||
| } |
12 changes: 12 additions & 0 deletions
12
...totagging-commons/src/main/java/org/opensearch/rule/attribute_extractor/package-info.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,12 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * This package contains feature attribute extractor interface and its implementations | ||
| */ | ||
| package org.opensearch.rule.attribute_extractor; |
12 changes: 12 additions & 0 deletions
12
libs/autotagging-commons/src/main/java/org/opensearch/rule/package-info.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,12 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * Rule based auto-tagging generic constructs | ||
| */ | ||
| package org.opensearch.rule; |
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
52 changes: 52 additions & 0 deletions
52
...tagging-commons/src/main/java/org/opensearch/rule/storage/AttributeValueStoreFactory.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,52 @@ | ||
| /* | ||
| * 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.rule.storage; | ||
|
|
||
| import org.opensearch.autotagging.Attribute; | ||
| import org.opensearch.autotagging.FeatureType; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Factory class for AttributeValueStore per feature type as two feature types can potentially share same attribute | ||
| */ | ||
| public class AttributeValueStoreFactory { | ||
| private final Map<String, AttributeValueStore<String, String>> attributeValueStores = new HashMap<>(); | ||
|
|
||
| /** | ||
| * Constructor | ||
| * @param featureType is the feature which are using rule based auto tagging | ||
| * @param attributeValueStoreSupplier supplies the feature level AttributeValueStore instance | ||
| */ | ||
| public AttributeValueStoreFactory(FeatureType featureType, Supplier<AttributeValueStore<String, String>> attributeValueStoreSupplier) { | ||
| for (Attribute attribute : featureType.getAllowedAttributesRegistry().values()) { | ||
| attributeValueStores.put(attribute.getName(), attributeValueStoreSupplier.get()); | ||
kaushalmahi12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Factory method which returns the {@link AttributeValueStore} for the given attribute | ||
| * @param attribute | ||
| * @return | ||
| */ | ||
| public AttributeValueStore<String, String> getAttributeValueStore(final Attribute attribute) { | ||
| final String attributeName = attribute.getName(); | ||
| if (attributeValueStores == null) { | ||
| throw new IllegalStateException("AttributeValueStoreFactory is not initialized yet."); | ||
| } | ||
|
|
||
| if (!attributeValueStores.containsKey(attributeName)) { | ||
| throw new IllegalArgumentException("[" + attributeName + "] is not a valid attribute for enabled features."); | ||
| } | ||
|
|
||
| return attributeValueStores.get(attributeName); | ||
| } | ||
| } | ||
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.