From cb2e22eef91129cbfbac7ff10da4fad38ab74090 Mon Sep 17 00:00:00 2001 From: Subhobrata Dey Date: Fri, 18 Oct 2024 02:07:23 +0000 Subject: [PATCH] fix number of shards of query index to 0 and auto expand replicas to 0-1 Signed-off-by: Subhobrata Dey --- .../alerting/util/DocLevelMonitorQueries.kt | 3 + .../alerting/MonitorDataSourcesIT.kt | 59 +++++++++++++++++++ .../transport/AlertingSingleNodeTestCase.kt | 7 +++ 3 files changed, 69 insertions(+) diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt b/alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt index d47537c7f..1d7d768f9 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt @@ -31,6 +31,7 @@ import org.opensearch.alerting.MonitorRunnerService.monitorCtx import org.opensearch.alerting.opensearchapi.suspendUntil import org.opensearch.client.Client import org.opensearch.cluster.ClusterState +import org.opensearch.cluster.metadata.IndexMetadata import org.opensearch.cluster.service.ClusterService import org.opensearch.common.settings.Settings import org.opensearch.common.unit.TimeValue @@ -130,6 +131,8 @@ class DocLevelMonitorQueries(private val client: Client, private val clusterServ .alias(Alias(alias)) .settings( Settings.builder().put("index.hidden", true) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS, "0-1") .build() ) return try { diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/MonitorDataSourcesIT.kt b/alerting/src/test/kotlin/org/opensearch/alerting/MonitorDataSourcesIT.kt index a4e5ad08f..a790b1736 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/MonitorDataSourcesIT.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/MonitorDataSourcesIT.kt @@ -18,6 +18,7 @@ import org.opensearch.action.admin.indices.mapping.get.GetMappingsRequest import org.opensearch.action.admin.indices.mapping.put.PutMappingRequest import org.opensearch.action.admin.indices.open.OpenIndexRequest import org.opensearch.action.admin.indices.refresh.RefreshRequest +import org.opensearch.action.admin.indices.settings.get.GetSettingsResponse import org.opensearch.action.bulk.BulkRequest import org.opensearch.action.bulk.BulkResponse import org.opensearch.action.fieldcaps.FieldCapabilitiesRequest @@ -30,6 +31,7 @@ import org.opensearch.alerting.transport.AlertingSingleNodeTestCase import org.opensearch.alerting.util.DocLevelMonitorQueries import org.opensearch.alerting.util.DocLevelMonitorQueries.Companion.INDEX_PATTERN_SUFFIX import org.opensearch.alerting.workflow.CompositeWorkflowRunner +import org.opensearch.cluster.metadata.IndexMetadata import org.opensearch.common.settings.Settings import org.opensearch.common.xcontent.LoggingDeprecationHandler import org.opensearch.common.xcontent.XContentHelper @@ -6174,4 +6176,61 @@ class MonitorDataSourcesIT : AlertingSingleNodeTestCase() { Assert.assertEquals(completedAlert1.alerts[0].state, Alert.State.COMPLETED) Assert.assertTrue(completedAlert1.alerts[0].endTime!! > acknowledgedAlert.alerts[0].lastNotificationTime!!) } + + fun `test query index created with single primary and single replica shard`() { + val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3", fields = listOf()) + val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery)) + val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN) + var monitor = randomDocumentLevelMonitor( + inputs = listOf(docLevelInput), + triggers = listOf(trigger), + dataSources = DataSources(queryIndex = ".opensearch-alerting-custom-queries") + ) + val monitorResponse = createMonitor(monitor) + val testTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now().truncatedTo(MILLIS)) + val testDoc = """{ + "message" : "This is an error from IAD region", + "test_strict_date_time" : "$testTime", + "test_field" : "us-west-2" + }""" + assertFalse(monitorResponse?.id.isNullOrEmpty()) + monitor = monitorResponse!!.monitor + indexDoc(index, "1", testDoc) + val id = monitorResponse.id + executeMonitor(monitor, id, false) + + var response: GetSettingsResponse? = getIndexSettings(".opensearch-alerting-custom-queries-000001") + assertEquals( + "1", + response!!.getSetting( + ".opensearch-alerting-custom-queries-000001", + IndexMetadata.SETTING_NUMBER_OF_SHARDS + ) + ) + assertEquals( + "0", + response.getSetting( + ".opensearch-alerting-custom-queries-000001", + IndexMetadata.SETTING_NUMBER_OF_REPLICAS + ) + ) + + executeMonitor(monitor, id, false) + + response = getIndexSettings(".opensearch-alerting-custom-queries-000001") + assertEquals( + "1", + response!!.getSetting( + ".opensearch-alerting-custom-queries-000001", + IndexMetadata.SETTING_NUMBER_OF_SHARDS + ) + ) + assertEquals( + "0", + response.getSetting( + ".opensearch-alerting-custom-queries-000001", + IndexMetadata.SETTING_NUMBER_OF_REPLICAS + ) + ) + } } diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/transport/AlertingSingleNodeTestCase.kt b/alerting/src/test/kotlin/org/opensearch/alerting/transport/AlertingSingleNodeTestCase.kt index 4198f0afd..9979f7969 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/transport/AlertingSingleNodeTestCase.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/transport/AlertingSingleNodeTestCase.kt @@ -13,6 +13,8 @@ import org.opensearch.action.admin.indices.get.GetIndexRequestBuilder import org.opensearch.action.admin.indices.get.GetIndexResponse import org.opensearch.action.admin.indices.refresh.RefreshAction import org.opensearch.action.admin.indices.refresh.RefreshRequest +import org.opensearch.action.admin.indices.settings.get.GetSettingsRequest +import org.opensearch.action.admin.indices.settings.get.GetSettingsResponse import org.opensearch.action.support.IndicesOptions import org.opensearch.action.support.WriteRequest import org.opensearch.alerting.AlertingPlugin @@ -493,6 +495,11 @@ abstract class AlertingSingleNodeTestCase : OpenSearchSingleNodeTestCase() { return client().execute(ExecuteWorkflowAction.INSTANCE, request).get() } + protected fun getIndexSettings(index: String): GetSettingsResponse? { + val request = GetSettingsRequest().indices(index) + return client().admin().indices().getSettings(request).get() + } + override fun nodeSettings(): Settings { return Settings.builder() .put(super.nodeSettings())