|  | @@ -10,6 +10,7 @@ import org.elasticsearch.action.ActionListener;
 | 
	
		
			
				|  |  |  import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
 | 
	
		
			
				|  |  |  import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
 | 
	
		
			
				|  |  |  import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
 | 
	
		
			
				|  |  | +import org.elasticsearch.action.admin.indices.shrink.ResizeType;
 | 
	
		
			
				|  |  |  import org.elasticsearch.cluster.ProjectState;
 | 
	
		
			
				|  |  |  import org.elasticsearch.cluster.metadata.AliasMetadata;
 | 
	
		
			
				|  |  |  import org.elasticsearch.cluster.metadata.IndexMetadata;
 | 
	
	
		
			
				|  | @@ -29,60 +30,81 @@ import static org.elasticsearch.common.IndexNameGenerator.generateValidIndexName
 | 
	
		
			
				|  |  |  import static org.elasticsearch.xpack.core.ilm.ShrinkIndexNameSupplier.SHRUNKEN_INDEX_PREFIX;
 | 
	
		
			
				|  |  |  import static org.hamcrest.Matchers.equalTo;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -public class ShrinkStepTests extends AbstractStepTestCase<ShrinkStep> {
 | 
	
		
			
				|  |  | +public class ResizeIndexStepTests extends AbstractStepTestCase<ResizeIndexStep> {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  | -    public ShrinkStep createRandomInstance() {
 | 
	
		
			
				|  |  | +    public ResizeIndexStep createRandomInstance() {
 | 
	
		
			
				|  |  |          StepKey stepKey = randomStepKey();
 | 
	
		
			
				|  |  |          StepKey nextStepKey = randomStepKey();
 | 
	
		
			
				|  |  | -        Integer numberOfShards = null;
 | 
	
		
			
				|  |  | +        ResizeType resizeType = randomFrom(ResizeType.values());
 | 
	
		
			
				|  |  | +        Settings.Builder settings = Settings.builder();
 | 
	
		
			
				|  |  |          ByteSizeValue maxPrimaryShardSize = null;
 | 
	
		
			
				|  |  | -        if (randomBoolean()) {
 | 
	
		
			
				|  |  | -            numberOfShards = randomIntBetween(1, 20);
 | 
	
		
			
				|  |  | +        // Only shrink supports max_primary_shard_size, so if we pick shrink we sometimes set it, otherwise we always
 | 
	
		
			
				|  |  | +        // set number_of_shards.
 | 
	
		
			
				|  |  | +        if (resizeType != ResizeType.SHRINK || randomBoolean()) {
 | 
	
		
			
				|  |  | +            settings.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 20));
 | 
	
		
			
				|  |  |          } else {
 | 
	
		
			
				|  |  |              maxPrimaryShardSize = ByteSizeValue.ofBytes(between(1, 100));
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        return new ShrinkStep(stepKey, nextStepKey, client, numberOfShards, maxPrimaryShardSize);
 | 
	
		
			
				|  |  | +        return new ResizeIndexStep(
 | 
	
		
			
				|  |  | +            stepKey,
 | 
	
		
			
				|  |  | +            nextStepKey,
 | 
	
		
			
				|  |  | +            client,
 | 
	
		
			
				|  |  | +            resizeType,
 | 
	
		
			
				|  |  | +            (index, state) -> randomAlphaOfLength(5) + index,
 | 
	
		
			
				|  |  | +            indexMetadata -> settings.build(),
 | 
	
		
			
				|  |  | +            maxPrimaryShardSize
 | 
	
		
			
				|  |  | +        );
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  | -    public ShrinkStep mutateInstance(ShrinkStep instance) {
 | 
	
		
			
				|  |  | +    public ResizeIndexStep mutateInstance(ResizeIndexStep instance) {
 | 
	
		
			
				|  |  |          StepKey key = instance.getKey();
 | 
	
		
			
				|  |  |          StepKey nextKey = instance.getNextStepKey();
 | 
	
		
			
				|  |  | -        Integer numberOfShards = instance.getNumberOfShards();
 | 
	
		
			
				|  |  | +        ResizeType resizeType = instance.getResizeType();
 | 
	
		
			
				|  |  |          ByteSizeValue maxPrimaryShardSize = instance.getMaxPrimaryShardSize();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          switch (between(0, 2)) {
 | 
	
		
			
				|  |  |              case 0 -> key = new StepKey(key.phase(), key.action(), key.name() + randomAlphaOfLength(5));
 | 
	
		
			
				|  |  |              case 1 -> nextKey = new StepKey(nextKey.phase(), nextKey.action(), nextKey.name() + randomAlphaOfLength(5));
 | 
	
		
			
				|  |  |              case 2 -> {
 | 
	
		
			
				|  |  | -                if (numberOfShards != null) {
 | 
	
		
			
				|  |  | -                    numberOfShards = numberOfShards + 1;
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -                if (maxPrimaryShardSize != null) {
 | 
	
		
			
				|  |  | -                    maxPrimaryShardSize = ByteSizeValue.ofBytes(maxPrimaryShardSize.getBytes() + 1);
 | 
	
		
			
				|  |  | +                if (resizeType != ResizeType.SHRINK || randomBoolean()) {
 | 
	
		
			
				|  |  | +                    resizeType = randomValueOtherThan(resizeType, () -> randomFrom(ResizeType.values()));
 | 
	
		
			
				|  |  | +                    maxPrimaryShardSize = null;
 | 
	
		
			
				|  |  | +                } else {
 | 
	
		
			
				|  |  | +                    maxPrimaryShardSize = randomValueOtherThan(maxPrimaryShardSize, () -> ByteSizeValue.ofBytes(between(1, 100)));
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              default -> throw new AssertionError("Illegal randomisation branch");
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        return new ShrinkStep(key, nextKey, instance.getClientWithoutProject(), numberOfShards, maxPrimaryShardSize);
 | 
	
		
			
				|  |  | +        return new ResizeIndexStep(
 | 
	
		
			
				|  |  | +            key,
 | 
	
		
			
				|  |  | +            nextKey,
 | 
	
		
			
				|  |  | +            instance.getClientWithoutProject(),
 | 
	
		
			
				|  |  | +            resizeType,
 | 
	
		
			
				|  |  | +            instance.getTargetIndexNameSupplier(),
 | 
	
		
			
				|  |  | +            instance.getTargetIndexSettingsSupplier(),
 | 
	
		
			
				|  |  | +            maxPrimaryShardSize
 | 
	
		
			
				|  |  | +        );
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  | -    public ShrinkStep copyInstance(ShrinkStep instance) {
 | 
	
		
			
				|  |  | -        return new ShrinkStep(
 | 
	
		
			
				|  |  | +    public ResizeIndexStep copyInstance(ResizeIndexStep instance) {
 | 
	
		
			
				|  |  | +        return new ResizeIndexStep(
 | 
	
		
			
				|  |  |              instance.getKey(),
 | 
	
		
			
				|  |  |              instance.getNextStepKey(),
 | 
	
		
			
				|  |  |              instance.getClientWithoutProject(),
 | 
	
		
			
				|  |  | -            instance.getNumberOfShards(),
 | 
	
		
			
				|  |  | +            instance.getResizeType(),
 | 
	
		
			
				|  |  | +            instance.getTargetIndexNameSupplier(),
 | 
	
		
			
				|  |  | +            instance.getTargetIndexSettingsSupplier(),
 | 
	
		
			
				|  |  |              instance.getMaxPrimaryShardSize()
 | 
	
		
			
				|  |  |          );
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      public void testPerformAction() throws Exception {
 | 
	
		
			
				|  |  |          String lifecycleName = randomAlphaOfLength(5);
 | 
	
		
			
				|  |  | -        ShrinkStep step = createRandomInstance();
 | 
	
		
			
				|  |  | +        ResizeIndexStep step = createRandomInstance();
 | 
	
		
			
				|  |  |          LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder();
 | 
	
		
			
				|  |  |          lifecycleState.setPhase(step.getKey().phase());
 | 
	
		
			
				|  |  |          lifecycleState.setAction(step.getKey().action());
 | 
	
	
		
			
				|  | @@ -103,22 +125,12 @@ public class ShrinkStepTests extends AbstractStepTestCase<ShrinkStep> {
 | 
	
		
			
				|  |  |              assertThat(request.getSourceIndex(), equalTo(sourceIndexMetadata.getIndex().getName()));
 | 
	
		
			
				|  |  |              assertThat(request.getTargetIndexRequest().aliases(), equalTo(Set.of()));
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            Settings.Builder builder = Settings.builder();
 | 
	
		
			
				|  |  | -            builder.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, sourceIndexMetadata.getNumberOfReplicas())
 | 
	
		
			
				|  |  | -                .put(LifecycleSettings.LIFECYCLE_NAME, lifecycleName)
 | 
	
		
			
				|  |  | +            Settings expectedSettings = Settings.builder()
 | 
	
		
			
				|  |  | +                .put(step.getTargetIndexSettingsSupplier().apply(null))
 | 
	
		
			
				|  |  |                  .put(LifecycleSettings.LIFECYCLE_SKIP, true)
 | 
	
		
			
				|  |  | -                .put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", (String) null);
 | 
	
		
			
				|  |  | -            if (step.getNumberOfShards() != null) {
 | 
	
		
			
				|  |  | -                builder.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, step.getNumberOfShards());
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -            assertThat(request.getTargetIndexRequest().settings(), equalTo(builder.build()));
 | 
	
		
			
				|  |  | -            if (step.getNumberOfShards() != null) {
 | 
	
		
			
				|  |  | -                assertThat(
 | 
	
		
			
				|  |  | -                    request.getTargetIndexRequest().settings().getAsInt(IndexMetadata.SETTING_NUMBER_OF_SHARDS, -1),
 | 
	
		
			
				|  |  | -                    equalTo(step.getNumberOfShards())
 | 
	
		
			
				|  |  | -                );
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -            request.setMaxPrimaryShardSize(step.getMaxPrimaryShardSize());
 | 
	
		
			
				|  |  | +                .build();
 | 
	
		
			
				|  |  | +            assertThat(request.getTargetIndexRequest().settings(), equalTo(expectedSettings));
 | 
	
		
			
				|  |  | +            assertThat(request.getMaxPrimaryShardSize(), equalTo(step.getMaxPrimaryShardSize()));
 | 
	
		
			
				|  |  |              listener.onResponse(new CreateIndexResponse(true, true, sourceIndexMetadata.getIndex().getName()));
 | 
	
		
			
				|  |  |              return null;
 | 
	
		
			
				|  |  |          }).when(indicesClient).resizeIndex(Mockito.any(), Mockito.any());
 | 
	
	
		
			
				|  | @@ -136,7 +148,7 @@ public class ShrinkStepTests extends AbstractStepTestCase<ShrinkStep> {
 | 
	
		
			
				|  |  |      public void testPerformActionShrunkenIndexExists() throws Exception {
 | 
	
		
			
				|  |  |          String sourceIndexName = randomAlphaOfLength(10);
 | 
	
		
			
				|  |  |          String lifecycleName = randomAlphaOfLength(5);
 | 
	
		
			
				|  |  | -        ShrinkStep step = createRandomInstance();
 | 
	
		
			
				|  |  | +        ResizeIndexStep step = createRandomInstance();
 | 
	
		
			
				|  |  |          LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder();
 | 
	
		
			
				|  |  |          lifecycleState.setPhase(step.getKey().phase());
 | 
	
		
			
				|  |  |          lifecycleState.setAction(step.getKey().action());
 | 
	
	
		
			
				|  | @@ -180,7 +192,7 @@ public class ShrinkStepTests extends AbstractStepTestCase<ShrinkStep> {
 | 
	
		
			
				|  |  |              .numberOfShards(randomIntBetween(1, 5))
 | 
	
		
			
				|  |  |              .numberOfReplicas(randomIntBetween(0, 5))
 | 
	
		
			
				|  |  |              .build();
 | 
	
		
			
				|  |  | -        ShrinkStep step = createRandomInstance();
 | 
	
		
			
				|  |  | +        ResizeIndexStep step = createRandomInstance();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          Mockito.doAnswer(invocation -> {
 | 
	
		
			
				|  |  |              @SuppressWarnings("unchecked")
 | 
	
	
		
			
				|  | @@ -209,7 +221,7 @@ public class ShrinkStepTests extends AbstractStepTestCase<ShrinkStep> {
 | 
	
		
			
				|  |  |              .numberOfReplicas(randomIntBetween(0, 5))
 | 
	
		
			
				|  |  |              .build();
 | 
	
		
			
				|  |  |          Exception exception = new RuntimeException();
 | 
	
		
			
				|  |  | -        ShrinkStep step = createRandomInstance();
 | 
	
		
			
				|  |  | +        ResizeIndexStep step = createRandomInstance();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          Mockito.doAnswer(invocation -> {
 | 
	
		
			
				|  |  |              @SuppressWarnings("unchecked")
 |