Skip to content

Commit 6239d7b

Browse files
authored
delete query index only if put mappings throws an exception (#1685)
Signed-off-by: Subhobrata Dey <[email protected]>
1 parent ba3e715 commit 6239d7b

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,6 @@ class DocumentLevelMonitorRunner : MonitorRunner() {
121121
throw IndexNotFoundException(docLevelMonitorInput.indices.joinToString(","))
122122
}
123123

124-
if (monitor.deleteQueryIndexInEveryRun == true &&
125-
monitorCtx.docLevelMonitorQueries!!.docLevelQueryIndexExists(monitor.dataSources)
126-
) {
127-
val ack = monitorCtx.docLevelMonitorQueries!!.deleteDocLevelQueryIndex(monitor.dataSources)
128-
if (!ack) {
129-
logger.error(
130-
"Deletion of concrete queryIndex:${monitor.dataSources.queryIndex} is not ack'd! " +
131-
"for monitor ${monitor.id}"
132-
)
133-
}
134-
}
135124
monitorCtx.docLevelMonitorQueries!!.initDocLevelQueryIndex(monitor.dataSources)
136125
monitorCtx.docLevelMonitorQueries!!.indexDocLevelQueries(
137126
monitor = monitor,

alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,8 @@ class DocLevelMonitorQueries(private val client: Client, private val clusterServ
358358
monitorMetadata,
359359
updatedIndexName,
360360
sourceIndexFieldLimit,
361-
updatedProperties
361+
updatedProperties,
362+
indexTimeout
362363
)
363364

364365
if (updateMappingResponse.isAcknowledged) {
@@ -487,7 +488,8 @@ class DocLevelMonitorQueries(private val client: Client, private val clusterServ
487488
monitorMetadata: MonitorMetadata,
488489
sourceIndex: String,
489490
sourceIndexFieldLimit: Long,
490-
updatedProperties: MutableMap<String, Any>
491+
updatedProperties: MutableMap<String, Any>,
492+
indexTimeout: TimeValue
491493
): Pair<AcknowledgedResponse, String> {
492494
var targetQueryIndex = monitorMetadata.sourceToQueryIndexMapping[sourceIndex + monitor.id]
493495
if (
@@ -551,9 +553,48 @@ class DocLevelMonitorQueries(private val client: Client, private val clusterServ
551553
}
552554
}
553555
} else {
554-
log.debug("unknown exception during PUT mapping on queryIndex: $targetQueryIndex")
555-
val unwrappedException = ExceptionsHelper.unwrapCause(e) as Exception
556-
throw AlertingException.wrap(unwrappedException)
556+
// retry with deleting query index
557+
if (monitor.deleteQueryIndexInEveryRun == true) {
558+
try {
559+
log.error(
560+
"unknown exception during PUT mapping on queryIndex: $targetQueryIndex, " +
561+
"retrying with deletion of query index",
562+
e
563+
)
564+
if (docLevelQueryIndexExists(monitor.dataSources)) {
565+
val ack = monitorCtx.docLevelMonitorQueries!!.deleteDocLevelQueryIndex(monitor.dataSources)
566+
if (!ack) {
567+
log.error(
568+
"Deletion of concrete queryIndex:${monitor.dataSources.queryIndex} is not ack'd! " +
569+
"for monitor ${monitor.id}"
570+
)
571+
}
572+
}
573+
initDocLevelQueryIndex(monitor.dataSources)
574+
indexDocLevelQueries(
575+
monitor = monitor,
576+
monitorId = monitor.id,
577+
monitorMetadata,
578+
indexTimeout = indexTimeout
579+
)
580+
} catch (e: Exception) {
581+
log.error(
582+
"Doc level monitor ${monitor.id}: unknown exception during " +
583+
"PUT mapping on queryIndex: $targetQueryIndex",
584+
e
585+
)
586+
val unwrappedException = ExceptionsHelper.unwrapCause(e) as Exception
587+
throw AlertingException.wrap(unwrappedException)
588+
}
589+
} else {
590+
log.error(
591+
"Doc level monitor ${monitor.id}: unknown exception during " +
592+
"PUT mapping on queryIndex: $targetQueryIndex",
593+
e
594+
)
595+
val unwrappedException = ExceptionsHelper.unwrapCause(e) as Exception
596+
throw AlertingException.wrap(unwrappedException)
597+
}
557598
}
558599
}
559600
// We did rollover, so try to apply mappings again on new targetQueryIndex

sample-remote-monitor-plugin/src/main/java/org/opensearch/alerting/SampleRemoteMonitorRestHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient
9494
"id", null)), trigger1Serialized)),
9595
Map.of(),
9696
new DataSources(),
97-
true,
97+
false,
9898
"sample-remote-monitor-plugin"
9999
);
100100
IndexMonitorRequest indexMonitorRequest1 = new IndexMonitorRequest(
@@ -155,7 +155,7 @@ public void onFailure(Exception e) {
155155
List.of(),
156156
Map.of(),
157157
new DataSources(),
158-
true,
158+
false,
159159
"sample-remote-monitor-plugin"
160160
);
161161
IndexMonitorRequest indexMonitorRequest2 = new IndexMonitorRequest(
@@ -239,7 +239,7 @@ public void onFailure(Exception e) {
239239
"id", null)), trigger1Serialized)),
240240
Map.of(),
241241
new DataSources(),
242-
true,
242+
false,
243243
"sample-remote-monitor-plugin"
244244
);
245245
IndexMonitorRequest indexDocLevelMonitorRequest = new IndexMonitorRequest(

0 commit comments

Comments
 (0)