- 
                Notifications
    
You must be signed in to change notification settings  - Fork 25.6k
 
          Refactor ShrinkStep to ResizeStep
          #133591
        
          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
      
      
    
  
     Merged
                    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
    
  
  
    
              
        
          
          
            150 changes: 150 additions & 0 deletions
          
          150 
        
  x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ResizeIndexStep.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,150 @@ | ||
| /* | ||
| * 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
| package org.elasticsearch.xpack.core.ilm; | ||
| 
     | 
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; | ||
| import org.elasticsearch.action.admin.indices.shrink.ResizeType; | ||
| import org.elasticsearch.client.internal.Client; | ||
| import org.elasticsearch.cluster.ClusterStateObserver; | ||
| import org.elasticsearch.cluster.ProjectState; | ||
| import org.elasticsearch.cluster.metadata.IndexMetadata; | ||
| import org.elasticsearch.cluster.metadata.LifecycleExecutionState; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.common.unit.ByteSizeValue; | ||
| import org.elasticsearch.core.Nullable; | ||
| import org.elasticsearch.core.TimeValue; | ||
| 
     | 
||
| import java.util.Objects; | ||
| import java.util.function.BiFunction; | ||
| import java.util.function.Function; | ||
| 
     | 
||
| /** | ||
| * Resizes an index with the specified settings, using the name that was generated in a previous {@link GenerateUniqueIndexNameStep} step. | ||
| */ | ||
| public class ResizeIndexStep extends AsyncActionStep { | ||
| 
     | 
||
| public static final String SHRINK = "shrink"; | ||
| public static final String CLONE = "clone"; | ||
| private static final Logger logger = LogManager.getLogger(ResizeIndexStep.class); | ||
| 
     | 
||
| private final ResizeType resizeType; | ||
| private final BiFunction<String, LifecycleExecutionState, String> targetIndexNameSupplier; | ||
| /** A supplier that takes the index metadata of the <i>original</i> index and returns settings for the target index . */ | ||
| private final Function<IndexMetadata, Settings> targetIndexSettingsSupplier; | ||
| @Nullable | ||
| private final ByteSizeValue maxPrimaryShardSize; | ||
| 
     | 
||
| public ResizeIndexStep( | ||
| StepKey key, | ||
| StepKey nextStepKey, | ||
| Client client, | ||
| ResizeType resizeType, | ||
| BiFunction<String, LifecycleExecutionState, String> targetIndexNameSupplier, | ||
| Function<IndexMetadata, Settings> targetIndexSettingsSupplier, | ||
| @Nullable ByteSizeValue maxPrimaryShardSize | ||
| ) { | ||
| super(key, nextStepKey, client); | ||
| this.resizeType = resizeType; | ||
| this.targetIndexNameSupplier = targetIndexNameSupplier; | ||
| this.targetIndexSettingsSupplier = targetIndexSettingsSupplier; | ||
| this.maxPrimaryShardSize = maxPrimaryShardSize; | ||
| assert resizeType == ResizeType.SHRINK || maxPrimaryShardSize == null : "maxPrimaryShardSize can only be set for shrink operations"; | ||
| } | ||
| 
     | 
||
| @Override | ||
| public boolean isRetryable() { | ||
| return true; | ||
| } | ||
| 
     | 
||
| @Override | ||
| public void performAction( | ||
| IndexMetadata indexMetadata, | ||
| ProjectState currentState, | ||
| ClusterStateObserver observer, | ||
| ActionListener<Void> listener | ||
| ) { | ||
| LifecycleExecutionState lifecycleState = indexMetadata.getLifecycleExecutionState(); | ||
| if (lifecycleState.lifecycleDate() == null) { | ||
| throw new IllegalStateException("source index [" + indexMetadata.getIndex().getName() + "] is missing lifecycle date"); | ||
| } | ||
| 
     | 
||
| final String targetIndexName = targetIndexNameSupplier.apply(indexMetadata.getIndex().getName(), lifecycleState); | ||
| if (currentState.metadata().index(targetIndexName) != null) { | ||
| logger.warn( | ||
| "skipping [{}] step for index [{}] as part of policy [{}] as the target index [{}] already exists", | ||
| getKey().name(), | ||
| indexMetadata.getIndex().getName(), | ||
| indexMetadata.getLifecyclePolicyName(), | ||
| targetIndexName | ||
| ); | ||
| listener.onResponse(null); | ||
| return; | ||
| } | ||
| 
     | 
||
| Settings relevantTargetSettings = Settings.builder() | ||
| .put(targetIndexSettingsSupplier.apply(indexMetadata)) | ||
| // We add the skip setting to prevent ILM from processing the shrunken index before the execution state has been copied - which | ||
| // could happen if the shards of the shrunken index take a long time to allocate. | ||
| .put(LifecycleSettings.LIFECYCLE_SKIP, true) | ||
| .build(); | ||
| 
     | 
||
| ResizeRequest resizeRequest = new ResizeRequest(targetIndexName, indexMetadata.getIndex().getName()).masterNodeTimeout( | ||
| TimeValue.MAX_VALUE | ||
| ); | ||
| resizeRequest.setResizeType(resizeType); | ||
| resizeRequest.getTargetIndexRequest().settings(relevantTargetSettings); | ||
| if (resizeType == ResizeType.SHRINK) { | ||
| resizeRequest.setMaxPrimaryShardSize(maxPrimaryShardSize); | ||
| } | ||
| 
     | 
||
| // This request does not wait for (successful) completion of the resize operation - it fires-and-forgets. | ||
| // It's up to a subsequent step to check for the existence of the target index and wait for it to be green. | ||
| getClient(currentState.projectId()).admin() | ||
| .indices() | ||
| .resizeIndex(resizeRequest, listener.delegateFailureAndWrap((l, response) -> l.onResponse(null))); | ||
| 
     | 
||
| } | ||
| 
     | 
||
| public ResizeType getResizeType() { | ||
| return resizeType; | ||
| } | ||
| 
     | 
||
| public BiFunction<String, LifecycleExecutionState, String> getTargetIndexNameSupplier() { | ||
| return targetIndexNameSupplier; | ||
| } | ||
| 
     | 
||
| public Function<IndexMetadata, Settings> getTargetIndexSettingsSupplier() { | ||
| return targetIndexSettingsSupplier; | ||
| } | ||
| 
     | 
||
| public ByteSizeValue getMaxPrimaryShardSize() { | ||
| return maxPrimaryShardSize; | ||
| } | ||
| 
     | 
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(super.hashCode(), resizeType, maxPrimaryShardSize); | ||
| } | ||
| 
     | 
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (obj == null) { | ||
| return false; | ||
| } | ||
| if (getClass() != obj.getClass()) { | ||
| return false; | ||
| } | ||
| ResizeIndexStep other = (ResizeIndexStep) obj; | ||
| return super.equals(obj) | ||
| && Objects.equals(resizeType, other.resizeType) | ||
| && Objects.equals(maxPrimaryShardSize, other.maxPrimaryShardSize); | ||
| } | ||
| 
     | 
||
| } | ||
  
    
      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
    
  
  
    
              
        
          
          
            129 changes: 0 additions & 129 deletions
          
          129 
        
  x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java
  
  
      
      
   
        
      
      
    This file was deleted.
      
      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.