/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ package org.opensearch.indexmanagement.indexstatemanagement.step.readwrite import org.apache.logging.log4j.LogManager import org.opensearch.ExceptionsHelper import org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest import org.opensearch.action.support.master.AcknowledgedResponse import org.opensearch.cluster.metadata.IndexMetadata.SETTING_BLOCKS_WRITE import org.opensearch.common.settings.Settings 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 import org.opensearch.transport.RemoteTransportException class SetReadWriteStep : Step(name) { private val logger = LogManager.getLogger(javaClass) private var stepStatus = StepStatus.STARTING private var info: Map? = null override suspend fun execute(): Step { val context = this.context ?: return this val indexName = context.metadata.index try { val updateSettingsRequest = UpdateSettingsRequest() .indices(indexName) .settings( Settings.builder().put(SETTING_BLOCKS_WRITE, false) ) val response: AcknowledgedResponse = context.client.admin().indices() .suspendUntil { updateSettings(updateSettingsRequest, it) } if (response.isAcknowledged) { stepStatus = StepStatus.COMPLETED info = mapOf("message" to getSuccessMessage(indexName)) } else { val message = getFailedMessage(indexName) logger.warn(message) stepStatus = StepStatus.FAILED info = mapOf("message" to message) } } catch (e: RemoteTransportException) { handleException(indexName, ExceptionsHelper.unwrapCause(e) as Exception) } catch (e: Exception) { handleException(indexName, e) } return this } private fun handleException(indexName: String, e: Exception) { val message = getFailedMessage(indexName) 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() } override fun getUpdatedManagedIndexMetadata(currentMetadata: ManagedIndexMetaData): ManagedIndexMetaData { return currentMetadata.copy( stepMetaData = StepMetaData(name, getStepStartTime(currentMetadata).toEpochMilli(), stepStatus), transitionTo = null, info = info ) } override fun isIdempotent(): Boolean = true companion object { const val name = "set_read_write" fun getFailedMessage(index: String) = "Failed to set index to read-write [index=$index]" fun getSuccessMessage(index: String) = "Successfully set index to read-write [index=$index]" } }