From 37ab6fac75fa7557f8c7b4ec548263793181967b Mon Sep 17 00:00:00 2001 From: Sicheng Song Date: Mon, 30 Sep 2024 21:22:49 -0700 Subject: [PATCH] Support index.auto_expand_replicas 0-all for .plugins-ml-config (#3017) Signed-off-by: b4sjoo --- .../org/opensearch/ml/common/CommonValue.java | 2 +- .../ml/common/utils/IndexUtils.java | 23 +++++++++++++++---- .../ml/common/utils/IndexUtilsTest.java | 22 ++++++++++++++---- .../memory/index/ConversationMetaIndex.java | 4 ++-- .../ml/memory/index/InteractionsIndex.java | 4 ++-- .../ml/engine/indices/MLIndicesHandler.java | 18 +++++++++++---- 6 files changed, 56 insertions(+), 17 deletions(-) diff --git a/common/src/main/java/org/opensearch/ml/common/CommonValue.java b/common/src/main/java/org/opensearch/ml/common/CommonValue.java index 6a48cd5081..2dc4790bb2 100644 --- a/common/src/main/java/org/opensearch/ml/common/CommonValue.java +++ b/common/src/main/java/org/opensearch/ml/common/CommonValue.java @@ -69,7 +69,7 @@ public class CommonValue { public static final Integer ML_TASK_INDEX_SCHEMA_VERSION = 3; public static final Integer ML_CONNECTOR_SCHEMA_VERSION = 3; public static final String ML_CONFIG_INDEX = ".plugins-ml-config"; - public static final Integer ML_CONFIG_INDEX_SCHEMA_VERSION = 3; + public static final Integer ML_CONFIG_INDEX_SCHEMA_VERSION = 4; public static final String ML_CONTROLLER_INDEX = ".plugins-ml-controller"; public static final Integer ML_CONTROLLER_INDEX_SCHEMA_VERSION = 1; public static final String ML_MAP_RESPONSE_KEY = "response"; diff --git a/common/src/main/java/org/opensearch/ml/common/utils/IndexUtils.java b/common/src/main/java/org/opensearch/ml/common/utils/IndexUtils.java index 32c8cd6e25..298bd3ec96 100644 --- a/common/src/main/java/org/opensearch/ml/common/utils/IndexUtils.java +++ b/common/src/main/java/org/opensearch/ml/common/utils/IndexUtils.java @@ -12,9 +12,24 @@ @Log4j2 public class IndexUtils { - // This setting is for index creation. - public static final Map INDEX_SETTINGS = Map.of("index.number_of_shards", "1", "index.auto_expand_replicas", "0-1"); + /** + * Default settings for index creation with a single shard and one replica. + * - Sets the number of shards to 1 for better performance in small indices. + * - Uses auto-expand replicas (0-1) to ensure high availability while minimizing resource usage. + */ + public static final Map DEFAULT_INDEX_SETTINGS = Map + .of("index.number_of_shards", "1", "index.auto_expand_replicas", "0-1"); + /** + * Default settings for index creation with replicas on all nodes. + * - Sets the number of shards to 1 for better performance in small indices. + * - Uses auto-expand replicas (0-all) to ensure a replica on every node, maximizing availability. + * - Caution: This can significantly increase storage requirements and indexing load. + * - Suitable for small, critical indices where maximum redundancy is required. + */ + public static final Map ALL_NODES_REPLICA_INDEX_SETTINGS = Map + .of("index.number_of_shards", "1", "index.auto_expand_replicas", "0-all"); - // This setting is for index update only, so only dynamic settings should be contained! - public static final Map UPDATED_INDEX_SETTINGS = Map.of("index.auto_expand_replicas", "0-1"); + // Note: This does not include static settings like number of shards, which can't be changed after index creation. + public static final Map UPDATED_DEFAULT_INDEX_SETTINGS = Map.of("index.auto_expand_replicas", "0-1"); + public static final Map UPDATED_ALL_NODES_REPLICA_INDEX_SETTINGS = Map.of("index.auto_expand_replicas", "0-all"); } diff --git a/common/src/test/java/org/opensearch/ml/common/utils/IndexUtilsTest.java b/common/src/test/java/org/opensearch/ml/common/utils/IndexUtilsTest.java index ed50be13ca..8cfad37c98 100644 --- a/common/src/test/java/org/opensearch/ml/common/utils/IndexUtilsTest.java +++ b/common/src/test/java/org/opensearch/ml/common/utils/IndexUtilsTest.java @@ -14,18 +14,32 @@ public class IndexUtilsTest { @Test - public void testIndexSettingsContainsExpectedValues() { - Map indexSettings = IndexUtils.INDEX_SETTINGS; + public void testDefaultIndexSettingsContainsExpectedValues() { + Map indexSettings = IndexUtils.DEFAULT_INDEX_SETTINGS; assertEquals("index.number_of_shards should be 1", indexSettings.get("index.number_of_shards"), "1"); assertEquals("index.auto_expand_replicas should be 0-1", indexSettings.get("index.auto_expand_replicas"), "0-1"); assertEquals("INDEX_SETTINGS should contain exactly 2 settings", 2, indexSettings.size()); } @Test - public void testUpdatedIndexSettingsContainsExpectedValues() { - Map updatedIndexSettings = IndexUtils.UPDATED_INDEX_SETTINGS; + public void testAllNodesReplicaIndexSettingsContainsExpectedValues() { + Map indexSettings = IndexUtils.ALL_NODES_REPLICA_INDEX_SETTINGS; + assertEquals("index.number_of_shards should be 1", indexSettings.get("index.number_of_shards"), "1"); + assertEquals("index.auto_expand_replicas should be 0-all", indexSettings.get("index.auto_expand_replicas"), "0-all"); + assertEquals("INDEX_SETTINGS should contain exactly 2 settings", 2, indexSettings.size()); + } + + @Test + public void testUpdatedDefaultIndexSettingsContainsExpectedValues() { + Map updatedIndexSettings = IndexUtils.UPDATED_DEFAULT_INDEX_SETTINGS; assertEquals("index.auto_expand_replicas should be 0-1", updatedIndexSettings.get("index.auto_expand_replicas"), "0-1"); assertEquals("INDEX_SETTINGS should contain exactly 1 settings", 1, updatedIndexSettings.size()); } + @Test + public void testUpdatedAllNodesReplicaIndexSettingsContainsExpectedValues() { + Map updatedIndexSettings = IndexUtils.UPDATED_ALL_NODES_REPLICA_INDEX_SETTINGS; + assertEquals("index.auto_expand_replicas should be 0-all", updatedIndexSettings.get("index.auto_expand_replicas"), "0-all"); + assertEquals("INDEX_SETTINGS should contain exactly 1 settings", 1, updatedIndexSettings.size()); + } } diff --git a/memory/src/main/java/org/opensearch/ml/memory/index/ConversationMetaIndex.java b/memory/src/main/java/org/opensearch/ml/memory/index/ConversationMetaIndex.java index 2bcd2541fa..5e128e4d6f 100644 --- a/memory/src/main/java/org/opensearch/ml/memory/index/ConversationMetaIndex.java +++ b/memory/src/main/java/org/opensearch/ml/memory/index/ConversationMetaIndex.java @@ -18,7 +18,7 @@ package org.opensearch.ml.memory.index; import static org.opensearch.ml.common.conversation.ConversationalIndexConstants.META_INDEX_NAME; -import static org.opensearch.ml.common.utils.IndexUtils.INDEX_SETTINGS; +import static org.opensearch.ml.common.utils.IndexUtils.DEFAULT_INDEX_SETTINGS; import java.io.IOException; import java.time.Instant; @@ -90,7 +90,7 @@ public void initConversationMetaIndexIfAbsent(ActionListener listener) CreateIndexRequest request = Requests .createIndexRequest(META_INDEX_NAME) .mapping(ConversationalIndexConstants.META_MAPPING, XContentType.JSON) - .settings(INDEX_SETTINGS); + .settings(DEFAULT_INDEX_SETTINGS); try (ThreadContext.StoredContext threadContext = client.threadPool().getThreadContext().stashContext()) { ActionListener internalListener = ActionListener.runBefore(listener, () -> threadContext.restore()); ActionListener al = ActionListener.wrap(createIndexResponse -> { diff --git a/memory/src/main/java/org/opensearch/ml/memory/index/InteractionsIndex.java b/memory/src/main/java/org/opensearch/ml/memory/index/InteractionsIndex.java index 3084a8621f..e8cf4bd7ae 100644 --- a/memory/src/main/java/org/opensearch/ml/memory/index/InteractionsIndex.java +++ b/memory/src/main/java/org/opensearch/ml/memory/index/InteractionsIndex.java @@ -18,7 +18,7 @@ package org.opensearch.ml.memory.index; import static org.opensearch.ml.common.conversation.ConversationalIndexConstants.INTERACTIONS_INDEX_NAME; -import static org.opensearch.ml.common.utils.IndexUtils.INDEX_SETTINGS; +import static org.opensearch.ml.common.utils.IndexUtils.DEFAULT_INDEX_SETTINGS; import java.io.IOException; import java.time.Instant; @@ -93,7 +93,7 @@ public void initInteractionsIndexIfAbsent(ActionListener listener) { CreateIndexRequest request = Requests .createIndexRequest(INTERACTIONS_INDEX_NAME) .mapping(ConversationalIndexConstants.INTERACTIONS_MAPPINGS, XContentType.JSON) - .settings(INDEX_SETTINGS); + .settings(DEFAULT_INDEX_SETTINGS); try (ThreadContext.StoredContext threadContext = client.threadPool().getThreadContext().stashContext()) { ActionListener internalListener = ActionListener.runBefore(listener, () -> threadContext.restore()); ActionListener al = ActionListener.wrap(r -> { diff --git a/ml-algorithms/src/main/java/org/opensearch/ml/engine/indices/MLIndicesHandler.java b/ml-algorithms/src/main/java/org/opensearch/ml/engine/indices/MLIndicesHandler.java index 3c9d3cd649..d99eb4fa7b 100644 --- a/ml-algorithms/src/main/java/org/opensearch/ml/engine/indices/MLIndicesHandler.java +++ b/ml-algorithms/src/main/java/org/opensearch/ml/engine/indices/MLIndicesHandler.java @@ -7,8 +7,10 @@ import static org.opensearch.ml.common.CommonValue.META; import static org.opensearch.ml.common.CommonValue.SCHEMA_VERSION_FIELD; -import static org.opensearch.ml.common.utils.IndexUtils.INDEX_SETTINGS; -import static org.opensearch.ml.common.utils.IndexUtils.UPDATED_INDEX_SETTINGS; +import static org.opensearch.ml.common.utils.IndexUtils.ALL_NODES_REPLICA_INDEX_SETTINGS; +import static org.opensearch.ml.common.utils.IndexUtils.DEFAULT_INDEX_SETTINGS; +import static org.opensearch.ml.common.utils.IndexUtils.UPDATED_ALL_NODES_REPLICA_INDEX_SETTINGS; +import static org.opensearch.ml.common.utils.IndexUtils.UPDATED_DEFAULT_INDEX_SETTINGS; import java.util.HashMap; import java.util.Map; @@ -108,7 +110,9 @@ public void initMLIndexIfAbsent(MLIndex index, ActionListener listener) internalListener.onFailure(e); } }); - CreateIndexRequest request = new CreateIndexRequest(indexName).mapping(mapping, XContentType.JSON).settings(INDEX_SETTINGS); + CreateIndexRequest request = new CreateIndexRequest(indexName) + .mapping(mapping, XContentType.JSON) + .settings(indexName.equals(MLIndex.CONFIG.getIndexName()) ? ALL_NODES_REPLICA_INDEX_SETTINGS : DEFAULT_INDEX_SETTINGS); client.admin().indices().create(request, actionListener); } else { log.debug("index:{} is already created", indexName); @@ -124,7 +128,13 @@ public void initMLIndexIfAbsent(MLIndex index, ActionListener listener) ActionListener.wrap(response -> { if (response.isAcknowledged()) { UpdateSettingsRequest updateSettingRequest = new UpdateSettingsRequest(); - updateSettingRequest.indices(indexName).settings(UPDATED_INDEX_SETTINGS); + updateSettingRequest + .indices(indexName) + .settings( + indexName.equals(MLIndex.CONFIG.getIndexName()) + ? UPDATED_ALL_NODES_REPLICA_INDEX_SETTINGS + : UPDATED_DEFAULT_INDEX_SETTINGS + ); client .admin() .indices()