From 6ac125cabbbed5cfb3b37c6f5dc328058c3ee0f2 Mon Sep 17 00:00:00 2001 From: Chase Engelbrecht Date: Tue, 19 Dec 2023 22:41:36 -0800 Subject: [PATCH 1/2] Don't attempt to parse workflow if it doesn't exist Signed-off-by: Chase Engelbrecht --- .../TransportDeleteWorkflowAction.kt | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteWorkflowAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteWorkflowAction.kt index 9b076a600..3aee9b2c0 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteWorkflowAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteWorkflowAction.kt @@ -60,6 +60,7 @@ import org.opensearch.index.reindex.DeleteByQueryRequestBuilder import org.opensearch.search.builder.SearchSourceBuilder import org.opensearch.tasks.Task import org.opensearch.transport.TransportService +import java.util.Optional private val scope: CoroutineScope = CoroutineScope(Dispatchers.IO) /** @@ -119,7 +120,12 @@ class TransportDeleteWorkflowAction @Inject constructor( ) { suspend fun resolveUserAndStart() { try { - val workflow = getWorkflow() + val optionalWorkflow = getWorkflow() + if (optionalWorkflow.isEmpty) { + return + } + + val workflow = optionalWorkflow.get() val canDelete = user == null || !doFilterForUser(user) || @@ -296,17 +302,27 @@ class TransportDeleteWorkflowAction @Inject constructor( return deletableMonitors } - private suspend fun getWorkflow(): Workflow { + private suspend fun getWorkflow(): Optional { val getRequest = GetRequest(ScheduledJob.SCHEDULED_JOBS_INDEX, workflowId) val getResponse: GetResponse = client.suspendUntil { get(getRequest, it) } - if (getResponse.isExists == false) { - actionListener.onFailure( - AlertingException.wrap( - OpenSearchStatusException("Workflow not found.", RestStatus.NOT_FOUND) - ) - ) + if (!getResponse.isExists) { + handleWorkflowMissing() + return Optional.empty() } + + return Optional.of(parseWorkflow(getResponse)) + } + + private fun handleWorkflowMissing() { + actionListener.onFailure( + AlertingException.wrap( + OpenSearchStatusException("Workflow not found.", RestStatus.NOT_FOUND) + ) + ) + } + + private fun parseWorkflow(getResponse: GetResponse): Workflow { val xcp = XContentHelper.createParser( xContentRegistry, LoggingDeprecationHandler.INSTANCE, getResponse.sourceAsBytesRef, XContentType.JSON From d4ba8de352283dfe0bacf0ee1a573ebac2e9161b Mon Sep 17 00:00:00 2001 From: Chase Engelbrecht Date: Wed, 3 Jan 2024 13:46:55 -0800 Subject: [PATCH 2/2] Use kotlin syntax instead of optional Signed-off-by: Chase Engelbrecht --- .../transport/TransportDeleteWorkflowAction.kt | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteWorkflowAction.kt b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteWorkflowAction.kt index 3aee9b2c0..5a9938f56 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteWorkflowAction.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportDeleteWorkflowAction.kt @@ -60,7 +60,6 @@ import org.opensearch.index.reindex.DeleteByQueryRequestBuilder import org.opensearch.search.builder.SearchSourceBuilder import org.opensearch.tasks.Task import org.opensearch.transport.TransportService -import java.util.Optional private val scope: CoroutineScope = CoroutineScope(Dispatchers.IO) /** @@ -120,12 +119,7 @@ class TransportDeleteWorkflowAction @Inject constructor( ) { suspend fun resolveUserAndStart() { try { - val optionalWorkflow = getWorkflow() - if (optionalWorkflow.isEmpty) { - return - } - - val workflow = optionalWorkflow.get() + val workflow: Workflow = getWorkflow() ?: return val canDelete = user == null || !doFilterForUser(user) || @@ -302,16 +296,16 @@ class TransportDeleteWorkflowAction @Inject constructor( return deletableMonitors } - private suspend fun getWorkflow(): Optional { + private suspend fun getWorkflow(): Workflow? { val getRequest = GetRequest(ScheduledJob.SCHEDULED_JOBS_INDEX, workflowId) val getResponse: GetResponse = client.suspendUntil { get(getRequest, it) } if (!getResponse.isExists) { handleWorkflowMissing() - return Optional.empty() + return null } - return Optional.of(parseWorkflow(getResponse)) + return parseWorkflow(getResponse) } private fun handleWorkflowMissing() {