From bb47686c4df5df2a1f1043c76a791ccfd751aba9 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Thu, 21 Jul 2022 10:38:10 -0700 Subject: [PATCH] Removed recursion from Explain Action to avoid stackoverflow in some situations (#419) (#421) Signed-off-by: Petar Dzepina (cherry picked from commit 76de9766734df2cffe8a8888c8de9959d111f18d) Co-authored-by: Petar Dzepina (cherry picked from commit b63320b64da2aa52d5e5faa98a2e4c3da7cfc707) --- .../action/explain/TransportExplainAction.kt | 80 +++++-------------- 1 file changed, 21 insertions(+), 59 deletions(-) diff --git a/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/transport/action/explain/TransportExplainAction.kt b/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/transport/action/explain/TransportExplainAction.kt index f0b084177..ee7b7a33d 100644 --- a/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/transport/action/explain/TransportExplainAction.kt +++ b/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/transport/action/explain/TransportExplainAction.kt @@ -52,6 +52,7 @@ import org.opensearch.indexmanagement.indexstatemanagement.util.MetadataCheck import org.opensearch.indexmanagement.indexstatemanagement.util.checkMetadata import org.opensearch.indexmanagement.indexstatemanagement.util.managedIndexMetadataID import org.opensearch.indexmanagement.opensearchapi.parseWithType +import org.opensearch.indexmanagement.opensearchapi.suspendUntil import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ISMIndexMetadata import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ManagedIndexMetaData import org.opensearch.indexmanagement.util.SecurityUtils.Companion.buildUser @@ -339,68 +340,29 @@ class TransportExplainAction @Inject constructor( val filteredPolicies = mutableListOf() val enabledStatus = mutableMapOf() val filteredAppliedPolicies = mutableMapOf() - filter(0, filteredIndices, filteredMetadata, filteredPolicies, enabledStatus, filteredAppliedPolicies) - } - - @Suppress("LongParameterList") - private fun filter( - current: Int, - filteredIndices: MutableList, - filteredMetadata: MutableList, - filteredPolicies: MutableList, - enabledStatus: MutableMap, - filteredAppliedPolicies: MutableMap - ) { - val request = ManagedIndexRequest().indices(indexNames[current]) - client.execute( - ManagedIndexAction.INSTANCE, - request, - object : ActionListener { - override fun onResponse(response: AcknowledgedResponse) { - filteredIndices.add(indexNames[current]) - filteredMetadata.add(indexMetadatas[current]) - filteredPolicies.add(indexPolicyIDs[current]) - enabledState[indexNames[current]]?.let { enabledStatus[indexNames[current]] = it } - appliedPolicies[indexNames[current]]?.let { filteredAppliedPolicies[indexNames[current]] = it } - if (current < indexNames.count() - 1) { - // do nothing - skip the index and go to next one - filter(current + 1, filteredIndices, filteredMetadata, filteredPolicies, enabledStatus, filteredAppliedPolicies) - } else { - sendResponse( - filteredIndices, filteredMetadata, filteredPolicies, enabledStatus, - totalManagedIndices, filteredAppliedPolicies - ) - } - } - override fun onFailure(e: Exception) { - when (e is OpenSearchSecurityException) { - true -> { - totalManagedIndices -= 1 - if (current < indexNames.count() - 1) { - // do nothing - skip the index and go to next one - filter( - current + 1, - filteredIndices, - filteredMetadata, - filteredPolicies, - enabledStatus, - filteredAppliedPolicies - ) - } else { - sendResponse( - filteredIndices, filteredMetadata, filteredPolicies, enabledStatus, - totalManagedIndices, filteredAppliedPolicies - ) - } - } - false -> { - actionListener.onFailure(e) - } - } + CoroutineScope(Dispatchers.IO).launch { + // filter out indicies for which user doesn't have manage index permissions + for (i in 0 until indexNames.count()) { + val request = ManagedIndexRequest().indices(indexNames[i]) + try { + client.suspendUntil { execute(ManagedIndexAction.INSTANCE, request, it) } + filteredIndices.add(indexNames[i]) + filteredMetadata.add(indexMetadatas[i]) + filteredPolicies.add(indexPolicyIDs[i]) + enabledState[indexNames[i]]?.let { enabledStatus[indexNames[i]] = it } + appliedPolicies[indexNames[i]]?.let { filteredAppliedPolicies[indexNames[i]] = it } + } catch (e: OpenSearchSecurityException) { + totalManagedIndices -= 1 + } catch (e: Exception) { + actionListener.onFailure(e) } } - ) + sendResponse( + filteredIndices, filteredMetadata, filteredPolicies, enabledStatus, + totalManagedIndices, filteredAppliedPolicies + ) + } } @Suppress("LongParameterList")