Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

separate doc-level monitor query indices for externally defined monitors #1664

Merged
merged 4 commits into from
Sep 25, 2024

Conversation

sbcd90
Copy link
Collaborator

@sbcd90 sbcd90 commented Sep 25, 2024

Description

separate doc-level monitor query indices for externally defined monitors
Adds boolean flag in monitor config to deleteQueryIndexInEveryRun

Related Issues

Resolves #[Issue number to be closed when this PR is merged]

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Signed-off-by: Subhobrata Dey <sbcd90@gmail.com>
@@ -374,6 +374,13 @@ class DocumentLevelMonitorRunner : MonitorRunner() {
// Clean up any queries created by the dry run monitor
monitorCtx.docLevelMonitorQueries!!.deleteDocLevelQueriesOnDryRun(monitorMetadata)
}

if (monitor.dataSources.queryIndex.contains("optimized")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz add a flag in monitor creation called deleteQueryIndexBeforeEveryRun (or something briefer)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (monitor.dataSources.queryIndex.contains("optimized")) {
val ack = monitorCtx.docLevelMonitorQueries!!.deleteDocLevelQueryIndex(monitor.dataSources)
if (!ack) {
logger.error("Deletion of concrete queryIndex:${monitor.dataSources.queryIndex} is not ack'd!")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz log monitor id

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it.

@@ -385,6 +392,14 @@ class DocumentLevelMonitorRunner : MonitorRunner() {
RestStatus.INTERNAL_SERVER_ERROR,
e
)
if (monitor.dataSources.queryIndex.contains("optimized") &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz dont write this twice
move this to finally block

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it.

@@ -95,57 +95,69 @@ object DeleteMonitorService :

private suspend fun deleteDocLevelMonitorQueriesAndIndices(monitor: Monitor) {
try {
val metadata = MonitorMetadataService.getMetadata(monitor)
metadata?.sourceToQueryIndexMapping?.forEach { (_, queryIndex) ->
if (monitor.owner == "alerting") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz delete based on a boolean flag that defaults to false.
This is an anti pattern and makes code unmaintainable

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it based on boolean flag passed from common-utils.

@@ -537,7 +537,8 @@ class TransportIndexMonitorAction @Inject constructor(
if (
request.monitor.isMonitorOfStandardType() &&
Monitor.MonitorType.valueOf(request.monitor.monitorType.uppercase(Locale.ROOT)) ==
Monitor.MonitorType.DOC_LEVEL_MONITOR
Monitor.MonitorType.DOC_LEVEL_MONITOR &&
request.monitor.owner == "alerting"
) {
indexDocLevelMonitorQueries(request.monitor, indexResponse.id, metadata, request.refreshPolicy)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/opensearch-project/alerting/pull/1664/files#diff-58c6e3aa339ebbcc7ee2452426f17164dc91d435c1f24ee55fdc4f06ec1a371eR546

here source to query mapping is updated but in case of new change query index doesnt even get created and wrong value would come up right?
this needs to be handled too

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the source to query mapping is dynamically updated during the first monitor run as shown in this logic. https://github.com/opensearch-project/alerting/blob/main/alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt#L481

Copy link
Member

@eirsep eirsep Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this method being called for the monitors with dedicated queryindex also?
https://github.com/opensearch-project/alerting/blob/main/alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt#L473C25-L473C49

this code snippet will always throw exception right?

  targetQueryIndex = getWriteIndexNameForAlias(monitor.dataSources.queryIndex)
            if (targetQueryIndex == null) {
                val message = "Failed to get write index for queryIndex alias:${monitor.dataSources.queryIndex}"
                log.error(message)
                throw AlertingException.wrap(
                    OpenSearchStatusException(message, RestStatus.INTERNAL_SERVER_ERROR)
                )
            }

because the queryIndex is not an alias anymore?

.execute(it)
}
}
if (currentMonitor.owner == "alerting") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactor to use flag

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it.

@@ -181,6 +182,16 @@ class DocLevelMonitorQueries(private val client: Client, private val clusterServ
}
}

suspend fun deleteDocLevelQueryIndex(dataSources: DataSources): Boolean {
val ack: AcknowledgedResponse = client.suspendUntil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do exists check before deleting

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it.

Copy link
Member

@eirsep eirsep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz remove request.monitor.owner == "alerting" and replace with a flag from monitor creation that defaults to false.
Boolean flag should determine if query index should be deleted in every run

Signed-off-by: Subhobrata Dey <sbcd90@gmail.com>
@sbcd90
Copy link
Collaborator Author

sbcd90 commented Sep 25, 2024

hi @eirsep , removed equest.monitor.owner == "alerting" and replaced with a flag from monitor creation that defaults to false.

Copy link
Member

@eirsep eirsep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scheduled jobs json mapping needs to be updated

Signed-off-by: Subhobrata Dey <sbcd90@gmail.com>
@sbcd90
Copy link
Collaborator Author

sbcd90 commented Sep 25, 2024

scheduled jobs json mapping needs to be updated

fixed this.

Signed-off-by: Subhobrata Dey <sbcd90@gmail.com>
@@ -537,7 +537,8 @@ class TransportIndexMonitorAction @Inject constructor(
if (
request.monitor.isMonitorOfStandardType() &&
Monitor.MonitorType.valueOf(request.monitor.monitorType.uppercase(Locale.ROOT)) ==
Monitor.MonitorType.DOC_LEVEL_MONITOR
Monitor.MonitorType.DOC_LEVEL_MONITOR &&
request.monitor.owner == "alerting"
) {
indexDocLevelMonitorQueries(request.monitor, indexResponse.id, metadata, request.refreshPolicy)
}
Copy link
Member

@eirsep eirsep Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this method being called for the monitors with dedicated queryindex also?
https://github.com/opensearch-project/alerting/blob/main/alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt#L473C25-L473C49

this code snippet will always throw exception right?

  targetQueryIndex = getWriteIndexNameForAlias(monitor.dataSources.queryIndex)
            if (targetQueryIndex == null) {
                val message = "Failed to get write index for queryIndex alias:${monitor.dataSources.queryIndex}"
                log.error(message)
                throw AlertingException.wrap(
                    OpenSearchStatusException(message, RestStatus.INTERNAL_SERVER_ERROR)
                )
            }

because the queryIndex is not an alias anymore?

@@ -711,7 +711,7 @@ class TransportIndexMonitorAction @Inject constructor(
.execute(it)
}
}
if (currentMonitor.owner == "alerting") {
if (currentMonitor.deleteQueryIndexInEveryRun == false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


plz change it to error log

triggers = listOf(trigger)
triggers = listOf(trigger),
dataSources = DataSources(),
owner = "alerting"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz add 2 new tests-

  1. scenario where delete query index flag is false and monitor execution fails due to mapping conflict on alias rollover
  2. scenario where delete query index flag is true and monitor execution succeeds

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in first test plz also update monitor with flag false to true and do rollover again and this time assert monitor execution succeeeds

@sbcd90
Copy link
Collaborator Author

sbcd90 commented Sep 25, 2024

#1664 (comment)
query index is same name as query index alias.

Copy link
Member

@eirsep eirsep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow up PR with tests menttioned and error message

@sbcd90 sbcd90 merged commit abdeca9 into opensearch-project:main Sep 25, 2024
18 checks passed
opensearch-trigger-bot bot pushed a commit that referenced this pull request Sep 26, 2024
…ors (#1664)

Signed-off-by: Subhobrata Dey <sbcd90@gmail.com>
(cherry picked from commit abdeca9)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
opensearch-trigger-bot bot pushed a commit that referenced this pull request Sep 26, 2024
…ors (#1664)

Signed-off-by: Subhobrata Dey <sbcd90@gmail.com>
(cherry picked from commit abdeca9)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
opensearch-trigger-bot bot pushed a commit that referenced this pull request Sep 26, 2024
…ors (#1664)

Signed-off-by: Subhobrata Dey <sbcd90@gmail.com>
(cherry picked from commit abdeca9)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
sbcd90 pushed a commit that referenced this pull request Sep 26, 2024
…ors (#1664) (#1667)

Signed-off-by: Subhobrata Dey <sbcd90@gmail.com>
opensearch-trigger-bot bot pushed a commit that referenced this pull request Sep 26, 2024
…ors (#1664)

Signed-off-by: Subhobrata Dey <sbcd90@gmail.com>
(cherry picked from commit abdeca9)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
sbcd90 pushed a commit to sbcd90/alerting that referenced this pull request Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants