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 index priority action (opensearch-project#729)
* Error Prevention: Add index priority action Signed-off-by: Angie Zhang <[email protected]> * Error Prevention: Add index priority action Signed-off-by: Angie Zhang <[email protected]> --------- Signed-off-by: Angie Zhang <[email protected]>
- Loading branch information
Angie Zhang
authored
Apr 13, 2023
1 parent
eeeaac4
commit 30d3dfc
Showing
3 changed files
with
124 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
57 changes: 57 additions & 0 deletions
57
...n/org/opensearch/indexmanagement/indexstatemanagement/validation/ValidateIndexPriority.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,57 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/* | ||
* 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.IndexMetadata | ||
import org.opensearch.cluster.service.ClusterService | ||
import org.opensearch.common.settings.Settings | ||
import org.opensearch.indexmanagement.util.OpenForTesting | ||
import org.opensearch.indexmanagement.spi.indexstatemanagement.Validate | ||
import org.opensearch.monitor.jvm.JvmService | ||
|
||
@OpenForTesting | ||
class ValidateIndexPriority( | ||
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 index_priority action | ||
if (hasReadOnlyAllowDeleteBlock(indexName)) { | ||
return this | ||
} | ||
validationMessage = getValidationPassedMessage(indexName) | ||
return this | ||
} | ||
|
||
fun hasReadOnlyAllowDeleteBlock(indexName: String): Boolean { | ||
val readOnlyAllowDeleteBlock = settings.get(IndexMetadata.SETTING_READ_ONLY_ALLOW_DELETE) | ||
if (!readOnlyAllowDeleteBlock.isNullOrEmpty() && readOnlyAllowDeleteBlock.toBoolean()) { | ||
val message = getReadOnlyAllowDeleteBlockMessage(indexName) | ||
logger.warn(message) | ||
validationStatus = ValidationStatus.FAILED | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
@Suppress("TooManyFunctions") | ||
companion object { | ||
const val name = "validate_index_priority" | ||
fun getReadOnlyAllowDeleteBlockMessage(index: String) = "read_only_allow_delete block is not null for index [index=$index]" | ||
fun getValidationPassedMessage(index: String) = "Index Priority action validation passed for [index=$index]" | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...org/opensearch/indexmanagement/indexstatemanagement/validation/ValidateIndexPriorityIT.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 | ||
*/ | ||
|
||
/* | ||
* 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.IndexPriorityAction | ||
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 ValidateIndexPriorityIT : IndexStateManagementRestTestCase() { | ||
private val testIndexName = javaClass.simpleName.lowercase(Locale.ROOT) | ||
|
||
fun `test basic index priority`() { | ||
val indexName = "${testIndexName}_index_1" | ||
val policyID = "${testIndexName}_testPolicyName_1" | ||
val actionConfig = IndexPriorityAction(50, 0) | ||
val states = listOf(State(name = "SetPriorityState", actions = listOf(actionConfig), transitions = 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) | ||
|
||
val managedIndexConfig = getExistingManagedIndexConfig(indexName) | ||
// Change the runJob start time so the job will trigger in 2 seconds | ||
updateManagedIndexConfigStartTime(managedIndexConfig) | ||
|
||
// ism policy initialized | ||
waitFor { assertEquals(policyID, getExplainManagedIndexMetaData(indexName).policyID) } | ||
|
||
// change the runJob start time to change index priority | ||
updateManagedIndexConfigStartTime(managedIndexConfig) | ||
|
||
waitFor { assertEquals("Index did not set index_priority to ${actionConfig.indexPriority}", actionConfig.indexPriority, getIndexPrioritySetting(indexName)) } | ||
|
||
waitFor { | ||
val data = getExplainValidationResult(indexName) | ||
assertEquals( | ||
"Index Priority action validation status is PASSED.", | ||
Validate.ValidationStatus.PASSED, | ||
data.validationStatus | ||
) | ||
} | ||
} | ||
} |