@@ -10,6 +10,7 @@ import org.apache.hc.core5.http.io.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.core.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
@@ -470,6 +475,86 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() {
470
475
assertTrue(" Findings saved for test monitor" , findings[1 ].relatedDocIds.contains(" 1" ))
471
476
}
472
477
478
+ fun `test monitor run generates no error alerts with versionconflictengineexception with locks` () {
479
+ val testIndex = createTestIndex()
480
+ val testTime = DateTimeFormatter .ISO_OFFSET_DATE_TIME .format(ZonedDateTime .now().truncatedTo(MILLIS ))
481
+ val testDoc = """ {
482
+ "message" : "This is an error from IAD region",
483
+ "test_strict_date_time" : "$testTime ",
484
+ "test_field" : "us-west-2"
485
+ }"""
486
+
487
+ val docQuery = DocLevelQuery (query = " test_field:\" us-west-2\" " , name = " 3" , fields = listOf ())
488
+ val docLevelInput = DocLevelMonitorInput (" description" , listOf (testIndex), listOf (docQuery))
489
+
490
+ val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN )
491
+ val monitor = createMonitor(
492
+ randomDocumentLevelMonitor(
493
+ name = " __lag-monitor-test__" ,
494
+ inputs = listOf (docLevelInput),
495
+ triggers = listOf (trigger),
496
+ schedule = IntervalSchedule (interval = 1 , unit = ChronoUnit .MINUTES )
497
+ )
498
+ )
499
+ assertNotNull(monitor.id)
500
+
501
+ indexDoc(testIndex, " 1" , testDoc)
502
+ indexDoc(testIndex, " 5" , testDoc)
503
+ Thread .sleep(240000 )
504
+
505
+ val inputMap = HashMap <String , Any >()
506
+ inputMap[" searchString" ] = monitor.name
507
+
508
+ val responseMap = getAlerts(inputMap).asMap()
509
+ val alerts = (responseMap[" alerts" ] as ArrayList <Map <String , Any >>)
510
+ alerts.forEach {
511
+ assertTrue(it[" error_message" ] == null )
512
+ }
513
+ }
514
+
515
+ @AwaitsFix(bugUrl = " " )
516
+ fun `test monitor run generate lock and monitor delete removes lock` () {
517
+ val testIndex = createTestIndex()
518
+ val testTime = DateTimeFormatter .ISO_OFFSET_DATE_TIME .format(ZonedDateTime .now().truncatedTo(MILLIS ))
519
+ val testDoc = """ {
520
+ "message" : "This is an error from IAD region",
521
+ "test_strict_date_time" : "$testTime ",
522
+ "test_field" : "us-west-2"
523
+ }"""
524
+
525
+ val docQuery = DocLevelQuery (query = " test_field:\" us-west-2\" " , name = " 3" , fields = listOf ())
526
+ val docLevelInput = DocLevelMonitorInput (" description" , listOf (testIndex), listOf (docQuery))
527
+
528
+ val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN )
529
+ val monitor = createMonitor(
530
+ randomDocumentLevelMonitor(
531
+ inputs = listOf (docLevelInput),
532
+ triggers = listOf (trigger),
533
+ schedule = IntervalSchedule (interval = 1 , unit = ChronoUnit .MINUTES )
534
+ )
535
+ )
536
+ assertNotNull(monitor.id)
537
+
538
+ indexDoc(testIndex, " 1" , testDoc)
539
+ indexDoc(testIndex, " 5" , testDoc)
540
+ OpenSearchTestCase .waitUntil({
541
+ val response = client().makeRequest(" HEAD" , LockService .LOCK_INDEX_NAME )
542
+ return @waitUntil (response.restStatus().status == 200 )
543
+ }, 240 , TimeUnit .SECONDS )
544
+
545
+ var response = client().makeRequest(" GET" , LockService .LOCK_INDEX_NAME + " /_search" )
546
+ var responseMap = entityAsMap(response)
547
+ var noOfLocks = ((responseMap[" hits" ] as Map <String , Any >)[" hits" ] as List <Any >).size
548
+ assertEquals(1 , noOfLocks)
549
+
550
+ deleteMonitor(monitor)
551
+ refreshIndex(LockService .LOCK_INDEX_NAME )
552
+ response = client().makeRequest(" GET" , LockService .LOCK_INDEX_NAME + " /_search" )
553
+ responseMap = entityAsMap(response)
554
+ noOfLocks = ((responseMap[" hits" ] as Map <String , Any >)[" hits" ] as List <Any >).size
555
+ assertEquals(0 , noOfLocks)
556
+ }
557
+
473
558
fun `test execute monitor with tag as trigger condition generates alerts and findings` () {
474
559
val testIndex = createTestIndex()
475
560
val testTime = DateTimeFormatter .ISO_OFFSET_DATE_TIME .format(ZonedDateTime .now().truncatedTo(MILLIS ))
0 commit comments