|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.indexmanagement.indexstatemanagement.step.stopreplication |
| 7 | + |
| 8 | +import org.apache.logging.log4j.LogManager |
| 9 | +import org.opensearch.ExceptionsHelper |
| 10 | +import org.opensearch.action.support.master.AcknowledgedResponse |
| 11 | +import org.opensearch.commons.replication.ReplicationPluginInterface |
| 12 | +import org.opensearch.commons.replication.action.StopIndexReplicationRequest |
| 13 | +import org.opensearch.indexmanagement.opensearchapi.suspendUntil |
| 14 | +import org.opensearch.indexmanagement.spi.indexstatemanagement.Step |
| 15 | +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ManagedIndexMetaData |
| 16 | +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.StepMetaData |
| 17 | +import org.opensearch.snapshots.SnapshotInProgressException |
| 18 | +import org.opensearch.transport.RemoteTransportException |
| 19 | + |
| 20 | +class AttemptStopReplicationStep : Step(name) { |
| 21 | + private val logger = LogManager.getLogger(javaClass) |
| 22 | + private var stepStatus = StepStatus.STARTING |
| 23 | + private var info: Map<String, Any>? = null |
| 24 | + private var replicationPluginInterface: ReplicationPluginInterface = ReplicationPluginInterface() |
| 25 | + fun setReplicationPluginInterface(replicationPluginInterface: ReplicationPluginInterface) { |
| 26 | + this.replicationPluginInterface = replicationPluginInterface |
| 27 | + } |
| 28 | + |
| 29 | + override suspend fun execute(): Step { |
| 30 | + val context = this.context ?: return this |
| 31 | + val indexName = context.metadata.index |
| 32 | + try { |
| 33 | + val stopIndexReplicationRequestObj = StopIndexReplicationRequest(indexName) |
| 34 | + val response: AcknowledgedResponse = context.client.admin().indices().suspendUntil { |
| 35 | + replicationPluginInterface.stopReplication( |
| 36 | + context.client, |
| 37 | + stopIndexReplicationRequestObj, |
| 38 | + it, |
| 39 | + ) |
| 40 | + } |
| 41 | + if (response.isAcknowledged) { |
| 42 | + stepStatus = StepStatus.COMPLETED |
| 43 | + info = mapOf("message" to getSuccessMessage(indexName)) |
| 44 | + } else { |
| 45 | + val message = getFailedMessage(indexName) |
| 46 | + logger.warn(message) |
| 47 | + stepStatus = StepStatus.FAILED |
| 48 | + info = mapOf("message" to message) |
| 49 | + } |
| 50 | + } catch (e: RemoteTransportException) { |
| 51 | + val cause = ExceptionsHelper.unwrapCause(e) |
| 52 | + if (cause is SnapshotInProgressException) { |
| 53 | + handleSnapshotException(indexName, cause) |
| 54 | + } else { |
| 55 | + handleException(indexName, cause as Exception) |
| 56 | + } |
| 57 | + } catch (e: SnapshotInProgressException) { |
| 58 | + handleSnapshotException(indexName, e) |
| 59 | + } catch (e: Exception) { |
| 60 | + handleException(indexName, e) |
| 61 | + } |
| 62 | + return this |
| 63 | + } |
| 64 | + |
| 65 | + private fun handleSnapshotException(indexName: String, e: SnapshotInProgressException) { |
| 66 | + val message = getSnapshotMessage(indexName) |
| 67 | + logger.error(message, e) |
| 68 | + stepStatus = StepStatus.FAILED |
| 69 | + info = mapOf("message" to message) |
| 70 | + } |
| 71 | + |
| 72 | + private fun handleException(indexName: String, e: Exception) { |
| 73 | + val message = getFailedMessage(indexName) |
| 74 | + logger.error(message, e) |
| 75 | + stepStatus = StepStatus.FAILED |
| 76 | + val mutableInfo = mutableMapOf("message" to message) |
| 77 | + val errorMessage = e.message |
| 78 | + if (errorMessage != null) mutableInfo["cause"] = errorMessage |
| 79 | + info = mutableInfo.toMap() |
| 80 | + } |
| 81 | + |
| 82 | + override fun getUpdatedManagedIndexMetadata(currentMetadata: ManagedIndexMetaData): ManagedIndexMetaData = currentMetadata.copy( |
| 83 | + stepMetaData = StepMetaData(name, getStepStartTime(currentMetadata).toEpochMilli(), stepStatus), |
| 84 | + transitionTo = null, |
| 85 | + info = info, |
| 86 | + ) |
| 87 | + |
| 88 | + override fun isIdempotent() = false |
| 89 | + |
| 90 | + companion object { |
| 91 | + const val name = "attempt_stop_replication" |
| 92 | + |
| 93 | + fun getFailedMessage(index: String) = "Failed to stop replication [index=$index]" |
| 94 | + |
| 95 | + fun getSuccessMessage(index: String) = "Successfully stopped replication [index=$index]" |
| 96 | + |
| 97 | + fun getSnapshotMessage(index: String) = "Index had snapshot in progress, retrying stop replication [index=$index]" |
| 98 | + } |
| 99 | +} |
0 commit comments