Skip to content

Commit

Permalink
add auto expand replica settings to memories (opensearch-project#1819)
Browse files Browse the repository at this point in the history
Signed-off-by: Xun Zhang <[email protected]>
  • Loading branch information
Zhangxunmt authored and austintlee committed Mar 18, 2024
1 parent a49fca6 commit 5f73537
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.time.Instant;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.opensearch.OpenSearchSecurityException;
import org.opensearch.OpenSearchWrapperException;
Expand Down Expand Up @@ -73,6 +74,8 @@ public class ConversationMetaIndex {
private Client client;
private ClusterService clusterService;

public static final Map<String, Object> INDEX_SETTINGS = Map.of("index.auto_expand_replicas", "0-1");

private String getUserStrFromThreadContext() {
return client.threadPool().getThreadContext().getTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT);
}
Expand All @@ -84,7 +87,10 @@ private String getUserStrFromThreadContext() {
public void initConversationMetaIndexIfAbsent(ActionListener<Boolean> listener) {
if (!clusterService.state().metadata().hasIndex(META_INDEX_NAME)) {
log.debug("No conversational meta index found. Adding it");
CreateIndexRequest request = Requests.createIndexRequest(META_INDEX_NAME).mapping(ConversationalIndexConstants.META_MAPPING);
CreateIndexRequest request = Requests
.createIndexRequest(META_INDEX_NAME)
.mapping(ConversationalIndexConstants.META_MAPPING)
.settings(INDEX_SETTINGS);
try (ThreadContext.StoredContext threadContext = client.threadPool().getThreadContext().stashContext()) {
ActionListener<Boolean> internalListener = ActionListener.runBefore(listener, () -> threadContext.restore());
ActionListener<CreateIndexResponse> al = ActionListener.wrap(createIndexResponse -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +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 java.io.IOException;
import java.time.Instant;
Expand Down Expand Up @@ -87,7 +88,8 @@ public void initInteractionsIndexIfAbsent(ActionListener<Boolean> listener) {
log.debug("No interactions index found. Adding it");
CreateIndexRequest request = Requests
.createIndexRequest(INTERACTIONS_INDEX_NAME)
.mapping(ConversationalIndexConstants.INTERACTIONS_MAPPINGS);
.mapping(ConversationalIndexConstants.INTERACTIONS_MAPPINGS)
.settings(INDEX_SETTINGS);
try (ThreadContext.StoredContext threadContext = client.threadPool().getThreadContext().stashContext()) {
ActionListener<Boolean> internalListener = ActionListener.runBefore(listener, () -> threadContext.restore());
ActionListener<CreateIndexResponse> al = ActionListener.wrap(r -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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 java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -37,7 +38,6 @@ public class MLIndicesHandler {

ClusterService clusterService;
Client client;
private static final Map<String, Object> indexSettings = Map.of("index.auto_expand_replicas", "0-1");
private static final Map<String, AtomicBoolean> indexMappingUpdated = new HashMap<>();

static {
Expand Down Expand Up @@ -100,7 +100,7 @@ public void initMLIndexIfAbsent(MLIndex index, ActionListener<Boolean> listener)
log.error("Failed to create index " + indexName, e);
internalListener.onFailure(e);
});
CreateIndexRequest request = new CreateIndexRequest(indexName).mapping(mapping).settings(indexSettings);
CreateIndexRequest request = new CreateIndexRequest(indexName).mapping(mapping).settings(INDEX_SETTINGS);
client.admin().indices().create(request, actionListener);
} else {
log.debug("index:{} is already created", indexName);
Expand All @@ -116,7 +116,7 @@ public void initMLIndexIfAbsent(MLIndex index, ActionListener<Boolean> listener)
ActionListener.wrap(response -> {
if (response.isAcknowledged()) {
UpdateSettingsRequest updateSettingRequest = new UpdateSettingsRequest();
updateSettingRequest.indices(indexName).settings(indexSettings);
updateSettingRequest.indices(indexName).settings(INDEX_SETTINGS);
client
.admin()
.indices()
Expand Down

0 comments on commit 5f73537

Please sign in to comment.