diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/DocumentLevelMonitorRunner.kt b/alerting/src/main/kotlin/org/opensearch/alerting/DocumentLevelMonitorRunner.kt index 8a961b3b9..05500fb3e 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/DocumentLevelMonitorRunner.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/DocumentLevelMonitorRunner.kt @@ -193,7 +193,7 @@ class DocumentLevelMonitorRunner : MonitorRunner() { indexLastRunContext.toMutableMap(), concreteIndexName, shardCount - ) as MutableMap + ) if (IndexUtils.isAlias(indexName, monitorCtx.clusterService!!.state()) || IndexUtils.isDataStream(indexName, monitorCtx.clusterService!!.state()) ) { diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/MonitorFanOutUtils.kt b/alerting/src/main/kotlin/org/opensearch/alerting/MonitorFanOutUtils.kt index 294d154d9..69a4e0359 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/MonitorFanOutUtils.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/MonitorFanOutUtils.kt @@ -59,7 +59,7 @@ fun initializeNewLastRunContext( lastRunContext: Map, index: String, shardCount: Int, -): Map { +): MutableMap { val updatedLastRunContext = lastRunContext.toMutableMap() // Only initialize shards that don't already have a sequence number diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/MonitorMetadataService.kt b/alerting/src/main/kotlin/org/opensearch/alerting/MonitorMetadataService.kt index d1883a3f2..21958a812 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/MonitorMetadataService.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/MonitorMetadataService.kt @@ -206,9 +206,12 @@ object MonitorMetadataService : else if (monitor.monitorType.endsWith(Monitor.MonitorType.DOC_LEVEL_MONITOR.value)) (monitor.inputs[0] as RemoteDocLevelMonitorInput).docLevelMonitorInput.indices[0] else null - val runContext = if (monitor.monitorType.endsWith(Monitor.MonitorType.DOC_LEVEL_MONITOR.value)) - createFullRunContext(monitorIndex, metadata.lastRunContext as MutableMap>) - else null + val runContext = if (monitor.monitorType.endsWith(Monitor.MonitorType.DOC_LEVEL_MONITOR.value)) { + @Suppress("UNCHECKED_CAST") + val lastRunCtx = if (metadata.lastRunContext.isEmpty()) null + else (metadata.lastRunContext as MutableMap>) + createFullRunContext(monitorIndex, lastRunCtx) + } else null return if (runContext != null) { metadata.copy( lastRunContext = runContext diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/remote/monitors/RemoteDocumentLevelMonitorRunner.kt b/alerting/src/main/kotlin/org/opensearch/alerting/remote/monitors/RemoteDocumentLevelMonitorRunner.kt index c12356cab..7b35bb041 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/remote/monitors/RemoteDocumentLevelMonitorRunner.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/remote/monitors/RemoteDocumentLevelMonitorRunner.kt @@ -126,7 +126,7 @@ class RemoteDocumentLevelMonitorRunner : MonitorRunner() { indexLastRunContext.toMutableMap(), concreteIndexName, shardCount - ) as MutableMap + ) if (IndexUtils.isAlias(indexName, monitorCtx.clusterService!!.state()) || IndexUtils.isDataStream(indexName, monitorCtx.clusterService!!.state()) ) { diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt b/alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt index 455d9e5ec..02df99dca 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt @@ -228,18 +228,18 @@ class DocLevelMonitorQueries(private val client: Client, private val clusterServ // Compute full path relative to root val fullPath = if (currentPath.isEmpty()) it.key else "$currentPath.${it.key}" - val nodeProps = it.value as MutableMap + val nodeProps = (it.value as Map).toMutableMap() // If it has type property and type is not "nested" then this is a leaf if (nodeProps.containsKey(TYPE) && nodeProps[TYPE] != NESTED) { // At this point we know full path of node, so we add it to output array flattenPaths.put(fullPath, nodeProps) // Calls processLeafFn and gets old node name, new node name and new properties of node. // This is all information we need to update this node - val (oldName, newName, props) = processLeafFn(it.key, fullPath, it.value as MutableMap) + val (oldName, newName, props) = processLeafFn(it.key, fullPath, (it.value as Map).toMutableMap()) newNodes.add(Triple(oldName, newName, props)) } else if (nodeProps.containsKey(PROPERTIES) && nodeProps[PROPERTIES] != null) { // Internal(non-leaf) node - visit children - traverseMappingsAndUpdate(nodeProps[PROPERTIES] as MutableMap, fullPath, processLeafFn, flattenPaths) + traverseMappingsAndUpdate((nodeProps[PROPERTIES] as Map).toMutableMap(), fullPath, processLeafFn, flattenPaths) } } // Here we can update all processed leaves in tree