Skip to content

Commit

Permalink
throw exception only if all fanouts failed
Browse files Browse the repository at this point in the history
Signed-off-by: Subhobrata Dey <sbcd90@gmail.com>
  • Loading branch information
sbcd90 committed Apr 16, 2024
1 parent fd5c3e5 commit 7356f94
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ class DocumentLevelMonitorRunner : MonitorRunner() {
docLevelMonitorFanOutResponses.addAll(responses)
}
}

val isFanOutSuccessful = checkAndThrowExceptionIfAllFanOutsFailed(docLevelMonitorFanOutResponses)
if (isFanOutSuccessful != null) {
throw isFanOutSuccessful
}
updateLastRunContextFromFanOutResponses(docLevelMonitorFanOutResponses, updatedLastRunContext)
val triggerResults = buildTriggerResults(docLevelMonitorFanOutResponses)
val inputRunResults = buildInputRunResults(docLevelMonitorFanOutResponses)
Expand Down Expand Up @@ -415,6 +420,20 @@ class DocumentLevelMonitorRunner : MonitorRunner() {
}
}

private fun checkAndThrowExceptionIfAllFanOutsFailed(
docLevelMonitorFanOutResponses: MutableList<DocLevelMonitorFanOutResponse>
): AlertingException? {
val exceptions = mutableListOf<AlertingException>()
for (res in docLevelMonitorFanOutResponses) {
if (res.exception == null) {
return null
} else {
exceptions.add(res.exception)
}
}
return AlertingException.merge(*exceptions.toTypedArray())
}

private fun buildTriggerResults(
docLevelMonitorFanOutResponses: MutableList<DocLevelMonitorFanOutResponse>,
): MutableMap<String, DocumentLevelTriggerRunResult> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TriggerExpressionResolverTests : OpenSearchTestCase() {
val equation = TriggerExpressionParser(eqString).parse()
val queryToDocIds = mutableMapOf<DocLevelQuery, Set<String>>()
queryToDocIds[DocLevelQuery("", "sigma-123", listOf(), "", emptyList())] = mutableSetOf("6", "3", "7")
queryToDocIds[DocLevelQuery("id1456", "", listOf(), "", emptyList())] = mutableSetOf("1", "2", "3")
queryToDocIds[DocLevelQuery("id1456", "sigma-456", listOf(), "", emptyList())] = mutableSetOf("1", "2", "3")
Assert.assertEquals("query[name=sigma-123] query[id=id1456] && ", equation.toString())
Assert.assertEquals(mutableSetOf("3"), equation.evaluate(queryToDocIds))
}
Expand All @@ -37,7 +37,7 @@ class TriggerExpressionResolverTests : OpenSearchTestCase() {
val equation = TriggerExpressionParser(eqString).parse()
val queryToDocIds = mutableMapOf<DocLevelQuery, Set<String>>()
queryToDocIds[DocLevelQuery("", "sigma-123", listOf(), "", emptyList())] = mutableSetOf("6", "8", "7")
queryToDocIds[DocLevelQuery("", "", listOf(), "", mutableListOf("tag=sev2"))] = mutableSetOf("1", "2", "3")
queryToDocIds[DocLevelQuery("", "sigma-456", listOf(), "", mutableListOf("tag=sev2"))] = mutableSetOf("1", "2", "3")
Assert.assertEquals("query[name=sigma-123] query[tag=sev2] && ", equation.toString())
Assert.assertEquals(emptySet<String>(), equation.evaluate(queryToDocIds))
}
Expand All @@ -57,7 +57,7 @@ class TriggerExpressionResolverTests : OpenSearchTestCase() {
val equation = TriggerExpressionParser(eqString).parse()
val queryToDocIds = mutableMapOf<DocLevelQuery, Set<String>>()
queryToDocIds[DocLevelQuery("", "sigma-123", listOf(), "", emptyList())] = mutableSetOf("6", "3", "7")
queryToDocIds[DocLevelQuery("id1456", "", listOf(), "", emptyList())] = mutableSetOf("1", "2", "3")
queryToDocIds[DocLevelQuery("id1456", "sigma-456", listOf(), "", emptyList())] = mutableSetOf("1", "2", "3")
Assert.assertEquals("query[name=sigma-123] query[id=id1456] || ", equation.toString())
Assert.assertEquals(mutableSetOf("6", "3", "7", "1", "2", "3"), equation.evaluate(queryToDocIds))
}
Expand All @@ -67,7 +67,7 @@ class TriggerExpressionResolverTests : OpenSearchTestCase() {
val equation = TriggerExpressionParser(eqString).parse()
val queryToDocIds = mutableMapOf<DocLevelQuery, Set<String>>()
queryToDocIds[DocLevelQuery("", "sigma-123", listOf(), "", emptyList())] = mutableSetOf("6", "8", "7")
queryToDocIds[DocLevelQuery("", "", listOf(), "", mutableListOf("tag=sev2"))] = emptySet()
queryToDocIds[DocLevelQuery("", "sigma-456", listOf(), "", mutableListOf("tag=sev2"))] = emptySet()
Assert.assertEquals("query[name=sigma-123] query[tag=sev2] || ", equation.toString())
Assert.assertEquals(mutableSetOf("6", "8", "7"), equation.evaluate(queryToDocIds))
}
Expand All @@ -88,7 +88,7 @@ class TriggerExpressionResolverTests : OpenSearchTestCase() {
val queryToDocIds = mutableMapOf<DocLevelQuery, Set<String>>()
queryToDocIds[DocLevelQuery("", "sigma-123", listOf(), "", emptyList())] = mutableSetOf("1", "2", "3", "11")
queryToDocIds[DocLevelQuery("", "sigma-456", listOf(), "", emptyList())] = mutableSetOf("3", "4", "5")
queryToDocIds[DocLevelQuery("id_new", "", listOf(), "", emptyList())] = mutableSetOf("11", "12", "13")
queryToDocIds[DocLevelQuery("id_new", "sigma-789", listOf(), "", emptyList())] = mutableSetOf("11", "12", "13")
Assert.assertEquals("query[name=sigma-123] query[name=sigma-456] ! && ", equation.toString())
Assert.assertEquals(mutableSetOf("1", "2", "11"), equation.evaluate(queryToDocIds))
}
Expand All @@ -98,8 +98,8 @@ class TriggerExpressionResolverTests : OpenSearchTestCase() {
val equation = TriggerExpressionParser(eqString).parse()
val queryToDocIds = mutableMapOf<DocLevelQuery, Set<String>>()
queryToDocIds[DocLevelQuery("", "sigma-123", listOf(), "", emptyList())] = mutableSetOf("6", "3", "7")
queryToDocIds[DocLevelQuery("id1456", "", listOf(), "", emptyList())] = mutableSetOf("11", "12", "15")
queryToDocIds[DocLevelQuery("id_new", "", listOf(), "", emptyList())] = mutableSetOf("11", "12", "13")
queryToDocIds[DocLevelQuery("id1456", "sigma-456", listOf(), "", emptyList())] = mutableSetOf("11", "12", "15")
queryToDocIds[DocLevelQuery("id_new", "sigma-789", listOf(), "", emptyList())] = mutableSetOf("11", "12", "13")
Assert.assertEquals("query[name=sigma-123] query[id=id1456] ! || ", equation.toString())
Assert.assertEquals(mutableSetOf("6", "3", "7", "13"), equation.evaluate(queryToDocIds))
}
Expand All @@ -110,9 +110,9 @@ class TriggerExpressionResolverTests : OpenSearchTestCase() {

val queryToDocIds = mutableMapOf<DocLevelQuery, Set<String>>()
queryToDocIds[DocLevelQuery("", "sigma-123", listOf(), "", emptyList())] = mutableSetOf("1", "2", "3")
queryToDocIds[DocLevelQuery("id_random1", "", listOf(), "", mutableListOf("sev1"))] = mutableSetOf("2", "3", "4")
queryToDocIds[DocLevelQuery("id_random1", "sigma-456", listOf(), "", mutableListOf("sev1"))] = mutableSetOf("2", "3", "4")
queryToDocIds[DocLevelQuery("", "sigma-789", listOf(), "", emptyList())] = mutableSetOf("11", "12", "13")
queryToDocIds[DocLevelQuery("id-2aw34", "", listOf(), "", emptyList())] = mutableSetOf("13", "14", "15")
queryToDocIds[DocLevelQuery("id-2aw34", "sigma-101112", listOf(), "", emptyList())] = mutableSetOf("13", "14", "15")

Assert.assertEquals(
"query[name=sigma-123] query[tag=sev1] && query[name=sigma-789] ! query[id=id-2aw34] || ! || ",
Expand Down

0 comments on commit 7356f94

Please sign in to comment.