Skip to content

Commit aeb2f2d

Browse files
[BUG] ExecuteMonitor inserting metadata doc during dry run (#758) (#777)
* execute monitor bugfix Signed-off-by: Petar Dzepina <[email protected]> * added IT Signed-off-by: Petar Dzepina <[email protected]> * fixed created retval when skipIndex=true Signed-off-by: Petar Dzepina <[email protected]> --------- Signed-off-by: Petar Dzepina <[email protected]> (cherry picked from commit ce7094a) Co-authored-by: Petar Dzepina <[email protected]>
1 parent a69d762 commit aeb2f2d

File tree

6 files changed

+62
-6
lines changed

6 files changed

+62
-6
lines changed

alerting/src/main/kotlin/org/opensearch/alerting/DocumentLevelMonitorRunner.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ object DocumentLevelMonitorRunner : MonitorRunner() {
6565
dryrun: Boolean
6666
): MonitorRunResult<DocumentLevelTriggerRunResult> {
6767
logger.debug("Document-level-monitor is running ...")
68+
val isTempMonitor = dryrun || monitor.id == Monitor.NO_ID
6869
var monitorResult = MonitorRunResult<DocumentLevelTriggerRunResult>(monitor.name, periodStart, periodEnd)
6970

7071
try {
@@ -84,13 +85,16 @@ object DocumentLevelMonitorRunner : MonitorRunner() {
8485
monitorResult = monitorResult.copy(error = AlertingException.wrap(e))
8586
}
8687

87-
var (monitorMetadata, _) = MonitorMetadataService.getOrCreateMetadata(monitor, createWithRunContext = false)
88+
var (monitorMetadata, _) = MonitorMetadataService.getOrCreateMetadata(
89+
monitor = monitor,
90+
createWithRunContext = false,
91+
skipIndex = isTempMonitor
92+
)
8893

8994
val docLevelMonitorInput = monitor.inputs[0] as DocLevelMonitorInput
9095
val index = docLevelMonitorInput.indices[0]
9196
val queries: List<DocLevelQuery> = docLevelMonitorInput.queries
9297

93-
val isTempMonitor = dryrun || monitor.id == Monitor.NO_ID
9498
val lastRunContext = if (monitorMetadata.lastRunContext.isNullOrEmpty()) mutableMapOf()
9599
else monitorMetadata.lastRunContext.toMutableMap() as MutableMap<String, MutableMap<String, Any>>
96100

alerting/src/main/kotlin/org/opensearch/alerting/MonitorMetadataService.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,23 @@ object MonitorMetadataService :
110110
}
111111
}
112112

113-
suspend fun getOrCreateMetadata(monitor: Monitor, createWithRunContext: Boolean = true): Pair<MonitorMetadata, Boolean> {
113+
suspend fun getOrCreateMetadata(
114+
monitor: Monitor,
115+
createWithRunContext: Boolean = true,
116+
skipIndex: Boolean = false
117+
): Pair<MonitorMetadata, Boolean> {
114118
try {
115119
val created = true
116120
val metadata = getMetadata(monitor)
117121
return if (metadata != null) {
118122
metadata to !created
119123
} else {
120124
val newMetadata = createNewMetadata(monitor, createWithRunContext = createWithRunContext)
121-
upsertMetadata(newMetadata, updating = false) to created
125+
if (skipIndex) {
126+
newMetadata to created
127+
} else {
128+
upsertMetadata(newMetadata, updating = false) to created
129+
}
122130
}
123131
} catch (e: Exception) {
124132
throw AlertingException.wrap(e)

alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportExecuteMonitorAction.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class TransportExecuteMonitorAction @Inject constructor(
130130
docLevelMonitorQueries.initDocLevelQueryIndex(monitor.dataSources)
131131
log.info("Central Percolation index ${ScheduledJob.DOC_LEVEL_QUERIES_INDEX} created")
132132
}
133-
val (metadata, _) = MonitorMetadataService.getOrCreateMetadata(monitor)
133+
val (metadata, _) = MonitorMetadataService.getOrCreateMetadata(monitor, skipIndex = true)
134134
docLevelMonitorQueries.indexDocLevelQueries(
135135
monitor,
136136
monitor.id,

alerting/src/test/kotlin/org/opensearch/alerting/DocumentMonitorRunnerIT.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() {
269269
}
270270
}
271271

272+
refreshAllIndices()
273+
272274
val alerts = searchAlertsWithFilter(monitor)
273275
assertEquals("Alert saved for test monitor", 2, alerts.size)
274276

alerting/src/test/kotlin/org/opensearch/alerting/MonitorDataSourcesIT.kt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import org.opensearch.alerting.action.SearchMonitorAction
1919
import org.opensearch.alerting.action.SearchMonitorRequest
2020
import org.opensearch.alerting.alerts.AlertIndices
2121
import org.opensearch.alerting.core.ScheduledJobIndices
22+
import org.opensearch.alerting.model.DocumentLevelTriggerRunResult
2223
import org.opensearch.alerting.transport.AlertingSingleNodeTestCase
2324
import org.opensearch.common.settings.Settings
2425
import org.opensearch.common.xcontent.XContentType
@@ -342,6 +343,47 @@ class MonitorDataSourcesIT : AlertingSingleNodeTestCase() {
342343
assertEquals("Didn't match all 4 queries", 1, findings[0].docLevelQueries.size)
343344
}
344345

346+
fun `test execute monitor without create when no monitors exists`() {
347+
val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3")
348+
val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery))
349+
val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN)
350+
val customQueryIndex = "custom_alerts_index"
351+
val analyzer = "whitespace"
352+
var monitor = randomDocumentLevelMonitor(
353+
inputs = listOf(docLevelInput),
354+
triggers = listOf(trigger),
355+
dataSources = DataSources(
356+
queryIndex = customQueryIndex,
357+
queryIndexMappingsByType = mapOf(Pair("text", mapOf(Pair("analyzer", analyzer)))),
358+
)
359+
)
360+
var executeMonitorResponse = executeMonitor(monitor, null)
361+
val testTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now().truncatedTo(MILLIS))
362+
val testDoc = """{
363+
"message" : "This is an error from IAD region",
364+
"test_strict_date_time" : "$testTime",
365+
"test_field" : "us-west-2"
366+
}"""
367+
368+
assertIndexNotExists(SCHEDULED_JOBS_INDEX)
369+
370+
val createMonitorResponse = createMonitor(monitor)
371+
372+
assertIndexExists(SCHEDULED_JOBS_INDEX)
373+
374+
indexDoc(index, "1", testDoc)
375+
376+
executeMonitorResponse = executeMonitor(monitor, createMonitorResponse?.id, dryRun = false)
377+
378+
Assert.assertEquals(executeMonitorResponse!!.monitorRunResult.monitorName, monitor.name)
379+
Assert.assertEquals(executeMonitorResponse.monitorRunResult.triggerResults.size, 1)
380+
Assert.assertEquals(
381+
(executeMonitorResponse.monitorRunResult.triggerResults.iterator().next().value as DocumentLevelTriggerRunResult)
382+
.triggeredDocs.size,
383+
1
384+
)
385+
}
386+
345387
fun `test execute monitor with custom query index and custom field mappings`() {
346388
val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3")
347389
val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery))

alerting/src/test/kotlin/org/opensearch/alerting/transport/AlertingSingleNodeTestCase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ abstract class AlertingSingleNodeTestCase : OpenSearchSingleNodeTestCase() {
6969
return getIndexResponse.indices().toList()
7070
}
7171

72-
protected fun executeMonitor(monitor: Monitor, id: String, dryRun: Boolean = true): ExecuteMonitorResponse? {
72+
protected fun executeMonitor(monitor: Monitor, id: String?, dryRun: Boolean = true): ExecuteMonitorResponse? {
7373
val request = ExecuteMonitorRequest(dryRun, TimeValue(Instant.now().toEpochMilli()), id, monitor)
7474
return client().execute(ExecuteMonitorAction.INSTANCE, request).get()
7575
}

0 commit comments

Comments
 (0)