- 
                Notifications
    
You must be signed in to change notification settings  - Fork 25.6k
 
Additional index settings provider validation #113838
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
      
      
            martijnvg
  merged 7 commits into
  elastic:main
from
martijnvg:index_settings_provider_validation
  
      
      
   
  Oct 11, 2024 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      a13ad8d
              
                Fail if an index settings provider adds a setting that was added by a…
              
              
                martijnvg 74459e2
              
                Merge remote-tracking branch 'es/main' into index_settings_provider_v…
              
              
                martijnvg 345d10b
              
                Merge remote-tracking branch 'es/main' into index_settings_provider_v…
              
              
                martijnvg 517df77
              
                added test
              
              
                martijnvg 26ae8f6
              
                cleanup
              
              
                martijnvg cb07a08
              
                spotless
              
              
                martijnvg f804fbe
              
                Merge remote-tracking branch 'es/main' into index_settings_provider_v…
              
              
                martijnvg 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
    
  
  
    
              
        
          
          
            95 changes: 95 additions & 0 deletions
          
          95 
        
  server/src/test/java/org/elasticsearch/index/IndexSettingProviderTests.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,95 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
| 
     | 
||
| package org.elasticsearch.index; | ||
| 
     | 
||
| import org.elasticsearch.cluster.metadata.Metadata; | ||
| import org.elasticsearch.common.compress.CompressedXContent; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.plugins.Plugin; | ||
| import org.elasticsearch.test.ESSingleNodeTestCase; | ||
| 
     | 
||
| import java.time.Instant; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| 
     | 
||
| public class IndexSettingProviderTests extends ESSingleNodeTestCase { | ||
| 
     | 
||
| public void testIndexCreation() throws Exception { | ||
| var indexService = createIndex("my-index1"); | ||
| assertFalse(indexService.getIndexSettings().getSettings().hasValue("index.refresh_interval")); | ||
| 
     | 
||
| INDEX_SETTING_PROVIDER1_ENABLED.set(true); | ||
| indexService = createIndex("my-index2"); | ||
| assertTrue(indexService.getIndexSettings().getSettings().hasValue("index.refresh_interval")); | ||
| 
     | 
||
| INDEX_SETTING_PROVIDER2_ENABLED.set(true); | ||
| var e = expectThrows(IllegalArgumentException.class, () -> createIndex("my-index3")); | ||
| assertEquals( | ||
| "additional index setting [index.refresh_interval] added by [TestIndexSettingsProvider] is already present", | ||
| e.getMessage() | ||
| ); | ||
| } | ||
| 
     | 
||
| @Override | ||
| protected Collection<Class<? extends Plugin>> getPlugins() { | ||
| return List.of(Plugin1.class, Plugin2.class); | ||
| } | ||
| 
     | 
||
| public static class Plugin1 extends Plugin { | ||
| 
     | 
||
| @Override | ||
| public Collection<IndexSettingProvider> getAdditionalIndexSettingProviders(IndexSettingProvider.Parameters parameters) { | ||
| return List.of(new TestIndexSettingsProvider("index.refresh_interval", "-1", INDEX_SETTING_PROVIDER1_ENABLED)); | ||
| } | ||
| 
     | 
||
| } | ||
| 
     | 
||
| public static class Plugin2 extends Plugin { | ||
| 
     | 
||
| @Override | ||
| public Collection<IndexSettingProvider> getAdditionalIndexSettingProviders(IndexSettingProvider.Parameters parameters) { | ||
| return List.of(new TestIndexSettingsProvider("index.refresh_interval", "100s", INDEX_SETTING_PROVIDER2_ENABLED)); | ||
| } | ||
| } | ||
| 
     | 
||
| private static final AtomicBoolean INDEX_SETTING_PROVIDER1_ENABLED = new AtomicBoolean(false); | ||
| private static final AtomicBoolean INDEX_SETTING_PROVIDER2_ENABLED = new AtomicBoolean(false); | ||
| 
     | 
||
| static class TestIndexSettingsProvider implements IndexSettingProvider { | ||
| 
     | 
||
| private final String settingName; | ||
| private final String settingValue; | ||
| private final AtomicBoolean enabled; | ||
| 
     | 
||
| TestIndexSettingsProvider(String settingName, String settingValue, AtomicBoolean enabled) { | ||
| this.settingName = settingName; | ||
| this.settingValue = settingValue; | ||
| this.enabled = enabled; | ||
| } | ||
| 
     | 
||
| @Override | ||
| public Settings getAdditionalIndexSettings( | ||
| String indexName, | ||
| String dataStreamName, | ||
| boolean isTimeSeries, | ||
| Metadata metadata, | ||
| Instant resolvedAt, | ||
| Settings indexTemplateAndCreateRequestSettings, | ||
| List<CompressedXContent> combinedTemplateMappings | ||
| ) { | ||
| if (enabled.get()) { | ||
| return Settings.builder().put(settingName, settingValue).build(); | ||
| } else { | ||
| return Settings.EMPTY; | ||
| } | ||
| } | ||
| } | ||
| } | 
  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.
Here we validate the
newAdditionalSettingsagainst theadditionalSettingsseen so far and not values previously set by the user for exampletemplateAndRequestSettings. Are these 2 settings sets disjoint ?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.
The
newAdditionalSettingsvariable contains any settings that the current index settings provider might have set. TheadditionalIndexSettingsvariable tracks any settings that any index settings provider may have set while looping over the list of index settings providers. The new validation requires thatnewAdditionalSettingscannot contain any settings that are already defined inadditionalIndexSettings. So yes, these two Settings variables should be a disjoint.