override suspend fun execute()

in src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/transition/AttemptTransitionStep.kt [51:127]


    override suspend fun execute(): AttemptTransitionStep {
        try {
            if (config.transitions.isEmpty()) {
                logger.info("$indexName transitions are empty, completing policy")
                policyCompleted = true
                stepStatus = StepStatus.COMPLETED
                return this
            }

            val indexMetaData = clusterService.state().metadata().index(indexName)
            val indexCreationDate = indexMetaData.creationDate
            val indexCreationDateInstant = Instant.ofEpochMilli(indexCreationDate)
            if (indexCreationDate == -1L) {
                logger.warn("$indexName had an indexCreationDate=-1L, cannot use for comparison")
            }
            val stepStartTime = getStepStartTime()
            var numDocs: Long? = null
            var indexSize: ByteSizeValue? = null
            val rolloverDate: Instant? = indexMetaData.getOldestRolloverTime()

            if (config.transitions.any { it.conditions?.rolloverAge !== null }) {
                // if we have a transition with rollover age condition, then we must have a rollover date
                // otherwise fail this transition
                if (rolloverDate == null) {
                    val message = getFailedRolloverDateMessage(indexName)
                    logger.warn(message)
                    stepStatus = StepStatus.FAILED
                    info = mapOf("message" to message)
                    return this
                }
            }

            if (config.transitions.any { it.hasStatsConditions() }) {
                val statsRequest = IndicesStatsRequest()
                    .indices(indexName).clear().docs(true)
                val statsResponse: IndicesStatsResponse = client.admin().indices().suspendUntil { stats(statsRequest, it) }

                if (statsResponse.status != RestStatus.OK) {
                    val message = getFailedStatsMessage(indexName)
                    logger.warn("$message - ${statsResponse.status}")
                    stepStatus = StepStatus.FAILED
                    info = mapOf(
                        "message" to message,
                        "shard_failures" to statsResponse.shardFailures.map { it.getUsefulCauseString() }
                    )
                    return this
                }
                numDocs = statsResponse.primaries.getDocs()?.count ?: 0
                indexSize = ByteSizeValue(statsResponse.primaries.getDocs()?.totalSizeInBytes ?: 0)
            }

            // Find the first transition that evaluates to true and get the state to transition to, otherwise return null if none are true
            stateName = config.transitions.find {
                it.evaluateConditions(indexCreationDateInstant, numDocs, indexSize, stepStartTime, rolloverDate)
            }?.stateName
            val message: String
            val stateName = stateName // shadowed on purpose to prevent var from changing
            if (stateName != null) {
                logger.info(
                    "$indexName transition conditions evaluated to true [indexCreationDate=$indexCreationDate," +
                        " numDocs=$numDocs, indexSize=${indexSize?.bytes},stepStartTime=${stepStartTime.toEpochMilli()}]"
                )
                stepStatus = StepStatus.COMPLETED
                message = getSuccessMessage(indexName, stateName)
            } else {
                stepStatus = StepStatus.CONDITION_NOT_MET
                message = getEvaluatingMessage(indexName)
            }
            info = mapOf("message" to message)
        } catch (e: RemoteTransportException) {
            handleException(ExceptionsHelper.unwrapCause(e) as Exception)
        } catch (e: Exception) {
            handleException(e)
        }

        return this
    }