forked from opensearch-project/index-management
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error Prevention: Add close action (opensearch-project#728)
* Error Prevention: Add close action Signed-off-by: Angie Zhang <[email protected]> * Error Prevention: Add close action Signed-off-by: Angie Zhang <[email protected]> * Error Prevention: Add close action Signed-off-by: Angie Zhang <[email protected]> * Error Prevention: Add close action Signed-off-by: Angie Zhang <[email protected]> * Fixed comments for close action validation. Signed-off-by: Angie Zhang <[email protected]> * Added return false for validIndex method Signed-off-by: Angie Zhang <[email protected]> * Updated test case name Signed-off-by: Angie Zhang <[email protected]> --------- Signed-off-by: Angie Zhang <[email protected]>
- Loading branch information
Angie Zhang
authored
Apr 14, 2023
1 parent
f9d57b1
commit 363086f
Showing
3 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...in/kotlin/org/opensearch/indexmanagement/indexstatemanagement/validation/ValidateClose.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.indexmanagement.indexstatemanagement.validation | ||
|
||
import org.apache.logging.log4j.LogManager | ||
import org.opensearch.cluster.metadata.MetadataCreateIndexService | ||
import org.opensearch.cluster.service.ClusterService | ||
import org.opensearch.common.settings.Settings | ||
import org.opensearch.indexmanagement.spi.indexstatemanagement.Validate | ||
import org.opensearch.indexmanagement.util.OpenForTesting | ||
import org.opensearch.indices.InvalidIndexNameException | ||
import org.opensearch.monitor.jvm.JvmService | ||
|
||
@OpenForTesting | ||
class ValidateClose( | ||
settings: Settings, | ||
clusterService: ClusterService, | ||
jvmService: JvmService | ||
) : Validate(settings, clusterService, jvmService) { | ||
|
||
private val logger = LogManager.getLogger(javaClass) | ||
|
||
@Suppress("ReturnSuppressCount", "ReturnCount") | ||
override fun execute(indexName: String): Validate { | ||
// if these conditions are false, fail validation and do not execute close action | ||
if (!indexExists(indexName) || !validIndex(indexName)) { | ||
validationStatus = ValidationStatus.FAILED | ||
return this | ||
} | ||
validationMessage = getValidationPassedMessage(indexName) | ||
return this | ||
} | ||
|
||
private fun indexExists(indexName: String): Boolean { | ||
val isIndexExists = clusterService.state().metadata.indices.containsKey(indexName) | ||
if (!isIndexExists) { | ||
val message = getNoIndexMessage(indexName) | ||
logger.warn(message) | ||
validationMessage = message | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
private fun validIndex(indexName: String): Boolean { | ||
val exceptionGenerator: (String, String) -> RuntimeException = { index_name, reason -> InvalidIndexNameException(index_name, reason) } | ||
// If the index name is invalid for any reason, this will throw an exception giving the reason why in the message. | ||
// That will be displayed to the user as the cause. | ||
try { | ||
MetadataCreateIndexService.validateIndexOrAliasName(indexName, exceptionGenerator) | ||
} catch (e: Exception) { | ||
val message = getIndexNotValidMessage(indexName) | ||
logger.warn(message) | ||
validationMessage = message | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
@Suppress("TooManyFunctions") | ||
companion object { | ||
const val name = "validate_close" | ||
fun getNoIndexMessage(index: String) = "No such index [index=$index] for close action." | ||
fun getIndexNotValidMessage(index: String) = "Index [index=$index] is not valid. Abort close action on it." | ||
fun getValidationPassedMessage(index: String) = "Close action validation passed for [index=$index]" | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
.../kotlin/org/opensearch/indexmanagement/indexstatemanagement/validation/ValidateCloseIT.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.indexmanagement.indexstatemanagement.validation | ||
|
||
import org.opensearch.indexmanagement.indexstatemanagement.IndexStateManagementRestTestCase | ||
import org.opensearch.indexmanagement.indexstatemanagement.action.CloseAction | ||
import org.opensearch.indexmanagement.indexstatemanagement.model.Policy | ||
import org.opensearch.indexmanagement.indexstatemanagement.model.State | ||
import org.opensearch.indexmanagement.indexstatemanagement.randomErrorNotification | ||
import org.opensearch.indexmanagement.spi.indexstatemanagement.Validate | ||
import org.opensearch.indexmanagement.waitFor | ||
import java.time.Instant | ||
import java.time.temporal.ChronoUnit | ||
import java.util.Locale | ||
|
||
class ValidateCloseIT : IndexStateManagementRestTestCase() { | ||
private val testIndexName = javaClass.simpleName.lowercase(Locale.ROOT) | ||
|
||
fun `test basic close action validation`() { | ||
enableValidationService() | ||
val indexName = "${testIndexName}_index_1" | ||
val policyID = "${testIndexName}_testPolicyName_1" | ||
val actionConfig = CloseAction(0) | ||
val states = listOf( | ||
State("CloseState", listOf(actionConfig), listOf()) | ||
) | ||
|
||
val policy = Policy( | ||
id = policyID, | ||
description = "$testIndexName description", | ||
schemaVersion = 1L, | ||
lastUpdatedTime = Instant.now().truncatedTo(ChronoUnit.MILLIS), | ||
errorNotification = randomErrorNotification(), | ||
defaultState = states[0].name, | ||
states = states | ||
) | ||
createPolicy(policy, policyID) | ||
createIndex(indexName, policyID) | ||
|
||
assertEquals("open", getIndexState(indexName)) | ||
|
||
val managedIndexConfig = getExistingManagedIndexConfig(indexName) | ||
// Change the start time so the job will trigger in 2 seconds. | ||
updateManagedIndexConfigStartTime(managedIndexConfig) | ||
|
||
waitFor { assertEquals(policyID, getExplainManagedIndexMetaData(indexName).policyID) } | ||
|
||
// Need to wait two cycles. | ||
// Change the start time so the job will trigger in 2 seconds. | ||
updateManagedIndexConfigStartTime(managedIndexConfig) | ||
|
||
waitFor { assertEquals("close", getIndexState(indexName)) } | ||
|
||
waitFor { | ||
val data = getExplainValidationResult(indexName) | ||
assertEquals( | ||
"Index close action validation status is PASSED.", | ||
Validate.ValidationStatus.PASSED, | ||
data.validationStatus | ||
) | ||
} | ||
} | ||
} |