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

Commit f1b7375

Browse files
Misc fixes for 1.11 release (#274)
* delete dead code, fix flaky tests, revert to runBlocking for monitor runner
1 parent 043ba5c commit f1b7375

File tree

4 files changed

+21
-39
lines changed

4 files changed

+21
-39
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import kotlinx.coroutines.Dispatchers
6060
import kotlinx.coroutines.Job
6161
import kotlinx.coroutines.SupervisorJob
6262
import kotlinx.coroutines.launch
63+
import kotlinx.coroutines.runBlocking
6364
import kotlinx.coroutines.withContext
6465
import org.apache.logging.log4j.LogManager
6566
import org.elasticsearch.ExceptionsHelper
@@ -223,7 +224,7 @@ class MonitorRunner(
223224
logger.error("Error loading alerts for monitor: $id", e)
224225
return monitorResult.copy(error = e)
225226
}
226-
withContext(InjectorContextElement(monitor.id, settings, threadPool.threadContext, roles)) {
227+
runBlocking(InjectorContextElement(monitor.id, settings, threadPool.threadContext, roles)) {
227228
monitorResult = monitorResult.copy(inputResults = collectInputResults(monitor, periodStart, periodEnd))
228229
}
229230
val updatedAlerts = mutableListOf<Alert>()

alerting/src/main/kotlin/com/amazon/opendistroforelasticsearch/alerting/transport/TransportGetAlertsAction.kt

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -118,42 +118,7 @@ class TransportGetAlertsAction @Inject constructor(
118118
resolve(getAlertsRequest, searchSourceBuilder, actionListener)
119119
}
120120
}
121-
/*
122-
if (getDestinationsRequest.authHeader.isNullOrEmpty()) {
123-
// auth header is null when: 1/ security is disabled. 2/when user is super-admin.
124-
search(searchSourceBuilder, actionListener)
125-
} else if (!filterByEnabled) {
126-
// security is enabled and filterby is disabled.
127-
search(searchSourceBuilder, actionListener)
128-
} else {
129-
// security is enabled and filterby is enabled.
130-
val authRequest = AuthUserRequestBuilder(
131-
getDestinationsRequest.authHeader
132-
).build()
133-
restClient.performRequestAsync(authRequest, object : ResponseListener {
134-
override fun onSuccess(response: Response) {
135-
try {
136-
val user = User(response)
137-
addFilter(user, searchSourceBuilder)
138-
search(searchSourceBuilder, actionListener)
139-
} catch (ex: IOException) {
140-
actionListener.onFailure(AlertingException.wrap(ex))
141-
}
142-
}
143121

144-
override fun onFailure(ex: Exception) {
145-
when (ex.message?.contains("Connection refused")) {
146-
// Connection is refused when security plugin is not present. This case can happen only with integration tests.
147-
true -> {
148-
addFilter(User(), searchSourceBuilder)
149-
search(searchSourceBuilder, actionListener)
150-
}
151-
false -> actionListener.onFailure(AlertingException.wrap(ex))
152-
}
153-
}
154-
})
155-
}
156-
*/
157122
fun resolve(
158123
getAlertsRequest: GetAlertsRequest,
159124
searchSourceBuilder: SearchSourceBuilder,

alerting/src/test/kotlin/com/amazon/opendistroforelasticsearch/alerting/AlertingRestTestCase.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import org.apache.http.message.BasicHeader
4040
import org.elasticsearch.action.search.SearchResponse
4141
import org.elasticsearch.client.Response
4242
import org.elasticsearch.client.RestClient
43+
import org.elasticsearch.client.WarningFailureException
4344
import org.elasticsearch.common.settings.Settings
4445
import org.elasticsearch.common.unit.TimeValue
4546
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler
@@ -460,12 +461,16 @@ abstract class AlertingRestTestCase : ODFERestTestCase() {
460461
return index
461462
}
462463

463-
protected fun createTestHiddenIndex(index: String = randomAlphaOfLength(10).toLowerCase(Locale.ROOT)): String {
464-
createIndex(index, Settings.builder().put("hidden", true).build(), """
464+
protected fun createTestConfigIndex(index: String = "." + randomAlphaOfLength(10).toLowerCase(Locale.ROOT)): String {
465+
try {
466+
createIndex(index, Settings.builder().build(), """
465467
"properties" : {
466468
"test_strict_date_time" : { "type" : "date", "format" : "strict_date_time" }
467469
}
468470
""".trimIndent())
471+
} catch (ex: WarningFailureException) {
472+
// ignore
473+
}
469474
return index
470475
}
471476

alerting/src/test/kotlin/com/amazon/opendistroforelasticsearch/alerting/MonitorRunnerIT.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,17 @@ class MonitorRunnerIT : AlertingRestTestCase() {
272272
}
273273

274274
fun `test execute monitor search with period`() {
275-
val query = QueryBuilders.rangeQuery("monitor.last_update_time").gte("{{period_start}}").lte("{{period_end}}")
275+
// We cant query .opendistro-alerting-config as its system index. Create a test index starting with "."
276+
val testIndex = createTestConfigIndex()
277+
val fiveDaysAgo = ZonedDateTime.now().minus(5, DAYS).truncatedTo(MILLIS)
278+
val testTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(fiveDaysAgo)
279+
val testDoc = """{ "test_strict_date_time" : "$testTime" }"""
280+
indexDoc(testIndex, "1", testDoc)
281+
282+
val query = QueryBuilders.rangeQuery("test_strict_date_time")
283+
.gt("{{period_end}}||-10d")
284+
.lte("{{period_end}}")
285+
.format("epoch_millis")
276286
val input = SearchInput(indices = listOf(".*"), query = SearchSourceBuilder().query(query))
277287
val triggerScript = """
278288
// make sure there is at least one monitor
@@ -588,6 +598,7 @@ class MonitorRunnerIT : AlertingRestTestCase() {
588598
verifyAlert(activeAlert1.single(), monitor, ACTIVE)
589599
val actionResults1 = verifyActionExecutionResultInAlert(activeAlert1[0], mutableMapOf(Pair(actionThrottleEnabled.id, 0)))
590600

601+
Thread.sleep(200)
591602
updateMonitor(monitor.copy(triggers = listOf(trigger.copy(condition = NEVER_RUN)), id = monitor.id))
592603
executeMonitor(monitor.id)
593604
val completedAlert = searchAlerts(monitor, AlertIndices.ALL_INDEX_PATTERN).single()

0 commit comments

Comments
 (0)