Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stash thread context when indexing agent configuration #1767

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.inject.Inject;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.commons.authuser.User;
import org.opensearch.core.action.ActionListener;
Expand Down Expand Up @@ -109,14 +110,21 @@ private void registerAgent(MLAgent agent, ActionListener<MLRegisterAgentResponse
MLAgent mlAgent = agent.toBuilder().createdTime(now).lastUpdateTime(now).build();
mlIndicesHandler.initMLAgentIndex(ActionListener.wrap(result -> {
if (result) {
IndexRequest indexRequest = new IndexRequest(ML_AGENT_INDEX);
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
mlAgent.toXContent(builder, ToXContent.EMPTY_PARAMS);
indexRequest.source(builder);
client.index(indexRequest, ActionListener.wrap(r -> { listener.onResponse(new MLRegisterAgentResponse(r.getId())); }, e -> {
try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) {
IndexRequest indexRequest = new IndexRequest(ML_AGENT_INDEX);
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
mlAgent.toXContent(builder, ToXContent.EMPTY_PARAMS);
indexRequest.source(builder);
client.index(indexRequest, ActionListener.runBefore(ActionListener.wrap(r -> {
listener.onResponse(new MLRegisterAgentResponse(r.getId()));
}, e -> {
log.error("Failed to index ML agent", e);
listener.onFailure(e);
}), context::restore));
} catch (Exception e) {
log.error("Failed to index ML agent", e);
listener.onFailure(e);
}));
}
} else {
log.error("Failed to create ML agent index");
}
Expand Down
Loading