diff --git a/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/DefaultIndexMetadataService.kt b/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/DefaultIndexMetadataService.kt index 724e18f73..fb6fa43c8 100644 --- a/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/DefaultIndexMetadataService.kt +++ b/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/DefaultIndexMetadataService.kt @@ -9,6 +9,7 @@ import org.opensearch.action.admin.cluster.state.ClusterStateRequest import org.opensearch.action.admin.cluster.state.ClusterStateResponse import org.opensearch.action.support.IndicesOptions import org.opensearch.client.Client +import org.opensearch.cluster.metadata.IndexMetadata import org.opensearch.cluster.service.ClusterService import org.opensearch.common.unit.TimeValue import org.opensearch.indexmanagement.opensearchapi.suspendUntil @@ -38,11 +39,7 @@ class DefaultIndexMetadataService(val customUUIDSetting: String? = null) : Index response.state.metadata.indices.forEach { // TODO waiting to add document count until it is definitely needed - val uuid = if (customUUIDSetting != null) { - it.value.settings.get(customUUIDSetting, it.value.indexUUID) - } else { - it.value.indexUUID - } + val uuid = getCustomIndexUUID(it.value) val indexMetadata = ISMIndexMetadata(uuid, it.value.creationDate, -1) indexNameToMetadata[it.key] = indexMetadata } @@ -50,6 +47,19 @@ class DefaultIndexMetadataService(val customUUIDSetting: String? = null) : Index return indexNameToMetadata } + /* + * If an extension wants Index Management to determine cluster state indices UUID based on a custom index setting if + * present of cluster state, the extension will override this customUUID setting. This allows an index to migrate off + * cluster and back while using this persistent uuid. + */ + fun getCustomIndexUUID(indexMetadata: IndexMetadata): String { + return if (customUUIDSetting != null) { + indexMetadata.settings.get(customUUIDSetting, indexMetadata.indexUUID) + } else { + indexMetadata.indexUUID + } + } + override suspend fun getMetadataForAllIndices(client: Client, clusterService: ClusterService): Map { return getMetadata(listOf("*"), client, clusterService) } diff --git a/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt b/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt index 3ab50b46a..1e53c1657 100644 --- a/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt +++ b/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt @@ -245,10 +245,15 @@ object ManagedIndexRunner : return } - var indexMetadata = getIndexMetadata(managedIndexConfig.index) - // The index uuids not matching can happen when two jobs share a name but have different index types - if (indexMetadata == null || indexMetadata.indexUUID != managedIndexConfig.indexUuid) { - indexMetadata = null + // Check the cluster state for the index metadata + var clusterStateIndexMetadata = getIndexMetadata(managedIndexConfig.index) + val defaultIndexMetadataService = indexMetadataProvider.services[DEFAULT_INDEX_TYPE] as DefaultIndexMetadataService + val clusterStateIndexUUID = clusterStateIndexMetadata?.let { defaultIndexMetadataService.getCustomIndexUUID(it) } + // If the index metadata is null, the index is not in the cluster state. If the index metadata is not null, but + // the cluster state index uuid differs from the one in the managed index config then the config is referring + // to a different index which does not exist in the cluster. We need to check all of the extensions to confirm an index exists + if (clusterStateIndexMetadata == null || clusterStateIndexUUID != managedIndexConfig.indexUuid) { + clusterStateIndexMetadata = null // If the cluster state/default index type didn't have an index with a matching name and uuid combination, try all other index types val nonDefaultIndexTypes = indexMetadataProvider.services.keys.filter { it != DEFAULT_INDEX_TYPE } val multiTypeIndexNameToMetaData = @@ -262,7 +267,7 @@ object ManagedIndexRunner : return } } else { - val clusterStateMetadata = indexMetadata.getManagedIndexMetadata() + val clusterStateMetadata = clusterStateIndexMetadata.getManagedIndexMetadata() val metadataCheck = checkMetadata(clusterStateMetadata, managedIndexMetaData, managedIndexConfig.indexUuid, logger) if (metadataCheck != MetadataCheck.SUCCESS) { logger.info("Skipping execution while metadata status is $metadataCheck")