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.
…opensearch-project#589) * Adds an alias action (opensearch-project#35) Signed-off-by: Megha Goyal <[email protected]> Co-authored-by: Megha Goyal <[email protected]>
- Loading branch information
1 parent
2ee5bec
commit 2c8160a
Showing
11 changed files
with
611 additions
and
3 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
63 changes: 63 additions & 0 deletions
63
src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/AliasAction.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 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 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 -> { | ||
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 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 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
Oops, something went wrong.