This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an Action to Setting Index Priority (#241)
* add action for setting index priority * resolve PR comments * unit test for unhappy path of set index priority action * bump version of ism-config schema to 3 from 2
- Loading branch information
1 parent
b4ab545
commit c011253
Showing
11 changed files
with
399 additions
and
7 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
.../com/amazon/opendistroforelasticsearch/indexstatemanagement/action/IndexPriorityAction.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,39 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.indexstatemanagement.action | ||
|
||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.ManagedIndexMetaData | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.action.ActionConfig.ActionType | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.action.IndexPriorityActionConfig | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.step.Step | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.step.indexpriority.AttemptSetIndexPriorityStep | ||
import org.elasticsearch.client.Client | ||
import org.elasticsearch.cluster.service.ClusterService | ||
|
||
class IndexPriorityAction( | ||
clusterService: ClusterService, | ||
client: Client, | ||
managedIndexMetaData: ManagedIndexMetaData, | ||
config: IndexPriorityActionConfig | ||
) : Action(ActionType.INDEX_PRIORITY, config, managedIndexMetaData) { | ||
|
||
private val attemptSetIndexPriorityStep = AttemptSetIndexPriorityStep(clusterService, client, config, managedIndexMetaData) | ||
private val steps = listOf(attemptSetIndexPriorityStep) | ||
|
||
override fun getSteps(): List<Step> = steps | ||
|
||
override fun getStepToExecute(): Step = attemptSetIndexPriorityStep | ||
} |
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
82 changes: 82 additions & 0 deletions
82
...opendistroforelasticsearch/indexstatemanagement/model/action/IndexPriorityActionConfig.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,82 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.indexstatemanagement.model.action | ||
|
||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.action.Action | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.action.IndexPriorityAction | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.ManagedIndexMetaData | ||
import org.elasticsearch.client.Client | ||
import org.elasticsearch.cluster.service.ClusterService | ||
import org.elasticsearch.common.xcontent.ToXContent | ||
import org.elasticsearch.common.xcontent.ToXContentObject | ||
import org.elasticsearch.common.xcontent.XContentBuilder | ||
import org.elasticsearch.common.xcontent.XContentParser | ||
import org.elasticsearch.common.xcontent.XContentParser.Token | ||
import org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken | ||
import org.elasticsearch.script.ScriptService | ||
import java.io.IOException | ||
|
||
data class IndexPriorityActionConfig( | ||
val indexPriority: Int, | ||
val index: Int | ||
) : ToXContentObject, ActionConfig(ActionType.INDEX_PRIORITY, index) { | ||
|
||
init { | ||
require(indexPriority >= 0) { "IndexPriorityActionConfig index_priority value must be a non-negative number" } | ||
} | ||
|
||
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { | ||
builder.startObject() | ||
super.toXContent(builder, params).startObject(ActionType.INDEX_PRIORITY.type) | ||
builder.field(INDEX_PRIORITY_FIELD, indexPriority) | ||
return builder.endObject().endObject() | ||
} | ||
|
||
override fun isFragment(): Boolean = super<ToXContentObject>.isFragment() | ||
|
||
override fun toAction( | ||
clusterService: ClusterService, | ||
scriptService: ScriptService, | ||
client: Client, | ||
managedIndexMetaData: ManagedIndexMetaData | ||
): Action = IndexPriorityAction(clusterService, client, managedIndexMetaData, this) | ||
|
||
companion object { | ||
const val INDEX_PRIORITY_FIELD = "priority" | ||
|
||
@JvmStatic | ||
@Throws(IOException::class) | ||
fun parse(xcp: XContentParser, index: Int): IndexPriorityActionConfig { | ||
var indexPriority: Int? = null | ||
|
||
ensureExpectedToken(Token.START_OBJECT, xcp.currentToken(), xcp::getTokenLocation) | ||
while (xcp.nextToken() != Token.END_OBJECT) { | ||
val fieldName = xcp.currentName() | ||
xcp.nextToken() | ||
|
||
when (fieldName) { | ||
INDEX_PRIORITY_FIELD -> indexPriority = xcp.intValue() | ||
else -> throw IllegalArgumentException("Invalid field: [$fieldName] found in IndexPriorityActionConfig.") | ||
} | ||
} | ||
|
||
return IndexPriorityActionConfig( | ||
indexPriority = requireNotNull(indexPriority) { "$INDEX_PRIORITY_FIELD is null" }, | ||
index = index | ||
) | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...roforelasticsearch/indexstatemanagement/step/indexpriority/AttemptSetIndexPriorityStep.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,79 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.indexstatemanagement.step.indexpriority | ||
|
||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.elasticapi.suspendUntil | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.ManagedIndexMetaData | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.action.IndexPriorityActionConfig | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.managedindexmetadata.StepMetaData | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.step.Step | ||
import org.apache.logging.log4j.LogManager | ||
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest | ||
import org.elasticsearch.action.support.master.AcknowledgedResponse | ||
import org.elasticsearch.client.Client | ||
import org.elasticsearch.cluster.service.ClusterService | ||
import org.elasticsearch.common.settings.Settings | ||
|
||
class AttemptSetIndexPriorityStep( | ||
val clusterService: ClusterService, | ||
val client: Client, | ||
val config: IndexPriorityActionConfig, | ||
managedIndexMetaData: ManagedIndexMetaData | ||
) : Step("attempt_set_index_priority", managedIndexMetaData) { | ||
|
||
private val logger = LogManager.getLogger(javaClass) | ||
private var stepStatus = StepStatus.STARTING | ||
private var info: Map<String, Any>? = null | ||
|
||
override fun isIdempotent() = true | ||
|
||
@Suppress("TooGenericExceptionCaught") | ||
override suspend fun execute() { | ||
val indexPriority = config.indexPriority | ||
try { | ||
logger.info("Executing $name on ${managedIndexMetaData.index}") | ||
val updateSettingsRequest = UpdateSettingsRequest() | ||
.indices(managedIndexMetaData.index) | ||
.settings(Settings.builder().put("index.priority", indexPriority)) | ||
val response: AcknowledgedResponse = client.admin().indices() | ||
.suspendUntil { updateSettings(updateSettingsRequest, it) } | ||
|
||
if (response.isAcknowledged) { | ||
logger.info("Successfully executed $name on ${managedIndexMetaData.index}") | ||
stepStatus = StepStatus.COMPLETED | ||
info = mapOf("message" to "Successfully set index priority to $indexPriority") | ||
} else { | ||
stepStatus = StepStatus.FAILED | ||
info = mapOf("message" to "Failed to set index priority to $indexPriority") | ||
} | ||
} catch (e: Exception) { | ||
logger.error("Failed to set index priority [index=${managedIndexMetaData.index}]", e) | ||
stepStatus = StepStatus.FAILED | ||
val mutableInfo = mutableMapOf("message" to "Failed to set index priority to $indexPriority") | ||
val errorMessage = e.message | ||
if (errorMessage != null) mutableInfo["cause"] = errorMessage | ||
info = mutableInfo.toMap() | ||
} | ||
} | ||
|
||
override fun getUpdatedManagedIndexMetaData(currentMetaData: ManagedIndexMetaData): ManagedIndexMetaData { | ||
return currentMetaData.copy( | ||
stepMetaData = StepMetaData(name, getStepStartTime().toEpochMilli(), stepStatus), | ||
transitionTo = null, | ||
info = info | ||
) | ||
} | ||
} |
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
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
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
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
63 changes: 63 additions & 0 deletions
63
...om/amazon/opendistroforelasticsearch/indexstatemanagement/action/IndexPriorityActionIT.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,63 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.indexstatemanagement.action | ||
|
||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.IndexStateManagementRestTestCase | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.Policy | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.State | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.action.IndexPriorityActionConfig | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.randomErrorNotification | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.waitFor | ||
import java.time.Instant | ||
import java.time.temporal.ChronoUnit | ||
import java.util.Locale | ||
|
||
class IndexPriorityActionIT : IndexStateManagementRestTestCase() { | ||
|
||
private val testIndexName = javaClass.simpleName.toLowerCase(Locale.ROOT) | ||
|
||
fun `test basic index priority`() { | ||
val indexName = "${testIndexName}_index_1" | ||
val policyID = "${testIndexName}_testPolicyName_1" | ||
val actionConfig = IndexPriorityActionConfig(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)) } | ||
} | ||
} |
Oops, something went wrong.