-
Notifications
You must be signed in to change notification settings - Fork 104
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
optimize execution of workflow consisting of bucket-level followed by doc-level monitors #1729
Changes from 2 commits
bc90d8f
14f9371
3495cf8
f6a6375
036f01c
605105b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -297,19 +297,33 @@ class TransportDocLevelMonitorFanOutAction | |
createFindings(monitor, docsToQueries, idQueryMap, true) | ||
} | ||
} else { | ||
monitor.triggers.forEach { | ||
triggerResults[it.id] = runForEachDocTrigger( | ||
monitorResult, | ||
it as DocumentLevelTrigger, | ||
monitor, | ||
idQueryMap, | ||
docsToQueries, | ||
queryToDocIds, | ||
dryrun, | ||
executionId = executionId, | ||
findingIdToDocSource, | ||
workflowRunContext = workflowRunContext | ||
) | ||
if (monitor.ignoreFindingsAndAlerts == null || monitor.ignoreFindingsAndAlerts == false) { | ||
monitor.triggers.forEach { | ||
triggerResults[it.id] = runForEachDocTrigger( | ||
monitorResult, | ||
it as DocumentLevelTrigger, | ||
monitor, | ||
idQueryMap, | ||
docsToQueries, | ||
queryToDocIds, | ||
dryrun, | ||
executionId = executionId, | ||
findingIdToDocSource, | ||
workflowRunContext = workflowRunContext | ||
) | ||
} | ||
} else if (monitor.ignoreFindingsAndAlerts == true) { | ||
monitor.triggers.forEach { | ||
triggerResults[it.id] = runForEachDocTriggerIgnoringFindingsAndAlerts( | ||
monitorResult, | ||
it as DocumentLevelTrigger, | ||
monitor, | ||
queryToDocIds, | ||
dryrun, | ||
executionId, | ||
workflowRunContext | ||
) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -349,6 +363,50 @@ class TransportDocLevelMonitorFanOutAction | |
} | ||
} | ||
|
||
private suspend fun runForEachDocTriggerIgnoringFindingsAndAlerts( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. plz add code comments There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added comments. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we seem to be creating one alert this method name is misleading There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a comment. |
||
monitorResult: MonitorRunResult<DocumentLevelTriggerRunResult>, | ||
trigger: DocumentLevelTrigger, | ||
monitor: Monitor, | ||
queryToDocIds: Map<DocLevelQuery, Set<String>>, | ||
dryrun: Boolean, | ||
executionId: String, | ||
workflowRunContext: WorkflowRunContext? | ||
): DocumentLevelTriggerRunResult { | ||
val triggerResult = triggerService.runDocLevelTrigger(monitor, trigger, queryToDocIds) | ||
if (triggerResult.triggeredDocs.isNotEmpty()) { | ||
val triggerCtx = DocumentLevelTriggerExecutionContext(monitor, trigger) | ||
val alert = alertService.composeDocLevelAlert( | ||
listOf(), | ||
triggerResult.triggeredDocs, | ||
triggerCtx, | ||
monitorResult.alertError() ?: triggerResult.alertError(), | ||
executionId = executionId, | ||
workflorwRunContext = workflowRunContext | ||
) | ||
for (action in trigger.actions) { | ||
this.runAction(action, triggerCtx.copy(alerts = listOf(AlertContext(alert))), monitor, dryrun) | ||
} | ||
|
||
if (!dryrun && monitor.id != Monitor.NO_ID) { | ||
val actionResults = triggerResult.actionResultsMap.getOrDefault(alert.id, emptyMap()) | ||
val actionExecutionResults = actionResults.values.map { actionRunResult -> | ||
ActionExecutionResult(actionRunResult.actionId, actionRunResult.executionTime, if (actionRunResult.throttled) 1 else 0) | ||
} | ||
val updatedAlert = alert.copy(actionExecutionResults = actionExecutionResults) | ||
|
||
retryPolicy.let { | ||
alertService.saveAlerts( | ||
monitor.dataSources, | ||
listOf(updatedAlert), | ||
it, | ||
routingId = monitor.id | ||
) | ||
} | ||
} | ||
} | ||
return DocumentLevelTriggerRunResult(trigger.name, listOf(), monitorResult.error) | ||
} | ||
|
||
private suspend fun runForEachDocTrigger( | ||
monitorResult: MonitorRunResult<DocumentLevelTriggerRunResult>, | ||
trigger: DocumentLevelTrigger, | ||
|
@@ -512,7 +570,7 @@ class TransportDocLevelMonitorFanOutAction | |
.string() | ||
log.debug("Findings: $findingStr") | ||
|
||
if (shouldCreateFinding) { | ||
if (shouldCreateFinding and (monitor.ignoreFindingsAndAlerts == null || monitor.ignoreFindingsAndAlerts == false)) { | ||
indexRequests += IndexRequest(monitor.dataSources.findingsIndex) | ||
.source(findingStr, XContentType.JSON) | ||
.id(finding.id) | ||
|
@@ -524,13 +582,15 @@ class TransportDocLevelMonitorFanOutAction | |
bulkIndexFindings(monitor, indexRequests) | ||
} | ||
|
||
try { | ||
findings.forEach { finding -> | ||
publishFinding(monitor, finding) | ||
if (monitor.ignoreFindingsAndAlerts == null || monitor.ignoreFindingsAndAlerts == false) { | ||
try { | ||
findings.forEach { finding -> | ||
publishFinding(monitor, finding) | ||
} | ||
} catch (e: Exception) { | ||
// suppress exception | ||
log.error("Optional finding callback failed", e) | ||
} | ||
} catch (e: Exception) { | ||
// suppress exception | ||
log.error("Optional finding callback failed", e) | ||
} | ||
this.findingsToTriggeredQueries += findingsToTriggeredQueries | ||
|
||
|
@@ -688,6 +748,7 @@ class TransportDocLevelMonitorFanOutAction | |
var to: Long = Long.MAX_VALUE | ||
while (to >= from) { | ||
val hits: SearchHits = searchShard( | ||
monitor, | ||
indexExecutionCtx.concreteIndexName, | ||
shard, | ||
from, | ||
|
@@ -870,6 +931,7 @@ class TransportDocLevelMonitorFanOutAction | |
* This method hence fetches only docs from shard which haven't been queried before | ||
*/ | ||
private suspend fun searchShard( | ||
monitor: Monitor, | ||
index: String, | ||
shard: String, | ||
prevSeqNo: Long?, | ||
|
@@ -883,8 +945,16 @@ class TransportDocLevelMonitorFanOutAction | |
val boolQueryBuilder = BoolQueryBuilder() | ||
boolQueryBuilder.filter(QueryBuilders.rangeQuery("_seq_no").gt(prevSeqNo).lte(maxSeqNo)) | ||
|
||
if (!docIds.isNullOrEmpty()) { | ||
boolQueryBuilder.filter(QueryBuilders.termsQuery("_id", docIds)) | ||
if (monitor.ignoreFindingsAndAlerts == null || monitor.ignoreFindingsAndAlerts == false) { | ||
if (!docIds.isNullOrEmpty()) { | ||
boolQueryBuilder.filter(QueryBuilders.termsQuery("_id", docIds)) | ||
} | ||
} else if (monitor.ignoreFindingsAndAlerts == true) { | ||
val docIdsParam = mutableListOf<String>() | ||
if (docIds != null) { | ||
docIdsParam.addAll(docIds) | ||
} | ||
boolQueryBuilder.filter(QueryBuilders.termsQuery("_id", docIdsParam)) | ||
} | ||
|
||
val request: SearchRequest = SearchRequest() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we sorting based on _seq_no?
there is already a range query based on
period_end
variablethis seems incorrect
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to sort this because without
sort
, we getrandom 10 docs in period of last 15 minutes
by default for a workflow runningevery 1 min
. Now, the aggregation may have grouped1000 docs
.So,
10 out of 1000 docs generated
may not be the latest ones. We pass these10 docs
to the delegateddoc-level monitor
which has already moved itsseq_no
past these10 random docs
and hence do not generate an alert.sort by seq_no
ensures we always get thelatest 10 docs out of the 1000 docs
considered for aggregation. Thus, when the doc-level monitor runs next time, it getslatest 10 docs
and it goes on to geenrate an alert.