Skip to content

Commit

Permalink
Add more error notification at fail points (#1000) (#1009)
Browse files Browse the repository at this point in the history
* Add more error notification at fail points

* Handle exception gracefully

* small fix

---------

Signed-off-by: bowenlan-amzn <bowenlan23@gmail.com>
(cherry picked from commit a6a1f0c)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Oct 14, 2023
1 parent 319bbb2 commit 2d53845
Showing 1 changed file with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ object ManagedIndexRunner :
)
if (result.metadataSaved) {
disableManagedIndexConfig(managedIndexConfig)
publishErrorNotification(policy, managedIndexMetaData)
}
return
}
Expand All @@ -339,7 +340,10 @@ object ManagedIndexRunner :
managedIndexMetaData
.copy(actionMetaData = currentActionMetaData?.copy(failed = true), info = info)
)
if (updated.metadataSaved) disableManagedIndexConfig(managedIndexConfig)
if (updated.metadataSaved) {
disableManagedIndexConfig(managedIndexConfig)
publishErrorNotification(policy, managedIndexMetaData)
}
return
}

Expand All @@ -365,7 +369,10 @@ object ManagedIndexRunner :
policyRetryInfo = PolicyRetryInfoMetaData(true, 0), info = info
)
)
if (updated.metadataSaved) disableManagedIndexConfig(managedIndexConfig)
if (updated.metadataSaved) {
disableManagedIndexConfig(managedIndexConfig)
publishErrorNotification(policy, managedIndexMetaData)

Check warning on line 374 in src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt#L373-L374

Added lines #L373 - L374 were not covered by tests
}
return
}
}
Expand All @@ -379,7 +386,10 @@ object ManagedIndexRunner :
policyRetryInfo = PolicyRetryInfoMetaData(true, 0), info = info
)
)
if (updated.metadataSaved) disableManagedIndexConfig(managedIndexConfig)
if (updated.metadataSaved) {
disableManagedIndexConfig(managedIndexConfig)
publishErrorNotification(policy, managedIndexMetaData)

Check warning on line 391 in src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt#L390-L391

Added lines #L390 - L391 were not covered by tests
}
return
}

Expand All @@ -392,7 +402,10 @@ object ManagedIndexRunner :
policyRetryInfo = PolicyRetryInfoMetaData(true, 0), info = info
)
)
if (updated.metadataSaved) disableManagedIndexConfig(managedIndexConfig)
if (updated.metadataSaved) {
disableManagedIndexConfig(managedIndexConfig)
publishErrorNotification(policy, managedIndexMetaData)
}
return
}

Expand Down Expand Up @@ -434,16 +447,7 @@ object ManagedIndexRunner :
var executedManagedIndexMetaData = startingManagedIndexMetaData.getCompletedManagedIndexMetaData(action, step)

if (executedManagedIndexMetaData.isFailed) {
try {
// if the policy has no error_notification this will do nothing otherwise it will try to send the configured error message
publishErrorNotification(policy, executedManagedIndexMetaData)
} catch (e: Exception) {
logger.error("Failed to publish error notification", e)
val errorMessage = e.message ?: "Failed to publish error notification"
val mutableInfo = executedManagedIndexMetaData.info?.toMutableMap() ?: mutableMapOf()
mutableInfo["errorNotificationFailure"] = errorMessage
executedManagedIndexMetaData = executedManagedIndexMetaData.copy(info = mutableInfo.toMap())
}
executedManagedIndexMetaData = publishErrorNotification(policy, executedManagedIndexMetaData)
}

if (executedManagedIndexMetaData.isSuccessfulDelete) {
Expand Down Expand Up @@ -810,13 +814,37 @@ object ManagedIndexRunner :
}
}

private suspend fun publishErrorNotification(policy: Policy, managedIndexMetaData: ManagedIndexMetaData) {
policy.errorNotification?.run {
errorNotificationRetryPolicy.retry(logger) {
val compiledMessage = compileTemplate(messageTemplate, managedIndexMetaData)
destination?.buildLegacyBaseMessage(null, compiledMessage)?.publishLegacyNotification(client)
channel?.sendNotification(client, ErrorNotification.CHANNEL_TITLE, managedIndexMetaData, compiledMessage, policy.user)
private suspend fun publishErrorNotification(policy: Policy, metadata: ManagedIndexMetaData): ManagedIndexMetaData {
return try {
val errorNotification = policy.errorNotification
if (errorNotification != null) {
policy.errorNotification.run {
errorNotificationRetryPolicy.retry(logger) {
val compiledMessage = compileTemplate(messageTemplate, metadata)

Check warning on line 823 in src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt#L821-L823

Added lines #L821 - L823 were not covered by tests
destination?.buildLegacyBaseMessage(null, compiledMessage)?.publishLegacyNotification(client)
channel?.sendNotification(
client,
ErrorNotification.CHANNEL_TITLE,
metadata,
compiledMessage,
policy.user

Check warning on line 830 in src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt#L827-L830

Added lines #L827 - L830 were not covered by tests
)
}
}
val message = "Successfully published error notification [index = ${metadata.index}]"
logger.info(message)

Check warning on line 835 in src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt#L834-L835

Added lines #L834 - L835 were not covered by tests
val mutableInfo = metadata.info?.toMutableMap() ?: mutableMapOf()
mutableInfo["error_notification"] = message
metadata.copy(info = mutableInfo.toMap())

Check warning on line 838 in src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt#L837-L838

Added lines #L837 - L838 were not covered by tests
} else {
return metadata
}
} catch (e: Exception) {
logger.error("Failed to publish error notification", e)

Check warning on line 843 in src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt#L842-L843

Added lines #L842 - L843 were not covered by tests
val errorMessage = e.message ?: "Failed to publish error notification"
val mutableInfo = metadata.info?.toMutableMap() ?: mutableMapOf()
mutableInfo["error_notification"] = errorMessage
metadata.copy(info = mutableInfo.toMap())

Check warning on line 847 in src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt#L846-L847

Added lines #L846 - L847 were not covered by tests
}
}

Expand Down

0 comments on commit 2d53845

Please sign in to comment.