Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't attempt to parse workflow if it doesn't exist #1346

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
/**
Expand Down Expand Up @@ -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) ||
Expand Down Expand Up @@ -296,17 +302,27 @@ class TransportDeleteWorkflowAction @Inject constructor(
return deletableMonitors
}

private suspend fun getWorkflow(): Workflow {
private suspend fun getWorkflow(): Optional<Workflow> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the workflow object is already nullable in kotlin. so, we do not need to use java.util.Optional here. https://typealias.com/guides/java-optionals-and-kotlin-nulls/
can we adjust this?

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
Expand Down
Loading