From 0713dcd4daaf00fa62446d3cdf095877dd95ba81 Mon Sep 17 00:00:00 2001 From: Sicheng Song Date: Mon, 26 Feb 2024 10:32:29 -0800 Subject: [PATCH] Change the index update settings to make it only contain dynamic settings (#2156) --- .../ml/common/utils/IndexUtils.java | 19 ++++++++++++ .../ml/common/utils/IndexUtilsTest.java | 29 +++++++++++++++++++ .../memory/index/ConversationMetaIndex.java | 4 +-- .../ml/memory/index/InteractionsIndex.java | 2 +- .../ml/engine/indices/MLIndicesHandler.java | 5 ++-- 5 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 common/src/main/java/org/opensearch/ml/common/utils/IndexUtils.java create mode 100644 common/src/test/java/org/opensearch/ml/common/utils/IndexUtilsTest.java 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 new file mode 100644 index 0000000000..beccb6fdb4 --- /dev/null +++ b/common/src/main/java/org/opensearch/ml/common/utils/IndexUtils.java @@ -0,0 +1,19 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.ml.common.utils; + +import lombok.extern.log4j.Log4j2; +import java.util.Map; + +@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"); + + // 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"); +} 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 new file mode 100644 index 0000000000..4c979cf0c4 --- /dev/null +++ b/common/src/test/java/org/opensearch/ml/common/utils/IndexUtilsTest.java @@ -0,0 +1,29 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.ml.common.utils; + +import org.junit.Test; +import java.util.Map; +import static org.junit.Assert.assertEquals; + +public class IndexUtilsTest { + + @Test + public void testIndexSettingsContainsExpectedValues() { + Map indexSettings = IndexUtils.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; + 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()); + } + +} 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 dcaa8f1e7f..21fba2bf9d 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,12 +18,12 @@ 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 java.io.IOException; import java.time.Instant; import java.util.LinkedList; import java.util.List; -import java.util.Map; import org.opensearch.OpenSearchStatusException; import org.opensearch.OpenSearchWrapperException; @@ -74,8 +74,6 @@ public class ConversationMetaIndex { private Client client; private ClusterService clusterService; - public static final Map INDEX_SETTINGS = Map.of("index.number_of_shards", "1", "index.auto_expand_replicas", "0-1"); - private String getUserStrFromThreadContext() { return client.threadPool().getThreadContext().getTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT); } 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 bf3c020f0b..0911a5785c 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.memory.index.ConversationMetaIndex.INDEX_SETTINGS; +import static org.opensearch.ml.common.utils.IndexUtils.INDEX_SETTINGS; import java.io.IOException; import java.time.Instant; 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 34b06212a7..f55f9e75ae 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,7 +7,8 @@ import static org.opensearch.ml.common.CommonValue.META; import static org.opensearch.ml.common.CommonValue.SCHEMA_VERSION_FIELD; -import static org.opensearch.ml.memory.index.ConversationMetaIndex.INDEX_SETTINGS; +import static org.opensearch.ml.common.utils.IndexUtils.INDEX_SETTINGS; +import static org.opensearch.ml.common.utils.IndexUtils.UPDATED_INDEX_SETTINGS; import java.util.HashMap; import java.util.Map; @@ -123,7 +124,7 @@ public void initMLIndexIfAbsent(MLIndex index, ActionListener listener) ActionListener.wrap(response -> { if (response.isAcknowledged()) { UpdateSettingsRequest updateSettingRequest = new UpdateSettingsRequest(); - updateSettingRequest.indices(indexName).settings(INDEX_SETTINGS); + updateSettingRequest.indices(indexName).settings(UPDATED_INDEX_SETTINGS); client .admin() .indices()