- 
        Couldn't load subscription status. 
- Fork 2.3k
[Rule based auto tagging] Add WLM action filter to automate tagging for search traffic #17791
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 24 commits into
  opensearch-project:main
from
kaushalmahi12:wlm/action-filter
  
      
      
   
  May 23, 2025 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            24 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      98a832c
              
                add in-memory rule processing service
              
              
                kaushalmahi12 4f41221
              
                add missing javadoc
              
              
                kaushalmahi12 6b41de8
              
                fix generics error
              
              
                kaushalmahi12 a9bb0e4
              
                add auto tagging ActionFilter
              
              
                kaushalmahi12 d448325
              
                add action filter unit tests
              
              
                kaushalmahi12 42d136b
              
                add in-memory rule processing service
              
              
                kaushalmahi12 4c64b2f
              
                add missing javadoc
              
              
                kaushalmahi12 c4b6b9b
              
                remove stubs
              
              
                kaushalmahi12 8437fd6
              
                move generic logic to lib
              
              
                kaushalmahi12 b83cb09
              
                refactor as per lib changes
              
              
                kaushalmahi12 3ec6326
              
                add CHANGELOG entry
              
              
                kaushalmahi12 d83a78e
              
                apply spotless
              
              
                kaushalmahi12 46383f1
              
                fix Unit test
              
              
                kaushalmahi12 4495de0
              
                change the incorrect import
              
              
                kaushalmahi12 6caa89e
              
                fix precommit error
              
              
                kaushalmahi12 a456af5
              
                rebase with mainline
              
              
                kaushalmahi12 4dce3f8
              
                apply spotless check
              
              
                kaushalmahi12 3095a26
              
                add testImplementation to fix action filter tests
              
              
                kaushalmahi12 68d5241
              
                add missing filter chain call
              
              
                kaushalmahi12 75520b8
              
                fix UTs
              
              
                kaushalmahi12 1072f69
              
                fix gradle to include rule-framework for rest tests
              
              
                kaushalmahi12 ea40027
              
                address comments
              
              
                kaushalmahi12 86e27ff
              
                remove support for ScrollRequests
              
              
                kaushalmahi12 d9bf1b5
              
                remove dead imports
              
              
                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
    
  
  
    
              
  
    
      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
    
  
  
    
              
        
          
  
    
      
          
            68 changes: 68 additions & 0 deletions
          
          68 
        
  .../workload-management/src/main/java/org/opensearch/plugin/wlm/AutoTaggingActionFilter.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,68 @@ | ||
| /* | ||
| * 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.plugin.wlm; | ||
|  | ||
| import org.opensearch.action.ActionRequest; | ||
| import org.opensearch.action.IndicesRequest; | ||
| import org.opensearch.action.search.SearchRequest; | ||
| import org.opensearch.action.support.ActionFilter; | ||
| import org.opensearch.action.support.ActionFilterChain; | ||
| import org.opensearch.core.action.ActionListener; | ||
| import org.opensearch.core.action.ActionResponse; | ||
| import org.opensearch.plugin.wlm.rule.attribute_extractor.IndicesExtractor; | ||
| import org.opensearch.rule.InMemoryRuleProcessingService; | ||
| import org.opensearch.tasks.Task; | ||
| import org.opensearch.threadpool.ThreadPool; | ||
| import org.opensearch.wlm.WorkloadGroupTask; | ||
|  | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|  | ||
| /** | ||
| * This class is responsible to evaluate and assign the WORKLOAD_GROUP_ID header in ThreadContext | ||
| */ | ||
| public class AutoTaggingActionFilter implements ActionFilter { | ||
| private final InMemoryRuleProcessingService ruleProcessingService; | ||
|         
                  kaushalmahi12 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| ThreadPool threadPool; | ||
|  | ||
| /** | ||
| * Main constructor | ||
| * @param ruleProcessingService provides access to in memory view of rules | ||
| * @param threadPool to access assign the label | ||
| */ | ||
| public AutoTaggingActionFilter(InMemoryRuleProcessingService ruleProcessingService, ThreadPool threadPool) { | ||
| this.ruleProcessingService = ruleProcessingService; | ||
| this.threadPool = threadPool; | ||
| } | ||
|  | ||
| @Override | ||
| public int order() { | ||
| return Integer.MAX_VALUE; | ||
| } | ||
|  | ||
| @Override | ||
| public <Request extends ActionRequest, Response extends ActionResponse> void apply( | ||
| Task task, | ||
| String action, | ||
| Request request, | ||
| ActionListener<Response> listener, | ||
| ActionFilterChain<Request, Response> chain | ||
| ) { | ||
| final boolean isValidRequest = request instanceof SearchRequest; | ||
|  | ||
| if (!isValidRequest) { | ||
| chain.proceed(task, action, request, listener); | ||
| return; | ||
| } | ||
| Optional<String> label = ruleProcessingService.evaluateLabel(List.of(new IndicesExtractor((IndicesRequest) request))); | ||
|         
                  kaushalmahi12 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| label.ifPresent(s -> threadPool.getThreadContext().putHeader(WorkloadGroupTask.WORKLOAD_GROUP_ID_HEADER, s)); | ||
|         
                  kaushalmahi12 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| chain.proceed(task, action, request, listener); | ||
| } | ||
| } | ||
  
    
      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
    
  
  
    
              
        
          
  
    
      
          
            128 changes: 128 additions & 0 deletions
          
          128 
        
  ...load-management/src/test/java/org/opensearch/plugin/wlm/AutoTaggingActionFilterTests.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,128 @@ | ||
| /* | ||
| * 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.plugin.wlm; | ||
|  | ||
| import org.opensearch.action.ActionRequest; | ||
| import org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest; | ||
| import org.opensearch.action.search.SearchRequest; | ||
| import org.opensearch.action.support.ActionFilterChain; | ||
| import org.opensearch.common.util.concurrent.ThreadContext; | ||
| import org.opensearch.core.action.ActionListener; | ||
| import org.opensearch.core.action.ActionResponse; | ||
| import org.opensearch.core.common.io.stream.StreamOutput; | ||
| import org.opensearch.rule.InMemoryRuleProcessingService; | ||
| import org.opensearch.rule.autotagging.Attribute; | ||
| import org.opensearch.rule.autotagging.FeatureType; | ||
| import org.opensearch.rule.storage.DefaultAttributeValueStore; | ||
| import org.opensearch.tasks.Task; | ||
| import org.opensearch.test.OpenSearchTestCase; | ||
| import org.opensearch.threadpool.TestThreadPool; | ||
| import org.opensearch.threadpool.ThreadPool; | ||
| import org.opensearch.wlm.WorkloadGroupTask; | ||
|  | ||
| import java.io.IOException; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|  | ||
| import static org.mockito.Mockito.anyList; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.spy; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|  | ||
| public class AutoTaggingActionFilterTests extends OpenSearchTestCase { | ||
|  | ||
| AutoTaggingActionFilter autoTaggingActionFilter; | ||
| InMemoryRuleProcessingService ruleProcessingService; | ||
| ThreadPool threadPool; | ||
|  | ||
| public void setUp() throws Exception { | ||
| super.setUp(); | ||
| threadPool = new TestThreadPool("AutoTaggingActionFilterTests"); | ||
| ruleProcessingService = spy(new InMemoryRuleProcessingService(WLMFeatureType.WLM, DefaultAttributeValueStore::new)); | ||
| autoTaggingActionFilter = new AutoTaggingActionFilter(ruleProcessingService, threadPool); | ||
| } | ||
|  | ||
| public void tearDown() throws Exception { | ||
| super.tearDown(); | ||
| threadPool.shutdownNow(); | ||
| } | ||
|  | ||
| public void testOrder() { | ||
| assertEquals(Integer.MAX_VALUE, autoTaggingActionFilter.order()); | ||
| } | ||
|  | ||
| public void testApplyForValidRequest() { | ||
| SearchRequest request = mock(SearchRequest.class); | ||
| ActionFilterChain<ActionRequest, ActionResponse> mockFilterChain = mock(TestActionFilterChain.class); | ||
| when(request.indices()).thenReturn(new String[] { "foo" }); | ||
| try (ThreadContext.StoredContext context = threadPool.getThreadContext().stashContext()) { | ||
| when(ruleProcessingService.evaluateLabel(anyList())).thenReturn(Optional.of("TestQG_ID")); | ||
| autoTaggingActionFilter.apply(mock(Task.class), "Test", request, null, mockFilterChain); | ||
|  | ||
| assertEquals("TestQG_ID", threadPool.getThreadContext().getHeader(WorkloadGroupTask.WORKLOAD_GROUP_ID_HEADER)); | ||
| verify(ruleProcessingService, times(1)).evaluateLabel(anyList()); | ||
| } | ||
| } | ||
|  | ||
| public void testApplyForInValidRequest() { | ||
| ActionFilterChain<ActionRequest, ActionResponse> mockFilterChain = mock(TestActionFilterChain.class); | ||
| CancelTasksRequest request = new CancelTasksRequest(); | ||
| autoTaggingActionFilter.apply(mock(Task.class), "Test", request, null, mockFilterChain); | ||
|  | ||
| verify(ruleProcessingService, times(0)).evaluateLabel(anyList()); | ||
| } | ||
|  | ||
| public enum WLMFeatureType implements FeatureType { | ||
| WLM; | ||
|  | ||
| @Override | ||
| public String getName() { | ||
| return ""; | ||
| } | ||
|  | ||
| @Override | ||
| public Map<String, Attribute> getAllowedAttributesRegistry() { | ||
| return Map.of("test_attribute", TestAttribute.TEST_ATTRIBUTE); | ||
| } | ||
|  | ||
| @Override | ||
| public void registerFeatureType() {} | ||
| } | ||
|  | ||
| public enum TestAttribute implements Attribute { | ||
| TEST_ATTRIBUTE("test_attribute"), | ||
| INVALID_ATTRIBUTE("invalid_attribute"); | ||
|  | ||
| private final String name; | ||
|  | ||
| TestAttribute(String name) { | ||
| this.name = name; | ||
| } | ||
|  | ||
| @Override | ||
| public String getName() { | ||
| return name; | ||
| } | ||
|  | ||
| @Override | ||
| public void validateAttribute() {} | ||
|  | ||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException {} | ||
| } | ||
|  | ||
| private static class TestActionFilterChain implements ActionFilterChain<ActionRequest, ActionResponse> { | ||
| @Override | ||
| public void proceed(Task task, String action, ActionRequest request, ActionListener<ActionResponse> listener) { | ||
|  | ||
| } | ||
| } | ||
| } | 
      
      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.