Skip to content

Commit

Permalink
fix number of shards of query index to 0 and auto expand replicas to …
Browse files Browse the repository at this point in the history
…0-1 (#1702)

Signed-off-by: Subhobrata Dey <sbcd90@gmail.com>
(cherry picked from commit a1bf5fb)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Oct 18, 2024
1 parent 94cace8 commit 38da5bb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -6176,4 +6178,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
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 38da5bb

Please sign in to comment.