This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add throttle on action level fixes #48
Add throttle on action level We only support MINUTES unit now.
- Loading branch information
Showing
11 changed files
with
341 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...main/kotlin/com/amazon/opendistroforelasticsearch/alerting/model/ActionExecutionResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.alerting.model | ||
|
||
import com.amazon.opendistroforelasticsearch.alerting.elasticapi.instant | ||
import com.amazon.opendistroforelasticsearch.alerting.elasticapi.optionalTimeField | ||
import org.elasticsearch.common.xcontent.ToXContent | ||
import org.elasticsearch.common.xcontent.ToXContentObject | ||
import org.elasticsearch.common.xcontent.XContentBuilder | ||
import org.elasticsearch.common.xcontent.XContentParser | ||
import org.elasticsearch.common.xcontent.XContentParserUtils | ||
import java.io.IOException | ||
import java.lang.IllegalStateException | ||
import java.time.Instant | ||
|
||
/** | ||
* When an alert triggered, the trigger's actions will be executed. | ||
* Action execution result records action throttle result and is a part of Alert. | ||
*/ | ||
data class ActionExecutionResult( | ||
val actionId: String, | ||
val lastExecutionTime: Instant?, | ||
val throttledCount: Int = 0 | ||
) : ToXContentObject { | ||
|
||
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { | ||
return builder.startObject() | ||
.field(ACTION_ID_FIELD, actionId) | ||
.optionalTimeField(LAST_EXECUTION_TIME_FIELD, lastExecutionTime) | ||
.field(THROTTLED_COUNT_FIELD, throttledCount) | ||
.endObject() | ||
} | ||
|
||
companion object { | ||
const val ACTION_ID_FIELD = "action_id" | ||
const val LAST_EXECUTION_TIME_FIELD = "last_execution_time" | ||
const val THROTTLED_COUNT_FIELD = "throttled_count" | ||
|
||
@JvmStatic | ||
@Throws(IOException::class) | ||
fun parse(xcp: XContentParser): ActionExecutionResult { | ||
lateinit var actionId: String | ||
var throttledCount: Int = 0 | ||
var lastExecutionTime: Instant? = null | ||
|
||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp::getTokenLocation) | ||
while (xcp.nextToken() != XContentParser.Token.END_OBJECT) { | ||
val fieldName = xcp.currentName() | ||
xcp.nextToken() | ||
when (fieldName) { | ||
ACTION_ID_FIELD -> actionId = xcp.text() | ||
THROTTLED_COUNT_FIELD -> throttledCount = xcp.intValue() | ||
LAST_EXECUTION_TIME_FIELD -> lastExecutionTime = xcp.instant() | ||
|
||
else -> { | ||
throw IllegalStateException("Unexpected field: $fieldName, while parsing action") | ||
} | ||
} | ||
} | ||
|
||
requireNotNull(actionId) { "Must set action id" } | ||
return ActionExecutionResult(actionId, lastExecutionTime, throttledCount) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.