diff --git a/modules/data-streams/src/internalClusterTest/java/org/elasticsearch/datastreams/TSDBPassthroughIndexingIT.java b/modules/data-streams/src/internalClusterTest/java/org/elasticsearch/datastreams/TSDBPassthroughIndexingIT.java index 27cfc62822441..fe99d78fa6823 100644 --- a/modules/data-streams/src/internalClusterTest/java/org/elasticsearch/datastreams/TSDBPassthroughIndexingIT.java +++ b/modules/data-streams/src/internalClusterTest/java/org/elasticsearch/datastreams/TSDBPassthroughIndexingIT.java @@ -15,6 +15,7 @@ import org.elasticsearch.action.admin.indices.rollover.RolloverRequest; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; import org.elasticsearch.action.admin.indices.shrink.ResizeType; +import org.elasticsearch.action.admin.indices.shrink.TransportResizeAction; import org.elasticsearch.action.admin.indices.template.put.TransportPutComposableIndexTemplateAction; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.delete.DeleteRequest; @@ -44,6 +45,7 @@ import java.util.List; import java.util.Map; +import static org.elasticsearch.action.admin.indices.ResizeIndexTestUtils.resizeRequest; import static org.elasticsearch.test.MapMatcher.assertMap; import static org.elasticsearch.test.MapMatcher.matchesMap; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; @@ -267,12 +269,10 @@ public void testIndexingGettingAndSearchingShrunkIndex() throws Exception { assertThat(updateSettingsResponse.isAcknowledged(), is(true)); String shrunkenTarget = "k8s-shrunken"; - var shrinkIndexResponse = client().admin() - .indices() - .prepareResizeIndex(sourceIndex, shrunkenTarget) - .setResizeType(ResizeType.SHRINK) - .setSettings(indexSettings(2, 0).build()) - .get(); + final var shrinkIndexResponse = client().execute( + TransportResizeAction.TYPE, + resizeRequest(ResizeType.SHRINK, sourceIndex, shrunkenTarget, indexSettings(2, 0)) + ).actionGet(); assertThat(shrinkIndexResponse.isAcknowledged(), is(true)); assertThat(shrinkIndexResponse.index(), equalTo(shrunkenTarget)); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CloneIndexIT.java b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CloneIndexIT.java index 654a1393aa146..0e25b1071a271 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CloneIndexIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CloneIndexIT.java @@ -25,6 +25,7 @@ import java.util.List; +import static org.elasticsearch.action.admin.indices.ResizeIndexTestUtils.executeResize; import static org.elasticsearch.action.admin.indices.create.ShrinkIndexIT.assertNoResizeSourceIndexSettings; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; @@ -67,11 +68,12 @@ public void testCreateCloneIndex() { final boolean createWithReplicas = randomBoolean(); assertAcked( - indicesAdmin().prepareResizeIndex("source", "target") - .setResizeType(ResizeType.CLONE) - .setSettings( - Settings.builder().put("index.number_of_replicas", createWithReplicas ? 1 : 0).putNull("index.blocks.write").build() - ) + executeResize( + ResizeType.CLONE, + "source", + "target", + Settings.builder().put("index.number_of_replicas", createWithReplicas ? 1 : 0).putNull("index.blocks.write") + ) ); ensureGreen(); assertNoResizeSourceIndexSettings("target"); @@ -125,9 +127,10 @@ public void testResizeChangeIndexMode() { Settings.builder().put("index.mode", "lookup").build() ); for (Settings settings : indexSettings) { - IllegalArgumentException error = expectThrows(IllegalArgumentException.class, () -> { - indicesAdmin().prepareResizeIndex("source", "target").setResizeType(ResizeType.CLONE).setSettings(settings).get(); - }); + IllegalArgumentException error = expectThrows( + IllegalArgumentException.class, + () -> executeResize(ResizeType.CLONE, "source", "target", Settings.builder().put(settings)).actionGet() + ); assertThat(error.getMessage(), equalTo("can't change setting [index.mode] during resize")); } } @@ -137,12 +140,15 @@ public void testResizeChangeSyntheticSource() { .setMapping("@timestamp", "type=date", "host.name", "type=keyword") .get(); updateIndexSettings(Settings.builder().put("index.blocks.write", true), "source"); - IllegalArgumentException error = expectThrows(IllegalArgumentException.class, () -> { - indicesAdmin().prepareResizeIndex("source", "target") - .setResizeType(ResizeType.CLONE) - .setSettings(Settings.builder().put("index.mapping.source.mode", "synthetic").putNull("index.blocks.write").build()) - .get(); - }); + IllegalArgumentException error = expectThrows( + IllegalArgumentException.class, + () -> executeResize( + ResizeType.CLONE, + "source", + "target", + Settings.builder().put("index.mapping.source.mode", "synthetic").putNull("index.blocks.write") + ).actionGet() + ); assertThat(error.getMessage(), containsString("can't change setting [index.mapping.source.mode] during resize")); } @@ -159,26 +165,26 @@ public void testResizeChangeRecoveryUseSyntheticSource() { ) ).setMapping("@timestamp", "type=date", "host.name", "type=keyword").get(); updateIndexSettings(Settings.builder().put("index.blocks.write", true), "source"); - IllegalArgumentException error = expectThrows(IllegalArgumentException.class, () -> { - indicesAdmin().prepareResizeIndex("source", "target") - .setResizeType(ResizeType.CLONE) - .setSettings( - Settings.builder() - .put( - "index.version.created", - IndexVersionUtils.randomVersionBetween( - random(), - IndexVersions.USE_SYNTHETIC_SOURCE_FOR_RECOVERY, - IndexVersion.current() - ) + IllegalArgumentException error = expectThrows( + IllegalArgumentException.class, + () -> executeResize( + ResizeType.CLONE, + "source", + "target", + Settings.builder() + .put( + "index.version.created", + IndexVersionUtils.randomVersionBetween( + random(), + IndexVersions.USE_SYNTHETIC_SOURCE_FOR_RECOVERY, + IndexVersion.current() ) - .put("index.recovery.use_synthetic_source", true) - .put("index.mode", "logsdb") - .putNull("index.blocks.write") - .build() - ) - .get(); - }); + ) + .put("index.recovery.use_synthetic_source", true) + .put("index.mode", "logsdb") + .putNull("index.blocks.write") + ).actionGet() + ); // The index.recovery.use_synthetic_source setting requires either index.mode or index.mapping.source.mode // to be present in the settings. Since these are all unmodifiable settings with a non-deterministic evaluation // order, any of them may trigger a failure first. @@ -196,12 +202,11 @@ public void testResizeChangeIndexSorts() { .setMapping("@timestamp", "type=date", "host.name", "type=keyword") .get(); updateIndexSettings(Settings.builder().put("index.blocks.write", true), "source"); - ValidationException error = expectThrows(ValidationException.class, () -> { - indicesAdmin().prepareResizeIndex("source", "target") - .setResizeType(ResizeType.CLONE) - .setSettings(Settings.builder().putList("index.sort.field", List.of("@timestamp")).build()) - .get(); - }); + ValidationException error = expectThrows( + ValidationException.class, + () -> executeResize(ResizeType.CLONE, "source", "target", Settings.builder().putList("index.sort.field", List.of("@timestamp"))) + .actionGet() + ); assertThat(error.getMessage(), containsString("can't override index sort when resizing an index")); } @@ -216,11 +221,13 @@ public void testCloneLogsdbIndexWithNonDefaultTimestamp() { ensureGreen(); // Clone the index - indicesAdmin().prepareResizeIndex("source", "target") - .setResizeType(ResizeType.CLONE) + executeResize( + ResizeType.CLONE, + "source", + "target", // We need to explicitly set the number of replicas in case the source has 0 replicas and the cluster has only 1 data node - .setSettings(Settings.builder().put("index.number_of_replicas", numberOfReplicas).build()) - .get(); + Settings.builder().put("index.number_of_replicas", numberOfReplicas) + ).actionGet(); // Verify that the target index has the correct @timestamp mapping final var targetMappings = indicesAdmin().prepareGetMappings(TEST_REQUEST_TIMEOUT, "target").get(); @@ -246,11 +253,13 @@ public void testCloneTimeSeriesIndexWithNonDefaultTimestamp() { ensureGreen(); // Clone the index - indicesAdmin().prepareResizeIndex("source", "target") - .setResizeType(ResizeType.CLONE) + executeResize( + ResizeType.CLONE, + "source", + "target", // We need to explicitly set the number of replicas in case the source has 0 replicas and the cluster has only 1 data node - .setSettings(Settings.builder().put("index.number_of_replicas", numberOfReplicas).build()) - .get(); + Settings.builder().put("index.number_of_replicas", numberOfReplicas) + ).actionGet(); // Verify that the target index has the correct @timestamp mapping final var targetMappings = indicesAdmin().prepareGetMappings(TEST_REQUEST_TIMEOUT, "target").get(); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/ShrinkIndexIT.java b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/ShrinkIndexIT.java index 31831f7eb6fe5..75f6a949c976d 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/ShrinkIndexIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/ShrinkIndexIT.java @@ -18,11 +18,13 @@ import org.elasticsearch.action.admin.cluster.reroute.TransportClusterRerouteAction; import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest; import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; +import org.elasticsearch.action.admin.indices.ResizeIndexTestUtils; import org.elasticsearch.action.admin.indices.segments.IndexShardSegments; import org.elasticsearch.action.admin.indices.segments.IndicesSegmentResponse; import org.elasticsearch.action.admin.indices.segments.ShardSegments; import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse; import org.elasticsearch.action.admin.indices.shrink.ResizeType; +import org.elasticsearch.action.admin.indices.shrink.TransportResizeAction; import org.elasticsearch.action.admin.indices.stats.CommonStats; import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse; import org.elasticsearch.action.admin.indices.stats.ShardStats; @@ -58,6 +60,7 @@ import java.util.Map; import java.util.stream.IntStream; +import static org.elasticsearch.action.admin.indices.ResizeIndexTestUtils.executeResize; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.hamcrest.Matchers.containsString; @@ -102,8 +105,7 @@ public void testCreateShrinkIndexToN() { ensureGreen(); // now merge source into a 4 shard index assertAcked( - indicesAdmin().prepareResizeIndex("source", "first_shrink") - .setSettings(indexSettings(shardSplits[1], 0).putNull("index.blocks.write").build()) + executeResize(ResizeType.SHRINK, "source", "first_shrink", indexSettings(shardSplits[1], 0).putNull("index.blocks.write")) ); ensureGreen(); assertHitCount(prepareSearch("first_shrink").setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")), 20); @@ -128,10 +130,12 @@ public void testCreateShrinkIndexToN() { ensureGreen(); // now merge source into a 2 shard index assertAcked( - indicesAdmin().prepareResizeIndex("first_shrink", "second_shrink") - .setSettings( - indexSettings(shardSplits[2], 0).putNull("index.blocks.write").putNull("index.routing.allocation.require._name").build() - ) + executeResize( + ResizeType.SHRINK, + "first_shrink", + "second_shrink", + indexSettings(shardSplits[2], 0).putNull("index.blocks.write").putNull("index.routing.allocation.require._name") + ) ); ensureGreen(); assertHitCount(prepareSearch("second_shrink").setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")), 20); @@ -221,8 +225,7 @@ public void testShrinkIndexPrimaryTerm() throws Exception { final long beforeShrinkPrimaryTerm = IntStream.range(0, numberOfShards).mapToLong(indexMetadata::primaryTerm).max().getAsLong(); // now merge source into target - final Settings shrinkSettings = indexSettings(numberOfTargetShards, 0).build(); - assertAcked(indicesAdmin().prepareResizeIndex("source", "target").setSettings(shrinkSettings).get()); + assertAcked(executeResize(ResizeType.SHRINK, "source", "target", indexSettings(numberOfTargetShards, 0))); ensureGreen(TimeValue.timeValueSeconds(120)); @@ -273,15 +276,17 @@ public void testCreateShrinkIndex() { // now merge source into a single shard index final boolean createWithReplicas = randomBoolean(); assertAcked( - indicesAdmin().prepareResizeIndex("source", "target") - .setSettings( - Settings.builder() - .put("index.number_of_replicas", createWithReplicas ? 1 : 0) - .putNull("index.blocks.write") - .putNull("index.routing.allocation.require._name") - .build() - ) + executeResize( + ResizeType.SHRINK, + "source", + "target", + Settings.builder() + .put("index.number_of_replicas", createWithReplicas ? 1 : 0) + .putNull("index.blocks.write") + .putNull("index.routing.allocation.require._name") + ) ); + ensureGreen(); assertNoResizeSourceIndexSettings("target"); @@ -374,16 +379,17 @@ public void testCreateShrinkIndexFails() throws Exception { ensureGreen(); // now merge source into a single shard index - indicesAdmin().prepareResizeIndex("source", "target") - .setWaitForActiveShards(ActiveShardCount.NONE) - .setSettings( - Settings.builder() - .put("index.routing.allocation.exclude._name", mergeNode) // we manually exclude the merge node to forcefully fuck it up - .put("index.number_of_replicas", 0) - .put("index.allocation.max_retries", 1) - .build() - ) - .get(); + final var resizeRequest = ResizeIndexTestUtils.resizeRequest( + ResizeType.SHRINK, + "source", + "target", + Settings.builder() + .put("index.routing.allocation.exclude._name", mergeNode) // we manually exclude the merge node to forcefully mess it up + .put("index.number_of_replicas", 0) + .put("index.allocation.max_retries", 1) + ); + resizeRequest.setWaitForActiveShards(ActiveShardCount.NONE); + client().execute(TransportResizeAction.TYPE, resizeRequest).actionGet(); clusterAdmin().prepareHealth(TEST_REQUEST_TIMEOUT, "target").setWaitForEvents(Priority.LANGUID).get(); // now we move all shards away from the merge node @@ -464,14 +470,12 @@ public void testCreateShrinkWithIndexSort() throws Exception { // check that index sort cannot be set on the target index IllegalArgumentException exc = expectThrows( IllegalArgumentException.class, - indicesAdmin().prepareResizeIndex("source", "target").setSettings(indexSettings(2, 0).put("index.sort.field", "foo").build()) + executeResize(ResizeType.SHRINK, "source", "target", indexSettings(2, 0).put("index.sort.field", "foo")) ); assertThat(exc.getMessage(), containsString("can't override index sort when resizing an index")); // check that the index sort order of `source` is correctly applied to the `target` - assertAcked( - indicesAdmin().prepareResizeIndex("source", "target").setSettings(indexSettings(2, 0).putNull("index.blocks.write").build()) - ); + assertAcked(executeResize(ResizeType.SHRINK, "source", "target", indexSettings(2, 0).putNull("index.blocks.write"))); ensureGreen(); assertNoResizeSourceIndexSettings("target"); @@ -516,10 +520,7 @@ public void testShrinkCommitsMergeOnIdle() throws Exception { updateClusterSettings(Settings.builder().put(EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), "none")); try { // now merge source into a single shard index - assertAcked( - indicesAdmin().prepareResizeIndex("source", "target") - .setSettings(Settings.builder().put("index.number_of_replicas", 0).build()) - ); + assertAcked(executeResize(ResizeType.SHRINK, "source", "target", Settings.builder().put("index.number_of_replicas", 0))); ensureGreen(); assertNoResizeSourceIndexSettings("target"); @@ -584,13 +585,14 @@ public void testShrinkThenSplitWithFailedNode() throws Exception { ensureGreen(); assertAcked( - indicesAdmin().prepareResizeIndex("original", "shrunk") - .setSettings( - indexSettings(1, 1).putNull( - IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getConcreteSettingForNamespace("_name").getKey() - ).build() + executeResize( + ResizeType.SHRINK, + "original", + "shrunk", + indexSettings(1, 1).putNull( + IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getConcreteSettingForNamespace("_name").getKey() ) - .setResizeType(ResizeType.SHRINK) + ) ); ensureGreen(); @@ -603,13 +605,14 @@ public void testShrinkThenSplitWithFailedNode() throws Exception { logger.info("--> executing split"); assertAcked( - indicesAdmin().prepareResizeIndex("shrunk", "splitagain") - .setSettings( - indexSettings(shardCount, 0).putNull( - IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getConcreteSettingForNamespace("_name").getKey() - ).build() + executeResize( + ResizeType.SPLIT, + "shrunk", + "splitagain", + indexSettings(shardCount, 0).putNull( + IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getConcreteSettingForNamespace("_name").getKey() ) - .setResizeType(ResizeType.SPLIT) + ) ); ensureGreen("splitagain"); assertNoResizeSourceIndexSettings("splitagain"); @@ -627,11 +630,13 @@ public void testShrinkLogsdbIndexWithNonDefaultTimestamp() { ensureGreen(); // Shrink the index - indicesAdmin().prepareResizeIndex("source", "target") - .setResizeType(ResizeType.SHRINK) + executeResize( + ResizeType.SHRINK, + "source", + "target", // We need to explicitly set the number of replicas in case the source has 0 replicas and the cluster has only 1 data node - .setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).build()) - .get(); + indexSettings(1, 0) + ).actionGet(); // Verify that the target index has the correct @timestamp mapping final var targetMappings = indicesAdmin().prepareGetMappings(TEST_REQUEST_TIMEOUT, "target").get(); @@ -657,11 +662,13 @@ public void testShrinkTimeSeriesIndexWithNonDefaultTimestamp() { ensureGreen(); // Shrink the index - indicesAdmin().prepareResizeIndex("source", "target") - .setResizeType(ResizeType.SHRINK) + executeResize( + ResizeType.SHRINK, + "source", + "target", // We need to explicitly set the number of replicas in case the source has 0 replicas and the cluster has only 1 data node - .setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).build()) - .get(); + indexSettings(1, 0) + ).actionGet(); // Verify that the target index has the correct @timestamp mapping final var targetMappings = indicesAdmin().prepareGetMappings(TEST_REQUEST_TIMEOUT, "target").get(); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/SplitIndexIT.java b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/SplitIndexIT.java index 5055430310703..ffa479bfe1ff2 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/SplitIndexIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/SplitIndexIT.java @@ -57,6 +57,7 @@ import java.util.function.BiFunction; import java.util.stream.IntStream; +import static org.elasticsearch.action.admin.indices.ResizeIndexTestUtils.executeResize; import static org.elasticsearch.action.admin.indices.create.ShrinkIndexIT.assertNoResizeSourceIndexSettings; import static org.elasticsearch.index.query.QueryBuilders.nestedQuery; import static org.elasticsearch.index.query.QueryBuilders.termQuery; @@ -184,11 +185,7 @@ private void splitToN(int sourceShards, int firstSplitShards, int secondSplitSha if (sourceShards == 1 && useRoutingPartition == false && randomBoolean()) { // try to set it if we have a source index with 1 shard firstSplitSettingsBuilder.put("index.number_of_routing_shards", secondSplitShards); } - assertAcked( - indicesAdmin().prepareResizeIndex("source", "first_split") - .setResizeType(ResizeType.SPLIT) - .setSettings(firstSplitSettingsBuilder.build()) - ); + assertAcked(executeResize(ResizeType.SPLIT, "source", "first_split", firstSplitSettingsBuilder)); ensureGreen(); assertHitCount(prepareSearch("first_split").setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")), numDocs); assertNoResizeSourceIndexSettings("first_split"); @@ -212,9 +209,12 @@ private void splitToN(int sourceShards, int firstSplitShards, int secondSplitSha ensureGreen(); // now split source into a new index assertAcked( - indicesAdmin().prepareResizeIndex("first_split", "second_split") - .setResizeType(ResizeType.SPLIT) - .setSettings(indexSettings(secondSplitShards, 0).putNull("index.blocks.write").build()) + executeResize( + ResizeType.SPLIT, + "first_split", + "second_split", + indexSettings(secondSplitShards, 0).putNull("index.blocks.write") + ) ); ensureGreen(); assertHitCount(prepareSearch("second_split").setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")), numDocs); @@ -325,8 +325,9 @@ public void testSplitIndexPrimaryTerm() throws Exception { final long beforeSplitPrimaryTerm = IntStream.range(0, numberOfShards).mapToLong(indexMetadata::primaryTerm).max().getAsLong(); // now split source into target - final Settings splitSettings = indexSettings(numberOfTargetShards, 0).putNull("index.blocks.write").build(); - assertAcked(indicesAdmin().prepareResizeIndex("source", "target").setResizeType(ResizeType.SPLIT).setSettings(splitSettings).get()); + assertAcked( + executeResize(ResizeType.SPLIT, "source", "target", indexSettings(numberOfTargetShards, 0).putNull("index.blocks.write")) + ); ensureGreen(TimeValue.timeValueSeconds(120)); // needs more than the default to relocate many shards @@ -372,9 +373,12 @@ public void testCreateSplitIndex() { final boolean createWithReplicas = randomBoolean(); assertAcked( - indicesAdmin().prepareResizeIndex("source", "target") - .setResizeType(ResizeType.SPLIT) - .setSettings(indexSettings(2, createWithReplicas ? 1 : 0).putNull("index.blocks.write").build()) + executeResize( + ResizeType.SPLIT, + "source", + "target", + indexSettings(2, createWithReplicas ? 1 : 0).putNull("index.blocks.write") + ) ); ensureGreen(); assertNoResizeSourceIndexSettings("target"); @@ -470,18 +474,12 @@ public void testCreateSplitWithIndexSort() throws Exception { // check that index sort cannot be set on the target index IllegalArgumentException exc = expectThrows( IllegalArgumentException.class, - indicesAdmin().prepareResizeIndex("source", "target") - .setResizeType(ResizeType.SPLIT) - .setSettings(indexSettings(4, 0).put("index.sort.field", "foo").build()) + executeResize(ResizeType.SPLIT, "source", "target", indexSettings(4, 0).put("index.sort.field", "foo")) ); assertThat(exc.getMessage(), containsString("can't override index sort when resizing an index")); // check that the index sort order of `source` is correctly applied to the `target` - assertAcked( - indicesAdmin().prepareResizeIndex("source", "target") - .setResizeType(ResizeType.SPLIT) - .setSettings(indexSettings(4, 0).putNull("index.blocks.write").build()) - ); + assertAcked(executeResize(ResizeType.SPLIT, "source", "target", indexSettings(4, 0).putNull("index.blocks.write"))); ensureGreen(); flushAndRefresh(); GetSettingsResponse settingsResponse = indicesAdmin().prepareGetSettings(TEST_REQUEST_TIMEOUT, "target").get(); @@ -510,11 +508,13 @@ public void testSplitLogsdbIndexWithNonDefaultTimestamp() { ensureGreen(); // Split the index - indicesAdmin().prepareResizeIndex("source", "target") - .setResizeType(ResizeType.SPLIT) + executeResize( + ResizeType.SPLIT, + "source", + "target", // We need to explicitly set the number of replicas in case the source has 0 replicas and the cluster has only 1 data node - .setSettings(Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", numberOfReplicas).build()) - .get(); + indexSettings(2, numberOfReplicas) + ).actionGet(); // Verify that the target index has the correct @timestamp mapping final var targetMappings = indicesAdmin().prepareGetMappings(TEST_REQUEST_TIMEOUT, "target").get(); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderIT.java b/server/src/internalClusterTest/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderIT.java index 8918d995646b8..560e01187febc 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderIT.java @@ -12,9 +12,10 @@ import org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteUtils; import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse; +import org.elasticsearch.action.admin.indices.ResizeIndexTestUtils; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; -import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; import org.elasticsearch.action.admin.indices.shrink.ResizeType; +import org.elasticsearch.action.admin.indices.shrink.TransportResizeAction; import org.elasticsearch.action.admin.indices.stats.ShardStats; import org.elasticsearch.action.support.ActionTestUtils; import org.elasticsearch.action.support.ActiveShardCount; @@ -23,7 +24,6 @@ import org.elasticsearch.cluster.ClusterInfoServiceUtils; import org.elasticsearch.cluster.DiskUsageIntegTestCase; import org.elasticsearch.cluster.InternalClusterInfoService; -import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.routing.IndexRoutingTable; import org.elasticsearch.cluster.routing.IndexShardRoutingTable; import org.elasticsearch.cluster.routing.ShardRouting; @@ -143,18 +143,16 @@ public void testAllocateCloneIgnoresLowWatermark() throws Exception { refreshDiskUsage(); final var targetIndexName = "target-" + randomIdentifier(); - final var resizeRequest = new ResizeRequest(targetIndexName, sourceIndexName); - resizeRequest.setResizeType(ResizeType.CLONE); - resizeRequest.masterNodeTimeout(TEST_REQUEST_TIMEOUT); - resizeRequest.ackTimeout(TEST_REQUEST_TIMEOUT); + final var resizeRequest = ResizeIndexTestUtils.resizeRequest( + ResizeType.CLONE, + sourceIndexName, + targetIndexName, + indexSettings(1, 0) + ); resizeRequest.setWaitForActiveShards(ActiveShardCount.ALL); - resizeRequest.getTargetIndexRequest() - .settings( - Settings.builder().put(resizeRequest.getTargetIndexRequest().settings()).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - ); safeAwait( - SubscribableListener.newForked(l -> indicesAdmin().resizeIndex(resizeRequest, l)) + SubscribableListener.newForked(l -> client().execute(TransportResizeAction.TYPE, resizeRequest, l)) .andThenAccept( createIndexResponse -> assertThat( true, diff --git a/server/src/internalClusterTest/java/org/elasticsearch/cluster/routing/allocation/shards/ShardsAvailabilityHealthIndicatorServiceIT.java b/server/src/internalClusterTest/java/org/elasticsearch/cluster/routing/allocation/shards/ShardsAvailabilityHealthIndicatorServiceIT.java index e3586f330a32e..dda581d15a9e0 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/cluster/routing/allocation/shards/ShardsAvailabilityHealthIndicatorServiceIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/cluster/routing/allocation/shards/ShardsAvailabilityHealthIndicatorServiceIT.java @@ -9,6 +9,7 @@ package org.elasticsearch.cluster.routing.allocation.shards; +import org.elasticsearch.action.admin.indices.ResizeIndexTestUtils; import org.elasticsearch.action.admin.indices.shrink.ResizeType; import org.elasticsearch.cluster.ClusterChangedEvent; import org.elasticsearch.cluster.ClusterStateListener; @@ -109,7 +110,7 @@ public void testIsGreenDuringIndexClone() { updateIndexSettings(Settings.builder().put("index.blocks.write", true), sourceIndex); assertHealthDuring(equalTo(GREEN), () -> { - indicesAdmin().prepareResizeIndex(sourceIndex, targetIndex).setResizeType(ResizeType.CLONE).get(); + ResizeIndexTestUtils.executeResize(ResizeType.CLONE, sourceIndex, targetIndex, Settings.builder()).actionGet(); ensureGreen(targetIndex); }); } diff --git a/server/src/internalClusterTest/java/org/elasticsearch/index/LookupIndexModeIT.java b/server/src/internalClusterTest/java/org/elasticsearch/index/LookupIndexModeIT.java index 583ab18d4f8b3..aec61b877766e 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/index/LookupIndexModeIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/index/LookupIndexModeIT.java @@ -9,11 +9,12 @@ package org.elasticsearch.index; +import org.elasticsearch.action.admin.indices.ResizeIndexTestUtils; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.TransportCreateIndexAction; -import org.elasticsearch.action.admin.indices.shrink.ResizeAction; import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; import org.elasticsearch.action.admin.indices.shrink.ResizeType; +import org.elasticsearch.action.admin.indices.shrink.TransportResizeAction; import org.elasticsearch.action.fieldcaps.FieldCapabilitiesIndexResponse; import org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest; import org.elasticsearch.action.search.SearchResponse; @@ -140,9 +141,7 @@ public void testResizeLookupIndex() { assertAcked(client().admin().indices().execute(TransportCreateIndexAction.TYPE, createIndexRequest)); client().admin().indices().prepareAddBlock(IndexMetadata.APIBlock.WRITE, "lookup-1").get(); - ResizeRequest clone = new ResizeRequest("lookup-2", "lookup-1"); - clone.setResizeType(ResizeType.CLONE); - assertAcked(client().admin().indices().execute(ResizeAction.INSTANCE, clone).actionGet()); + assertAcked(ResizeIndexTestUtils.executeResize(ResizeType.CLONE, "lookup-1", "lookup-2", Settings.builder())); Settings settings = client().admin() .indices() .prepareGetSettings(TEST_REQUEST_TIMEOUT, "lookup-2") @@ -152,12 +151,15 @@ public void testResizeLookupIndex() { assertThat(settings.get("index.mode"), equalTo("lookup")); assertThat(settings.get("index.number_of_shards"), equalTo("1")); - ResizeRequest split = new ResizeRequest("lookup-3", "lookup-1"); - split.setResizeType(ResizeType.SPLIT); - split.getTargetIndexRequest().settings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 3)); + ResizeRequest split = ResizeIndexTestUtils.resizeRequest( + ResizeType.SPLIT, + "lookup-1", + "lookup-3", + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 3) + ); IllegalArgumentException error = expectThrows( IllegalArgumentException.class, - () -> client().admin().indices().execute(ResizeAction.INSTANCE, split).actionGet() + () -> client().execute(TransportResizeAction.TYPE, split).actionGet() ); assertThat( error.getMessage(), @@ -186,27 +188,29 @@ public void testResizeRegularIndexToLookup() { .setSettings(Settings.builder().put("index.number_of_replicas", 0)) .get(); - ResizeRequest clone = new ResizeRequest("lookup-3", "regular-1"); - clone.setResizeType(ResizeType.CLONE); - clone.getTargetIndexRequest().settings(Settings.builder().put("index.mode", "lookup")); + ResizeRequest clone = ResizeIndexTestUtils.resizeRequest( + ResizeType.CLONE, + "regular-1", + "lookup-3", + Settings.builder().put("index.mode", "lookup") + ); IllegalArgumentException error = expectThrows( IllegalArgumentException.class, - () -> client().admin().indices().execute(ResizeAction.INSTANCE, clone).actionGet() + () -> client().execute(TransportResizeAction.TYPE, clone).actionGet() ); assertThat( error.getMessage(), equalTo("index with [lookup] mode must have [index.number_of_shards] set to 1 or unset; provided 2") ); - ResizeRequest shrink = new ResizeRequest("lookup-4", "regular-1"); - shrink.setResizeType(ResizeType.SHRINK); - shrink.getTargetIndexRequest() - .settings(Settings.builder().put("index.mode", "lookup").put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)); - - error = expectThrows( - IllegalArgumentException.class, - () -> client().admin().indices().execute(ResizeAction.INSTANCE, shrink).actionGet() + ResizeRequest shrink = ResizeIndexTestUtils.resizeRequest( + ResizeType.SHRINK, + "regular-1", + "lookup-4", + Settings.builder().put("index.mode", "lookup").put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) ); + + error = expectThrows(IllegalArgumentException.class, () -> client().execute(TransportResizeAction.TYPE, shrink).actionGet()); assertThat(error.getMessage(), equalTo("can't change setting [index.mode] during resize")); } diff --git a/server/src/internalClusterTest/java/org/elasticsearch/routing/PartitionedRoutingIT.java b/server/src/internalClusterTest/java/org/elasticsearch/routing/PartitionedRoutingIT.java index 68bc6656cec7f..adda57d8d290d 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/routing/PartitionedRoutingIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/routing/PartitionedRoutingIT.java @@ -10,6 +10,7 @@ package org.elasticsearch.routing; import org.apache.lucene.util.Constants; +import org.elasticsearch.action.admin.indices.shrink.ResizeType; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.settings.IndexScopedSettings; @@ -23,6 +24,7 @@ import java.util.Map; import java.util.Set; +import static org.elasticsearch.action.admin.indices.ResizeIndexTestUtils.executeResize; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse; import static org.hamcrest.CoreMatchers.containsString; @@ -117,9 +119,12 @@ public void testShrinking() throws Exception { index = "index_" + currentShards; logger.info("--> shrinking index [" + previousIndex + "] to [" + index + "]"); - indicesAdmin().prepareResizeIndex(previousIndex, index) - .setSettings(indexSettings(currentShards, numberOfReplicas()).putNull("index.routing.allocation.require._name").build()) - .get(); + executeResize( + ResizeType.SHRINK, + previousIndex, + index, + indexSettings(currentShards, numberOfReplicas()).putNull("index.routing.allocation.require._name") + ).actionGet(); ensureGreen(); } } diff --git a/server/src/internalClusterTest/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreIT.java b/server/src/internalClusterTest/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreIT.java index 46d665fde38e8..42ab675008d94 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreIT.java @@ -18,6 +18,8 @@ import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotStats; import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotStatus; import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse; +import org.elasticsearch.action.admin.indices.ResizeIndexTestUtils; +import org.elasticsearch.action.admin.indices.shrink.ResizeType; import org.elasticsearch.action.admin.indices.stats.ShardStats; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.support.ActionTestUtils; @@ -653,7 +655,7 @@ public void testRestoreShrinkIndex() throws Exception { logger.info("--> shrink the index"); updateIndexSettings(Settings.builder().put("index.blocks.write", true), sourceIdx); - assertAcked(indicesAdmin().prepareResizeIndex(sourceIdx, shrunkIdx).get()); + assertAcked(ResizeIndexTestUtils.executeResize(ResizeType.SHRINK, sourceIdx, shrunkIdx, Settings.builder())); logger.info("--> snapshot the shrunk index"); createSnapshot(repo, snapshot, Collections.singletonList(shrunkIdx)); diff --git a/server/src/main/java/org/elasticsearch/action/ActionModule.java b/server/src/main/java/org/elasticsearch/action/ActionModule.java index 8074fee2826f7..7ac39ab974e3a 100644 --- a/server/src/main/java/org/elasticsearch/action/ActionModule.java +++ b/server/src/main/java/org/elasticsearch/action/ActionModule.java @@ -151,7 +151,6 @@ import org.elasticsearch.action.admin.indices.settings.get.TransportGetSettingsAction; import org.elasticsearch.action.admin.indices.settings.put.TransportUpdateSettingsAction; import org.elasticsearch.action.admin.indices.shards.TransportIndicesShardStoresAction; -import org.elasticsearch.action.admin.indices.shrink.ResizeAction; import org.elasticsearch.action.admin.indices.shrink.TransportResizeAction; import org.elasticsearch.action.admin.indices.stats.FieldUsageStatsAction; import org.elasticsearch.action.admin.indices.stats.IndicesStatsAction; @@ -705,7 +704,7 @@ public void reg actions.register(IndicesSegmentsAction.INSTANCE, TransportIndicesSegmentsAction.class); actions.register(TransportIndicesShardStoresAction.TYPE, TransportIndicesShardStoresAction.class); actions.register(TransportCreateIndexAction.TYPE, TransportCreateIndexAction.class); - actions.register(ResizeAction.INSTANCE, TransportResizeAction.class); + actions.register(TransportResizeAction.TYPE, TransportResizeAction.class); actions.register(RolloverAction.INSTANCE, TransportRolloverAction.class); actions.register(LazyRolloverAction.INSTANCE, LazyRolloverAction.TransportLazyRolloverAction.class); actions.register(TransportDeleteIndexAction.TYPE, TransportDeleteIndexAction.class); diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/shrink/ResizeAction.java b/server/src/main/java/org/elasticsearch/action/admin/indices/shrink/ResizeAction.java deleted file mode 100644 index 3c8ee569670dc..0000000000000 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/shrink/ResizeAction.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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.action.admin.indices.shrink; - -import org.elasticsearch.action.ActionType; -import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; - -public class ResizeAction extends ActionType { - - public static final ResizeAction INSTANCE = new ResizeAction(); - public static final String NAME = "indices:admin/resize"; - - private ResizeAction() { - super(NAME); - } - -} diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/shrink/ResizeRequest.java b/server/src/main/java/org/elasticsearch/action/admin/indices/shrink/ResizeRequest.java index 21013525fcc8c..8430ad210eb3e 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/shrink/ResizeRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/shrink/ResizeRequest.java @@ -10,7 +10,6 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.IndicesRequest; -import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.support.ActiveShardCount; @@ -19,11 +18,11 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -34,10 +33,11 @@ /** * Request class to shrink an index into a single shard */ -public class ResizeRequest extends AcknowledgedRequest implements IndicesRequest, ToXContentObject { +public class ResizeRequest extends AcknowledgedRequest implements IndicesRequest { public static final ObjectParser PARSER = new ObjectParser<>("resize_request"); - private static final ParseField MAX_PRIMARY_SHARD_SIZE = new ParseField("max_primary_shard_size"); + public static final ParseField MAX_PRIMARY_SHARD_SIZE = new ParseField("max_primary_shard_size"); + static { PARSER.declareField( (parser, request, context) -> request.getTargetIndexRequest().settings(parser.map()), @@ -58,8 +58,8 @@ public class ResizeRequest extends AcknowledgedRequest implements } private CreateIndexRequest targetIndexRequest; - private String sourceIndex; - private ResizeType type = ResizeType.SHRINK; + private final String sourceIndex; + private final ResizeType type; private Boolean copySettings = true; private ByteSizeValue maxPrimaryShardSize; @@ -74,14 +74,11 @@ public ResizeRequest(StreamInput in) throws IOException { } } - ResizeRequest() { - super(TRAPPY_IMPLICIT_DEFAULT_MASTER_NODE_TIMEOUT, DEFAULT_ACK_TIMEOUT); - } - - public ResizeRequest(String targetIndex, String sourceIndex) { - super(TRAPPY_IMPLICIT_DEFAULT_MASTER_NODE_TIMEOUT, DEFAULT_ACK_TIMEOUT); + public ResizeRequest(TimeValue masterNodeTimeout, TimeValue ackTimeout, ResizeType resizeType, String sourceIndex, String targetIndex) { + super(masterNodeTimeout, ackTimeout); this.targetIndexRequest = new CreateIndexRequest(targetIndex); this.sourceIndex = sourceIndex; + this.type = resizeType; } @Override @@ -106,10 +103,6 @@ public ActionRequestValidationException validate() { return validationException; } - public void setSourceIndex(String index) { - this.sourceIndex = index; - } - @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); @@ -175,13 +168,6 @@ public void setWaitForActiveShards(final int waitForActiveShards) { setWaitForActiveShards(ActiveShardCount.from(waitForActiveShards)); } - /** - * The type of the resize operation - */ - public void setResizeType(ResizeType type) { - this.type = Objects.requireNonNull(type); - } - /** * Returns the type of the resize operation */ @@ -219,30 +205,6 @@ public ByteSizeValue getMaxPrimaryShardSize() { return maxPrimaryShardSize; } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - { - builder.startObject(CreateIndexRequest.SETTINGS.getPreferredName()); - { - targetIndexRequest.settings().toXContent(builder, params); - } - builder.endObject(); - builder.startObject(CreateIndexRequest.ALIASES.getPreferredName()); - { - for (Alias alias : targetIndexRequest.aliases()) { - alias.toXContent(builder, params); - } - } - builder.endObject(); - if (maxPrimaryShardSize != null) { - builder.field(MAX_PRIMARY_SHARD_SIZE.getPreferredName(), maxPrimaryShardSize); - } - } - builder.endObject(); - return builder; - } - public void fromXContent(XContentParser parser) throws IOException { PARSER.parse(parser, this, null); } @@ -263,4 +225,8 @@ public boolean equals(Object obj) { public int hashCode() { return Objects.hash(targetIndexRequest, sourceIndex, type, copySettings, maxPrimaryShardSize); } + + public void setTargetIndexSettings(Settings.Builder settings) { + targetIndexRequest.settings(settings); + } } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/shrink/ResizeRequestBuilder.java b/server/src/main/java/org/elasticsearch/action/admin/indices/shrink/ResizeRequestBuilder.java deleted file mode 100644 index 8d14ca18c7c20..0000000000000 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/shrink/ResizeRequestBuilder.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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.action.admin.indices.shrink; - -import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; -import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; -import org.elasticsearch.action.support.ActiveShardCount; -import org.elasticsearch.action.support.master.AcknowledgedRequestBuilder; -import org.elasticsearch.client.internal.ElasticsearchClient; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.unit.ByteSizeValue; - -public class ResizeRequestBuilder extends AcknowledgedRequestBuilder { - public ResizeRequestBuilder(ElasticsearchClient client) { - super(client, ResizeAction.INSTANCE, new ResizeRequest()); - } - - public ResizeRequestBuilder setTargetIndex(CreateIndexRequest request) { - this.request.setTargetIndex(request); - return this; - } - - public ResizeRequestBuilder setSourceIndex(String index) { - this.request.setSourceIndex(index); - return this; - } - - public ResizeRequestBuilder setSettings(Settings settings) { - this.request.getTargetIndexRequest().settings(settings); - return this; - } - - /** - * Sets the number of shard copies that should be active for creation of the - * new shrunken index to return. Defaults to {@link ActiveShardCount#DEFAULT}, which will - * wait for one shard copy (the primary) to become active. Set this value to - * {@link ActiveShardCount#ALL} to wait for all shards (primary and all replicas) to be active - * before returning. Otherwise, use {@link ActiveShardCount#from(int)} to set this value to any - * non-negative integer, up to the number of copies per shard (number of replicas + 1), - * to wait for the desired amount of shard copies to become active before returning. - * Index creation will only wait up until the timeout value for the number of shard copies - * to be active before returning. Check {@link CreateIndexResponse#isShardsAcknowledged()} to - * determine if the requisite shard copies were all started before returning or timing out. - * - * @param waitForActiveShards number of active shard copies to wait on - */ - public ResizeRequestBuilder setWaitForActiveShards(ActiveShardCount waitForActiveShards) { - this.request.setWaitForActiveShards(waitForActiveShards); - return this; - } - - /** - * A shortcut for {@link #setWaitForActiveShards(ActiveShardCount)} where the numerical - * shard count is passed in, instead of having to first call {@link ActiveShardCount#from(int)} - * to get the ActiveShardCount. - */ - public ResizeRequestBuilder setWaitForActiveShards(final int waitForActiveShards) { - return setWaitForActiveShards(ActiveShardCount.from(waitForActiveShards)); - } - - public ResizeRequestBuilder setResizeType(ResizeType type) { - this.request.setResizeType(type); - return this; - } - - /** - * Sets the max primary shard size of the target index. - */ - public ResizeRequestBuilder setMaxPrimaryShardSize(ByteSizeValue maxPrimaryShardSize) { - this.request.setMaxPrimaryShardSize(maxPrimaryShardSize); - return this; - } -} diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/shrink/TransportResizeAction.java b/server/src/main/java/org/elasticsearch/action/admin/indices/shrink/TransportResizeAction.java index 2f8765fc20b8c..4a781a5a761fe 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/shrink/TransportResizeAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/shrink/TransportResizeAction.java @@ -10,6 +10,7 @@ package org.elasticsearch.action.admin.indices.shrink; import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.ActionType; import org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; @@ -48,6 +49,8 @@ */ public class TransportResizeAction extends TransportMasterNodeAction { + public static final ActionType TYPE = new ActionType<>("indices:admin/resize"); + private final MetadataCreateIndexService createIndexService; private final ProjectResolver projectResolver; private final Client client; @@ -62,7 +65,7 @@ public TransportResizeAction( ProjectResolver projectResolver, Client client ) { - this(ResizeAction.NAME, transportService, clusterService, threadPool, createIndexService, actionFilters, projectResolver, client); + this(TYPE.name(), transportService, clusterService, threadPool, createIndexService, actionFilters, projectResolver, client); } protected TransportResizeAction( diff --git a/server/src/main/java/org/elasticsearch/client/internal/IndicesAdminClient.java b/server/src/main/java/org/elasticsearch/client/internal/IndicesAdminClient.java index 98d621955926a..b7f265b81f442 100644 --- a/server/src/main/java/org/elasticsearch/client/internal/IndicesAdminClient.java +++ b/server/src/main/java/org/elasticsearch/client/internal/IndicesAdminClient.java @@ -94,9 +94,6 @@ import org.elasticsearch.action.admin.indices.settings.put.TransportUpdateSettingsAction; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequestBuilder; -import org.elasticsearch.action.admin.indices.shrink.ResizeAction; -import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; -import org.elasticsearch.action.admin.indices.shrink.ResizeRequestBuilder; import org.elasticsearch.action.admin.indices.stats.IndicesStatsAction; import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest; import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequestBuilder; @@ -466,14 +463,6 @@ public GetSettingsRequestBuilder prepareGetSettings(TimeValue masterTimeout, Str return new GetSettingsRequestBuilder(this, masterTimeout, indices); } - public ResizeRequestBuilder prepareResizeIndex(String sourceIndex, String targetIndex) { - return new ResizeRequestBuilder(this).setSourceIndex(sourceIndex).setTargetIndex(new CreateIndexRequest(targetIndex)); - } - - public void resizeIndex(ResizeRequest request, ActionListener listener) { - execute(ResizeAction.INSTANCE, request, listener); - } - public RolloverRequestBuilder prepareRolloverIndex(String alias) { return new RolloverRequestBuilder(this).setRolloverTarget(alias); } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestResizeHandler.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestResizeHandler.java index 5fd4ec83c1a18..c6b8a01f78bf9 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestResizeHandler.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestResizeHandler.java @@ -11,6 +11,7 @@ import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; import org.elasticsearch.action.admin.indices.shrink.ResizeType; +import org.elasticsearch.action.admin.indices.shrink.TransportResizeAction; import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.rest.BaseRestHandler; @@ -36,13 +37,16 @@ public abstract class RestResizeHandler extends BaseRestHandler { @Override public final RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { - final ResizeRequest resizeRequest = new ResizeRequest(request.param("target"), request.param("index")); - resizeRequest.setResizeType(getResizeType()); + final ResizeRequest resizeRequest = new ResizeRequest( + getMasterNodeTimeout(request), + getAckTimeout(request), + getResizeType(), + request.param("index"), + request.param("target") + ); request.applyContentParser(resizeRequest::fromXContent); - resizeRequest.ackTimeout(getAckTimeout(request)); - resizeRequest.masterNodeTimeout(getMasterNodeTimeout(request)); resizeRequest.setWaitForActiveShards(ActiveShardCount.parseString(request.param("wait_for_active_shards"))); - return channel -> client.admin().indices().resizeIndex(resizeRequest, new RestToXContentListener<>(channel)); + return channel -> client.execute(TransportResizeAction.TYPE, resizeRequest, new RestToXContentListener<>(channel)); } // no @ServerlessScope on purpose, not available diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/ResizeRequestTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/ResizeRequestTests.java index e1316b40e7ce3..efd797c769f04 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/ResizeRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/ResizeRequestTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.index.RandomCreateIndexGenerator; import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.test.hamcrest.ElasticsearchAssertions; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; @@ -34,6 +35,7 @@ import java.util.function.Supplier; import static org.elasticsearch.action.admin.indices.create.CreateIndexRequestTests.assertAliasesEqual; +import static org.elasticsearch.action.admin.indices.shrink.ResizeRequest.MAX_PRIMARY_SHARD_SIZE; import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS; import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS; import static org.hamcrest.Matchers.containsString; @@ -53,7 +55,13 @@ public void testCopySettingsValidation() { private void runTestCopySettingsValidation(final Boolean copySettings, final Consumer> consumer) { consumer.accept(() -> { - final ResizeRequest request = new ResizeRequest(); + final ResizeRequest request = new ResizeRequest( + TEST_REQUEST_TIMEOUT, + TEST_REQUEST_TIMEOUT, + randomFrom(ResizeType.values()), + randomIdentifier(), + randomIdentifier() + ); request.setCopySettings(copySettings); return request; }); @@ -61,19 +69,25 @@ private void runTestCopySettingsValidation(final Boolean copySettings, final Con public void testToXContent() throws IOException { { - ResizeRequest request = new ResizeRequest("target", "source"); - String actualRequestBody = Strings.toString(request); + ResizeRequest request = new ResizeRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, ResizeType.SPLIT, "source", "target"); + String actualRequestBody = Strings.toString(resizeRequestToXContent(request)); assertEquals("{\"settings\":{},\"aliases\":{}}", actualRequestBody); } { - ResizeRequest request = new ResizeRequest("target", "source"); + ResizeRequest request = new ResizeRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, ResizeType.SPLIT, "source", "target"); request.setMaxPrimaryShardSize(ByteSizeValue.of(100, ByteSizeUnit.MB)); - String actualRequestBody = Strings.toString(request); + String actualRequestBody = Strings.toString(resizeRequestToXContent(request)); assertEquals(""" {"settings":{},"aliases":{},"max_primary_shard_size":"100mb"}""", actualRequestBody); } { - ResizeRequest request = new ResizeRequest(); + ResizeRequest request = new ResizeRequest( + TEST_REQUEST_TIMEOUT, + TEST_REQUEST_TIMEOUT, + randomFrom(ResizeType.values()), + randomIdentifier(), + randomIdentifier() + ); CreateIndexRequest target = new CreateIndexRequest("target"); Alias alias = new Alias("test_alias"); alias.routing("1"); @@ -85,7 +99,7 @@ public void testToXContent() throws IOException { settings.put(SETTING_NUMBER_OF_SHARDS, 10); target.settings(settings); request.setTargetIndex(target); - String actualRequestBody = Strings.toString(request); + String actualRequestBody = Strings.toString(resizeRequestToXContent(request)); String expectedRequestBody = """ { "settings": { @@ -114,11 +128,19 @@ public void testToAndFromXContent() throws IOException { boolean humanReadable = randomBoolean(); final XContentType xContentType = randomFrom(XContentType.values()); - BytesReference originalBytes = toShuffledXContent(resizeRequest, xContentType, EMPTY_PARAMS, humanReadable); + BytesReference originalBytes = toShuffledXContent( + resizeRequestToXContent(resizeRequest), + xContentType, + EMPTY_PARAMS, + humanReadable + ); ResizeRequest parsedResizeRequest = new ResizeRequest( - randomValueOtherThan(resizeRequest.getTargetIndexRequest().index(), () -> randomAlphaOfLength(5)), - randomValueOtherThan(resizeRequest.getSourceIndex(), () -> randomAlphaOfLength(5)) + TEST_REQUEST_TIMEOUT, + TEST_REQUEST_TIMEOUT, + randomFrom(ResizeType.values()), + randomValueOtherThan(resizeRequest.getSourceIndex(), () -> randomAlphaOfLength(5)), + randomValueOtherThan(resizeRequest.getTargetIndexRequest().index(), () -> randomAlphaOfLength(5)) ); try (XContentParser xParser = createParser(xContentType.xContent(), originalBytes)) { parsedResizeRequest.fromXContent(xParser); @@ -135,7 +157,12 @@ public void testToAndFromXContent() throws IOException { assertEquals(resizeRequest.getTargetIndexRequest().settings(), parsedResizeRequest.getTargetIndexRequest().settings()); assertEquals(resizeRequest.getMaxPrimaryShardSize(), parsedResizeRequest.getMaxPrimaryShardSize()); - BytesReference finalBytes = toShuffledXContent(parsedResizeRequest, xContentType, EMPTY_PARAMS, humanReadable); + BytesReference finalBytes = toShuffledXContent( + resizeRequestToXContent(parsedResizeRequest), + xContentType, + EMPTY_PARAMS, + humanReadable + ); ElasticsearchAssertions.assertToXContentEquivalent(originalBytes, finalBytes, xContentType); } @@ -163,7 +190,13 @@ protected Writeable.Reader instanceReader() { @Override protected ResizeRequest createTestInstance() { - ResizeRequest resizeRequest = new ResizeRequest(randomAlphaOfLengthBetween(3, 10), randomAlphaOfLengthBetween(3, 10)); + ResizeRequest resizeRequest = new ResizeRequest( + TEST_REQUEST_TIMEOUT, + TEST_REQUEST_TIMEOUT, + randomFrom(ResizeType.values()), + randomAlphaOfLengthBetween(3, 10), + randomAlphaOfLengthBetween(3, 10) + ); if (randomBoolean()) { resizeRequest.setTargetIndex(RandomCreateIndexGenerator.randomCreateIndexRequest()); } @@ -177,4 +210,29 @@ protected ResizeRequest createTestInstance() { protected ResizeRequest mutateInstance(ResizeRequest instance) { return null;// TODO implement https://github.com/elastic/elasticsearch/issues/25929 } + + private static ToXContentObject resizeRequestToXContent(ResizeRequest resizeRequest) { + return (builder, params) -> { + builder.startObject(); + { + builder.startObject(CreateIndexRequest.SETTINGS.getPreferredName()); + { + resizeRequest.getTargetIndexRequest().settings().toXContent(builder, params); + } + builder.endObject(); + builder.startObject(CreateIndexRequest.ALIASES.getPreferredName()); + { + for (Alias alias : resizeRequest.getTargetIndexRequest().aliases()) { + alias.toXContent(builder, params); + } + } + builder.endObject(); + if (resizeRequest.getMaxPrimaryShardSize() != null) { + builder.field(MAX_PRIMARY_SHARD_SIZE.getPreferredName(), resizeRequest.getMaxPrimaryShardSize()); + } + } + builder.endObject(); + return builder; + }; + } } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/TransportResizeActionTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/TransportResizeActionTests.java index 66fb8a58bba1c..dac353919d0f9 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/TransportResizeActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/TransportResizeActionTests.java @@ -46,6 +46,7 @@ import java.util.Collections; import java.util.Set; +import static org.elasticsearch.action.admin.indices.ResizeIndexTestUtils.resizeRequest; import static org.hamcrest.Matchers.equalTo; public class TransportResizeActionTests extends ESTestCase { @@ -94,7 +95,7 @@ public void testErrorCondition() { expectThrows( IllegalStateException.class, () -> TransportResizeAction.prepareCreateIndexRequest( - new ResizeRequest("target", "source"), + resizeRequest(ResizeType.SHRINK, "source", "target", Settings.builder()), projectId, state, "target", @@ -106,28 +107,28 @@ public void testErrorCondition() { ).getMessage().startsWith("Can't merge index with more than [2147483519] docs - too many documents in shards ") ); - assertTrue(expectThrows(IllegalStateException.class, () -> { - ResizeRequest req = new ResizeRequest("target", "source"); - req.getTargetIndexRequest().settings(Settings.builder().put("index.number_of_shards", 4)); - TransportResizeAction.prepareCreateIndexRequest( - req, - projectId, - createClusterState("source", 8, 1, Settings.builder().put("index.blocks.write", true).build()).metadata() - .getProject(projectId) - .index("source"), - "target", - new ResizeNumberOfShardsCalculator.ShrinkShardsCalculator( - new StoreStats(between(1, 100), between(0, 100), between(1, 100)), - (i) -> i == 2 || i == 3 ? new DocsStats(Integer.MAX_VALUE / 2, between(1, 1000), between(1, 10000)) : null + assertTrue( + expectThrows( + IllegalStateException.class, + () -> TransportResizeAction.prepareCreateIndexRequest( + resizeRequest(ResizeType.SHRINK, "source", "target", Settings.builder().put("index.number_of_shards", 4)), + projectId, + createClusterState("source", 8, 1, Settings.builder().put("index.blocks.write", true).build()).metadata() + .getProject(projectId) + .index("source"), + "target", + new ResizeNumberOfShardsCalculator.ShrinkShardsCalculator( + new StoreStats(between(1, 100), between(0, 100), between(1, 100)), + (i) -> i == 2 || i == 3 ? new DocsStats(Integer.MAX_VALUE / 2, between(1, 1000), between(1, 10000)) : null + ) ) - ); - }).getMessage().startsWith("Can't merge index with more than [2147483519] docs - too many documents in shards ")); - - IllegalArgumentException softDeletesError = expectThrows(IllegalArgumentException.class, () -> { - ResizeRequest req = new ResizeRequest("target", "source"); - req.getTargetIndexRequest().settings(Settings.builder().put("index.soft_deletes.enabled", false)); - TransportResizeAction.prepareCreateIndexRequest( - req, + ).getMessage().startsWith("Can't merge index with more than [2147483519] docs - too many documents in shards ") + ); + + IllegalArgumentException softDeletesError = expectThrows( + IllegalArgumentException.class, + () -> TransportResizeAction.prepareCreateIndexRequest( + resizeRequest(ResizeType.SHRINK, "source", "target", Settings.builder().put("index.soft_deletes.enabled", false)), projectId, createClusterState( "source", @@ -140,8 +141,8 @@ public void testErrorCondition() { new StoreStats(between(1, 100), between(0, 100), between(1, 100)), (i) -> new DocsStats(between(10, 1000), between(1, 10), between(1, 10000)) ) - ); - }); + ) + ); assertThat(softDeletesError.getMessage(), equalTo("Can't disable [index.soft_deletes.enabled] setting on resize")); // create one that won't fail @@ -164,7 +165,7 @@ public void testErrorCondition() { clusterState = ClusterState.builder(clusterState).putRoutingTable(projectId, routingTable).build(); TransportResizeAction.prepareCreateIndexRequest( - new ResizeRequest("target", "source"), + resizeRequest(ResizeType.SHRINK, "source", "target", Settings.builder()), projectId, clusterState.metadata().getProject(projectId).index("source"), "target", @@ -194,24 +195,22 @@ public void testPassNumRoutingShards() { routingTable = ESAllocationTestCase.startInitializingShardsAndReroute(service, clusterState, "source").routingTable(projectId); clusterState = ClusterState.builder(clusterState).putRoutingTable(projectId, routingTable).build(); - ResizeRequest resizeRequest = new ResizeRequest("target", "source"); - resizeRequest.setResizeType(ResizeType.SPLIT); - resizeRequest.getTargetIndexRequest().settings(Settings.builder().put("index.number_of_shards", 2).build()); IndexMetadata indexMetadata = clusterState.metadata().getProject(projectId).index("source"); TransportResizeAction.prepareCreateIndexRequest( - resizeRequest, + resizeRequest(ResizeType.SPLIT, "source", "target", Settings.builder().put("index.number_of_shards", 2)), projectId, indexMetadata, "target", new ResizeNumberOfShardsCalculator.SplitShardsCalculator() ); - resizeRequest.getTargetIndexRequest() - .settings( - Settings.builder().put("index.number_of_routing_shards", randomIntBetween(2, 10)).put("index.number_of_shards", 2).build() - ); TransportResizeAction.prepareCreateIndexRequest( - resizeRequest, + resizeRequest( + ResizeType.SPLIT, + "source", + "target", + Settings.builder().put("index.number_of_routing_shards", randomIntBetween(2, 10)).put("index.number_of_shards", 2) + ), projectId, indexMetadata, "target", @@ -239,26 +238,24 @@ public void testPassNumRoutingShardsAndFail() { routingTable = ESAllocationTestCase.startInitializingShardsAndReroute(service, clusterState, "source").routingTable(projectId); clusterState = ClusterState.builder(clusterState).putRoutingTable(projectId, routingTable).build(); - ResizeRequest resizeRequest = new ResizeRequest("target", "source"); - resizeRequest.setResizeType(ResizeType.SPLIT); - resizeRequest.getTargetIndexRequest().settings(Settings.builder().put("index.number_of_shards", numShards * 2).build()); TransportResizeAction.prepareCreateIndexRequest( - resizeRequest, + resizeRequest(ResizeType.SPLIT, "source", "target", Settings.builder().put("index.number_of_shards", numShards * 2)), projectId, clusterState.metadata().getProject(projectId).index("source"), "target", new ResizeNumberOfShardsCalculator.SplitShardsCalculator() ); - resizeRequest.getTargetIndexRequest() - .settings( - Settings.builder().put("index.number_of_shards", numShards * 2).put("index.number_of_routing_shards", numShards * 2).build() - ); ClusterState finalState = clusterState; IllegalArgumentException iae = expectThrows( IllegalArgumentException.class, () -> TransportResizeAction.prepareCreateIndexRequest( - resizeRequest, + resizeRequest( + ResizeType.SPLIT, + "source", + "target", + Settings.builder().put("index.number_of_shards", numShards * 2).put("index.number_of_routing_shards", numShards * 2) + ), projectId, finalState.metadata().getProject(projectId).index("source"), "target", @@ -290,7 +287,7 @@ public void testShrinkIndexSettings() { clusterState = ClusterState.builder(clusterState).putRoutingTable(projectId, routingTable).build(); int numSourceShards = clusterState.metadata().getProject(projectId).index(indexName).getNumberOfShards(); DocsStats stats = new DocsStats(between(0, (IndexWriter.MAX_DOCS) / numSourceShards), between(1, 1000), between(1, 10000)); - ResizeRequest target = new ResizeRequest("target", indexName); + ResizeRequest target = resizeRequest(ResizeType.SHRINK, indexName, "target", Settings.builder()); final ActiveShardCount activeShardCount = randomBoolean() ? ActiveShardCount.ALL : ActiveShardCount.ONE; target.setWaitForActiveShards(activeShardCount); CreateIndexClusterStateUpdateRequest request = TransportResizeAction.prepareCreateIndexRequest( @@ -318,9 +315,13 @@ public void testShrinkWithMaxPrimaryShardSize() { randomIntBetween(0, 10), Settings.builder().put("index.blocks.write", true).build() ).metadata().getProject(projectId).index("source"); - ResizeRequest resizeRequest = new ResizeRequest("target", "source"); + ResizeRequest resizeRequest = resizeRequest( + ResizeType.SHRINK, + "source", + "target", + Settings.builder().put("index.number_of_shards", 2) + ); resizeRequest.setMaxPrimaryShardSize(ByteSizeValue.ofBytes(10)); - resizeRequest.getTargetIndexRequest().settings(Settings.builder().put("index.number_of_shards", 2).build()); assertTrue( expectThrows( IllegalArgumentException.class, @@ -359,7 +360,7 @@ public void testShrinkWithMaxPrimaryShardSize() { DocsStats stats = new DocsStats(between(0, (IndexWriter.MAX_DOCS) / numSourceShards), between(1, 1000), between(1, 10000)); // each shard's storage will not be greater than the `max_primary_shard_size` - ResizeRequest target1 = new ResizeRequest("target", "source"); + ResizeRequest target1 = resizeRequest(ResizeType.SHRINK, "source", "target", Settings.builder()); target1.setMaxPrimaryShardSize(ByteSizeValue.ofBytes(2)); StoreStats storeStats = new StoreStats(10, between(0, 100), between(1, 100)); final int targetIndexShardsNum1 = 5; @@ -381,7 +382,7 @@ public void testShrinkWithMaxPrimaryShardSize() { // if `max_primary_shard_size` is less than the single shard size of the source index, // the shards number of the target index will be equal to the source index's shards number - ResizeRequest target2 = new ResizeRequest("target2", "source"); + ResizeRequest target2 = resizeRequest(ResizeType.SHRINK, "source", "target2", Settings.builder()); target2.setMaxPrimaryShardSize(ByteSizeValue.ofBytes(1)); StoreStats storeStats2 = new StoreStats(100, between(0, 100), between(1, 100)); final int targetIndexShardsNum2 = 10; diff --git a/test/framework/src/main/java/org/elasticsearch/action/admin/indices/ResizeIndexTestUtils.java b/test/framework/src/main/java/org/elasticsearch/action/admin/indices/ResizeIndexTestUtils.java new file mode 100644 index 0000000000000..7073c124e23d9 --- /dev/null +++ b/test/framework/src/main/java/org/elasticsearch/action/admin/indices/ResizeIndexTestUtils.java @@ -0,0 +1,39 @@ +/* + * 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.action.admin.indices; + +import org.elasticsearch.action.ActionFuture; +import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; +import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; +import org.elasticsearch.action.admin.indices.shrink.ResizeType; +import org.elasticsearch.action.admin.indices.shrink.TransportResizeAction; +import org.elasticsearch.common.settings.Settings; + +import static org.elasticsearch.test.ESIntegTestCase.client; +import static org.elasticsearch.test.ESTestCase.TEST_REQUEST_TIMEOUT; + +public enum ResizeIndexTestUtils { + ; + + public static ResizeRequest resizeRequest(ResizeType resizeType, String sourceIndex, String targetIndex, Settings.Builder settings) { + final var resizeRequest = new ResizeRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, resizeType, sourceIndex, targetIndex); + resizeRequest.setTargetIndexSettings(settings); + return resizeRequest; + } + + public static ActionFuture executeResize( + ResizeType resizeType, + String sourceIndex, + String targetIndex, + Settings.Builder settings + ) { + return client().execute(TransportResizeAction.TYPE, resizeRequest(resizeType, sourceIndex, targetIndex, settings)); + } +} diff --git a/x-pack/plugin/autoscaling/src/internalClusterTest/java/org/elasticsearch/xpack/autoscaling/storage/ReactiveStorageIT.java b/x-pack/plugin/autoscaling/src/internalClusterTest/java/org/elasticsearch/xpack/autoscaling/storage/ReactiveStorageIT.java index bca057b4d3416..0b18d4b152888 100644 --- a/x-pack/plugin/autoscaling/src/internalClusterTest/java/org/elasticsearch/xpack/autoscaling/storage/ReactiveStorageIT.java +++ b/x-pack/plugin/autoscaling/src/internalClusterTest/java/org/elasticsearch/xpack/autoscaling/storage/ReactiveStorageIT.java @@ -9,6 +9,7 @@ import org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteUtils; import org.elasticsearch.action.admin.indices.shrink.ResizeType; +import org.elasticsearch.action.admin.indices.shrink.TransportResizeAction; import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.support.ActiveShardCount; @@ -41,6 +42,7 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; +import static org.elasticsearch.action.admin.indices.ResizeIndexTestUtils.resizeRequest; import static org.elasticsearch.index.store.Store.INDEX_STORE_STATS_REFRESH_INTERVAL_SETTING; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.xpack.autoscaling.storage.ReactiveStorageDeciderService.AllocationState.MAX_AMOUNT_OF_SHARD_DECISIONS; @@ -347,16 +349,9 @@ public void testScaleWhileShrinking() throws Exception { }); String shrinkName = "shrink-" + indexName; - assertAcked( - indicesAdmin().prepareResizeIndex(indexName, shrinkName) - .setSettings( - Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .build() - ) - .setWaitForActiveShards(ActiveShardCount.NONE) - ); + final var shrinkRequest = resizeRequest(ResizeType.SHRINK, indexName, shrinkName, indexSettings(1, 0)); + shrinkRequest.setWaitForActiveShards(ActiveShardCount.NONE); + assertAcked(client().execute(TransportResizeAction.TYPE, shrinkRequest)); // * 2 since worst case is no hard links, see DiskThresholdDecider.getExpectedShardSize. long requiredSpaceForShrink = used * 2 + LOW_WATERMARK_BYTES; @@ -455,17 +450,9 @@ public void testScaleDuringSplit() throws Exception { updateIndexSettings(Settings.builder().put("index.blocks.write", true), indexName); String cloneName = "clone-" + indexName; int resizedShardCount = between(2, 10); - assertAcked( - indicesAdmin().prepareResizeIndex(indexName, cloneName) - .setSettings( - Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, resizedShardCount) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .build() - ) - .setWaitForActiveShards(ActiveShardCount.NONE) - .setResizeType(ResizeType.SPLIT) - ); + final var splitRequest = resizeRequest(ResizeType.SPLIT, indexName, cloneName, indexSettings(resizedShardCount, 0)); + splitRequest.setWaitForActiveShards(ActiveShardCount.NONE); + assertAcked(client().execute(TransportResizeAction.TYPE, splitRequest)); // * 2 since worst case is no hard links, see DiskThresholdDecider.getExpectedShardSize. long requiredSpaceForClone = used * 2 + LOW_WATERMARK_BYTES; diff --git a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDeciderIT.java b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDeciderIT.java index 041be4ea40e6d..421a010daec50 100644 --- a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDeciderIT.java +++ b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDeciderIT.java @@ -10,6 +10,7 @@ import org.elasticsearch.action.admin.cluster.allocation.ClusterAllocationExplanationUtils; import org.elasticsearch.action.admin.cluster.desirednodes.UpdateDesiredNodesAction; import org.elasticsearch.action.admin.cluster.desirednodes.UpdateDesiredNodesRequest; +import org.elasticsearch.action.admin.indices.ResizeIndexTestUtils; import org.elasticsearch.action.admin.indices.shrink.ResizeType; import org.elasticsearch.action.admin.indices.template.put.TransportPutComposableIndexTemplateAction; import org.elasticsearch.cluster.health.ClusterHealthStatus; @@ -324,12 +325,7 @@ public void testShrinkStaysOnTier() { .get(); indicesAdmin().prepareAddBlock(IndexMetadata.APIBlock.READ_ONLY, index).get(); - indicesAdmin().prepareResizeIndex(index, index + "-shrunk") - .setResizeType(ResizeType.SHRINK) - .setSettings( - Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build() - ) - .get(); + ResizeIndexTestUtils.executeResize(ResizeType.SHRINK, index, index + "-shrunk", indexSettings(1, 0)).actionGet(); ensureGreen(index + "-shrunk"); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ResizeIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ResizeIndexStep.java index 847647b0cd5d3..81315944498cf 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ResizeIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ResizeIndexStep.java @@ -11,6 +11,7 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; import org.elasticsearch.action.admin.indices.shrink.ResizeType; +import org.elasticsearch.action.admin.indices.shrink.TransportResizeAction; import org.elasticsearch.client.internal.Client; import org.elasticsearch.cluster.ClusterStateObserver; import org.elasticsearch.cluster.ProjectState; @@ -95,10 +96,13 @@ public void performAction( .put(LifecycleSettings.LIFECYCLE_SKIP, true) .build(); - ResizeRequest resizeRequest = new ResizeRequest(targetIndexName, indexMetadata.getIndex().getName()).masterNodeTimeout( - TimeValue.MAX_VALUE + ResizeRequest resizeRequest = new ResizeRequest( + TimeValue.MAX_VALUE, + TimeValue.MAX_VALUE, + resizeType, + indexMetadata.getIndex().getName(), + targetIndexName ); - resizeRequest.setResizeType(resizeType); resizeRequest.getTargetIndexRequest().settings(relevantTargetSettings); if (resizeType == ResizeType.SHRINK) { resizeRequest.setMaxPrimaryShardSize(maxPrimaryShardSize); @@ -106,9 +110,7 @@ public void performAction( // 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))); + getClient(currentState.projectId()).execute(TransportResizeAction.TYPE, resizeRequest, listener.map(response -> null)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ResizeIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ResizeIndexStepTests.java index 9fb230885a4b9..8316c587384af 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ResizeIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ResizeIndexStepTests.java @@ -8,9 +8,9 @@ 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.action.admin.indices.shrink.TransportResizeAction; import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.AliasMetadata; import org.elasticsearch.cluster.metadata.IndexMetadata; @@ -119,9 +119,8 @@ public void testPerformAction() throws Exception { .build(); Mockito.doAnswer(invocation -> { - ResizeRequest request = (ResizeRequest) invocation.getArguments()[0]; - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; + final ResizeRequest request = invocation.getArgument(1); + final ActionListener listener = invocation.getArgument(2); assertThat(request.getSourceIndex(), equalTo(sourceIndexMetadata.getIndex().getName())); assertThat(request.getTargetIndexRequest().aliases(), equalTo(Set.of())); @@ -133,16 +132,14 @@ public void testPerformAction() throws Exception { assertThat(request.getMaxPrimaryShardSize(), equalTo(step.getMaxPrimaryShardSize())); listener.onResponse(new CreateIndexResponse(true, true, sourceIndexMetadata.getIndex().getName())); return null; - }).when(indicesClient).resizeIndex(Mockito.any(), Mockito.any()); + }).when(projectClient).execute(Mockito.same(TransportResizeAction.TYPE), Mockito.any(), Mockito.any()); final var state = projectStateWithEmptyProject(); performActionAndWait(step, sourceIndexMetadata, state, null); Mockito.verify(client).projectClient(state.projectId()); - Mockito.verify(projectClient).admin(); - Mockito.verifyNoMoreInteractions(client); - Mockito.verify(adminClient, Mockito.only()).indices(); - Mockito.verify(indicesClient, Mockito.only()).resizeIndex(Mockito.any(), Mockito.any()); + Mockito.verify(projectClient).execute(Mockito.same(TransportResizeAction.TYPE), Mockito.any(), Mockito.any()); + Mockito.verifyNoMoreInteractions(client, projectClient); } public void testPerformActionShrunkenIndexExists() throws Exception { @@ -195,23 +192,20 @@ public void testPerformActionIsCompleteForUnAckedRequests() throws Exception { ResizeIndexStep step = createRandomInstance(); Mockito.doAnswer(invocation -> { - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; + final ActionListener listener = invocation.getArgument(2); listener.onResponse(new CreateIndexResponse(false, false, indexMetadata.getIndex().getName())); return null; - }).when(indicesClient).resizeIndex(Mockito.any(), Mockito.any()); + }).when(projectClient).execute(Mockito.same(TransportResizeAction.TYPE), Mockito.any(), Mockito.any()); final var state = projectStateWithEmptyProject(); performActionAndWait(step, indexMetadata, state, null); Mockito.verify(client).projectClient(state.projectId()); - Mockito.verify(projectClient).admin(); - Mockito.verifyNoMoreInteractions(client); - Mockito.verify(adminClient, Mockito.only()).indices(); - Mockito.verify(indicesClient, Mockito.only()).resizeIndex(Mockito.any(), Mockito.any()); + Mockito.verify(projectClient).execute(Mockito.same(TransportResizeAction.TYPE), Mockito.any(), Mockito.any()); + Mockito.verifyNoMoreInteractions(client, projectClient); } - public void testPerformActionFailure() throws Exception { + public void testPerformActionFailure() { LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setIndexCreationDate(randomNonNegativeLong()); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) @@ -224,20 +218,17 @@ public void testPerformActionFailure() throws Exception { ResizeIndexStep step = createRandomInstance(); Mockito.doAnswer(invocation -> { - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; + final ActionListener listener = invocation.getArgument(2); listener.onFailure(exception); return null; - }).when(indicesClient).resizeIndex(Mockito.any(), Mockito.any()); + }).when(projectClient).execute(Mockito.same(TransportResizeAction.TYPE), Mockito.any(), Mockito.any()); final var state = projectStateWithEmptyProject(); assertSame(exception, expectThrows(Exception.class, () -> performActionAndWait(step, indexMetadata, state, null))); Mockito.verify(client).projectClient(state.projectId()); - Mockito.verify(projectClient).admin(); - Mockito.verifyNoMoreInteractions(client); - Mockito.verify(adminClient, Mockito.only()).indices(); - Mockito.verify(indicesClient, Mockito.only()).resizeIndex(Mockito.any(), Mockito.any()); + Mockito.verify(projectClient).execute(Mockito.same(TransportResizeAction.TYPE), Mockito.any(), Mockito.any()); + Mockito.verifyNoMoreInteractions(client, projectClient); } } diff --git a/x-pack/plugin/logsdb/src/internalClusterTest/java/org/elasticsearch/xpack/logsdb/LogsIndexingIT.java b/x-pack/plugin/logsdb/src/internalClusterTest/java/org/elasticsearch/xpack/logsdb/LogsIndexingIT.java index bea3b7343de3b..d52b364d56888 100644 --- a/x-pack/plugin/logsdb/src/internalClusterTest/java/org/elasticsearch/xpack/logsdb/LogsIndexingIT.java +++ b/x-pack/plugin/logsdb/src/internalClusterTest/java/org/elasticsearch/xpack/logsdb/LogsIndexingIT.java @@ -12,6 +12,7 @@ import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest; import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse; import org.elasticsearch.action.admin.indices.shrink.ResizeType; +import org.elasticsearch.action.admin.indices.shrink.TransportResizeAction; import org.elasticsearch.action.admin.indices.template.put.TransportPutComposableIndexTemplateAction; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.get.GetRequest; @@ -37,6 +38,7 @@ import java.util.List; import java.util.UUID; +import static org.elasticsearch.action.admin.indices.ResizeIndexTestUtils.resizeRequest; import static org.elasticsearch.index.mapper.DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse; @@ -233,12 +235,9 @@ public void testShrink() throws Exception { client().bulk(bulkRequest).actionGet(); client().admin().indices().prepareFlush("my-logs").get(); client().admin().indices().prepareUpdateSettings("my-logs").setSettings(Settings.builder().put("index.blocks.write", true)).get(); - client().admin() - .indices() - .prepareResizeIndex("my-logs", "shrink-my-logs") - .setResizeType(ResizeType.SHRINK) - .setSettings(indexSettings(1, 0).build()) - .get(); + + client().execute(TransportResizeAction.TYPE, resizeRequest(ResizeType.SHRINK, "my-logs", "shrink-my-logs", indexSettings(1, 0))) + .actionGet(); assertNoFailures(client().admin().indices().prepareForceMerge("shrink-my-logs").setMaxNumSegments(1).setFlush(true).get()); } diff --git a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/FrozenSearchableSnapshotsIntegTests.java b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/FrozenSearchableSnapshotsIntegTests.java index 5c86ca4649194..83bb975131805 100644 --- a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/FrozenSearchableSnapshotsIntegTests.java +++ b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/FrozenSearchableSnapshotsIntegTests.java @@ -62,6 +62,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import static org.elasticsearch.action.admin.indices.ResizeIndexTestUtils.executeResize; import static org.elasticsearch.index.IndexSettings.INDEX_SOFT_DELETES_SETTING; import static org.elasticsearch.index.query.QueryBuilders.matchQuery; import static org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram; @@ -384,15 +385,15 @@ public void testCreateAndRestorePartialSearchableSnapshot() throws Exception { final String clonedIndexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT); assertAcked( - indicesAdmin().prepareResizeIndex(restoredIndexName, clonedIndexName) - .setResizeType(ResizeType.CLONE) - .setSettings( - Settings.builder() - .putNull(IndexModule.INDEX_STORE_TYPE_SETTING.getKey()) - .putNull(IndexModule.INDEX_RECOVERY_TYPE_SETTING.getKey()) - .put(DataTier.TIER_PREFERENCE, DataTier.DATA_HOT) - .build() - ) + executeResize( + ResizeType.CLONE, + restoredIndexName, + clonedIndexName, + Settings.builder() + .putNull(IndexModule.INDEX_STORE_TYPE_SETTING.getKey()) + .putNull(IndexModule.INDEX_RECOVERY_TYPE_SETTING.getKey()) + .put(DataTier.TIER_PREFERENCE, DataTier.DATA_HOT) + ) ); ensureGreen(clonedIndexName); assertTotalHits(clonedIndexName, originalAllHits, originalBarHits); diff --git a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsIntegTests.java b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsIntegTests.java index 0fe63df106862..69c3792a0f665 100644 --- a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsIntegTests.java +++ b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsIntegTests.java @@ -81,6 +81,7 @@ import java.util.stream.IntStream; import static org.elasticsearch.action.admin.cluster.allocation.ClusterAllocationExplanationUtils.getClusterAllocationExplanation; +import static org.elasticsearch.action.admin.indices.ResizeIndexTestUtils.executeResize; import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING; import static org.elasticsearch.index.IndexSettings.INDEX_SOFT_DELETES_SETTING; import static org.elasticsearch.index.query.QueryBuilders.matchQuery; @@ -330,14 +331,14 @@ public void testCreateAndRestoreSearchableSnapshot() throws Exception { final String clonedIndexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT); assertAcked( - indicesAdmin().prepareResizeIndex(restoredIndexName, clonedIndexName) - .setResizeType(ResizeType.CLONE) - .setSettings( - Settings.builder() - .putNull(IndexModule.INDEX_STORE_TYPE_SETTING.getKey()) - .putNull(IndexModule.INDEX_RECOVERY_TYPE_SETTING.getKey()) - .build() - ) + executeResize( + ResizeType.CLONE, + restoredIndexName, + clonedIndexName, + Settings.builder() + .putNull(IndexModule.INDEX_STORE_TYPE_SETTING.getKey()) + .putNull(IndexModule.INDEX_RECOVERY_TYPE_SETTING.getKey()) + ) ); ensureGreen(clonedIndexName); assertTotalHits(clonedIndexName, originalAllHits, originalBarHits); diff --git a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsResizeIntegTests.java b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsResizeIntegTests.java index 0a0cce5dd87ed..d6db625fa02c8 100644 --- a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsResizeIntegTests.java +++ b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsResizeIntegTests.java @@ -18,6 +18,7 @@ import java.util.List; +import static org.elasticsearch.action.admin.indices.ResizeIndexTestUtils.executeResize; import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING; import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING; import static org.elasticsearch.index.IndexSettings.INDEX_SOFT_DELETES_SETTING; @@ -55,13 +56,14 @@ public void tearDown() throws Exception { super.tearDown(); } + private static void runResizeAction(String targetIndexName, ResizeType resizeType, Settings.Builder settings) { + assertAcked(executeResize(resizeType, "mounted-index", targetIndexName, settings)); + } + public void testShrinkSearchableSnapshotIndex() { final IllegalArgumentException exception = expectThrows( IllegalArgumentException.class, - () -> indicesAdmin().prepareResizeIndex("mounted-index", "shrunk-index") - .setResizeType(ResizeType.SHRINK) - .setSettings(indexSettingsNoReplicas(1).build()) - .get() + () -> runResizeAction("shrunk-index", ResizeType.SHRINK, indexSettingsNoReplicas(1)) ); assertThat(exception.getMessage(), equalTo("can't shrink searchable snapshot index [mounted-index]")); } @@ -69,10 +71,7 @@ public void testShrinkSearchableSnapshotIndex() { public void testSplitSearchableSnapshotIndex() { final IllegalArgumentException exception = expectThrows( IllegalArgumentException.class, - () -> indicesAdmin().prepareResizeIndex("mounted-index", "split-index") - .setResizeType(ResizeType.SPLIT) - .setSettings(indexSettingsNoReplicas(4).build()) - .get() + () -> runResizeAction("split-index", ResizeType.SPLIT, indexSettingsNoReplicas(4)) ); assertThat(exception.getMessage(), equalTo("can't split searchable snapshot index [mounted-index]")); } @@ -80,7 +79,7 @@ public void testSplitSearchableSnapshotIndex() { public void testCloneSearchableSnapshotIndex() { IllegalArgumentException exception = expectThrows( IllegalArgumentException.class, - () -> indicesAdmin().prepareResizeIndex("mounted-index", "cloned-index").setResizeType(ResizeType.CLONE).get() + () -> runResizeAction("cloned-index", ResizeType.CLONE, Settings.builder()) ); assertThat( exception.getMessage(), @@ -89,27 +88,25 @@ public void testCloneSearchableSnapshotIndex() { exception = expectThrows( IllegalArgumentException.class, - () -> indicesAdmin().prepareResizeIndex("mounted-index", "cloned-index") - .setResizeType(ResizeType.CLONE) - .setSettings(Settings.builder().putNull(IndexModule.INDEX_STORE_TYPE_SETTING.getKey()).build()) - .get() + () -> runResizeAction( + "cloned-index", + ResizeType.CLONE, + Settings.builder().putNull(IndexModule.INDEX_STORE_TYPE_SETTING.getKey()) + ) ); assertThat( exception.getMessage(), equalTo("can't clone searchable snapshot index [mounted-index]; setting [index.recovery.type] should be overridden") ); - assertAcked( - indicesAdmin().prepareResizeIndex("mounted-index", "cloned-index") - .setResizeType(ResizeType.CLONE) - .setSettings( - Settings.builder() - .putNull(IndexModule.INDEX_STORE_TYPE_SETTING.getKey()) - .putNull(IndexModule.INDEX_RECOVERY_TYPE_SETTING.getKey()) - .put(DataTier.TIER_PREFERENCE, DataTier.DATA_HOT) - .put(INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0) - .build() - ) + runResizeAction( + "cloned-index", + ResizeType.CLONE, + Settings.builder() + .putNull(IndexModule.INDEX_STORE_TYPE_SETTING.getKey()) + .putNull(IndexModule.INDEX_RECOVERY_TYPE_SETTING.getKey()) + .put(DataTier.TIER_PREFERENCE, DataTier.DATA_HOT) + .put(INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0) ); ensureGreen("cloned-index"); assertAcked(indicesAdmin().prepareDelete("cloned-index")); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ShrinkIndexWithSecurityTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ShrinkIndexWithSecurityTests.java index 05dc4325a302a..3926a047c43aa 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ShrinkIndexWithSecurityTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ShrinkIndexWithSecurityTests.java @@ -6,6 +6,8 @@ */ package org.elasticsearch.integration; +import org.elasticsearch.action.admin.indices.ResizeIndexTestUtils; +import org.elasticsearch.action.admin.indices.shrink.ResizeType; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.query.TermsQueryBuilder; @@ -46,7 +48,8 @@ public void testShrinkIndex() throws Exception { // wait for green and then shrink ensureGreen(); - assertAcked(indicesAdmin().prepareResizeIndex("bigindex", "shrunk_bigindex").setSettings(indexSettings(1, 0).build())); + + assertAcked(ResizeIndexTestUtils.executeResize(ResizeType.SHRINK, "bigindex", "shrunk_bigindex", indexSettings(1, 0))); // verify all docs ensureGreen(); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/ResizeRequestInterceptorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/ResizeRequestInterceptorTests.java index e3b402d96d416..618d374984e41 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/ResizeRequestInterceptorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/ResizeRequestInterceptorTests.java @@ -8,8 +8,9 @@ import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.action.admin.indices.shrink.ResizeAction; import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; +import org.elasticsearch.action.admin.indices.shrink.ResizeType; +import org.elasticsearch.action.admin.indices.shrink.TransportResizeAction; import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; @@ -111,7 +112,12 @@ public void checkResizeWithDlsFlsConfigured(boolean dlsFlsFeatureEnabled, String ); PlainActionFuture plainActionFuture = new PlainActionFuture<>(); - RequestInfo requestInfo = new RequestInfo(authentication, new ResizeRequest("bar", "foo"), ResizeAction.NAME, null); + RequestInfo requestInfo = new RequestInfo( + authentication, + new ResizeRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, randomFrom(ResizeType.values()), "foo", "bar"), + TransportResizeAction.TYPE.name(), + null + ); AuthorizationEngine mockEngine = mock(AuthorizationEngine.class); doAnswer(invocationOnMock -> { ActionListener listener = (ActionListener) invocationOnMock.getArguments()[3]; @@ -147,7 +153,12 @@ public void testResizeRequestInterceptorThrowsWhenTargetHasGreaterPermissions() AuthorizationEngine mockEngine = mock(AuthorizationEngine.class); { PlainActionFuture plainActionFuture = new PlainActionFuture<>(); - RequestInfo requestInfo = new RequestInfo(authentication, new ResizeRequest("target", "source"), ResizeAction.NAME, null); + RequestInfo requestInfo = new RequestInfo( + authentication, + new ResizeRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, randomFrom(ResizeType.values()), "source", "target"), + TransportResizeAction.TYPE.name(), + null + ); doAnswer(invocationOnMock -> { ActionListener listener = (ActionListener) invocationOnMock.getArguments()[3]; listener.onResponse(AuthorizationResult.deny()); @@ -172,7 +183,12 @@ public void testResizeRequestInterceptorThrowsWhenTargetHasGreaterPermissions() // swap target and source for success { PlainActionFuture plainActionFuture = new PlainActionFuture<>(); - RequestInfo requestInfo = new RequestInfo(authentication, new ResizeRequest("source", "target"), ResizeAction.NAME, null); + RequestInfo requestInfo = new RequestInfo( + authentication, + new ResizeRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, randomFrom(ResizeType.values()), "target", "source"), + TransportResizeAction.TYPE.name(), + null + ); doAnswer(invocationOnMock -> { ActionListener listener = (ActionListener) invocationOnMock.getArguments()[3]; listener.onResponse(AuthorizationResult.granted());