-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport 2.x] Implemented support for configuring a cluster metrics …
…monitor to call cat/indices, and cat/shards. #992 (#1009) * Implemented support for configuring a cluster metrics monitor to call cat/indices, and cat/shards. (#992) * Implemented support for configuring a cluster metrics monitor to call cat/indices, and cat/shards. Signed-off-by: AWSHurneyt <[email protected]> * Fixed ktlint errors. Signed-off-by: AWSHurneyt <[email protected]> * Refactored executeTransportAction to use suspendUntil() instead of get() to receive responses. Signed-off-by: AWSHurneyt <[email protected]> --------- Signed-off-by: AWSHurneyt <[email protected]> * Resolved merge conflicts. Signed-off-by: AWSHurneyt <[email protected]> * Fixed ktlint errors. Signed-off-by: AWSHurneyt <[email protected]> * Refactored API calls from suspendUntil() to get(). Signed-off-by: AWSHurneyt <[email protected]> --------- Signed-off-by: AWSHurneyt <[email protected]> (cherry picked from commit 84e8b00)
- Loading branch information
1 parent
57b2ebf
commit 0971bfc
Showing
10 changed files
with
1,759 additions
and
21 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
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
859 changes: 859 additions & 0 deletions
859
...ain/kotlin/org/opensearch/alerting/util/clusterMetricsMonitorHelpers/CatIndicesHelpers.kt
Large diffs are not rendered by default.
Oops, something went wrong.
495 changes: 495 additions & 0 deletions
495
...main/kotlin/org/opensearch/alerting/util/clusterMetricsMonitorHelpers/CatShardsHelpers.kt
Large diffs are not rendered by default.
Oops, something went wrong.
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
2 changes: 2 additions & 0 deletions
2
alerting/src/main/resources/org/opensearch/alerting/settings/supported_json_payloads.json
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
173 changes: 173 additions & 0 deletions
173
.../kotlin/org/opensearch/alerting/util/clusterMetricsMonitorHelpers/CatIndicesWrappersIT.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,173 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.alerting.util.clusterMetricsMonitorHelpers | ||
|
||
import org.opensearch.action.support.WriteRequest | ||
import org.opensearch.alerting.randomClusterMetricsInput | ||
import org.opensearch.alerting.util.clusterMetricsMonitorHelpers.CatIndicesResponseWrapper.Companion.WRAPPER_FIELD | ||
import org.opensearch.common.xcontent.XContentType | ||
import org.opensearch.commons.alerting.model.ClusterMetricsInput | ||
import org.opensearch.core.common.Strings | ||
import org.opensearch.test.OpenSearchSingleNodeTestCase | ||
|
||
class CatIndicesWrappersIT : OpenSearchSingleNodeTestCase() { | ||
private val path = ClusterMetricsInput.ClusterMetricType.CAT_INDICES.defaultPath | ||
|
||
fun `test CatIndicesRequestWrapper validate valid pathParams`() { | ||
// GIVEN | ||
val pathParams = "index1,index-name-2,index-3" | ||
|
||
// WHEN | ||
val requestWrapper = CatIndicesRequestWrapper(pathParams = pathParams) | ||
|
||
// THEN | ||
assertEquals(3, requestWrapper.clusterHealthRequest.indices().size) | ||
assertEquals(3, requestWrapper.clusterStateRequest.indices().size) | ||
assertEquals(3, requestWrapper.indexSettingsRequest.indices().size) | ||
assertEquals(3, requestWrapper.indicesStatsRequest.indices().size) | ||
} | ||
|
||
fun `test CatIndicesRequestWrapper validate without providing pathParams`() { | ||
// GIVEN & WHEN | ||
val requestWrapper = CatIndicesRequestWrapper() | ||
|
||
// THEN | ||
assertNull(requestWrapper.clusterHealthRequest.indices()) | ||
assertEquals(Strings.EMPTY_ARRAY, requestWrapper.clusterStateRequest.indices()) | ||
assertEquals(Strings.EMPTY_ARRAY, requestWrapper.indexSettingsRequest.indices()) | ||
assertNull(requestWrapper.indicesStatsRequest.indices()) | ||
} | ||
|
||
fun `test CatIndicesRequestWrapper validate blank pathParams`() { | ||
// GIVEN | ||
val pathParams = " " | ||
|
||
// WHEN | ||
val requestWrapper = CatIndicesRequestWrapper(pathParams = pathParams) | ||
|
||
// THEN | ||
assertNull(requestWrapper.clusterHealthRequest.indices()) | ||
assertEquals(Strings.EMPTY_ARRAY, requestWrapper.clusterStateRequest.indices()) | ||
assertEquals(Strings.EMPTY_ARRAY, requestWrapper.indexSettingsRequest.indices()) | ||
assertNull(requestWrapper.indicesStatsRequest.indices()) | ||
} | ||
|
||
fun `test CatIndicesRequestWrapper validate empty pathParams`() { | ||
// GIVEN | ||
val pathParams = "" | ||
|
||
// WHEN | ||
val requestWrapper = CatIndicesRequestWrapper(pathParams = pathParams) | ||
|
||
// THEN | ||
assertNull(requestWrapper.clusterHealthRequest.indices()) | ||
assertEquals(Strings.EMPTY_ARRAY, requestWrapper.clusterStateRequest.indices()) | ||
assertEquals(Strings.EMPTY_ARRAY, requestWrapper.indexSettingsRequest.indices()) | ||
assertNull(requestWrapper.indicesStatsRequest.indices()) | ||
} | ||
|
||
fun `test CatIndicesRequestWrapper validate invalid pathParams`() { | ||
// GIVEN | ||
val pathParams = "_index1,index^2" | ||
|
||
// WHEN & THEN | ||
assertThrows(IllegalArgumentException::class.java) { CatIndicesRequestWrapper(pathParams = pathParams) } | ||
} | ||
|
||
fun `test CatIndicesResponseWrapper returns with only indices in pathParams`() { | ||
// GIVEN | ||
val testIndices = (1..5).map { | ||
"test-index${randomAlphaOfLength(10).lowercase()}" to randomIntBetween(1, 10) | ||
}.toMap() | ||
|
||
testIndices.forEach { (indexName, docCount) -> | ||
repeat(docCount) { | ||
val docId = (it + 1).toString() | ||
val docMessage = """ | ||
{ | ||
"message": "$indexName doc num $docId" | ||
} | ||
""".trimIndent() | ||
indexDoc(indexName, docId, docMessage) | ||
} | ||
} | ||
|
||
/* | ||
Creating a subset of indices to use for the pathParams to test that all indices on the cluster ARE NOT returned. | ||
*/ | ||
val pathParamsIndices = testIndices.keys.toList().subList(1, testIndices.size - 1) | ||
val pathParams = pathParamsIndices.joinToString(",") | ||
val input = randomClusterMetricsInput(path = path, pathParams = pathParams) | ||
|
||
// WHEN | ||
val responseMap = (executeTransportAction(input, client())).toMap() | ||
|
||
// THEN | ||
val shards = responseMap[WRAPPER_FIELD] as List<HashMap<String, String>> | ||
val returnedIndices = | ||
shards.map { (it[CatIndicesResponseWrapper.IndexInfo.INDEX_FIELD] as String) to it }.toMap() | ||
|
||
assertEquals(pathParamsIndices.size, returnedIndices.keys.size) | ||
testIndices.forEach { (indexName, docCount) -> | ||
if (pathParamsIndices.contains(indexName)) { | ||
assertEquals( | ||
indexName, | ||
returnedIndices[indexName]?.get(CatIndicesResponseWrapper.IndexInfo.INDEX_FIELD) as String | ||
) | ||
assertEquals( | ||
docCount.toString(), | ||
returnedIndices[indexName]?.get(CatIndicesResponseWrapper.IndexInfo.DOCS_COUNT_FIELD) as String | ||
) | ||
} | ||
} | ||
} | ||
|
||
fun `test CatIndicesResponseWrapper returns with all indices when empty pathParams`() { | ||
// GIVEN | ||
val testIndices = (1..5).map { | ||
"test-index${randomAlphaOfLength(10).lowercase()}" to randomIntBetween(1, 10) | ||
}.toMap() | ||
|
||
testIndices.forEach { (indexName, docCount) -> | ||
repeat(docCount) { | ||
val docId = (it + 1).toString() | ||
val docMessage = """ | ||
{ | ||
"message": "$indexName doc num $docId" | ||
} | ||
""".trimIndent() | ||
indexDoc(indexName, docId, docMessage) | ||
} | ||
} | ||
|
||
val input = randomClusterMetricsInput(path = path) | ||
|
||
// WHEN | ||
val responseMap = (executeTransportAction(input, client())).toMap() | ||
|
||
// THEN | ||
val shards = responseMap[WRAPPER_FIELD] as List<HashMap<String, String>> | ||
val returnedIndices = | ||
shards.map { (it[CatIndicesResponseWrapper.IndexInfo.INDEX_FIELD] as String) to it }.toMap() | ||
|
||
assertEquals(testIndices.size, returnedIndices.keys.size) | ||
testIndices.forEach { (indexName, docCount) -> | ||
assertEquals( | ||
indexName, | ||
returnedIndices[indexName]?.get(CatIndicesResponseWrapper.IndexInfo.INDEX_FIELD) as String | ||
) | ||
assertEquals( | ||
docCount.toString(), | ||
returnedIndices[indexName]?.get(CatIndicesResponseWrapper.IndexInfo.DOCS_COUNT_FIELD) as String | ||
) | ||
} | ||
} | ||
|
||
private fun indexDoc(index: String, id: String, doc: String) { | ||
client().prepareIndex(index).setId(id) | ||
.setSource(doc, XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get() | ||
} | ||
} |
Oops, something went wrong.