generated from amazon-archives/__template_Apache-2.0
    
        
        - 
                Notifications
    You must be signed in to change notification settings 
- Fork 24
Persist local indices on exporter type change #465
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
      
      
            dzane17
  wants to merge
  1
  commit into
  opensearch-project:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
dzane17:save-local-indices
  
      
      
   
  
    
  
  
  
 
  
      
    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.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
  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 | 
|---|---|---|
|  | @@ -10,10 +10,8 @@ | |
|  | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.ArgumentMatchers.argThat; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.doAnswer; | ||
| import static org.mockito.Mockito.doNothing; | ||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.spy; | ||
|  | @@ -36,8 +34,6 @@ | |
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.time.LocalDate; | ||
| import java.time.ZoneId; | ||
| import java.time.ZonedDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.HashMap; | ||
|  | @@ -324,61 +320,6 @@ public void testGetHealthStats() { | |
| assertTrue(fieldTypeCacheStats.containsKey(MISS_COUNT)); | ||
| } | ||
|  | ||
| public void testDeleteAllTopNIndices() throws IOException { | ||
| 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. instead of removing the tests, we should have tests to validate that the local index "will not be deleted" after disabling. | ||
| // Create 9 top_queries-* indices | ||
| Map<String, IndexMetadata> indexMetadataMap = new HashMap<>(); | ||
| for (int i = 1; i < 10; i++) { | ||
| String indexName = "top_queries-2024.01.0" | ||
| + i | ||
| + "-" | ||
| + generateLocalIndexDateHash(ZonedDateTime.of(2024, 1, i, 0, 0, 0, 0, ZoneId.of("UTC")).toLocalDate()); | ||
| long creationTime = Instant.now().minus(i, ChronoUnit.DAYS).toEpochMilli(); | ||
|  | ||
| IndexMetadata indexMetadata = IndexMetadata.builder(indexName) | ||
| .settings( | ||
| Settings.builder() | ||
| .put("index.version.created", Version.CURRENT.id) | ||
| .put("index.number_of_shards", 1) | ||
| .put("index.number_of_replicas", 1) | ||
| .put(SETTING_CREATION_DATE, creationTime) | ||
| ) | ||
| .putMapping( | ||
| new MappingMetadata("_doc", Map.of("_meta", Map.of(QUERY_INSIGHTS_INDEX_TAG_NAME, TOP_QUERIES_INDEX_TAG_VALUE))) | ||
| ) | ||
| .build(); | ||
| indexMetadataMap.put(indexName, indexMetadata); | ||
| } | ||
| // Create 5 user indices | ||
| for (int i = 0; i < 5; i++) { | ||
| String indexName = "my_index-" + i; | ||
| long creationTime = Instant.now().minus(i, ChronoUnit.DAYS).toEpochMilli(); | ||
|  | ||
| IndexMetadata indexMetadata = IndexMetadata.builder(indexName) | ||
| .settings( | ||
| Settings.builder() | ||
| .put("index.version.created", Version.CURRENT.id) | ||
| .put("index.number_of_shards", 1) | ||
| .put("index.number_of_replicas", 1) | ||
| .put(SETTING_CREATION_DATE, creationTime) | ||
| ) | ||
| .putMapping( | ||
| new MappingMetadata("_doc", Map.of("_meta", Map.of(QUERY_INSIGHTS_INDEX_TAG_NAME, TOP_QUERIES_INDEX_TAG_VALUE))) | ||
| ) | ||
| .build(); | ||
| indexMetadataMap.put(indexName, indexMetadata); | ||
| } | ||
| List<AbstractLifecycleComponent> updatedService = createQueryInsightsServiceWithIndexState(indexMetadataMap); | ||
| QueryInsightsService updatedQueryInsightsService = (QueryInsightsService) updatedService.get(0); | ||
| ClusterService updatedClusterService = (ClusterService) updatedService.get(1); | ||
|  | ||
| updatedQueryInsightsService.deleteAllTopNIndices(client, mockLocalIndexExporter); | ||
| // All 10 top_queries-* indices should be deleted, while none of the users indices should be deleted | ||
| verify(mockLocalIndexExporter, times(9)).deleteSingleIndex(argThat(str -> str.matches("top_queries-.*")), any()); | ||
|  | ||
| IOUtils.close(updatedClusterService); | ||
| updatedQueryInsightsService.doClose(); | ||
| } | ||
|  | ||
| public void testDeleteExpiredTopNIndices() throws InterruptedException, IOException { | ||
| // Test with a new cluster state with expired index mappings | ||
| // Create 9 top_queries-* indices with creation dates older than the retention period | ||
|  | @@ -456,15 +397,13 @@ public void testValidateExporterDeleteAfter() { | |
| public void testSetExporterAndReaderType_SwitchFromLocalIndexToNone() throws IOException { | ||
| // Mock current exporter and reader | ||
| queryInsightsServiceSpy.sinkType = SinkType.LOCAL_INDEX; | ||
| doNothing().when(queryInsightsServiceSpy).deleteAllTopNIndices(any(), any()); | ||
| // Mock current exporter and reader | ||
| when(queryInsightsExporterFactory.getExporter(TopQueriesService.TOP_QUERIES_EXPORTER_ID)).thenReturn(mockLocalIndexExporter); | ||
| when(queryInsightsReaderFactory.getReader(TopQueriesService.TOP_QUERIES_READER_ID)).thenReturn(mockReader); | ||
|  | ||
| // Execute method | ||
| queryInsightsServiceSpy.setExporterAndReaderType(SinkType.NONE); | ||
| // Verify cleanup of local indices | ||
| verify(queryInsightsServiceSpy, times(1)).deleteAllTopNIndices(client, mockLocalIndexExporter); | ||
| verify(queryInsightsExporterFactory, times(1)).closeExporter(mockLocalIndexExporter); | ||
| verify(queryInsightsReaderFactory, times(1)).closeReader(mockReader); | ||
| // Verify exporter is set to NONE | ||
|  | @@ -474,15 +413,13 @@ public void testSetExporterAndReaderType_SwitchFromLocalIndexToNone() throws IOE | |
| public void testSetExporterAndReaderType_SwitchFromLocalIndexToDebug() throws IOException { | ||
| // Mock current exporter and reader | ||
| queryInsightsServiceSpy.sinkType = SinkType.LOCAL_INDEX; | ||
| doNothing().when(queryInsightsServiceSpy).deleteAllTopNIndices(any(), any()); | ||
| // Mock current exporter and reader | ||
| when(queryInsightsExporterFactory.getExporter(TopQueriesService.TOP_QUERIES_EXPORTER_ID)).thenReturn(mockLocalIndexExporter); | ||
| when(queryInsightsReaderFactory.getReader(TopQueriesService.TOP_QUERIES_READER_ID)).thenReturn(mockReader); | ||
|  | ||
| // Execute method | ||
| queryInsightsServiceSpy.setExporterAndReaderType(SinkType.DEBUG); | ||
| // Verify cleanup of local indices | ||
| verify(queryInsightsServiceSpy, times(1)).deleteAllTopNIndices(client, mockLocalIndexExporter); | ||
| verify(queryInsightsExporterFactory, times(1)).closeExporter(mockLocalIndexExporter); | ||
| verify(queryInsightsReaderFactory, times(1)).closeReader(mockReader); | ||
| // Verify exporter is set to NONE | ||
|  | @@ -510,7 +447,6 @@ public void testSetExporterAndReaderType_SwitchFromNoneToLocalIndex() throws IOE | |
| ); | ||
| verify(queryInsightsExporterFactory, times(0)).closeExporter(any()); | ||
| verify(queryInsightsReaderFactory, times(0)).closeReader(any()); | ||
| verify(queryInsightsServiceSpy, times(0)).deleteAllTopNIndices(any(), any()); | ||
| assertEquals(SinkType.LOCAL_INDEX, queryInsightsServiceSpy.sinkType); | ||
| } | ||
|  | ||
|  | @@ -523,7 +459,6 @@ public void testSetExporterAndReaderType_SwitchFromNoneToDebug() throws IOExcept | |
| queryInsightsServiceSpy.setExporterAndReaderType(SinkType.DEBUG); | ||
| // Verify new local index exporter setup | ||
| verify(queryInsightsExporterFactory, times(1)).createDebugExporter(eq(TopQueriesService.TOP_QUERIES_EXPORTER_ID)); | ||
| verify(queryInsightsServiceSpy, times(0)).deleteAllTopNIndices(any(), any()); | ||
| verify(queryInsightsExporterFactory, times(0)).closeExporter(any()); | ||
| verify(queryInsightsReaderFactory, times(0)).closeReader(any()); | ||
| assertEquals(SinkType.DEBUG, queryInsightsServiceSpy.sinkType); | ||
|  | @@ -550,7 +485,6 @@ public void testSetExporterAndReaderType_SwitchFromDebugToLocalIndex() throws IO | |
| ); | ||
| verify(queryInsightsExporterFactory, times(1)).closeExporter(mockDebugExporter); | ||
| verify(queryInsightsReaderFactory, times(1)).closeReader(mockReader); | ||
| verify(queryInsightsServiceSpy, times(0)).deleteAllTopNIndices(any(), any()); | ||
| assertEquals(SinkType.LOCAL_INDEX, queryInsightsServiceSpy.sinkType); | ||
| } | ||
|  | ||
|  | @@ -563,15 +497,13 @@ public void testSetExporterAndReaderType_SwitchFromDebugToNone() throws IOExcept | |
| queryInsightsServiceSpy.setExporterAndReaderType(SinkType.NONE); | ||
| // Verify new local index exporter setup | ||
| // 2 times, one for initialization, one for the above method call | ||
| verify(queryInsightsServiceSpy, times(0)).deleteAllTopNIndices(client, mockLocalIndexExporter); | ||
| verify(queryInsightsExporterFactory, times(1)).closeExporter(mockDebugExporter); | ||
| verify(queryInsightsReaderFactory, times(1)).closeReader(mockReader); | ||
| assertEquals(SinkType.NONE, queryInsightsServiceSpy.sinkType); | ||
| } | ||
|  | ||
| public void testSetExporterAndReaderType_CloseWithException() throws IOException { | ||
| queryInsightsServiceSpy.sinkType = SinkType.LOCAL_INDEX; | ||
| doNothing().when(queryInsightsServiceSpy).deleteAllTopNIndices(any(), any()); | ||
| // Mock current exporter that throws an exception when closing | ||
| when(queryInsightsExporterFactory.getExporter(TopQueriesService.TOP_QUERIES_EXPORTER_ID)).thenReturn(mockLocalIndexExporter); | ||
| when(queryInsightsReaderFactory.getReader(TopQueriesService.TOP_QUERIES_READER_ID)).thenReturn(mockReader); | ||
|  | ||
      
      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.
Do you think we should add an API endpoint to do this?
Uh oh!
There was an error while loading. Please reload this page.
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.
No, admins can delete indices manually I believe and retention policy will still apply
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.
makes sense.
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.
I think one caveat here is, if there are issues with local index exporters, we won't have a quick solution to solve it. like, for the local index reader performance issue we encountered this year, we can simply disable this setting to remove all local index.
If any similar unknown issue happens later, we will need to delete user's indices manually (without the validation logic in our code), or wait for 1 day until the retention job kicks in.
That's the reason I feel it's better to have an API to delete all indices with correct validation logic would make sense. Another alternative is to lower the min value of the delete_after setting to say 1 hour. but the deletion logic will be more complicated since we will need to delete the documents instead of the indices.