From 4539d240c2e6e80e98f9b6bab8d252caaf6f1c8f Mon Sep 17 00:00:00 2001 From: Angie Zhang Date: Fri, 24 Mar 2023 10:31:52 -0700 Subject: [PATCH] Added & fixed test cases for shrink action Signed-off-by: Angie Zhang --- .../action/ShrinkActionIT.kt | 62 ++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/ShrinkActionIT.kt b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/ShrinkActionIT.kt index 0bdf32ab0..1a55bdbb3 100644 --- a/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/ShrinkActionIT.kt +++ b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/ShrinkActionIT.kt @@ -55,7 +55,9 @@ class ShrinkActionIT : IndexStateManagementRestTestCase() { } """.trimIndent() val res = client().makeRequest( - "PUT", "_cluster/settings", emptyMap(), + "PUT", + "_cluster/settings", + emptyMap(), StringEntity(request, ContentType.APPLICATION_JSON) ) assertEquals("Request failed", RestStatus.OK, res.restStatus()) @@ -447,7 +449,6 @@ class ShrinkActionIT : IndexStateManagementRestTestCase() { val indexName = "${testIndexName}_index_shard_noop" val policyID = "${testIndexName}_testPolicyName_shard_noop" - // Create a Policy with one State that only preforms a force_merge Action val shrinkAction = ShrinkAction( numNewShards = null, maxShardSize = null, @@ -500,6 +501,63 @@ class ShrinkActionIT : IndexStateManagementRestTestCase() { } } + fun `test source index already has target number of shard`() { + val logger = LogManager.getLogger(::ShrinkActionIT) + val indexName = "${testIndexName}_index_shard_noop" + val policyID = "${testIndexName}_testPolicyName_shard_noop" + + val shrinkAction = ShrinkAction( + numNewShards = 2, + maxShardSize = null, + percentageOfSourceShards = null, + targetIndexTemplate = Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, "{{ctx.index}}$testIndexSuffix", mapOf()), + aliases = null, + forceUnsafe = true, + index = 0 + ) + val states = listOf(State("ShrinkState", listOf(shrinkAction), listOf())) + + val policy = Policy( + id = policyID, + description = "$testIndexName description", + schemaVersion = 11L, + lastUpdatedTime = Instant.now().truncatedTo(ChronoUnit.MILLIS), + errorNotification = randomErrorNotification(), + defaultState = states[0].name, + states = states + ) + + createPolicy(policy, policyID) + createIndex(indexName, policyID, null, "0", "2", "") + + insertSampleData(indexName, 3) + + // Will change the startTime each execution so that it triggers in 2 seconds + // First execution: Policy is initialized + val managedIndexConfig = getExistingManagedIndexConfig(indexName) + + updateManagedIndexConfigStartTime(managedIndexConfig) + waitFor(Instant.ofEpochSecond(60)) { assertEquals(policyID, getExplainManagedIndexMetaData(indexName).policyID) } + logger.info("before attempt move shards") + // Starts AttemptMoveShardsStep + updateManagedIndexConfigStartTime(managedIndexConfig) + + // The action should be done after the no-op + waitFor(Instant.ofEpochSecond(60)) { + val metadata = getExplainManagedIndexMetaData(indexName) + assertEquals( + "Did not get the no-op due to no shard count change", + AttemptMoveShardsStep.NO_SHARD_COUNT_CHANGE_MESSAGE, + metadata.info?.get("message") + ) + assertEquals( + "Was not on the last step after no-op due to no shard count change", + WaitForShrinkStep.name, + metadata.stepMetaData?.name + ) + } + } + @Suppress("UNCHECKED_CAST") fun `test shrink with replicas`() { val logger = LogManager.getLogger(::ShrinkActionIT)