@@ -24,6 +24,7 @@ import org.opensearch.alerting.model.MonitorRunResult
24
24
import org.opensearch.alerting.model.userErrorMessage
25
25
import org.opensearch.alerting.opensearchapi.suspendUntil
26
26
import org.opensearch.alerting.script.DocumentLevelTriggerExecutionContext
27
+ import org.opensearch.alerting.settings.AlertingSettings.Companion.FINDINGS_INDEXING_BATCH_SIZE
27
28
import org.opensearch.alerting.util.AlertingException
28
29
import org.opensearch.alerting.util.IndexUtils
29
30
import org.opensearch.alerting.util.defaultToPerExecutionAction
@@ -476,6 +477,10 @@ object DocumentLevelMonitorRunner : MonitorRunner() {
476
477
val findingDocPairs = mutableListOf<Pair <String , String >>()
477
478
val findings = mutableListOf<Finding >()
478
479
val indexRequests = mutableListOf<IndexRequest >()
480
+ monitorCtx.findingsIndexBatchSize = FINDINGS_INDEXING_BATCH_SIZE .get(monitorCtx.settings)
481
+ monitorCtx.clusterService!! .clusterSettings.addSettingsUpdateConsumer(FINDINGS_INDEXING_BATCH_SIZE ) {
482
+ monitorCtx.findingsIndexBatchSize = it
483
+ }
479
484
480
485
docsToQueries.forEach {
481
486
val triggeredQueries = it.value.map { queryId -> idQueryMap[queryId]!! }
@@ -502,39 +507,55 @@ object DocumentLevelMonitorRunner : MonitorRunner() {
502
507
.string()
503
508
logger.debug(" Findings: $findingStr " )
504
509
505
- if (shouldCreateFinding) {
506
- indexRequests + = IndexRequest (monitor.dataSources.findingsIndex)
507
- .source(findingStr, XContentType .JSON )
508
- .id(finding.id)
509
- .routing(finding.id)
510
- .opType(DocWriteRequest .OpType .INDEX )
510
+ if (indexRequests.size > monitorCtx.findingsIndexBatchSize) {
511
+ // make bulk indexing call here and flush the indexRequest object
512
+ bulkIndexFindings(monitor, monitorCtx, indexRequests)
513
+ indexRequests.clear()
514
+ } else {
515
+ if (shouldCreateFinding) {
516
+ indexRequests + = IndexRequest (monitor.dataSources.findingsIndex)
517
+ .source(findingStr, XContentType .JSON )
518
+ .id(finding.id)
519
+ .routing(finding.id)
520
+ .opType(DocWriteRequest .OpType .INDEX )
521
+ }
511
522
}
512
523
}
513
524
525
+ if (indexRequests.size <= monitorCtx.findingsIndexBatchSize) {
526
+ bulkIndexFindings(monitor, monitorCtx, indexRequests)
527
+ }
528
+
529
+ try {
530
+ findings.forEach { finding ->
531
+ publishFinding(monitor, monitorCtx, finding)
532
+ }
533
+ } catch (e: Exception ) {
534
+ // suppress exception
535
+ logger.error(" Optional finding callback failed" , e)
536
+ }
537
+ return findingDocPairs
538
+ }
539
+
540
+ private suspend fun bulkIndexFindings (
541
+ monitor : Monitor ,
542
+ monitorCtx : MonitorRunnerExecutionContext ,
543
+ indexRequests : List <IndexRequest >
544
+ ) {
514
545
if (indexRequests.isNotEmpty()) {
515
546
val bulkResponse: BulkResponse = monitorCtx.client!! .suspendUntil {
516
547
bulk(BulkRequest ().add(indexRequests).setRefreshPolicy(WriteRequest .RefreshPolicy .IMMEDIATE ), it)
517
548
}
518
549
if (bulkResponse.hasFailures()) {
519
550
bulkResponse.items.forEach { item ->
520
551
if (item.isFailed) {
521
- logger.debug (" Failed indexing the finding ${item.id} of monitor [${monitor.id} ]" )
552
+ logger.error (" Failed indexing the finding ${item.id} of monitor [${monitor.id} ]" )
522
553
}
523
554
}
524
555
} else {
525
556
logger.debug(" [${bulkResponse.items.size} ] All findings successfully indexed." )
526
557
}
527
558
}
528
-
529
- try {
530
- findings.forEach { finding ->
531
- publishFinding(monitor, monitorCtx, finding)
532
- }
533
- } catch (e: Exception ) {
534
- // suppress exception
535
- logger.error(" Optional finding callback failed" , e)
536
- }
537
- return findingDocPairs
538
559
}
539
560
540
561
private fun publishFinding (
@@ -658,7 +679,7 @@ object DocumentLevelMonitorRunner : MonitorRunner() {
658
679
matchingDocs.addAll(getAllDocs(hits, index, concreteIndex, monitor.id, conflictingFields))
659
680
}
660
681
} catch (e: Exception ) {
661
- logger.warn (" Failed to run for shard $shard . Error: ${e.message} " )
682
+ logger.error (" Failed to run for shard $shard . Error: ${e.message} " )
662
683
}
663
684
}
664
685
return matchingDocs
0 commit comments