Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Remove PolicyID settings (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenlan-amzn authored Feb 1, 2021
1 parent c5054c5 commit cf7881f
Show file tree
Hide file tree
Showing 21 changed files with 508 additions and 477 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.amazon.opendistroforelasticsearch.indexmanagement.elasticapi.parseWit
import com.amazon.opendistroforelasticsearch.indexmanagement.indexstatemanagement.transport.action.updateindexmetadata.UpdateManagedIndexMetaDataAction
import com.amazon.opendistroforelasticsearch.indexmanagement.indexstatemanagement.transport.action.updateindexmetadata.UpdateManagedIndexMetaDataRequest
import com.amazon.opendistroforelasticsearch.indexmanagement.indexstatemanagement.elasticapi.getManagedIndexMetaData
import com.amazon.opendistroforelasticsearch.indexmanagement.indexstatemanagement.elasticapi.getPolicyID
import com.amazon.opendistroforelasticsearch.indexmanagement.elasticapi.retry
import com.amazon.opendistroforelasticsearch.indexmanagement.elasticapi.string
import com.amazon.opendistroforelasticsearch.indexmanagement.elasticapi.suspendUntil
Expand Down Expand Up @@ -212,13 +211,6 @@ object ManagedIndexRunner : ScheduledJobRunner,
return
}

// If there is a mismatch between _settings policy_id and job policy_id it means the user
// tried to change it using the _settings API or we failed to update during ChangePolicy
// so we will attempt to self heal and disallow the use of _settings API to modify policy_id
if (indexMetaData.getPolicyID() != managedIndexConfig.policyID) {
updateIndexPolicyIDSetting(managedIndexConfig.index, managedIndexConfig.policyID)
}

// If the policy was completed or failed then return early and disable job so it stops scheduling work
if (managedIndexMetaData.policyCompleted == true || managedIndexMetaData.isFailed) {
disableManagedIndexConfig(managedIndexConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,83 +21,24 @@ import com.amazon.opendistroforelasticsearch.indexmanagement.elasticapi.parseWit
import com.amazon.opendistroforelasticsearch.indexmanagement.indexstatemanagement.model.ISMTemplate
import com.amazon.opendistroforelasticsearch.indexmanagement.indexstatemanagement.model.ManagedIndexMetaData
import com.amazon.opendistroforelasticsearch.indexmanagement.indexstatemanagement.model.Policy
import com.amazon.opendistroforelasticsearch.indexmanagement.indexstatemanagement.model.coordinator.ClusterStateManagedIndexConfig
import com.amazon.opendistroforelasticsearch.indexmanagement.indexstatemanagement.settings.ManagedIndexSettings
import org.elasticsearch.cluster.ClusterState
import org.elasticsearch.action.search.SearchResponse
import org.elasticsearch.cluster.metadata.IndexMetadata
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler
import org.elasticsearch.common.xcontent.NamedXContentRegistry
import org.elasticsearch.common.xcontent.XContentFactory
import org.elasticsearch.common.xcontent.XContentType

/**
* Compares current and previous IndexMetaData to determine if we should create [ManagedIndexConfig].
*
* If [getPolicyID] returns null then we should not create a [ManagedIndexConfig].
* Else if the previous IndexMetaData is null then it means this is a newly created index that should be managed.
* Else if the previous IndexMetaData's [getPolicyID] is null then this is an existing index that had
* a policy_id added to it.
*
* @param previousIndexMetaData the previous [IndexMetaData].
* @return whether a [ManagedIndexConfig] should be created.
*/
fun IndexMetadata.shouldCreateManagedIndexConfig(previousIndexMetaData: IndexMetadata?): Boolean {
if (this.getPolicyID() == null) return false

return previousIndexMetaData?.getPolicyID() == null
}

/**
* Compares current and previous IndexMetadata to determine if we should delete [ManagedIndexConfig].
*
* If the previous IndexMetadata is null or its [getPolicyID] returns null then there should
* be no [ManagedIndexConfig] to delete. Else if the current [getPolicyID] returns null
* then it means we should delete the existing [ManagedIndexConfig].
*
* @param previousIndexMetaData the previous [IndexMetadata].
* @return whether a [ManagedIndexConfig] should be deleted.
*/
fun IndexMetadata.shouldDeleteManagedIndexConfig(previousIndexMetaData: IndexMetadata?): Boolean {
if (previousIndexMetaData?.getPolicyID() == null) return false

return this.getPolicyID() == null
}

/**
* Checks to see if the [ManagedIndexMetaData] should be removed.
*
* If [getPolicyID] returns null but [ManagedIndexMetaData] is not null then the policy was removed and
* the [ManagedIndexMetaData] remains and should be removed.
*/
fun IndexMetadata.shouldDeleteManagedIndexMetaData(): Boolean =
this.getPolicyID() == null && this.getManagedIndexMetaData() != null

/**
* Returns the current policy_id if it exists and is valid otherwise returns null.
* */
fun IndexMetadata.getPolicyID(): String? {
if (this.settings.get(ManagedIndexSettings.POLICY_ID.key).isNullOrBlank()) return null

return this.settings.get(ManagedIndexSettings.POLICY_ID.key)
}

/**
* Returns the current rollover_alias if it exists otherwise returns null.
* */
*/
fun IndexMetadata.getRolloverAlias(): String? {
if (this.settings.get(ManagedIndexSettings.ROLLOVER_ALIAS.key).isNullOrBlank()) return null

return this.settings.get(ManagedIndexSettings.ROLLOVER_ALIAS.key)
}

fun IndexMetadata.getClusterStateManagedIndexConfig(): ClusterStateManagedIndexConfig? {
val index = this.index.name
val uuid = this.index.uuid
val policyID = this.getPolicyID() ?: return null

return ClusterStateManagedIndexConfig(index = index, uuid = uuid, policyID = policyID)
}

fun IndexMetadata.getManagedIndexMetaData(): ManagedIndexMetaData? {
val existingMetaDataMap = this.getCustomData(ManagedIndexMetaData.MANAGED_INDEX_METADATA)

Expand All @@ -107,6 +48,18 @@ fun IndexMetadata.getManagedIndexMetaData(): ManagedIndexMetaData? {
return null
}

fun getUuidsForClosedIndices(state: ClusterState): MutableList<String> {
val indexMetadatas = state.metadata.indices
val closeList = mutableListOf<String>()
indexMetadatas.forEach {
// it.key is index name
if (it.value.state == IndexMetadata.State.CLOSE) {
closeList.add(it.value.indexUUID)
}
}
return closeList
}

/**
* Do a exists search query to retrieve all policy with ism_template field
* parse search response with this function
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,5 @@ class RestChangePolicyAction : BaseRestHandler() {
const val CHANGE_POLICY_BASE_URI = "$ISM_BASE_URI/change_policy"
const val INDEX_NOT_MANAGED = "This index is not being managed"
const val INDEX_IN_TRANSITION = "Cannot change policy while transitioning to new state"
const val INDEX_NOT_INITIALIZED = "This managed index has not been initialized yet"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import java.io.IOException
class AddPolicyRequest : ActionRequest {

val indices: List<String>
val policyID: String?
val policyID: String

constructor(
indices: List<String>,
policyID: String?
policyID: String
) : super() {
this.indices = indices
this.policyID = policyID
Expand All @@ -43,8 +43,8 @@ class AddPolicyRequest : ActionRequest {

override fun validate(): ActionRequestValidationException? {
var validationException: ActionRequestValidationException? = null
if (indices.isEmpty() || policyID.isNullOrBlank()) {
validationException = ValidateActions.addValidationError("Missing indices or policyID", validationException)
if (indices.isEmpty()) {
validationException = ValidateActions.addValidationError("Missing indices", validationException)
}
return validationException
}
Expand Down
Loading

0 comments on commit cf7881f

Please sign in to comment.