@@ -10,6 +10,7 @@ import org.apache.http.entity.StringEntity
10
10
import org.opensearch.action.search.SearchResponse
11
11
import org.opensearch.alerting.alerts.AlertIndices.Companion.ALL_ALERT_INDEX_PATTERN
12
12
import org.opensearch.alerting.alerts.AlertIndices.Companion.ALL_FINDING_INDEX_PATTERN
13
+ import org.opensearch.alerting.core.lock.LockService
13
14
import org.opensearch.alerting.settings.AlertingSettings
14
15
import org.opensearch.client.Response
15
16
import org.opensearch.client.ResponseException
@@ -18,16 +19,20 @@ import org.opensearch.commons.alerting.model.Alert
18
19
import org.opensearch.commons.alerting.model.DataSources
19
20
import org.opensearch.commons.alerting.model.DocLevelMonitorInput
20
21
import org.opensearch.commons.alerting.model.DocLevelQuery
22
+ import org.opensearch.commons.alerting.model.IntervalSchedule
21
23
import org.opensearch.commons.alerting.model.action.ActionExecutionPolicy
22
24
import org.opensearch.commons.alerting.model.action.AlertCategory
23
25
import org.opensearch.commons.alerting.model.action.PerAlertActionScope
24
26
import org.opensearch.commons.alerting.model.action.PerExecutionActionScope
25
27
import org.opensearch.rest.RestStatus
26
28
import org.opensearch.script.Script
29
+ import org.opensearch.test.OpenSearchTestCase
27
30
import java.time.ZonedDateTime
28
31
import java.time.format.DateTimeFormatter
32
+ import java.time.temporal.ChronoUnit
29
33
import java.time.temporal.ChronoUnit.MILLIS
30
34
import java.util.Locale
35
+ import java.util.concurrent.TimeUnit
31
36
32
37
class DocumentMonitorRunnerIT : AlertingRestTestCase () {
33
38
@@ -616,6 +621,86 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() {
616
621
assertTrue(" Findings saved for test monitor" , findings[1 ].relatedDocIds.contains(" 1" ))
617
622
}
618
623
624
+ fun `test monitor run generates no error alerts with versionconflictengineexception with locks` () {
625
+ val testIndex = createTestIndex()
626
+ val testTime = DateTimeFormatter .ISO_OFFSET_DATE_TIME .format(ZonedDateTime .now().truncatedTo(MILLIS ))
627
+ val testDoc = """ {
628
+ "message" : "This is an error from IAD region",
629
+ "test_strict_date_time" : "$testTime ",
630
+ "test_field" : "us-west-2"
631
+ }"""
632
+
633
+ val docQuery = DocLevelQuery (query = " test_field:\" us-west-2\" " , name = " 3" , fields = listOf ())
634
+ val docLevelInput = DocLevelMonitorInput (" description" , listOf (testIndex), listOf (docQuery))
635
+
636
+ val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN )
637
+ val monitor = createMonitor(
638
+ randomDocumentLevelMonitor(
639
+ name = " __lag-monitor-test__" ,
640
+ inputs = listOf (docLevelInput),
641
+ triggers = listOf (trigger),
642
+ schedule = IntervalSchedule (interval = 1 , unit = ChronoUnit .MINUTES )
643
+ )
644
+ )
645
+ assertNotNull(monitor.id)
646
+
647
+ indexDoc(testIndex, " 1" , testDoc)
648
+ indexDoc(testIndex, " 5" , testDoc)
649
+ Thread .sleep(240000 )
650
+
651
+ val inputMap = HashMap <String , Any >()
652
+ inputMap[" searchString" ] = monitor.name
653
+
654
+ val responseMap = getAlerts(inputMap).asMap()
655
+ val alerts = (responseMap[" alerts" ] as ArrayList <Map <String , Any >>)
656
+ alerts.forEach {
657
+ assertTrue(it[" error_message" ] == null )
658
+ }
659
+ }
660
+
661
+ @AwaitsFix(bugUrl = " " )
662
+ fun `test monitor run generate lock and monitor delete removes lock` () {
663
+ val testIndex = createTestIndex()
664
+ val testTime = DateTimeFormatter .ISO_OFFSET_DATE_TIME .format(ZonedDateTime .now().truncatedTo(MILLIS ))
665
+ val testDoc = """ {
666
+ "message" : "This is an error from IAD region",
667
+ "test_strict_date_time" : "$testTime ",
668
+ "test_field" : "us-west-2"
669
+ }"""
670
+
671
+ val docQuery = DocLevelQuery (query = " test_field:\" us-west-2\" " , name = " 3" , fields = listOf ())
672
+ val docLevelInput = DocLevelMonitorInput (" description" , listOf (testIndex), listOf (docQuery))
673
+
674
+ val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN )
675
+ val monitor = createMonitor(
676
+ randomDocumentLevelMonitor(
677
+ inputs = listOf (docLevelInput),
678
+ triggers = listOf (trigger),
679
+ schedule = IntervalSchedule (interval = 1 , unit = ChronoUnit .MINUTES )
680
+ )
681
+ )
682
+ assertNotNull(monitor.id)
683
+
684
+ indexDoc(testIndex, " 1" , testDoc)
685
+ indexDoc(testIndex, " 5" , testDoc)
686
+ OpenSearchTestCase .waitUntil({
687
+ val response = client().makeRequest(" HEAD" , LockService .LOCK_INDEX_NAME )
688
+ return @waitUntil (response.restStatus().status == 200 )
689
+ }, 240 , TimeUnit .SECONDS )
690
+
691
+ var response = client().makeRequest(" GET" , LockService .LOCK_INDEX_NAME + " /_search" )
692
+ var responseMap = entityAsMap(response)
693
+ var noOfLocks = ((responseMap[" hits" ] as Map <String , Any >)[" hits" ] as List <Any >).size
694
+ assertEquals(1 , noOfLocks)
695
+
696
+ deleteMonitor(monitor)
697
+ refreshIndex(LockService .LOCK_INDEX_NAME )
698
+ response = client().makeRequest(" GET" , LockService .LOCK_INDEX_NAME + " /_search" )
699
+ responseMap = entityAsMap(response)
700
+ noOfLocks = ((responseMap[" hits" ] as Map <String , Any >)[" hits" ] as List <Any >).size
701
+ assertEquals(0 , noOfLocks)
702
+ }
703
+
619
704
fun `test execute monitor with tag as trigger condition generates alerts and findings` () {
620
705
val testIndex = createTestIndex()
621
706
val testTime = DateTimeFormatter .ISO_OFFSET_DATE_TIME .format(ZonedDateTime .now().truncatedTo(MILLIS ))
0 commit comments