generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 130
Adds an alias action (#35) #575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e0bb3ef
Adds an alias action (#35)
goyamegh dc380a5
Enabling alias action on managed indices only
goyamegh 7989760
Merge branch 'opensearch-project:main' into main-goyamegh
goyamegh ae7021d
Add checks to aid debugging and testing steps for using alias action
goyamegh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/AliasAction.kt
This file contains hidden or 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 OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.indexmanagement.indexstatemanagement.action | ||
|
|
||
| import org.opensearch.action.admin.indices.alias.IndicesAliasesRequest | ||
| import org.opensearch.common.io.stream.StreamOutput | ||
| import org.opensearch.common.xcontent.ToXContent | ||
| import org.opensearch.common.xcontent.XContentBuilder | ||
| import org.opensearch.indexmanagement.indexstatemanagement.step.alias.AttemptAliasActionsStep | ||
| import org.opensearch.indexmanagement.spi.indexstatemanagement.Action | ||
| import org.opensearch.indexmanagement.spi.indexstatemanagement.Step | ||
| import org.opensearch.indexmanagement.spi.indexstatemanagement.model.StepContext | ||
|
|
||
| class AliasAction( | ||
| val actions: List<IndicesAliasesRequest.AliasActions>, | ||
| index: Int | ||
| ) : Action(name, index) { | ||
|
|
||
| /** | ||
| * Allowing the alias action to be only applicable on the managed index for ADD and REMOVE actions only. | ||
| * https://github.com/opensearch-project/OpenSearch/blob/4d045a164e12a382881140e32f9285a3224fecc7/server/src/main/java/org/opensearch/action/admin/indices/alias/IndicesAliasesRequest.java#L105 | ||
| */ | ||
| init { | ||
| require(actions.isNotEmpty()) { "At least one alias action needs to be specified." } | ||
| val allowedActionTypes = listOf(IndicesAliasesRequest.AliasActions.Type.ADD, IndicesAliasesRequest.AliasActions.Type.REMOVE) | ||
| require(actions.all { it.actionType() in allowedActionTypes }) { "Only ADD and REMOVE actions are allowed." } | ||
| require( | ||
| actions.all { it.indices().isNullOrEmpty() } | ||
| ) { "Alias action can only work on its applied index so don't accept index/indices parameter." } | ||
| require( | ||
| actions.all { it.aliases().isNotEmpty() } | ||
| ) { "At least one alias needs to be specified." } | ||
| } | ||
|
|
||
| private val attemptAliasActionsStep = AttemptAliasActionsStep(this) | ||
|
|
||
| private val steps = listOf(attemptAliasActionsStep) | ||
|
|
||
| override fun getStepToExecute(context: StepContext): Step { | ||
| return attemptAliasActionsStep | ||
| } | ||
|
|
||
| override fun getSteps(): List<Step> = steps | ||
|
|
||
| override fun populateAction(builder: XContentBuilder, params: ToXContent.Params) { | ||
| builder.startObject(type) | ||
| builder.field(ACTIONS, actions) | ||
| builder.endObject() | ||
| } | ||
|
|
||
| override fun populateAction(out: StreamOutput) { | ||
| out.writeList(actions) | ||
| out.writeInt(actionIndex) | ||
| } | ||
|
|
||
| companion object { | ||
| const val name = "alias" | ||
| const val ACTIONS = "actions" | ||
| } | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
...in/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/AliasActionParser.kt
This file contains hidden or 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,51 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.indexmanagement.indexstatemanagement.action | ||
|
|
||
| import org.apache.logging.log4j.LogManager | ||
| import org.opensearch.action.admin.indices.alias.IndicesAliasesRequest | ||
| import org.opensearch.common.io.stream.StreamInput | ||
| import org.opensearch.common.xcontent.XContentParser | ||
| import org.opensearch.common.xcontent.XContentParser.Token | ||
| import org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken | ||
| import org.opensearch.indexmanagement.indexstatemanagement.action.AliasAction.Companion.ACTIONS | ||
| import org.opensearch.indexmanagement.spi.indexstatemanagement.Action | ||
| import org.opensearch.indexmanagement.spi.indexstatemanagement.ActionParser | ||
|
|
||
| class AliasActionParser : ActionParser() { | ||
|
|
||
| private val logger = LogManager.getLogger(javaClass) | ||
| override fun fromStreamInput(sin: StreamInput): Action { | ||
| val actions = sin.readList(IndicesAliasesRequest::AliasActions) | ||
| val index = sin.readInt() | ||
| return AliasAction(actions, index) | ||
| } | ||
|
|
||
| override fun fromXContent(xcp: XContentParser, index: Int): Action { | ||
| val actions: MutableList<IndicesAliasesRequest.AliasActions> = mutableListOf() | ||
|
|
||
| ensureExpectedToken(Token.START_OBJECT, xcp.currentToken(), xcp) | ||
| while (xcp.nextToken() != Token.END_OBJECT) { | ||
| val fieldName = xcp.currentName() | ||
| xcp.nextToken() | ||
| when (fieldName) { | ||
| ACTIONS -> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Block
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added checks in initiate function to raise Illegal argument exception. |
||
| ensureExpectedToken(Token.START_ARRAY, xcp.currentToken(), xcp) | ||
| while (xcp.nextToken() != Token.END_ARRAY) { | ||
| actions.add(IndicesAliasesRequest.AliasActions.fromXContent(xcp)) | ||
| } | ||
| } | ||
| else -> { | ||
| logger.error("Invalid field: [$fieldName] found in AliasAction.") | ||
| throw IllegalArgumentException("Invalid field: [$fieldName] found in AliasAction.") | ||
| } | ||
| } | ||
| } | ||
| return AliasAction(actions, index) | ||
| } | ||
|
|
||
| override fun getActionType(): String = AliasAction.name | ||
| } | ||
85 changes: 85 additions & 0 deletions
85
...org/opensearch/indexmanagement/indexstatemanagement/step/alias/AttemptAliasActionsStep.kt
This file contains hidden or 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,85 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.indexmanagement.indexstatemanagement.step.alias | ||
|
|
||
| import org.apache.logging.log4j.LogManager | ||
| import org.opensearch.action.admin.indices.alias.IndicesAliasesRequest | ||
| import org.opensearch.action.support.master.AcknowledgedResponse | ||
| import org.opensearch.indexmanagement.indexstatemanagement.action.AliasAction | ||
| import org.opensearch.indexmanagement.opensearchapi.suspendUntil | ||
| import org.opensearch.indexmanagement.spi.indexstatemanagement.Step | ||
| import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ManagedIndexMetaData | ||
| import org.opensearch.indexmanagement.spi.indexstatemanagement.model.StepMetaData | ||
|
|
||
| class AttemptAliasActionsStep(private val action: AliasAction) : Step(name) { | ||
|
|
||
| private val logger = LogManager.getLogger(javaClass) | ||
| private var stepStatus = StepStatus.STARTING | ||
| private var info: Map<String, Any>? = null | ||
|
|
||
| override suspend fun execute(): Step { | ||
| val context = this.context ?: return this | ||
| val indexName = context.metadata.index | ||
| try { | ||
| val request = IndicesAliasesRequest() | ||
| action.actions.forEach { | ||
| // Applying the actions on the managed index. | ||
| it.indices(indexName) | ||
| request.addAliasAction(it) | ||
| } | ||
| val response: AcknowledgedResponse = context.client.admin().indices().suspendUntil { aliases(request, it) } | ||
| handleResponse(response, indexName, action.actions) | ||
| } catch (e: Exception) { | ||
| handleException(e, indexName, action.actions) | ||
| } | ||
| return this | ||
| } | ||
|
|
||
| private fun handleException(e: Exception, indexName: String, actions: List<IndicesAliasesRequest.AliasActions>) { | ||
| val message = getFailedMessage(indexName, actions) | ||
| logger.error(message, e) | ||
| stepStatus = StepStatus.FAILED | ||
| val mutableInfo = mutableMapOf("message" to message) | ||
| val errorMessage = e.message | ||
| if (errorMessage != null) mutableInfo["cause"] = errorMessage | ||
| info = mutableInfo.toMap() | ||
| } | ||
|
|
||
| private fun handleResponse( | ||
| response: AcknowledgedResponse, | ||
| indexName: String, | ||
| actions: List<IndicesAliasesRequest.AliasActions> | ||
| ) { | ||
| if (response.isAcknowledged) { | ||
| stepStatus = StepStatus.COMPLETED | ||
| info = mapOf("message" to getSuccessMessage(indexName)) | ||
| } else { | ||
| stepStatus = StepStatus.FAILED | ||
| info = mapOf("message" to getFailedMessage(indexName, actions)) | ||
| } | ||
| } | ||
|
|
||
| override fun getUpdatedManagedIndexMetadata(currentMetadata: ManagedIndexMetaData): ManagedIndexMetaData { | ||
| return currentMetadata.copy( | ||
| stepMetaData = StepMetaData(name, getStepStartTime(currentMetadata).toEpochMilli(), stepStatus), | ||
| transitionTo = null, | ||
| info = info | ||
| ) | ||
| } | ||
|
|
||
| override fun isIdempotent() = true | ||
|
|
||
| companion object { | ||
| val validTopContextFields = setOf("index") | ||
| const val name = "attempt_alias" | ||
| fun getFailedMessage( | ||
| index: String, | ||
| actions: List<IndicesAliasesRequest.AliasActions> | ||
| ) = "Failed to update alias [index=$index] for actions: [actions=$actions]" | ||
|
|
||
| fun getSuccessMessage(index: String) = "Successfully updated alias [index=$index]" | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.