Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit 56c49d9

Browse files
Vinoo VasudevanVinoo Vasudevan
Vinoo Vasudevan
authored and
Vinoo Vasudevan
committed
Remove custom monitor executor in favor of default dispatcher.
1 parent 21175b3 commit 56c49d9

File tree

2 files changed

+30
-37
lines changed

2 files changed

+30
-37
lines changed

alerting/src/main/kotlin/com/amazon/opendistroforelasticsearch/alerting/AlertingPlugin.kt

-4
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,4 @@ internal class AlertingPlugin : PainlessExtension, ActionPlugin, ScriptPlugin, P
175175
override fun getContexts(): List<ScriptContext<*>> {
176176
return listOf(TriggerScript.CONTEXT)
177177
}
178-
179-
// override fun getExecutorBuilders(settings: Settings): List<ExecutorBuilder<*>> {
180-
// return listOf(MonitorRunner.executorBuilder(settings))
181-
// }
182178
}

alerting/src/main/kotlin/com/amazon/opendistroforelasticsearch/alerting/MonitorRunner.kt

+30-33
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.amazon.opendistroforelasticsearch.alerting.elasticapi.convertToMap
2727
import com.amazon.opendistroforelasticsearch.alerting.elasticapi.firstFailureOrNull
2828
import com.amazon.opendistroforelasticsearch.alerting.elasticapi.retry
2929
import com.amazon.opendistroforelasticsearch.alerting.elasticapi.suspendUntil
30+
import com.amazon.opendistroforelasticsearch.alerting.model.ActionExecutionResult
3031
import com.amazon.opendistroforelasticsearch.alerting.model.ActionRunResult
3132
import com.amazon.opendistroforelasticsearch.alerting.model.Alert
3233
import com.amazon.opendistroforelasticsearch.alerting.model.Alert.State.ACKNOWLEDGED
@@ -48,13 +49,12 @@ import com.amazon.opendistroforelasticsearch.alerting.script.TriggerExecutionCon
4849
import com.amazon.opendistroforelasticsearch.alerting.script.TriggerScript
4950
import com.amazon.opendistroforelasticsearch.alerting.settings.AlertingSettings.Companion.ALERT_BACKOFF_COUNT
5051
import com.amazon.opendistroforelasticsearch.alerting.settings.AlertingSettings.Companion.ALERT_BACKOFF_MILLIS
51-
import com.amazon.opendistroforelasticsearch.alerting.settings.AlertingSettings.Companion.BULK_TIMEOUT
52-
import com.amazon.opendistroforelasticsearch.alerting.settings.AlertingSettings.Companion.INPUT_TIMEOUT
5352
import com.amazon.opendistroforelasticsearch.alerting.settings.AlertingSettings.Companion.MOVE_ALERTS_BACKOFF_COUNT
5453
import com.amazon.opendistroforelasticsearch.alerting.settings.AlertingSettings.Companion.MOVE_ALERTS_BACKOFF_MILLIS
5554
import org.apache.logging.log4j.LogManager
5655
import kotlinx.coroutines.CoroutineScope
5756
import kotlinx.coroutines.Dispatchers
57+
import kotlinx.coroutines.Job
5858
import kotlinx.coroutines.SupervisorJob
5959
import kotlinx.coroutines.launch
6060
import kotlinx.coroutines.withContext
@@ -73,6 +73,7 @@ import org.elasticsearch.client.Client
7373
import org.elasticsearch.cluster.service.ClusterService
7474
import org.elasticsearch.common.Strings
7575
import org.elasticsearch.common.bytes.BytesReference
76+
import org.elasticsearch.common.component.AbstractLifecycleComponent
7677
import org.elasticsearch.common.settings.Settings
7778
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler
7879
import org.elasticsearch.common.xcontent.NamedXContentRegistry
@@ -91,6 +92,7 @@ import org.elasticsearch.script.TemplateScript
9192
import org.elasticsearch.search.builder.SearchSourceBuilder
9293
import org.elasticsearch.threadpool.ThreadPool
9394
import java.time.Instant
95+
import kotlin.coroutines.CoroutineContext
9496

9597
class MonitorRunner(
9698
settings: Settings,
@@ -100,46 +102,44 @@ class MonitorRunner(
100102
private val xContentRegistry: NamedXContentRegistry,
101103
private val alertIndices: AlertIndices,
102104
clusterService: ClusterService
103-
) : JobRunner, CoroutineScope {
105+
) : JobRunner, CoroutineScope, AbstractLifecycleComponent() {
104106

105107
private val logger = LogManager.getLogger(MonitorRunner::class.java)
106108

107-
private val job = SupervisorJob()
108-
override val coroutineContext = Dispatchers.Default + job
109+
private lateinit var runnerSupervisor: Job
110+
override val coroutineContext: CoroutineContext
111+
get() = Dispatchers.Default + runnerSupervisor
109112

110-
@Volatile private var searchTimeout = INPUT_TIMEOUT.get(settings)
111-
@Volatile private var bulkTimeout = BULK_TIMEOUT.get(settings)
112-
@Volatile private var alertBackoffMillis = ALERT_BACKOFF_MILLIS.get(settings)
113-
@Volatile private var alertBackoffCount = ALERT_BACKOFF_COUNT.get(settings)
114-
@Volatile private var moveAlertsBackoffMillis = MOVE_ALERTS_BACKOFF_MILLIS.get(settings)
115-
@Volatile private var moveAlertsBackoffCount = MOVE_ALERTS_BACKOFF_COUNT.get(settings)
116-
@Volatile private var retryPolicy = BackoffPolicy.constantBackoff(alertBackoffMillis, alertBackoffCount)
117-
@Volatile private var moveAlertsRetryPolicy = BackoffPolicy.exponentialBackoff(moveAlertsBackoffMillis, moveAlertsBackoffCount)
113+
@Volatile private var retryPolicy =
114+
BackoffPolicy.constantBackoff(ALERT_BACKOFF_MILLIS.get(settings), ALERT_BACKOFF_COUNT.get(settings))
115+
@Volatile private var moveAlertsRetryPolicy =
116+
BackoffPolicy.exponentialBackoff(MOVE_ALERTS_BACKOFF_MILLIS.get(settings), MOVE_ALERTS_BACKOFF_COUNT.get(settings))
118117

119118
init {
120-
clusterService.clusterSettings.addSettingsUpdateConsumer(INPUT_TIMEOUT) { searchTimeout = it }
121-
clusterService.clusterSettings.addSettingsUpdateConsumer(BULK_TIMEOUT) { bulkTimeout = it }
122-
clusterService.clusterSettings.addSettingsUpdateConsumer(ALERT_BACKOFF_MILLIS) {
123-
retryPolicy = BackoffPolicy.constantBackoff(it, alertBackoffCount)
119+
clusterService.clusterSettings.addSettingsUpdateConsumer(ALERT_BACKOFF_MILLIS, ALERT_BACKOFF_COUNT) {
120+
millis, count -> retryPolicy = BackoffPolicy.constantBackoff(millis, count)
124121
}
125-
clusterService.clusterSettings.addSettingsUpdateConsumer(ALERT_BACKOFF_COUNT) {
126-
retryPolicy = BackoffPolicy.constantBackoff(alertBackoffMillis, it)
127-
}
128-
clusterService.clusterSettings.addSettingsUpdateConsumer(MOVE_ALERTS_BACKOFF_MILLIS) {
129-
moveAlertsRetryPolicy = BackoffPolicy.exponentialBackoff(it, moveAlertsBackoffCount)
130-
}
131-
clusterService.clusterSettings.addSettingsUpdateConsumer(MOVE_ALERTS_BACKOFF_COUNT) {
132-
moveAlertsRetryPolicy = BackoffPolicy.exponentialBackoff(alertBackoffMillis, it)
122+
clusterService.clusterSettings.addSettingsUpdateConsumer(MOVE_ALERTS_BACKOFF_MILLIS, MOVE_ALERTS_BACKOFF_COUNT) {
123+
millis, count -> moveAlertsRetryPolicy = BackoffPolicy.exponentialBackoff(millis, count)
133124
}
134125
}
135126

127+
override fun doStart() {
128+
runnerSupervisor = SupervisorJob()
129+
}
130+
131+
override fun doStop() {
132+
runnerSupervisor.cancel()
133+
}
134+
135+
override fun doClose() { }
136+
136137
override fun postIndex(job: ScheduledJob) {
137138
if (job !is Monitor) {
138139
throw IllegalArgumentException("Invalid job type")
139140
}
140141

141-
// Using Dispatchers.Unconfined as moveAlerts isn't CPU intensive so we can just run it on the calling thread.
142-
GlobalScope.launch(Dispatchers.Unconfined) {
142+
launch {
143143
try {
144144
moveAlertsRetryPolicy.retry(logger) {
145145
if (alertIndices.isInitialized()) {
@@ -153,11 +153,7 @@ class MonitorRunner(
153153
}
154154

155155
override fun postDelete(jobId: String) {
156-
// Using Dispatchers.Unconfined as moveAlerts isn't CPU intensive so we can just run it on the calling thread.
157-
GlobalScope.launch(Dispatchers.Unconfined) {
158-
// Using Unconfined dispatcher here as moveAlerts doesn't do much CPU intensive work, so we can just
159-
// run it on the ES built-in thread pools.
160-
launch(Dispatchers.Unconfined) {
156+
launch {
161157
try {
162158
moveAlertsRetryPolicy.retry(logger) {
163159
if (alertIndices.isInitialized()) {
@@ -451,8 +447,9 @@ class MonitorRunner(
451447
}
452448

453449
val jobSource = getResponse.sourceAsBytesRef
454-
val xcp = XContentHelper.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, jobSource, XContentType.JSON)
455450
return withContext(Dispatchers.IO) {
451+
val xcp = XContentHelper.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE,
452+
jobSource, XContentType.JSON)
456453
ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.nextToken(), xcp::getTokenLocation)
457454
ensureExpectedToken(XContentParser.Token.FIELD_NAME, xcp.nextToken(), xcp::getTokenLocation)
458455
ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.nextToken(), xcp::getTokenLocation)

0 commit comments

Comments
 (0)